Line data Source code
1 : /*
2 : * Copyright (C) 2011 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "support.h"
21 : #include "util.h"
22 : #include "stream.h"
23 :
24 : /****************************************************************************/
25 : /* stream */
26 :
27 : unsigned STREAM_SIZE = 1024 * 64;
28 :
29 1819 : STREAM* sopen_read(const char* file)
30 : {
31 : #if HAVE_POSIX_FADVISE
32 : int ret;
33 : #endif
34 1819 : STREAM* s = malloc_nofail(sizeof(STREAM));
35 :
36 1819 : s->handle_size = 1;
37 1819 : s->handle = malloc_nofail(sizeof(struct stream_handle));
38 :
39 1819 : pathcpy(s->handle[0].path, sizeof(s->handle[0].path), file);
40 1819 : s->handle[0].f = open(file, O_RDONLY | O_BINARY | O_SEQUENTIAL);
41 1819 : if (s->handle[0].f == -1) {
42 50 : free(s->handle);
43 50 : free(s);
44 50 : return 0;
45 : }
46 :
47 : #if HAVE_POSIX_FADVISE
48 : /* advise sequential access */
49 1769 : ret = posix_fadvise(s->handle[0].f, 0, 0, POSIX_FADV_SEQUENTIAL);
50 1769 : if (ret != 0) {
51 : /* LCOV_EXCL_START */
52 : close(s->handle[0].f);
53 : free(s->handle);
54 : free(s);
55 : return 0;
56 : /* LCOV_EXCL_STOP */
57 : }
58 : #endif
59 :
60 1769 : s->buffer = malloc_nofail_test(STREAM_SIZE);
61 1769 : s->pos = s->buffer;
62 1769 : s->end = s->buffer;
63 1769 : s->state = STREAM_STATE_READ;
64 1769 : s->state_index = 0;
65 1769 : s->offset = 0;
66 1769 : s->offset_uncached = 0;
67 1769 : s->crc = 0;
68 1769 : s->crc_uncached = 0;
69 1769 : s->crc_stream = CRC_IV;
70 :
71 1769 : return s;
72 : }
73 :
74 164 : STREAM* sopen_multi_write(unsigned count)
75 : {
76 : unsigned i;
77 :
78 164 : STREAM* s = malloc_nofail(sizeof(STREAM));
79 :
80 164 : s->handle_size = count;
81 164 : s->handle = malloc_nofail(count * sizeof(struct stream_handle));
82 :
83 1303 : for (i = 0; i < count; ++i)
84 1139 : s->handle[i].f = -1;
85 :
86 164 : s->buffer = malloc_nofail_test(STREAM_SIZE);
87 164 : s->pos = s->buffer;
88 164 : s->end = s->buffer + STREAM_SIZE;
89 164 : s->state = STREAM_STATE_WRITE;
90 164 : s->state_index = 0;
91 164 : s->offset = 0;
92 164 : s->offset_uncached = 0;
93 164 : s->crc = 0;
94 164 : s->crc_uncached = 0;
95 164 : s->crc_stream = CRC_IV;
96 :
97 164 : return s;
98 : }
99 :
100 1139 : int sopen_multi_file(STREAM* s, unsigned i, const char* file)
101 : {
102 : #if HAVE_POSIX_FADVISE
103 : int ret;
104 : #endif
105 : int f;
106 :
107 1139 : pathcpy(s->handle[i].path, sizeof(s->handle[i].path), file);
108 :
109 1139 : f = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_SEQUENTIAL, 0600);
110 1139 : if (f == -1) {
111 : /* LCOV_EXCL_START */
112 : return -1;
113 : /* LCOV_EXCL_STOP */
114 : }
115 :
116 : #if HAVE_POSIX_FADVISE
117 : /* advise sequential access */
118 1139 : ret = posix_fadvise(f, 0, 0, POSIX_FADV_SEQUENTIAL);
119 1139 : if (ret != 0) {
120 : /* LCOV_EXCL_START */
121 : close(f);
122 : return -1;
123 : /* LCOV_EXCL_STOP */
124 : }
125 : #endif
126 :
127 1139 : s->handle[i].f = f;
128 :
129 1139 : return 0;
130 : }
131 :
132 0 : STREAM* sopen_write(const char* file)
133 : {
134 0 : STREAM* s = sopen_multi_write(1);
135 :
136 0 : if (sopen_multi_file(s, 0, file) != 0) {
137 0 : sclose(s);
138 0 : return 0;
139 : }
140 :
141 0 : return s;
142 : }
143 :
144 1931 : int sclose(STREAM* s)
145 : {
146 1931 : int fail = 0;
147 : unsigned i;
148 :
149 1931 : if (s->state == STREAM_STATE_WRITE) {
150 164 : if (sflush(s) != 0) {
151 : /* LCOV_EXCL_START */
152 : fail = 1;
153 : /* LCOV_EXCL_STOP */
154 : }
155 : }
156 :
157 4837 : for (i = 0; i < s->handle_size; ++i) {
158 2906 : if (close(s->handle[i].f) != 0) {
159 : /* LCOV_EXCL_START */
160 : fail = 1;
161 : /* LCOV_EXCL_STOP */
162 : }
163 : }
164 :
165 1931 : free(s->handle);
166 1931 : free(s->buffer);
167 1931 : free(s);
168 :
169 1931 : if (fail) {
170 : /* LCOV_EXCL_START */
171 : return -1;
172 : /* LCOV_EXCL_STOP */
173 : }
174 :
175 1931 : return 0;
176 : }
177 :
178 245 : int shandle(STREAM* s)
179 : {
180 245 : if (!s->handle_size) {
181 : /* LCOV_EXCL_START */
182 : return -1;
183 : /* LCOV_EXCL_STOP */
184 : }
185 :
186 245 : return s->handle[0].f;
187 : }
188 :
189 : /**
190 : * Fill the read stream buffer.
191 : * \return 0 if at least on char is read, or EOF on error.
192 : */
193 607043 : static int sfill(STREAM* s)
194 : {
195 : ssize_t ret;
196 :
197 607043 : if (s->state != STREAM_STATE_READ) {
198 : /* LCOV_EXCL_START */
199 : return EOF;
200 : /* LCOV_EXCL_STOP */
201 : }
202 :
203 606019 : ret = read(s->handle[0].f, s->buffer, STREAM_SIZE);
204 :
205 606019 : if (ret < 0) {
206 : /* LCOV_EXCL_START */
207 : s->state = STREAM_STATE_ERROR;
208 : return EOF;
209 : /* LCOV_EXCL_STOP */
210 : }
211 606019 : if (ret == 0) {
212 1640 : s->state = STREAM_STATE_EOF;
213 1640 : return EOF;
214 : }
215 :
216 : /* update the crc */
217 604379 : s->crc_uncached = s->crc;
218 604379 : s->crc = crc32c(s->crc, s->buffer, ret);
219 :
220 : /* update the offset */
221 604379 : s->offset_uncached = s->offset;
222 604379 : s->offset += ret;
223 :
224 604379 : s->pos = s->buffer;
225 604379 : s->end = s->buffer + ret;
226 :
227 604379 : return 0;
228 : }
229 :
230 1140 : int sdeplete(STREAM* s, unsigned char* last)
231 : {
232 : /* last four bytes */
233 1140 : last[0] = 0;
234 1140 : last[1] = 0;
235 1140 : last[2] = 0;
236 1140 : last[3] = 0;
237 :
238 : while (1) {
239 : /* increase the position up to 4 bytes before the end */
240 308343 : if (s->pos + 4 <= s->end)
241 148468 : s->pos = s->end - 4;
242 :
243 : /* insert the last 4 bytes */
244 1470326 : while (s->pos < s->end) {
245 853640 : last[0] = last[1];
246 853640 : last[1] = last[2];
247 853640 : last[2] = last[3];
248 853640 : last[3] = *s->pos++;
249 : }
250 :
251 : /* fill again the buffer until the end of the file */
252 308343 : if (sfill(s) != 0) {
253 : /* on error fail */
254 1140 : if (serror(s)) {
255 : /* LCOV_EXCL_START */
256 : return EOF;
257 : /* LCOV_EXCL_STOP */
258 : }
259 :
260 : /* on EOF terminate */
261 1140 : break;
262 : }
263 307203 : }
264 :
265 1140 : return 0;
266 : }
267 :
268 39014 : int sflush(STREAM* s)
269 : {
270 : ssize_t ret;
271 : ssize_t size;
272 : unsigned i;
273 :
274 39014 : if (s->state != STREAM_STATE_WRITE) {
275 : /* LCOV_EXCL_START */
276 : return EOF;
277 : /* LCOV_EXCL_STOP */
278 : }
279 :
280 39014 : size = s->pos - s->buffer;
281 39014 : if (!size)
282 148 : return 0;
283 :
284 347067 : for (i = 0; i < s->handle_size; ++i) {
285 308201 : ret = write(s->handle[i].f, s->buffer, size);
286 :
287 308201 : if (ret != size) {
288 : /* LCOV_EXCL_START */
289 : s->state = STREAM_STATE_ERROR;
290 : s->state_index = i;
291 : return EOF;
292 : /* LCOV_EXCL_STOP */
293 : }
294 : }
295 :
296 : /*
297 : * Update the crc *after* writing the data.
298 : *
299 : * This must be done after the file write,
300 : * to be able to detect memory errors on the buffer,
301 : * happening during the write.
302 : */
303 38866 : s->crc = crc32c(s->crc, s->buffer, size);
304 38866 : s->crc_uncached = s->crc;
305 :
306 : /* update the offset */
307 38866 : s->offset += size;
308 38866 : s->offset_uncached = s->offset;
309 :
310 38866 : s->pos = s->buffer;
311 :
312 38866 : return 0;
313 : }
314 :
315 1 : int64_t stell(STREAM* s)
316 : {
317 1 : return s->offset_uncached + (s->pos - s->buffer);
318 : }
319 :
320 1676 : uint32_t scrc(STREAM*s)
321 : {
322 1676 : return crc32c(s->crc_uncached, s->buffer, s->pos - s->buffer);
323 : }
324 :
325 164 : uint32_t scrc_stream(STREAM*s)
326 : {
327 164 : return s->crc_stream ^ CRC_IV;
328 : }
329 :
330 298700 : int sgetc_uncached(STREAM* s)
331 : {
332 : /* if at the end of the buffer, fill it */
333 298700 : if (s->pos == s->end && sfill(s) != 0)
334 1524 : return EOF;
335 297176 : return *s->pos++;
336 : }
337 :
338 7960 : int sgettok(STREAM* f, char* str, int size)
339 : {
340 7960 : char* i = str;
341 7960 : char* send = str + size;
342 : int c;
343 :
344 : while (1) {
345 53173 : c = sgetc(f);
346 53173 : if (c == EOF) {
347 256 : break;
348 : }
349 52917 : if (c == ' ' || c == '\t') {
350 7447 : sungetc(c, f);
351 7447 : break;
352 : }
353 45470 : if (c == '\n') {
354 : /* remove ending carrige return to support the Windows CR+LF format */
355 257 : if (i != str && i[-1] == '\r')
356 0 : --i;
357 257 : sungetc(c, f);
358 257 : break;
359 : }
360 :
361 45213 : *i++ = c;
362 :
363 45213 : if (i == send) {
364 : /* LCOV_EXCL_START */
365 : return -1;
366 : /* LCOV_EXCL_STOP */
367 : }
368 45213 : }
369 :
370 7960 : *i = 0;
371 :
372 7960 : return i - str;
373 : }
374 :
375 12521197 : int sread(STREAM* f, void* void_data, unsigned size)
376 : {
377 12521197 : unsigned char* data = void_data;
378 :
379 : /* if there is enough space in memory */
380 12521197 : if (sptrlookup(f, size)) {
381 : /* optimized version with all the data in memory */
382 12494935 : unsigned char* pos = sptrget(f);
383 :
384 : /* copy it */
385 212722886 : while (size--)
386 187733016 : *data++ = *pos++;
387 :
388 12494935 : sptrset(f, pos);
389 : } else {
390 : /* standard version using sgetc() */
391 1376463 : while (size--) {
392 1323939 : int c = sgetc(f);
393 1323939 : if (c == EOF) {
394 : /* LCOV_EXCL_START */
395 : return -1;
396 : /* LCOV_EXCL_STOP */
397 : }
398 :
399 1323939 : *data++ = c;
400 : }
401 : }
402 :
403 12521197 : return 0;
404 : }
405 :
406 5297 : int sgetline(STREAM* f, char* str, int size)
407 : {
408 5297 : char* i = str;
409 5297 : char* send = str + size;
410 : int c;
411 :
412 : /* if there is enough data in memory */
413 5297 : if (sptrlookup(f, size)) {
414 : /* optimized version with all the data in memory */
415 0 : unsigned char* pos = sptrget(f);
416 :
417 : while (1) {
418 0 : c = *pos++;
419 0 : if (c == '\n') {
420 : /* remove ending carrige return to support the Windows CR+LF format */
421 0 : if (i != str && i[-1] == '\r')
422 0 : --i;
423 0 : --pos;
424 0 : break;
425 : }
426 :
427 0 : *i++ = c;
428 :
429 0 : if (i == send) {
430 : /* LCOV_EXCL_START */
431 : return -1;
432 : /* LCOV_EXCL_STOP */
433 : }
434 0 : }
435 :
436 0 : sptrset(f, pos);
437 : } else {
438 : while (1) {
439 72066 : c = sgetc(f);
440 72066 : if (c == EOF) {
441 : /* LCOV_EXCL_START */
442 : break;
443 : /* LCOV_EXCL_STOP */
444 : }
445 72066 : if (c == '\n') {
446 : /* remove ending carrige return to support the Windows CR+LF format */
447 5297 : if (i != str && i[-1] == '\r')
448 0 : --i;
449 5297 : sungetc(c, f);
450 5297 : break;
451 : }
452 :
453 66769 : *i++ = c;
454 :
455 66769 : if (i == send) {
456 : /* LCOV_EXCL_START */
457 : return -1;
458 : /* LCOV_EXCL_STOP */
459 : }
460 66769 : }
461 : }
462 :
463 5297 : *i = 0;
464 :
465 5297 : return i - str;
466 : }
467 :
468 5263 : int sgetlasttok(STREAM* f, char* str, int size)
469 : {
470 : int ret;
471 :
472 5263 : ret = sgetline(f, str, size);
473 5263 : if (ret < 0) {
474 : /* LCOV_EXCL_START */
475 : return ret;
476 : /* LCOV_EXCL_STOP */
477 : }
478 :
479 10526 : while (ret > 0 && (str[ret - 1] == ' ' || str[ret - 1] == '\t'))
480 0 : --ret;
481 :
482 5263 : str[ret] = 0;
483 :
484 5263 : return ret;
485 : }
486 :
487 257 : int sgetu32(STREAM* f, uint32_t* value)
488 : {
489 : int c;
490 :
491 257 : c = sgetc(f);
492 257 : if (c >= '0' && c <= '9') {
493 : uint32_t v;
494 :
495 257 : v = c - '0';
496 :
497 257 : c = sgetc(f);
498 514 : while (c >= '0' && c <= '9') {
499 0 : v *= 10;
500 0 : v += c - '0';
501 0 : c = sgetc(f);
502 : }
503 :
504 257 : *value = v;
505 :
506 257 : sungetc(c, f);
507 257 : return 0;
508 : } else {
509 : /* LCOV_EXCL_START */
510 : /* nothing read */
511 : return -1;
512 : /* LCOV_EXCL_STOP */
513 : }
514 : }
515 :
516 18262658 : int sgetb32(STREAM* f, uint32_t* value)
517 : {
518 : uint32_t v;
519 : unsigned char b;
520 : unsigned char s;
521 : int c;
522 :
523 18262658 : v = 0;
524 18262658 : s = 0;
525 : loop:
526 34952526 : c = sgetc(f);
527 34952526 : if (c == EOF) {
528 : /* LCOV_EXCL_START */
529 : return -1;
530 : /* LCOV_EXCL_STOP */
531 : }
532 :
533 34952526 : b = (unsigned char)c;
534 34952526 : if ((b & 0x80) == 0) {
535 16689868 : v |= (uint32_t)b << s;
536 16689868 : s += 7;
537 16689868 : if (s >= 32) {
538 : /* LCOV_EXCL_START */
539 : return -1;
540 : /* LCOV_EXCL_STOP */
541 : }
542 16689868 : goto loop;
543 : }
544 :
545 18262658 : v |= (uint32_t)(b & 0x7f) << s;
546 :
547 18262658 : *value = v;
548 :
549 18262658 : return 0;
550 : }
551 :
552 10607045 : int sgetb64(STREAM* f, uint64_t* value)
553 : {
554 : uint64_t v;
555 : unsigned char b;
556 : unsigned char s;
557 : int c;
558 :
559 10607045 : v = 0;
560 10607045 : s = 0;
561 : loop:
562 38787515 : c = sgetc(f);
563 38787515 : if (c == EOF) {
564 : /* LCOV_EXCL_START */
565 : return -1;
566 : /* LCOV_EXCL_STOP */
567 : }
568 :
569 38787515 : b = (unsigned char)c;
570 38787515 : if ((b & 0x80) == 0) {
571 28180470 : v |= (uint64_t)b << s;
572 28180470 : s += 7;
573 28180470 : if (s >= 64) {
574 : /* LCOV_EXCL_START */
575 : return -1;
576 : /* LCOV_EXCL_STOP */
577 : }
578 28180470 : goto loop;
579 : }
580 :
581 10607045 : v |= (uint64_t)(b & 0x7f) << s;
582 :
583 10607045 : *value = v;
584 :
585 10607045 : return 0;
586 : }
587 :
588 372 : int sgetble32(STREAM* f, uint32_t* value)
589 : {
590 : unsigned char buf[4];
591 :
592 372 : if (sread(f, buf, 4) != 0) {
593 : /* LCOV_EXCL_START */
594 : return -1;
595 : /* LCOV_EXCL_STOP */
596 : }
597 :
598 372 : *value = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
599 :
600 372 : return 0;
601 : }
602 :
603 3782904 : int sgetbs(STREAM* f, char* str, int size)
604 : {
605 : uint32_t len;
606 :
607 3782904 : if (sgetb32(f, &len) < 0) {
608 : /* LCOV_EXCL_START */
609 : return -1;
610 : /* LCOV_EXCL_STOP */
611 : }
612 :
613 3782904 : if (len + 1 > (uint32_t)size) {
614 : /* LCOV_EXCL_START */
615 : return -1;
616 : /* LCOV_EXCL_STOP */
617 : }
618 :
619 3782904 : str[len] = 0;
620 :
621 3782904 : return sread(f, str, (int)len);
622 : }
623 :
624 20473005 : int swrite(const void* void_data, unsigned size, STREAM* f)
625 : {
626 20473005 : const unsigned char* data = void_data;
627 :
628 : /* if there is enough space in memory */
629 20473005 : if (sptrlookup(f, size)) {
630 : /* optimized version with all the data in memory */
631 20466902 : unsigned char* pos = sptrget(f);
632 :
633 : /**
634 : * Update the crc *before* writing the data in the buffer
635 : *
636 : * This must be done before the memory write,
637 : * to be able to detect memory errors on the buffer,
638 : * happening before we write it on the file.
639 : */
640 20466902 : f->crc_stream = crc32c_plain(f->crc_stream, data, size);
641 :
642 : /* copy it */
643 170277665 : while (size--)
644 129343861 : *pos++ = *data++;
645 :
646 20466902 : sptrset(f, pos);
647 : } else {
648 : /* standard version using sputc() */
649 200827 : while (size--) {
650 188621 : if (sputc(*data++, f) != 0) {
651 : /* LCOV_EXCL_START */
652 : return -1;
653 : /* LCOV_EXCL_STOP */
654 : }
655 : }
656 : }
657 :
658 20473005 : return 0;
659 : }
660 :
661 9035051 : int sputb32(uint32_t value, STREAM* s)
662 : {
663 : unsigned char b;
664 : unsigned char buf[16];
665 : unsigned i;
666 :
667 9035051 : i = 0;
668 : loop:
669 17245172 : b = value & 0x7f;
670 17245172 : value >>= 7;
671 :
672 17245172 : if (value) {
673 8210121 : buf[i++] = b;
674 8210121 : goto loop;
675 : }
676 :
677 9035051 : buf[i++] = b | 0x80;
678 :
679 9035051 : return swrite(buf, i, s);
680 : }
681 :
682 5244883 : int sputb64(uint64_t value, STREAM* s)
683 : {
684 : unsigned char b;
685 : unsigned char buf[16];
686 : unsigned i;
687 :
688 5244883 : i = 0;
689 : loop:
690 19175251 : b = value & 0x7f;
691 19175251 : value >>= 7;
692 :
693 19175251 : if (value) {
694 13930368 : buf[i++] = b;
695 13930368 : goto loop;
696 : }
697 :
698 5244883 : buf[i++] = b | 0x80;
699 :
700 5244883 : return swrite(buf, i, s);
701 : }
702 :
703 164 : int sputble32(uint32_t value, STREAM* s)
704 : {
705 : unsigned char buf[4];
706 :
707 164 : buf[0] = value & 0xFF;
708 164 : buf[1] = (value >> 8) & 0xFF;
709 164 : buf[2] = (value >> 16) & 0xFF;
710 164 : buf[3] = (value >> 24) & 0xFF;
711 :
712 164 : return swrite(buf, 4, s);
713 : }
714 :
715 1867379 : int sputbs(const char* str, STREAM* f)
716 : {
717 1867379 : size_t len = strlen(str);
718 :
719 1867379 : if (sputb32(len, f) != 0) {
720 : /* LCOV_EXCL_START */
721 : return -1;
722 : /* LCOV_EXCL_STOP */
723 : }
724 :
725 1867379 : return swrite(str, len, f);
726 : }
727 :
728 : #if HAVE_FSYNC
729 148 : int ssync(STREAM* s)
730 : {
731 : unsigned i;
732 :
733 1159 : for (i = 0; i < s->handle_size; ++i) {
734 1011 : if (fsync(s->handle[i].f) != 0) {
735 : /* LCOV_EXCL_START */
736 : s->state = STREAM_STATE_ERROR;
737 : s->state_index = i;
738 : return -1;
739 : /* LCOV_EXCL_STOP */
740 : }
741 : }
742 :
743 148 : return 0;
744 : }
745 : #endif
746 :
|