isomorfeus-ferret 0.17.1 → 0.17.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  #include "frt_store.h"
2
2
  #include <string.h>
3
3
 
4
- #define VINT_MAX_LEN 10
5
- #define VINT_END FRT_BUFFER_SIZE - VINT_MAX_LEN
4
+
5
+
6
6
 
7
7
  /*
8
8
  * TODO: add try finally
@@ -87,517 +87,6 @@ void frt_store_close(FrtStore *store) {
87
87
  }
88
88
  }
89
89
 
90
- /**
91
- * Create a newly allocated and initialized OutStream object
92
- *
93
- * @return a newly allocated and initialized OutStream object
94
- */
95
- FrtOutStream *frt_os_new(void) {
96
- FrtOutStream *os = FRT_ALLOC(FrtOutStream);
97
- os->buf.start = 0;
98
- os->buf.pos = 0;
99
- os->buf.len = 0;
100
- return os;
101
- }
102
-
103
- /**
104
- * Flush the countents of the FrtOutStream's buffers
105
- *
106
- * @param the OutStream to flush
107
- */
108
- void frt_os_flush(FrtOutStream *os)
109
- {
110
- os->m->flush_i(os, os->buf.buf, os->buf.pos);
111
- os->buf.start += os->buf.pos;
112
- os->buf.pos = 0;
113
- }
114
-
115
- void frt_os_close(FrtOutStream *os)
116
- {
117
- frt_os_flush(os);
118
- os->m->close_i(os);
119
- free(os);
120
- }
121
-
122
- off_t frt_os_pos(FrtOutStream *os)
123
- {
124
- return os->buf.start + os->buf.pos;
125
- }
126
-
127
- void frt_os_seek(FrtOutStream *os, frt_off_t new_pos)
128
- {
129
- frt_os_flush(os);
130
- os->buf.start = new_pos;
131
- os->m->seek_i(os, new_pos);
132
- }
133
-
134
- /**
135
- * Unsafe alternative to os_write_byte. Only use this method if you know there
136
- * is no chance of buffer overflow.
137
- */
138
- #define write_byte(os, b) os->buf.buf[os->buf.pos++] = (frt_uchar)b
139
-
140
- /**
141
- * Write a single byte +b+ to the OutStream +os+
142
- *
143
- * @param os the OutStream to write to
144
- * @param b the byte to write
145
- * @raise FRT_IO_ERROR if there is an IO error writing to the filesystem
146
- */
147
- void frt_os_write_byte(FrtOutStream *os, frt_uchar b)
148
- {
149
- if (os->buf.pos >= (FRT_BUFFER_SIZE - 1)) {
150
- frt_os_flush(os);
151
- }
152
- write_byte(os, b);
153
- }
154
-
155
- void frt_os_write_bytes(FrtOutStream *os, const frt_uchar *buf, int len)
156
- {
157
- if (len < (FRT_BUFFER_SIZE - os->buf.pos)) {
158
- memcpy(os->buf.buf + os->buf.pos, buf, len);
159
- os->buf.pos += len;
160
- }
161
- else {
162
- frt_os_flush(os);
163
- int pos = 0;
164
- int size;
165
- while (pos < len) {
166
- if (len - pos < FRT_BUFFER_SIZE) {
167
- size = len - pos;
168
- }
169
- else {
170
- size = FRT_BUFFER_SIZE;
171
- }
172
- os->m->flush_i(os, buf + pos, size);
173
- pos += size;
174
- os->buf.start += size;
175
- }
176
- }
177
- }
178
-
179
- /**
180
- * Create a newly allocated and initialized InStream
181
- *
182
- * @return a newly allocated and initialized InStream
183
- */
184
- FrtInStream *frt_is_new(void) {
185
- FrtInStream *is = FRT_ALLOC(FrtInStream);
186
- is->f = FRT_ALLOC_AND_ZERO(FrtInStreamFile);
187
- is->f->ref_cnt = 1;
188
- is->buf.start = 0;
189
- is->buf.pos = 0;
190
- is->buf.len = 0;
191
- is->ref_cnt = 1;
192
- return is;
193
- }
194
-
195
- /**
196
- * Refill the InStream's buffer from the store source (filesystem or memory).
197
- *
198
- * @param is the InStream to refill
199
- * @raise FRT_IO_ERROR if there is a error reading from the filesystem
200
- * @raise FRT_EOF_ERROR if there is an attempt to read past the end of the file
201
- */
202
- static void is_refill(FrtInStream *is)
203
- {
204
- frt_off_t start = is->buf.start + is->buf.pos;
205
- frt_off_t last = start + FRT_BUFFER_SIZE;
206
- frt_off_t flen = is->m->length_i(is);
207
-
208
- if (last > flen) { /* don't read past EOF */
209
- last = flen;
210
- }
211
-
212
- is->buf.len = last - start;
213
- if (is->buf.len <= 0) {
214
- FRT_RAISE(FRT_EOF_ERROR, "current pos = %"FRT_OFF_T_PFX"d, "
215
- "file length = %"FRT_OFF_T_PFX"d", start, flen);
216
- }
217
-
218
- is->m->read_i(is, is->buf.buf, is->buf.len);
219
-
220
- is->buf.start = start;
221
- is->buf.pos = 0;
222
- }
223
-
224
- /**
225
- * Unsafe alternative to frt_is_read_byte. Only use this method when you know
226
- * there is no chance that you will read past the end of the InStream's
227
- * buffer.
228
- */
229
- #define read_byte(is) is->buf.buf[is->buf.pos++]
230
-
231
- /**
232
- * Read a singly byte (unsigned char) from the InStream +is+.
233
- *
234
- * @param is the Instream to read from
235
- * @return a single unsigned char read from the InStream +is+
236
- * @raise FRT_IO_ERROR if there is a error reading from the filesystem
237
- * @raise FRT_EOF_ERROR if there is an attempt to read past the end of the file
238
- */
239
- frt_uchar frt_is_read_byte(FrtInStream *is)
240
- {
241
- if (is->buf.pos >= is->buf.len) {
242
- is_refill(is);
243
- }
244
-
245
- return read_byte(is);
246
- }
247
-
248
- off_t frt_is_pos(FrtInStream *is)
249
- {
250
- return is->buf.start + is->buf.pos;
251
- }
252
-
253
- frt_uchar *frt_is_read_bytes(FrtInStream *is, frt_uchar *buf, int len)
254
- {
255
- int i;
256
- frt_off_t start;
257
-
258
- if ((is->buf.pos + len) < is->buf.len) {
259
- for (i = 0; i < len; i++) {
260
- buf[i] = read_byte(is);
261
- }
262
- }
263
- else { /* read all-at-once */
264
- start = frt_is_pos(is);
265
- is->m->seek_i(is, start);
266
- is->m->read_i(is, buf, len);
267
-
268
- is->buf.start = start + len; /* adjust stream variables */
269
- is->buf.pos = 0;
270
- is->buf.len = 0; /* trigger refill on read */
271
- }
272
- return buf;
273
- }
274
-
275
- void frt_is_seek(FrtInStream *is, frt_off_t pos) {
276
- if (pos >= is->buf.start && pos < (is->buf.start + is->buf.len)) {
277
- is->buf.pos = pos - is->buf.start; /* seek within buffer */
278
- } else {
279
- is->buf.start = pos;
280
- is->buf.pos = 0;
281
- is->buf.len = 0; /* trigger refill() on read() */
282
- is->m->seek_i(is, pos);
283
- }
284
- }
285
-
286
- void frt_is_close(FrtInStream *is) {
287
- if (is->ref_cnt == 0) {
288
- FRT_RAISE(FRT_STATE_ERROR, "is ref_cnt to low\n");
289
- }
290
-
291
- if (FRT_DEREF(is) == 0) {
292
- if (FRT_DEREF(is->f) == 0) {
293
- is->m->close_i(is);
294
- free(is->f);
295
- }
296
- free(is);
297
- }
298
- }
299
-
300
- FrtInStream *frt_is_clone(FrtInStream *is)
301
- {
302
- if (!(is->f))
303
- return NULL;
304
- FrtInStream *new_is = FRT_ALLOC(FrtInStream);
305
- memcpy(new_is, is, sizeof(FrtInStream));
306
- new_is->ref_cnt = 1;
307
- FRT_REF(new_is->f);
308
- return new_is;
309
- }
310
-
311
- frt_i32 frt_is_read_i32(FrtInStream *is)
312
- {
313
- return ((frt_i32)frt_is_read_byte(is) << 24) |
314
- ((frt_i32)frt_is_read_byte(is) << 16) |
315
- ((frt_i32)frt_is_read_byte(is) << 8) |
316
- ((frt_i32)frt_is_read_byte(is));
317
- }
318
-
319
-
320
- frt_i64 frt_is_read_i64(FrtInStream *is)
321
- {
322
- return ((frt_i64)frt_is_read_byte(is) << 56) |
323
- ((frt_i64)frt_is_read_byte(is) << 48) |
324
- ((frt_i64)frt_is_read_byte(is) << 40) |
325
- ((frt_i64)frt_is_read_byte(is) << 32) |
326
- ((frt_i64)frt_is_read_byte(is) << 24) |
327
- ((frt_i64)frt_is_read_byte(is) << 16) |
328
- ((frt_i64)frt_is_read_byte(is) << 8) |
329
- ((frt_i64)frt_is_read_byte(is));
330
- }
331
-
332
- frt_u32 frt_is_read_u32(FrtInStream *is)
333
- {
334
- return ((frt_u32)frt_is_read_byte(is) << 24) |
335
- ((frt_u32)frt_is_read_byte(is) << 16) |
336
- ((frt_u32)frt_is_read_byte(is) << 8) |
337
- ((frt_u32)frt_is_read_byte(is));
338
- }
339
-
340
- frt_u64 frt_is_read_u64(FrtInStream *is)
341
- {
342
- return ((frt_u64)frt_is_read_byte(is) << 56) |
343
- ((frt_u64)frt_is_read_byte(is) << 48) |
344
- ((frt_u64)frt_is_read_byte(is) << 40) |
345
- ((frt_u64)frt_is_read_byte(is) << 32) |
346
- ((frt_u64)frt_is_read_byte(is) << 24) |
347
- ((frt_u64)frt_is_read_byte(is) << 16) |
348
- ((frt_u64)frt_is_read_byte(is) << 8) |
349
- ((frt_u64)frt_is_read_byte(is));
350
- }
351
-
352
- /* optimized to use unchecked read_byte if there is definitely space */
353
- unsigned int frt_is_read_vint(FrtInStream *is)
354
- {
355
- register unsigned int res, b;
356
- register int shift = 7;
357
-
358
- if (is->buf.pos > (is->buf.len - VINT_MAX_LEN)) {
359
- b = frt_is_read_byte(is);
360
- res = b & 0x7F; /* 0x7F = 0b01111111 */
361
-
362
- while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
363
- b = frt_is_read_byte(is);
364
- res |= (b & 0x7F) << shift;
365
- shift += 7;
366
- }
367
- }
368
- else { /* unchecked optimization */
369
- b = read_byte(is);
370
- res = b & 0x7F; /* 0x7F = 0b01111111 */
371
-
372
- while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
373
- b = read_byte(is);
374
- res |= (b & 0x7F) << shift;
375
- shift += 7;
376
- }
377
- }
378
-
379
- return res;
380
- }
381
-
382
- /* optimized to use unchecked read_byte if there is definitely space */
383
- off_t frt_is_read_voff_t(FrtInStream *is)
384
- {
385
- register frt_off_t res, b;
386
- register int shift = 7;
387
-
388
- if (is->buf.pos > (is->buf.len - VINT_MAX_LEN)) {
389
- b = frt_is_read_byte(is);
390
- res = b & 0x7F; /* 0x7F = 0b01111111 */
391
-
392
- while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
393
- b = frt_is_read_byte(is);
394
- res |= (b & 0x7F) << shift;
395
- shift += 7;
396
- }
397
- }
398
- else { /* unchecked optimization */
399
- b = read_byte(is);
400
- res = b & 0x7F; /* 0x7F = 0b01111111 */
401
-
402
- while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
403
- b = read_byte(is);
404
- res |= (b & 0x7F) << shift;
405
- shift += 7;
406
- }
407
- }
408
-
409
- return res;
410
- }
411
-
412
- /* optimized to use unchecked read_byte if there is definitely space */
413
- frt_u64 frt_is_read_vll(FrtInStream *is)
414
- {
415
- register frt_u64 res, b;
416
- register int shift = 7;
417
-
418
- if (is->buf.pos > (is->buf.len - VINT_MAX_LEN)) {
419
- b = frt_is_read_byte(is);
420
- res = b & 0x7F; /* 0x7F = 0b01111111 */
421
-
422
- while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
423
- b = frt_is_read_byte(is);
424
- res |= (b & 0x7F) << shift;
425
- shift += 7;
426
- }
427
- }
428
- else { /* unchecked optimization */
429
- b = read_byte(is);
430
- res = b & 0x7F; /* 0x7F = 0b01111111 */
431
-
432
- while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
433
- b = read_byte(is);
434
- res |= (b & 0x7F) << shift;
435
- shift += 7;
436
- }
437
- }
438
-
439
- return res;
440
- }
441
-
442
- void frt_is_skip_vints(FrtInStream *is, register int cnt)
443
- {
444
- for (; cnt > 0; cnt--) {
445
- while ((frt_is_read_byte(is) & 0x80) != 0) {
446
- }
447
- }
448
- }
449
-
450
- char *frt_is_read_string(FrtInStream *is)
451
- {
452
- register int length = (int) frt_is_read_vint(is);
453
- char *str = FRT_ALLOC_N(char, length + 1);
454
- str[length] = '\0';
455
-
456
- if (is->buf.pos > (is->buf.len - length)) {
457
- register int i;
458
- for (i = 0; i < length; i++) {
459
- str[i] = frt_is_read_byte(is);
460
- }
461
- }
462
- else { /* unchecked optimization */
463
- memcpy(str, is->buf.buf + is->buf.pos, length);
464
- is->buf.pos += length;
465
- }
466
-
467
- return str;
468
- }
469
-
470
- char *frt_is_read_string_safe(FrtInStream *is)
471
- {
472
- register int length = (int) frt_is_read_vint(is);
473
- char *str = FRT_ALLOC_N(char, length + 1);
474
- str[length] = '\0';
475
-
476
- FRT_TRY
477
- if (is->buf.pos > (is->buf.len - length)) {
478
- register int i;
479
- for (i = 0; i < length; i++) {
480
- str[i] = frt_is_read_byte(is);
481
- }
482
- }
483
- else { /* unchecked optimization */
484
- memcpy(str, is->buf.buf + is->buf.pos, length);
485
- is->buf.pos += length;
486
- }
487
- FRT_XCATCHALL
488
- free(str);
489
- FRT_XENDTRY
490
-
491
- return str;
492
- }
493
-
494
- void frt_os_write_i32(FrtOutStream *os, frt_i32 num)
495
- {
496
- frt_os_write_byte(os, (frt_uchar)((num >> 24) & 0xFF));
497
- frt_os_write_byte(os, (frt_uchar)((num >> 16) & 0xFF));
498
- frt_os_write_byte(os, (frt_uchar)((num >> 8) & 0xFF));
499
- frt_os_write_byte(os, (frt_uchar)(num & 0xFF));
500
- }
501
-
502
- void frt_os_write_i64(FrtOutStream *os, frt_i64 num)
503
- {
504
- frt_os_write_byte(os, (frt_uchar)((num >> 56) & 0xFF));
505
- frt_os_write_byte(os, (frt_uchar)((num >> 48) & 0xFF));
506
- frt_os_write_byte(os, (frt_uchar)((num >> 40) & 0xFF));
507
- frt_os_write_byte(os, (frt_uchar)((num >> 32) & 0xFF));
508
- frt_os_write_byte(os, (frt_uchar)((num >> 24) & 0xFF));
509
- frt_os_write_byte(os, (frt_uchar)((num >> 16) & 0xFF));
510
- frt_os_write_byte(os, (frt_uchar)((num >> 8) & 0xFF));
511
- frt_os_write_byte(os, (frt_uchar)(num & 0xFF));
512
- }
513
-
514
- void frt_os_write_u32(FrtOutStream *os, frt_u32 num)
515
- {
516
- frt_os_write_byte(os, (frt_uchar)((num >> 24) & 0xFF));
517
- frt_os_write_byte(os, (frt_uchar)((num >> 16) & 0xFF));
518
- frt_os_write_byte(os, (frt_uchar)((num >> 8) & 0xFF));
519
- frt_os_write_byte(os, (frt_uchar)(num & 0xFF));
520
- }
521
-
522
- void frt_os_write_u64(FrtOutStream *os, frt_u64 num)
523
- {
524
- frt_os_write_byte(os, (frt_uchar)((num >> 56) & 0xFF));
525
- frt_os_write_byte(os, (frt_uchar)((num >> 48) & 0xFF));
526
- frt_os_write_byte(os, (frt_uchar)((num >> 40) & 0xFF));
527
- frt_os_write_byte(os, (frt_uchar)((num >> 32) & 0xFF));
528
- frt_os_write_byte(os, (frt_uchar)((num >> 24) & 0xFF));
529
- frt_os_write_byte(os, (frt_uchar)((num >> 16) & 0xFF));
530
- frt_os_write_byte(os, (frt_uchar)((num >> 8) & 0xFF));
531
- frt_os_write_byte(os, (frt_uchar)(num & 0xFF));
532
- }
533
-
534
- /* optimized to use an unchecked write if there is space */
535
- void frt_os_write_vint(FrtOutStream *os, register unsigned int num)
536
- {
537
- if (os->buf.pos > VINT_END) {
538
- while (num > 127) {
539
- frt_os_write_byte(os, (frt_uchar)((num & 0x7f) | 0x80));
540
- num >>= 7;
541
- }
542
- frt_os_write_byte(os, (frt_uchar)(num));
543
- }
544
- else {
545
- while (num > 127) {
546
- write_byte(os, (frt_uchar)((num & 0x7f) | 0x80));
547
- num >>= 7;
548
- }
549
- write_byte(os, (frt_uchar)(num));
550
- }
551
- }
552
-
553
- /* optimized to use an unchecked write if there is space */
554
- void frt_os_write_voff_t(FrtOutStream *os, register frt_off_t num)
555
- {
556
- if (os->buf.pos > VINT_END) {
557
- while (num > 127) {
558
- frt_os_write_byte(os, (frt_uchar)((num & 0x7f) | 0x80));
559
- num >>= 7;
560
- }
561
- frt_os_write_byte(os, (frt_uchar)num);
562
- }
563
- else {
564
- while (num > 127) {
565
- write_byte(os, (frt_uchar)((num & 0x7f) | 0x80));
566
- num >>= 7;
567
- }
568
- write_byte(os, (frt_uchar)num);
569
- }
570
- }
571
-
572
- /* optimized to use an unchecked write if there is space */
573
- void frt_os_write_vll(FrtOutStream *os, register frt_u64 num)
574
- {
575
- if (os->buf.pos > VINT_END) {
576
- while (num > 127) {
577
- frt_os_write_byte(os, (frt_uchar)((num & 0x7f) | 0x80));
578
- num >>= 7;
579
- }
580
- frt_os_write_byte(os, (frt_uchar)num);
581
- }
582
- else {
583
- while (num > 127) {
584
- write_byte(os, (frt_uchar)((num & 0x7f) | 0x80));
585
- num >>= 7;
586
- }
587
- write_byte(os, (frt_uchar)num);
588
- }
589
- }
590
-
591
- void frt_os_write_string_len(FrtOutStream *os, const char *str, int len)
592
- {
593
- frt_os_write_vint(os, len);
594
- frt_os_write_bytes(os, (frt_uchar *)str, len);
595
- }
596
- void frt_os_write_string(FrtOutStream *os, const char *str)
597
- {
598
- frt_os_write_string_len(os, str, (int)strlen(str));
599
- }
600
-
601
90
  /**
602
91
  * Determine if the filename is the name of a lock file. Return 1 if it is, 0
603
92
  * otherwise.