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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
#include <bh/internal/io.h>
#include <stdlib.h>
#include <string.h>
#define BH_IO_BUFFER_STEP 256
/**
* \defgroup io Input/Output
*
* Input/output device abstraction layer.
* \{
*/
/**
* \internal
* Returns pointer to the \a io peek buffer.
*
* \param io Pointer to the io object
*
* \return Pointer to the peek buffer.
*
* \warning Always use this function to get data of the buffer!
*/
static char *bh_io_buffer_data(bh_io_t *io)
{
if (io->buffer.capacity)
return io->buffer.data;
return (char *)&io->buffer.data;
}
/**
* \internal
* Returns peek buffer capacity of the /a io object.
*
* \param io Pointer to the io object
*
* \return Peek buffer capacity.
*
* \warning Always use this function to get capacity of the buffer!
*/
static size_t bh_io_buffer_capacity(bh_io_t *io)
{
if (io->buffer.capacity)
return io->buffer.capacity;
return sizeof(char *);
}
/**
* \internal
* Reduces the capacity of the \a io object peek buffer.
*
* \param io Pointer to the io object.
*
* \note This function does not guarantee that the capacity of the buffer will
* actually be reduced.
*/
static void bh_io_buffer_shrink(bh_io_t *io)
{
size_t capacity;
char *data;
/* Check if buffer is allocated */
if (!io->buffer.capacity)
return;
/* Calculate new capacity and check against old capacity */
capacity = (io->buffer.size + BH_IO_BUFFER_STEP - 1) / BH_IO_BUFFER_STEP;
capacity *= BH_IO_BUFFER_STEP;
if (io->buffer.capacity >= capacity)
return;
/* Allocate new buffer space */
data = malloc(capacity);
if (!data)
return;
/* Copy data */
if (io->buffer.size)
{
size_t block;
/* Depending on the tail-head positions copy data in two or one go */
if (io->buffer.tail <= io->buffer.head)
{
block = io->buffer.capacity - io->buffer.head;
memmove(data, io->buffer.data + io->buffer.head, block);
memmove(data + block, io->buffer.data, io->buffer.size - block);
}
else
{
block = io->buffer.size;
memmove(data, io->buffer.data + io->buffer.head, block);
}
}
/* Update fields */
free(io->buffer.data);
io->buffer.data = data;
io->buffer.head = 0;
io->buffer.tail = io->buffer.size;
io->buffer.capacity = capacity;
if (io->buffer.tail >= capacity)
io->buffer.tail = 0;
}
/**
* \internal
* Reserves the capacity of the \a io object peek buffer.
*
* \param io Pointer to the io object
* \param size New peek buffer capacity
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
static int bh_io_buffer_reserve(bh_io_t *io,
size_t size)
{
size_t capacity;
char *data;
/* Check if entire buffer can fit into a pointer */
if (size <= sizeof(char *))
return BH_OK;
/* Check that size can fit into a buffer */
if (io->buffer.capacity >= size)
return BH_OK;
/* Calculate new buffer capacity */
capacity = (size + BH_IO_BUFFER_STEP - 1) / BH_IO_BUFFER_STEP;
capacity *= BH_IO_BUFFER_STEP;
/* Allocate new buffer space */
data = malloc(capacity);
if (!data)
return BH_OOM;
/* Copy data */
if (io->buffer.size)
{
size_t block;
/* Depending on the tail-head positions copy data in two or one go */
if (io->buffer.tail <= io->buffer.head)
{
block = io->buffer.capacity - io->buffer.head;
memmove(data, bh_io_buffer_data(io) + io->buffer.head, block);
memmove(data + block, bh_io_buffer_data(io), io->buffer.size - block);
}
else
{
block = io->buffer.size;
memmove(data, bh_io_buffer_data(io) + io->buffer.head, block);
}
}
/* Update fields */
if (io->buffer.capacity)
free(io->buffer.data);
io->buffer.data = data;
io->buffer.head = 0;
io->buffer.tail = io->buffer.size;
io->buffer.capacity = capacity;
if (io->buffer.tail >= capacity)
io->buffer.tail = 0;
return BH_OK;
}
/**
* \internal
* Reads data from the \a io peek buffer.
*
* \param io Pointer to the io object
* \param data Pointer to the memory buffer
* \param size Maximum number of bytes to be read
* \param peek Reading or peeking flag
*
* \return On success, returns number of bytes successfuly read.
* \return On failure, returns zero.
*/
static size_t bh_io_buffer_read(bh_io_t *io,
char *data,
size_t size,
int peek)
{
size_t block;
/* Check if buffer is empty */
if (!io->buffer.size)
return 0;
/* Reading size can't be bigger then buffer size */
if (size > io->buffer.size)
size = io->buffer.size;
/* Copy data */
if (data)
{
/* Depending on the tail-head positions copy data in two or one go */
if (io->buffer.tail <= io->buffer.head)
{
block = bh_io_buffer_capacity(io) - io->buffer.head;
if (block > size)
block = size;
memmove(data, bh_io_buffer_data(io) + io->buffer.head, block);
memmove(data + block, bh_io_buffer_data(io), size - block);
}
else
{
block = size;
memmove(data, bh_io_buffer_data(io) + io->buffer.head, block);
}
}
/* Adjust head position and size if we are not peeking */
if (!peek)
{
io->buffer.head += size;
io->buffer.size -= size;
if (io->buffer.head >= bh_io_buffer_capacity(io))
io->buffer.head -= bh_io_buffer_capacity(io);
/* Shrink buffer if neccesary*/
bh_io_buffer_shrink(io);
}
/* Return readed size */
return size;
}
/**
* \internal
* Writes data to the \a io peek buffer.
*
* \param io Pointer to the io object
* \param data Pointer to the memory buffer
* \param size Maximum number of bytes to be read
*
* \return On success, returns number of bytes successfuly written.
* \return On failure, returns zero.
*/
static size_t bh_io_buffer_write(bh_io_t *io,
const char *data,
size_t size)
{
size_t block;
/* Reserve capacity in the buffer */
if (bh_io_buffer_reserve(io, io->buffer.size + size))
return 0;
/* Depending on the tail-head positions copy data in two or one go */
if (io->buffer.tail + size > bh_io_buffer_capacity(io))
{
block = io->buffer.capacity - io->buffer.tail;
memmove(bh_io_buffer_data(io) + io->buffer.tail, data, block);
memmove(bh_io_buffer_data(io), data + block, size - block);
}
else
{
block = size;
memmove(bh_io_buffer_data(io) + io->buffer.tail, data, block);
}
/* Adjust tail and size */
io->buffer.tail += size;
io->buffer.size += size;
if (io->buffer.tail >= bh_io_buffer_capacity(io))
io->buffer.tail -= bh_io_buffer_capacity(io);
return size;
}
/**
* \internal
* Adjusts \a io object peek buffer according to the specified seek direction
* \a dir and \a offset.
*
* \param io Pointer to the io object
* \param offset Number of bytes to seek in specified direciton
* \param dir Seeking direction
*
* \return Returns new offset for the io seek function.
*/
static bh_off_t bh_io_buffer_seek(bh_io_t *io,
bh_off_t offset,
int dir)
{
/* Check if seek is relative to current position */
if (dir != BH_IO_CURRENT)
{
/* It's not - reset peek buffer and return offset as is */
io->buffer.size = 0;
io->buffer.head = 0;
io->buffer.tail = 0;
return offset;
}
/* If offset is negative or past peek buffer size */
if (offset < 0 || offset >= (bh_off_t)io->buffer.size)
{
/* It's not - reset peek buffer and adjust offset position */
offset -= io->buffer.size;
io->buffer.size = 0;
io->buffer.head = 0;
io->buffer.tail = 0;
return offset;
}
/* Skip offset amount of bytes */
bh_io_buffer_read(io, NULL, offset, 0);
return 0;
}
/**
* Destroys base io object.
*
* \param io Pointer to the base io object
*
* \warning This function should be used in context of implementing child
* io objects (files, sockets, streaming compression, etc).
*/
void bh_io_destroy_base(bh_io_t *io)
{
if (io->buffer.capacity)
free(io->buffer.data);
}
/**
* Creates the new io object with specified \a table and \a size.
*
* The \a unified flag specifies the behaivor of the internal peek buffer.
* If the \a unified flag is set - the io object will assume that read and
* write operations are dependant on each other (ex. writing to the file
* affects next read operation).
* If the \a unified flag is not set - the io object will asusme that read and
* write operations are independant.
*
* \param table Pointer to the io table
* \param size Size of the io object
* \param unified Unified read/write stream flag
*
* \return On success, returns new semi-initialized io object.
* \return On failure, returns null pointer.
*
* \warning This function should be used in context of implementing child
* io objects (files, sockets, streaming compression, etc).
*/
bh_io_t *bh_io_new(const bh_io_table_t *table,
size_t size,
int unified)
{
bh_io_t *result;
result = malloc(size);
if (result)
bh_io_init(result, table, unified);
return result;
}
/**
* Frees the \a io object.
*
* \param io Pointer to the io object to be freed
*/
void bh_io_free(bh_io_t *io)
{
bh_io_destroy(io);
free(io);
}
/**
* Initializes the \a io object with specified \a table.
*
* The \a unified flag specifies the behaivor of the internal peek buffer.
* If the \a unified flag is set - the io object will assume that read and
* write operations are dependant on each other (ex. writing to the file
* affects next read operation).
* If the \a unified flag is not set - the io object will asusme that read and
* write operations are independant.
*
* \param io Pointer to the io object to be initialized
* \param table Pointer to the io table
* \param unified Unified read/write stream flag
*/
void bh_io_init(bh_io_t *io,
const bh_io_table_t *table,
int unified)
{
io->table = table;
io->flags = (unified) ? (BH_IO_UNIFIED) : (0);
io->buffer.capacity = 0;
io->buffer.head = 0;
io->buffer.tail = 0;
io->buffer.size = 0;
}
/**
* Destroys the \a io object.
*
* \param io Pointer to the io object to be destroyed
*/
void bh_io_destroy(bh_io_t *io)
{
io->table->destroy(io);
}
/**
* Opens the \a io object for specified \a mode of operation.
*
* \param io Pointer to the io object
* \param mode Bitmask determining access mode
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int bh_io_open(bh_io_t *io,
int mode)
{
return io->table->open(io, mode);
}
/**
* Closes the \a io object.
*
* \param io Pointer to the io object
*/
void bh_io_close(bh_io_t *io)
{
io->table->close(io);
}
/**
* Checks if the \a io is open.
*
* \param io Pointer to the io object
*
* \return If io object is open - returns non-zero.
* \return If io object is closed - returns zero.
*/
int bh_io_is_open(bh_io_t *io)
{
return io->table->is_open(io);
}
/**
* Reads up to \a size amount of bytes from the \a io object into memory buffer
* pointed by \a data pointer.
*
* \param io Pointer to the io object
* \param data Pointer to the memory buffer
* \param size Maximum number of bytes to be read
*
* \return On success, returns number of bytes successfuly read.
* \return On failure, returns zero.
*
* \note To check for end-of-file or error see bh_io_eof and bh_io_error.
*/
size_t bh_io_read(bh_io_t *io,
char *data,
size_t size)
{
size_t result;
/* Read as much data from peek buffer as possible */
result = bh_io_buffer_read(io, data, size, 0);
/* If that wasn't enough read from the device */
if (result != size)
result += io->table->read(io, data + result, size - result);
/* Return amount of bytes read */
return result;
}
/**
* Peeks up to \a size amount of bytes from the \a io object into memory buffer
* pointed by \a data pointer.
*
* \param io Pointer to the io object
* \param data Pointer to the memory buffer
* \param size Maximum number of bytes to be read
*
* \return On success, returns number of bytes successfuly read.
* \return On failure, returns zero.
*/
size_t bh_io_peek(bh_io_t *io,
char *data,
size_t size)
{
/* Fill peek buffer if it's size is less then requested */
if (io->buffer.size < size)
{
size_t read;
read = io->table->read(io, data, size - io->buffer.size);
bh_io_buffer_write(io, data, read);
}
/* Clear EOF flag if peek buffer is not empty */
if (io->buffer.size > 0)
io->flags &= ~BH_IO_EOF;
/* Read data from the peek buffer */
return bh_io_buffer_read(io, data, size, 1);
}
/**
* Writes up to \a size amount of bytes to the \a io object from memory buffer
* pointed by \a data pointer.
*
* \param io Pointer to the io object
* \param data Pointer to the memory buffer
* \param size Maximum number of bytes to be read
*
* \return On success, returns number of bytes successfuly written.
* \return On failure, returns zero.
*
* \note To check for error see bh_io_error.
*/
size_t bh_io_write(bh_io_t *io,
const char *data,
size_t size)
{
/* Clear peek buffer and adjust the position if IO is unified */
if (io->buffer.size && (io->flags & BH_IO_UNIFIED))
{
io->table->seek(io, -(bh_off_t)io->buffer.size, BH_IO_CURRENT);
io->buffer.head = 0;
io->buffer.tail = 0;
io->buffer.size = 0;
}
/* Write data */
return io->table->write(io, data, size);
}
/**
* Synchronizes the \a io object (if possible).
*
* In most cases, this function causes any unwritten/buffered data to be
* written.
*
* \param io Pointer to the io object
*/
void bh_io_flush(bh_io_t *io)
{
io->table->flush(io);
}
/**
* Seeks the \a io object in the specified direction \a dir and \a offset (if
* possible).
*
* \param io Pointer to the io object
* \param offset Number of bytes to seek in specified direciton
* \param dir Seeking direction
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int bh_io_seek(bh_io_t *io,
bh_off_t offset,
int dir)
{
offset = bh_io_buffer_seek(io, offset, dir);
return io->table->seek(io, offset, dir);
}
/**
* Returns total size of the \a io object (if possible)
*
* \param io Pointer to the io object
*
* \return On success, returns total size of the io object.
* \return On failure, returns -1.
*/
bh_off_t bh_io_size(bh_io_t *io)
{
return io->table->size(io);
}
/**
* Returns current position in the \a io object (if possible).
*
* \param io Pointer to the io object
*
* \return On success, returns current position in the io object.
* \return On failure, returns -1.
*/
bh_off_t bh_io_tell(bh_io_t *io)
{
return io->table->tell(io) - io->buffer.size;
}
/**
* Returns available number of bytes to be read from the \a io object.
*
* \param io Pointer to the io object
*
* \return On success, returns number of available bytes to be read.
* \return On failure, returns zero.
*/
bh_off_t bh_io_available(bh_io_t *io)
{
return io->table->available(io) + io->buffer.size;
}
/**
* Checks error flag of the \a io object.
*
* \param io Pointer to the io object
*
* \return If error flag is set, returns non-zero.
* \return If error flag is not set, returns zero.
*/
int bh_io_error(bh_io_t *io)
{
return (io->flags & BH_IO_ERROR) != 0;
}
/**
* Checks end-of-file flag of the \a io object.
*
* \param io Pointer to the io object
*
* \return If end-of-file flag is set, returns non-zero.
* \return If end-of-file flag is not set, returns zero.
*/
int bh_io_eof(bh_io_t *io)
{
return (io->flags & BH_IO_EOF) != 0;
}
/**
* Clears error of the \a io object.
*
* \param io Pointer to the io object
*/
void bh_io_clear(bh_io_t *io)
{
io->table->clear(io);
}
/**
* \}
*/
|