aboutsummaryrefslogtreecommitdiff
path: root/src/Bytes.c
blob: 562a38feafe1dcc594aa9da9cf1ae842219856b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <BH/IO.h>
#include <string.h>
#include <stdlib.h>


#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))


typedef struct BH_Bytes
{
    BH_IO parent;
    char *data;
    size_t size;
    size_t position;
} BH_Bytes;


static int bytesInit(BH_Bytes *bytes,
                     char *data,
                     size_t size)
{
    if (!data || !size)
        return BH_ERROR;

    bytes->data = data;
    bytes->size = size;
    bytes->position = 0;

    return BH_OK;
}


static int bytesDestroy(BH_Bytes *bytes)
{
    free(bytes);
    return BH_OK;
}


static int bytesRead(BH_Bytes *bytes,
                     BH_IOReadInfo *info)
{
    size_t size;

    size = MIN(info->size, bytes->size - bytes->position);
    memmove(info->data, bytes->data + bytes->position, size);
    bytes->position += size;

    if (info->actual)
        *info->actual = size;

    return BH_OK;
}


static int bytesWrite(BH_Bytes *bytes,
                      BH_IOWriteInfo *info)
{
    size_t size;

    size = MIN(info->size, bytes->size - bytes->position);
    memmove(bytes->data + bytes->position, info->data, size);
    bytes->position += size;

    if (info->actual)
        *info->actual = size;

    return BH_OK;
}

static int bytesSeek(BH_Bytes *bytes,
                     BH_IOSeekInfo *info)
{
    int64_t offset;

    switch (info->whence)
    {
    case BH_IO_SEEK_SET:
        offset = MAX(info->offset, 0);
        offset = MIN(offset, (int64_t)bytes->size);
        break;

    case BH_IO_SEEK_CUR:
        offset = MAX((int64_t)bytes->position + info->offset, 0);
        offset = MIN(offset, (int64_t)bytes->size);
        break;

    case BH_IO_SEEK_END:
        offset = MAX((int64_t)bytes->size + info->offset, 0);
        offset = MIN(offset, (int64_t)bytes->size);
        break;
    }

    bytes->position = offset;

    return BH_OK;
}


static int bytesTell(BH_Bytes *bytes,
                     int64_t *pos)
{
    *pos = bytes->position;

    return BH_OK;
}


static int bytesSize(BH_Bytes *bytes,
                     int64_t *size)
{
    *size = bytes->size;

    return BH_OK;
}


static int bytesFlags(BH_Bytes *bytes,
                     int *flags)
{
    if (bytes->position == bytes->size)
        *flags = BH_IO_FLAG_EOF;
    else
        *flags = 0;

    return BH_OK;
}


static int bytesCap(BH_Bytes *bytes,
                    int *op)
{
    BH_UNUSED(bytes);

    /* Return operations supported by the buffer input/output device */
    switch (*op)
    {
    case BH_IO_CTL_FLAGS:
    case BH_IO_CTL_SIZE:
    case BH_IO_CTL_TELL:
    case BH_IO_CTL_SEEK:
        return BH_OK;

    default:
        return BH_NOIMPL;
    }
}


static int bytesCtl(BH_Bytes *bytes,
                   BH_IOCtlInfo *info)
{
    /* Handle supported operations */
    switch (info->op)
    {
    case BH_IO_CTL_FLAGS:
        return bytesFlags(bytes, (int *)info->arg);

    case BH_IO_CTL_SIZE:
        return bytesSize(bytes, (int64_t *)(info->arg));

    case BH_IO_CTL_TELL:
        return bytesTell(bytes, (int64_t *)(info->arg));

    case BH_IO_CTL_SEEK:
        return bytesSeek(bytes, (BH_IOSeekInfo *)(info->arg));

    default:
        return BH_NOIMPL;
    }
}


static int bytesCallback(BH_Bytes *bytes,
                         int type,
                         void *arg)
{
    /* Handle basic input/output operations */
    switch (type)
    {
    case BH_IO_OP_DESTROY:  return bytesDestroy(bytes);
    case BH_IO_OP_READ:     return bytesRead(bytes, (BH_IOReadInfo *)arg);
    case BH_IO_OP_WRITE:    return bytesWrite(bytes, (BH_IOWriteInfo *)arg);
    case BH_IO_OP_CTL:      return bytesCtl(bytes, (BH_IOCtlInfo *)arg);
    case BH_IO_OP_CAP:      return bytesCap(bytes, (int*)arg);
    default:                return BH_NOIMPL;
    }
}


BH_IO *BH_BytesNew(char *data,
                   size_t size,
                   int *result)
{
    BH_Bytes *bytes;
    int code;

    code = BH_OOM;
    /* Allocate new buffer object and initialize it */
    if ((bytes = malloc(sizeof(*bytes))))
    {
        bytes->parent.callback = (BH_IOCallback)bytesCallback;
        if ((code = bytesInit(bytes, data, size)))
        {
            free(bytes);
            bytes = NULL;
        }
    }

    /* Report error code */
    if (result)
        *result = code;

    return (BH_IO*)bytes;
}