LCOV - code coverage report
Current view: top level - cmdline - stream.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 326 332 98.2 %
Date: 2015-05-12 Functions: 31 31 100.0 %

          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             : /* hex conversion table */
      26             : 
      27             : static char strhexset[16] = "0123456789abcdef";
      28             : 
      29             : static unsigned strdecset[256] =
      30             : {
      31             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      32             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      33             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      34             :         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
      35             :         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      36             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      37             :         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      38             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      39             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      40             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      41             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      42             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      43             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      44             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      45             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      46             :         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
      47             : };
      48             : 
      49             : /****************************************************************************/
      50             : /* stream */
      51             : 
      52             : unsigned STREAM_SIZE = 1024 * 64;
      53             : 
      54         637 : STREAM* sopen_read(const char* file)
      55             : {
      56             : #if HAVE_POSIX_FADVISE
      57             :         int ret;
      58             : #endif
      59         637 :         STREAM* s = malloc_nofail(sizeof(STREAM));
      60             : 
      61         637 :         s->handle_size = 1;
      62         637 :         s->handle = malloc_nofail(sizeof(struct stream_handle));
      63             : 
      64         637 :         pathcpy(s->handle[0].path, sizeof(s->handle[0].path), file);
      65         637 :         s->handle[0].f = open(file, O_RDONLY | O_BINARY | O_SEQUENTIAL);
      66         637 :         if (s->handle[0].f == -1) {
      67          50 :                 free(s->handle);
      68          50 :                 free(s);
      69          50 :                 return 0;
      70             :         }
      71             : 
      72             : #if HAVE_POSIX_FADVISE
      73             :         /* advise sequential access */
      74         587 :         ret = posix_fadvise(s->handle[0].f, 0, 0, POSIX_FADV_SEQUENTIAL);
      75         587 :         if (ret != 0) {
      76             :                 /* LCOV_EXCL_START */
      77             :                 close(s->handle[0].f);
      78             :                 free(s->handle);
      79             :                 free(s);
      80             :                 return 0;
      81             :                 /* LCOV_EXCL_STOP */
      82             :         }
      83             : #endif
      84             : 
      85         587 :         s->buffer = malloc_nofail_test(STREAM_SIZE);
      86         587 :         s->pos = s->buffer;
      87         587 :         s->end = s->buffer;
      88         587 :         s->state = STREAM_STATE_READ;
      89         587 :         s->state_index = 0;
      90         587 :         s->offset = 0;
      91         587 :         s->offset_uncached = 0;
      92         587 :         s->crc = 0;
      93         587 :         s->crc_uncached = 0;
      94         587 :         s->crc_stream = CRC_IV;
      95             : 
      96         587 :         return s;
      97             : }
      98             : 
      99         136 : STREAM* sopen_multi_write(unsigned count)
     100             : {
     101             :         unsigned i;
     102             : 
     103         136 :         STREAM* s = malloc_nofail(sizeof(STREAM));
     104             : 
     105         136 :         s->handle_size = count;
     106         136 :         s->handle = malloc_nofail(count * sizeof(struct stream_handle));
     107             : 
     108        1089 :         for (i = 0; i < count; ++i)
     109         953 :                 s->handle[i].f = -1;
     110             : 
     111         136 :         s->buffer = malloc_nofail_test(STREAM_SIZE);
     112         136 :         s->pos = s->buffer;
     113         136 :         s->end = s->buffer + STREAM_SIZE;
     114         136 :         s->state = STREAM_STATE_WRITE;
     115         136 :         s->state_index = 0;
     116         136 :         s->offset = 0;
     117         136 :         s->offset_uncached = 0;
     118         136 :         s->crc = 0;
     119         136 :         s->crc_uncached = 0;
     120         136 :         s->crc_stream = CRC_IV;
     121             : 
     122         136 :         return s;
     123             : }
     124             : 
     125         953 : int sopen_multi_file(STREAM* s, unsigned i, const char* file)
     126             : {
     127             : #if HAVE_POSIX_FADVISE
     128             :         int ret;
     129             : #endif
     130             :         int f;
     131             : 
     132         953 :         pathcpy(s->handle[i].path, sizeof(s->handle[i].path), file);
     133             : 
     134         953 :         f = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_SEQUENTIAL, 0600);
     135         953 :         if (f == -1) {
     136             :                 /* LCOV_EXCL_START */
     137             :                 return -1;
     138             :                 /* LCOV_EXCL_STOP */
     139             :         }
     140             : 
     141             : #if HAVE_POSIX_FADVISE
     142             :         /* advise sequential access */
     143         953 :         ret = posix_fadvise(f, 0, 0, POSIX_FADV_SEQUENTIAL);
     144         953 :         if (ret != 0) {
     145             :                 /* LCOV_EXCL_START */
     146             :                 close(f);
     147             :                 return -1;
     148             :                 /* LCOV_EXCL_STOP */
     149             :         }
     150             : #endif
     151             : 
     152         953 :         s->handle[i].f = f;
     153             : 
     154         953 :         return 0;
     155             : }
     156             : 
     157         722 : int sclose(STREAM* s)
     158             : {
     159         722 :         int fail = 0;
     160             :         unsigned i;
     161             : 
     162         722 :         if (s->state == STREAM_STATE_WRITE) {
     163         136 :                 if (sflush(s) != 0) {
     164             :                         /* LCOV_EXCL_START */
     165             :                         fail = 1;
     166             :                         /* LCOV_EXCL_STOP */
     167             :                 }
     168             :         }
     169             : 
     170        2261 :         for (i = 0; i < s->handle_size; ++i) {
     171        1539 :                 if (close(s->handle[i].f) != 0) {
     172             :                         /* LCOV_EXCL_START */
     173             :                         fail = 1;
     174             :                         /* LCOV_EXCL_STOP */
     175             :                 }
     176             :         }
     177             : 
     178         722 :         free(s->handle);
     179         722 :         free(s->buffer);
     180         722 :         free(s);
     181             : 
     182         722 :         if (fail) {
     183             :                 /* LCOV_EXCL_START */
     184             :                 return -1;
     185             :                 /* LCOV_EXCL_STOP */
     186             :         }
     187             : 
     188         722 :         return 0;
     189             : }
     190             : 
     191         224 : int shandle(STREAM* s)
     192             : {
     193         224 :         if (!s->handle_size) {
     194             :                 /* LCOV_EXCL_START */
     195             :                 return -1;
     196             :                 /* LCOV_EXCL_STOP */
     197             :         }
     198             : 
     199         224 :         return s->handle[0].f;
     200             : }
     201             : 
     202      298684 : int sfill(STREAM* s)
     203             : {
     204             :         ssize_t ret;
     205             : 
     206      298684 :         if (s->state != STREAM_STATE_READ) {
     207             :                 /* LCOV_EXCL_START */
     208             :                 return EOF;
     209             :                 /* LCOV_EXCL_STOP */
     210             :         }
     211             : 
     212      297729 :         ret = read(s->handle[0].f, s->buffer, STREAM_SIZE);
     213             : 
     214      297729 :         if (ret < 0) {
     215             :                 /* LCOV_EXCL_START */
     216             :                 s->state = STREAM_STATE_ERROR;
     217             :                 return EOF;
     218             :                 /* LCOV_EXCL_STOP */
     219             :         }
     220      297729 :         if (ret == 0) {
     221         459 :                 s->state = STREAM_STATE_EOF;
     222         459 :                 return EOF;
     223             :         }
     224             : 
     225             :         /* update the crc */
     226      297270 :         s->crc_uncached = s->crc;
     227      297270 :         s->crc = crc32c(s->crc, s->buffer, ret);
     228             : 
     229             :         /* update the offset */
     230      297270 :         s->offset_uncached = s->offset;
     231      297270 :         s->offset += ret;
     232             : 
     233      297270 :         s->pos = s->buffer;
     234      297270 :         s->end = s->buffer + ret;
     235             : 
     236      297270 :         return *s->pos++;
     237             : }
     238             : 
     239       39066 : int sflush(STREAM* s)
     240             : {
     241             :         ssize_t ret;
     242             :         ssize_t size;
     243             :         unsigned i;
     244             : 
     245       39066 :         if (s->state != STREAM_STATE_WRITE) {
     246             :                 /* LCOV_EXCL_START */
     247             :                 return EOF;
     248             :                 /* LCOV_EXCL_STOP */
     249             :         }
     250             : 
     251       39066 :         size = s->pos - s->buffer;
     252       39066 :         if (!size)
     253         120 :                 return 0;
     254             : 
     255             :         /* update the crc */
     256       38946 :         s->crc = crc32c(s->crc, s->buffer, size);
     257       38946 :         s->crc_uncached = s->crc;
     258             : 
     259      347852 :         for (i = 0; i < s->handle_size; ++i) {
     260      308906 :                 ret = write(s->handle[i].f, s->buffer, size);
     261             : 
     262      308906 :                 if (ret != size) {
     263             :                         /* LCOV_EXCL_START */
     264             :                         s->state = STREAM_STATE_ERROR;
     265             :                         s->state_index = i;
     266             :                         return EOF;
     267             :                         /* LCOV_EXCL_STOP */
     268             :                 }
     269             :         }
     270             : 
     271             :         /* update the offset */
     272       38946 :         s->offset += size;
     273       38946 :         s->offset_uncached = s->offset;
     274             : 
     275       38946 :         s->pos = s->buffer;
     276             : 
     277       38946 :         return 0;
     278             : }
     279             : 
     280           1 : int64_t stell(STREAM* s)
     281             : {
     282           1 :         return s->offset_uncached + (s->pos - s->buffer);
     283             : }
     284             : 
     285         478 : uint32_t scrc(STREAM*s)
     286             : {
     287         478 :         return crc32c(s->crc_uncached, s->buffer, s->pos - s->buffer);
     288             : }
     289             : 
     290         131 : uint32_t scrc_stream(STREAM*s)
     291             : {
     292         131 :         return s->crc_stream ^ CRC_IV;
     293             : }
     294             : 
     295      472066 : int sgettok(STREAM* f, char* str, int size)
     296             : {
     297      472066 :         char* i = str;
     298      472066 :         char* send = str + size;
     299             :         int c;
     300             : 
     301             :         while (1) {
     302     2207452 :                 c = sgetc(f);
     303     2207452 :                 if (c == EOF) {
     304         240 :                         break;
     305             :                 }
     306     2207212 :                 if (c == ' ' || c == '\t') {
     307      471501 :                         sungetc(c, f);
     308      471501 :                         break;
     309             :                 }
     310     1735711 :                 if (c == '\n') {
     311             :                         /* remove ending carrige return to support the Windows CR+LF format */
     312         325 :                         if (i != str && i[-1] == '\r')
     313           0 :                                 --i;
     314         325 :                         sungetc(c, f);
     315         325 :                         break;
     316             :                 }
     317             : 
     318     1735386 :                 *i++ = c;
     319             : 
     320     1735386 :                 if (i == send) {
     321             :                         /* LCOV_EXCL_START */
     322             :                         return -1;
     323             :                         /* LCOV_EXCL_STOP */
     324             :                 }
     325     1735386 :         }
     326             : 
     327      472066 :         *i = 0;
     328             : 
     329      472066 :         return i - str;
     330             : }
     331             : 
     332    12063841 : int sread(STREAM* f, void* void_data, unsigned size)
     333             : {
     334    12063841 :         unsigned char* data = void_data;
     335             : 
     336             :         /* if there is enough space in memory */
     337    12063841 :         if (sptrlookup(f, size)) {
     338             :                 /* optimized version with all the data in memory */
     339    12037955 :                 unsigned char* pos = sptrget(f);
     340             : 
     341             :                 /* copy it */
     342   204965594 :                 while (size--)
     343   180889684 :                         *data++ = *pos++;
     344             : 
     345    12037955 :                 sptrset(f, pos);
     346             :         } else {
     347             :                 /* standard version using sgetc() */
     348     1370910 :                 while (size--) {
     349     1319138 :                         int c = sgetc(f);
     350     1319138 :                         if (c == EOF) {
     351             :                                 /* LCOV_EXCL_START */
     352             :                                 return -1;
     353             :                                 /* LCOV_EXCL_STOP */
     354             :                         }
     355             : 
     356     1319138 :                         *data++ = c;
     357             :                 }
     358             :         }
     359             : 
     360    12063841 :         return 0;
     361             : }
     362             : 
     363      104980 : int sgetline(STREAM* f, char* str, int size)
     364             : {
     365      104980 :         char* i = str;
     366      104980 :         char* send = str + size;
     367             :         int c;
     368             : 
     369             :         /* if there is enough data in memory */
     370      104980 :         if (sptrlookup(f, size)) {
     371             :                 /* optimized version with all the data in memory */
     372       93954 :                 unsigned char* pos = sptrget(f);
     373             : 
     374             :                 while (1) {
     375     1288074 :                         c = *pos++;
     376     1288074 :                         if (c == '\n') {
     377             :                                 /* remove ending carrige return to support the Windows CR+LF format */
     378       93954 :                                 if (i != str && i[-1] == '\r')
     379           0 :                                         --i;
     380       93954 :                                 --pos;
     381       93954 :                                 break;
     382             :                         }
     383             : 
     384     1194120 :                         *i++ = c;
     385             : 
     386     1194120 :                         if (i == send) {
     387             :                                 /* LCOV_EXCL_START */
     388             :                                 return -1;
     389             :                                 /* LCOV_EXCL_STOP */
     390             :                         }
     391     1194120 :                 }
     392             : 
     393       93954 :                 sptrset(f, pos);
     394             :         } else {
     395             :                 while (1) {
     396      150901 :                         c = sgetc(f);
     397      150901 :                         if (c == EOF) {
     398             :                                 /* LCOV_EXCL_START */
     399             :                                 break;
     400             :                                 /* LCOV_EXCL_STOP */
     401             :                         }
     402      150901 :                         if (c == '\n') {
     403             :                                 /* remove ending carrige return to support the Windows CR+LF format */
     404       11026 :                                 if (i != str && i[-1] == '\r')
     405           0 :                                         --i;
     406       11026 :                                 sungetc(c, f);
     407       11026 :                                 break;
     408             :                         }
     409             : 
     410      139875 :                         *i++ = c;
     411             : 
     412      139875 :                         if (i == send) {
     413             :                                 /* LCOV_EXCL_START */
     414             :                                 return -1;
     415             :                                 /* LCOV_EXCL_STOP */
     416             :                         }
     417      139875 :                 }
     418             :         }
     419             : 
     420      104980 :         *i = 0;
     421             : 
     422      104980 :         return i - str;
     423             : }
     424             : 
     425        4785 : int sgetlasttok(STREAM* f, char* str, int size)
     426             : {
     427             :         int ret;
     428             : 
     429        4785 :         ret = sgetline(f, str, size);
     430        4785 :         if (ret < 0) {
     431             :                 /* LCOV_EXCL_START */
     432             :                 return ret;
     433             :                 /* LCOV_EXCL_STOP */
     434             :         }
     435             : 
     436        9570 :         while (ret > 0 && (str[ret - 1] == ' ' || str[ret - 1] == '\t'))
     437           0 :                 --ret;
     438             : 
     439        4785 :         str[ret] = 0;
     440             : 
     441        4785 :         return ret;
     442             : }
     443             : 
     444      396702 : int sgetu32(STREAM* f, uint32_t* value)
     445             : {
     446             :         int c;
     447             : 
     448      396702 :         c = sgetc(f);
     449      396702 :         if (c >= '0' && c <= '9') {
     450             :                 uint32_t v;
     451             : 
     452      396702 :                 v = c - '0';
     453             : 
     454      396702 :                 c = sgetc(f);
     455     2620869 :                 while (c >= '0' && c <= '9') {
     456     1827465 :                         v *= 10;
     457     1827465 :                         v += c - '0';
     458     1827465 :                         c = sgetc(f);
     459             :                 }
     460             : 
     461      396702 :                 *value = v;
     462             : 
     463      396702 :                 sungetc(c, f);
     464      396702 :                 return 0;
     465             :         } else {
     466             :                 /* LCOV_EXCL_START */
     467             :                 /* nothing read */
     468             :                 return -1;
     469             :                 /* LCOV_EXCL_STOP */
     470             :         }
     471             : }
     472             : 
     473      282276 : int sgetu64(STREAM* f, uint64_t* value)
     474             : {
     475             :         int c;
     476             : 
     477      282276 :         c = sgetc(f);
     478      282276 :         if (c >= '0' && c <= '9') {
     479             :                 uint64_t v;
     480             : 
     481      282276 :                 v = c - '0';
     482             : 
     483      282276 :                 c = sgetc(f);
     484     2325776 :                 while (c >= '0' && c <= '9') {
     485     1761224 :                         v *= 10;
     486     1761224 :                         v += c - '0';
     487     1761224 :                         c = sgetc(f);
     488             :                 }
     489             : 
     490      282276 :                 *value = v;
     491             : 
     492      282276 :                 sungetc(c, f);
     493      282276 :                 return 0;
     494             :         } else {
     495             :                 /* LCOV_EXCL_START */
     496             :                 /* nothing read */
     497             :                 return -1;
     498             :                 /* LCOV_EXCL_STOP */
     499             :         }
     500             : }
     501             : 
     502      232385 : int sgethex(STREAM* f, void* void_data, int size)
     503             : {
     504      232385 :         unsigned char* data = void_data;
     505             : 
     506             :         /* if there is enough data in memory */
     507      232385 :         if (sptrlookup(f, size * 2)) {
     508             :                 /* optimized version with all the data in memory */
     509      232285 :                 unsigned char* pos = sptrget(f);
     510      232285 :                 unsigned x = 0;
     511             : 
     512     4181130 :                 while (size--) {
     513             :                         unsigned b0;
     514             :                         unsigned b1;
     515             :                         unsigned b;
     516             : 
     517     3716560 :                         b0 = strdecset[pos[0]];
     518     3716560 :                         b1 = strdecset[pos[1]];
     519     3716560 :                         pos += 2;
     520             : 
     521     3716560 :                         b = (b0 << 4) | b1;
     522             : 
     523     3716560 :                         x |= b;
     524             : 
     525     3716560 :                         *data++ = b;
     526             :                 }
     527             : 
     528             :                 /* at the end check if a digit was wrong */
     529      232285 :                 if (x > 0xFF) {
     530             :                         /* LCOV_EXCL_START */
     531             :                         return -1;
     532             :                         /* LCOV_EXCL_STOP */
     533             :                 }
     534             : 
     535      232285 :                 sptrset(f, pos);
     536             :         } else {
     537             :                 /* standard version using sgetc() */
     538        1800 :                 while (size--) {
     539             :                         unsigned b0;
     540             :                         unsigned b1;
     541             :                         unsigned b;
     542             :                         int c;
     543             : 
     544        1600 :                         c = sgetc(f);
     545        1600 :                         if (c == EOF) {
     546             :                                 /* LCOV_EXCL_START */
     547             :                                 return -1;
     548             :                                 /* LCOV_EXCL_STOP */
     549             :                         }
     550        1600 :                         b0 = strdecset[(unsigned char)c];
     551             : 
     552        1600 :                         c = sgetc(f);
     553        1600 :                         if (c == EOF) {
     554             :                                 /* LCOV_EXCL_START */
     555             :                                 return -1;
     556             :                                 /* LCOV_EXCL_STOP */
     557             :                         }
     558        1600 :                         b1 = strdecset[(unsigned char)c];
     559             : 
     560        1600 :                         b = (b0 << 4) | b1;
     561             : 
     562        1600 :                         if (b > 0xFF) {
     563             :                                 /* LCOV_EXCL_START */
     564             :                                 return -1;
     565             :                                 /* LCOV_EXCL_STOP */
     566             :                         }
     567             : 
     568        1600 :                         *data++ = b;
     569             :                 }
     570             :         }
     571             : 
     572      232385 :         return 0;
     573             : }
     574             : 
     575    18305317 : int sgetb32(STREAM* f, uint32_t* value)
     576             : {
     577             :         uint32_t v;
     578             :         unsigned char b;
     579             :         unsigned char s;
     580             :         int c;
     581             : 
     582    18305317 :         v = 0;
     583    18305317 :         s = 0;
     584             : loop:
     585    35258730 :         c = sgetc(f);
     586    35258730 :         if (c == EOF) {
     587             :                 /* LCOV_EXCL_START */
     588             :                 return -1;
     589             :                 /* LCOV_EXCL_STOP */
     590             :         }
     591             : 
     592    35258730 :         b = (unsigned char)c;
     593    35258730 :         if ((b & 0x80) == 0) {
     594    16953413 :                 v |= (uint32_t)b << s;
     595    16953413 :                 s += 7;
     596    16953413 :                 if (s >= 32) {
     597             :                         /* LCOV_EXCL_START */
     598             :                         return -1;
     599             :                         /* LCOV_EXCL_STOP */
     600             :                 }
     601    16953413 :                 goto loop;
     602             :         }
     603             : 
     604    18305317 :         v |= (uint32_t)(b & 0x7f) << s;
     605             : 
     606    18305317 :         *value = v;
     607             : 
     608    18305317 :         return 0;
     609             : }
     610             : 
     611    10229372 : int sgetb64(STREAM* f, uint64_t* value)
     612             : {
     613             :         uint64_t v;
     614             :         unsigned char b;
     615             :         unsigned char s;
     616             :         int c;
     617             : 
     618    10229372 :         v = 0;
     619    10229372 :         s = 0;
     620             : loop:
     621    37406793 :         c = sgetc(f);
     622    37406793 :         if (c == EOF) {
     623             :                 /* LCOV_EXCL_START */
     624             :                 return -1;
     625             :                 /* LCOV_EXCL_STOP */
     626             :         }
     627             : 
     628    37406793 :         b = (unsigned char)c;
     629    37406793 :         if ((b & 0x80) == 0) {
     630    27177421 :                 v |= (uint64_t)b << s;
     631    27177421 :                 s += 7;
     632    27177421 :                 if (s >= 64) {
     633             :                         /* LCOV_EXCL_START */
     634             :                         return -1;
     635             :                         /* LCOV_EXCL_STOP */
     636             :                 }
     637    27177421 :                 goto loop;
     638             :         }
     639             : 
     640    10229372 :         v |= (uint64_t)(b & 0x7f) << s;
     641             : 
     642    10229372 :         *value = v;
     643             : 
     644    10229372 :         return 0;
     645             : }
     646             : 
     647         346 : int sgetble32(STREAM* f, uint32_t* value)
     648             : {
     649             :         unsigned char buf[4];
     650             : 
     651         346 :         if (sread(f, buf, 4) != 0) {
     652             :                 /* LCOV_EXCL_START */
     653             :                 return -1;
     654             :                 /* LCOV_EXCL_STOP */
     655             :         }
     656             : 
     657         346 :         *value = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
     658             : 
     659         346 :         return 0;
     660             : }
     661             : 
     662     3648313 : int sgetbs(STREAM* f, char* str, int size)
     663             : {
     664             :         uint32_t len;
     665             : 
     666     3648313 :         if (sgetb32(f, &len) < 0) {
     667             :                 /* LCOV_EXCL_START */
     668             :                 return -1;
     669             :                 /* LCOV_EXCL_STOP */
     670             :         }
     671             : 
     672     3648313 :         if (len + 1 > (uint32_t)size) {
     673             :                 /* LCOV_EXCL_START */
     674             :                 return -1;
     675             :                 /* LCOV_EXCL_STOP */
     676             :         }
     677             : 
     678     3648313 :         str[len] = 0;
     679             : 
     680     3648313 :         return sread(f, str, (int)len);
     681             : }
     682             : 
     683      208981 : int sputs(const char* str, STREAM* f)
     684             : {
     685     2284072 :         while (*str) {
     686     1866110 :                 if (sputc(*str++, f) != 0) {
     687             :                         /* LCOV_EXCL_START */
     688             :                         return -1;
     689             :                         /* LCOV_EXCL_STOP */
     690             :                 }
     691             :         }
     692             : 
     693      208981 :         return 0;
     694             : }
     695             : 
     696    20736098 : int swrite(const void* void_data, unsigned size, STREAM* f)
     697             : {
     698    20736098 :         const unsigned char* data = void_data;
     699             : 
     700             :         /* if there is enough space in memory */
     701    20736098 :         if (sptrlookup(f, size)) {
     702             :                 /* optimized version with all the data in memory */
     703    20729979 :                 unsigned char* pos = sptrget(f);
     704             : 
     705             :                 /* copy it */
     706   169093531 :                 while (size--) {
     707   127633573 :                         f->crc_stream = crc32c_plain(f->crc_stream, *data);
     708   127633573 :                         *pos++ = *data++;
     709             :                 }
     710             : 
     711    20729979 :                 sptrset(f, pos);
     712             :         } else {
     713             :                 /* standard version using sputc() */
     714      199614 :                 while (size--) {
     715      187376 :                         if (sputc(*data++, f) != 0) {
     716             :                                 /* LCOV_EXCL_START */
     717             :                                 return -1;
     718             :                                 /* LCOV_EXCL_STOP */
     719             :                         }
     720             :                 }
     721             :         }
     722             : 
     723    20736098 :         return 0;
     724             : }
     725             : 
     726      415233 : int sputu32(uint32_t value, STREAM* s)
     727             : {
     728             :         char buf[16];
     729             :         int i;
     730             : 
     731      415233 :         if (!value)
     732          45 :                 return sputc('0', s);
     733             : 
     734      415188 :         i = sizeof(buf);
     735             : 
     736     3157044 :         while (value) {
     737     2326668 :                 buf[--i] = (value % 10) + '0';
     738     2326668 :                 value /= 10;
     739             :         }
     740             : 
     741      415188 :         return swrite(buf + i, sizeof(buf) - i, s);
     742             : }
     743             : 
     744      298899 : int sputu64(uint64_t value, STREAM* s)
     745             : {
     746             :         char buf[32];
     747             :         uint32_t value32;
     748             :         int i;
     749             : 
     750      298899 :         if (!value)
     751         130 :                 return sputc('0', s);
     752             : 
     753      298769 :         i = sizeof(buf);
     754             : 
     755      597538 :         while (value > 0xFFFFFFFF) {
     756           0 :                 buf[--i] = (value % 10) + '0';
     757           0 :                 value /= 10;
     758             :         }
     759             : 
     760      298769 :         value32 = (uint32_t)value;
     761             : 
     762     2761238 :         while (value32) {
     763     2163700 :                 buf[--i] = (value32 % 10) + '0';
     764     2163700 :                 value32 /= 10;
     765             :         }
     766             : 
     767      298769 :         return swrite(buf + i, sizeof(buf) - i, s);
     768             : }
     769             : 
     770      245610 : int sputhex(const void* void_data, int size, STREAM* f)
     771             : {
     772      245610 :         const unsigned char* data = void_data;
     773             : 
     774             :         /* if there is enough space in memory */
     775      245610 :         if (sptrlookup(f, size * 2)) {
     776             :                 /* optimized version with all the data in memory */
     777      245505 :                 unsigned char* pos = sptrget(f);
     778             : 
     779     4419090 :                 while (size) {
     780     3928080 :                         unsigned b = *data;
     781             : 
     782     3928080 :                         *pos++ = strhexset[b >> 4];
     783     3928080 :                         *pos++ = strhexset[b & 0xF];
     784             : 
     785     3928080 :                         ++data;
     786     3928080 :                         --size;
     787             :                 }
     788             : 
     789      245505 :                 sptrset(f, pos);
     790             :         } else {
     791             :                 /* standard version using sputc() */
     792        1890 :                 while (size) {
     793        1680 :                         unsigned b = *data;
     794             : 
     795        1680 :                         if (sputc(strhexset[b >> 4], f) != 0) {
     796             :                                 /* LCOV_EXCL_START */
     797             :                                 return -1;
     798             :                                 /* LCOV_EXCL_STOP */
     799             :                         }
     800        1680 :                         if (sputc(strhexset[b & 0xF], f) != 0) {
     801             :                                 /* LCOV_EXCL_START */
     802             :                                 return -1;
     803             :                                 /* LCOV_EXCL_STOP */
     804             :                         }
     805             : 
     806        1680 :                         ++data;
     807        1680 :                         --size;
     808             :                 }
     809             :         }
     810             : 
     811      245610 :         return 0;
     812             : }
     813             : 
     814     8983972 : int sputb32(uint32_t value, STREAM* s)
     815             : {
     816             :         unsigned char b;
     817             :         unsigned char buf[16];
     818             :         unsigned i;
     819             : 
     820     8983972 :         i = 0;
     821             : loop:
     822    17090262 :         b = value & 0x7f;
     823    17090262 :         value >>= 7;
     824             : 
     825    17090262 :         if (value) {
     826     8106290 :                 buf[i++] = b;
     827     8106290 :                 goto loop;
     828             :         }
     829             : 
     830     8983972 :         buf[i++] = b | 0x80;
     831             : 
     832     8983972 :         return swrite(buf, i, s);
     833             : }
     834             : 
     835     4887004 : int sputb64(uint64_t value, STREAM* s)
     836             : {
     837             :         unsigned char b;
     838             :         unsigned char buf[16];
     839             :         unsigned i;
     840             : 
     841     4887004 :         i = 0;
     842             : loop:
     843    17866904 :         b = value & 0x7f;
     844    17866904 :         value >>= 7;
     845             : 
     846    17866904 :         if (value) {
     847    12979900 :                 buf[i++] = b;
     848    12979900 :                 goto loop;
     849             :         }
     850             : 
     851     4887004 :         buf[i++] = b | 0x80;
     852             : 
     853     4887004 :         return swrite(buf, i, s);
     854             : }
     855             : 
     856         131 : int sputble32(uint32_t value, STREAM* s)
     857             : {
     858             :         unsigned char buf[4];
     859             : 
     860         131 :         buf[0] = value & 0xFF;
     861         131 :         buf[1] = (value >> 8) & 0xFF;
     862         131 :         buf[2] = (value >> 16) & 0xFF;
     863         131 :         buf[3] = (value >> 24) & 0xFF;
     864             : 
     865         131 :         return swrite(buf, 4, s);
     866             : }
     867             : 
     868     1739788 : int sputbs(const char* str, STREAM* f)
     869             : {
     870     1739788 :         size_t len = strlen(str);
     871             : 
     872     1739788 :         if (sputb32(len, f) != 0) {
     873             :                 /* LCOV_EXCL_START */
     874             :                 return -1;
     875             :                 /* LCOV_EXCL_STOP */
     876             :         }
     877             : 
     878     1739788 :         return swrite(str, len, f);
     879             : }
     880             : 
     881             : #if HAVE_FSYNC
     882         120 : int ssync(STREAM* s)
     883             : {
     884             :         unsigned i;
     885             : 
     886         945 :         for (i = 0; i < s->handle_size; ++i) {
     887         825 :                 if (fsync(s->handle[i].f) != 0) {
     888             :                         /* LCOV_EXCL_START */
     889             :                         s->state = STREAM_STATE_ERROR;
     890             :                         s->state_index = i;
     891             :                         return -1;
     892             :                         /* LCOV_EXCL_STOP */
     893             :                 }
     894             :         }
     895             : 
     896         120 :         return 0;
     897             : }
     898             : #endif
     899             : 

Generated by: LCOV version 1.10