LCOV - code coverage report
Current view: top level - cmdline - stream.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 38 42 90.5 %
Date: 2026-03-15 15:58:19 Functions: 10 11 90.9 %

          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             : #ifndef __STREAM_H
      19             : #define __STREAM_H
      20             : 
      21             : #include "util.h"
      22             : 
      23             : /****************************************************************************/
      24             : /* stream */
      25             : 
      26             : #define STREAM_FLAGS_SEQUENTIAL 1 /**< Advise a squential read. */
      27             : #define STREAM_FLAGS_CRC 2 /**< Enable the CRC computation. */
      28             : 
      29             : /**
      30             :  * Size of the buffer of the stream.
      31             :  *
      32             :  * It's not a constant for testing purpose.
      33             :  */
      34             : extern unsigned STREAM_SIZE;
      35             : 
      36             : #define STREAM_STATE_READ 0 /**< The stream is in a normal state of read. */
      37             : #define STREAM_STATE_WRITE 1 /**< The stream is in a normal state of write. */
      38             : #define STREAM_STATE_ERROR -1 /**< An error was encountered. */
      39             : #define STREAM_STATE_EOF 2 /**< The end of file was encountered. */
      40             : 
      41             : struct stream_handle {
      42             :         int f; /**< Handle of the file. */
      43             :         char path[PATH_MAX]; /**< Path of the file. */
      44             : };
      45             : 
      46             : struct stream {
      47             :         unsigned char* buffer; /**< Buffer of the stream. */
      48             :         off_t buffer_size; /**< Size of the buffer */
      49             :         unsigned char* pos; /**< Current position in the buffer. */
      50             :         unsigned char* end; /**< End position of the buffer. */
      51             :         struct stream_handle* handle; /**< Set of handles. */
      52             :         off_t offset; /**< Offset into the file. */
      53             :         off_t offset_uncached; /**< Offset into the file excluding the cached data. */
      54             :         off_t size; /**< File size, only in read */
      55             : 
      56             :         int flags; /**< Flags passed on open */
      57             :         int state; /**< State of the stream. One of STREAM_STATE. */
      58             :         int state_index; /**< Index of the handle causing a state change. */
      59             :         unsigned handle_size; /**< Number of handles. */
      60             : 
      61             :         /**
      62             :          * CRC of the data read or written in the file.
      63             :          *
      64             :          * If reading, it's the CRC of all data read from the file,
      65             :          * including the one in the buffer.
      66             :          * If writing it's all the data wrote to the file,
      67             :          * excluding the one still in buffer yet to be written.
      68             :          */
      69             :         uint32_t crc;
      70             : 
      71             :         /**
      72             :          * CRC of the file excluding the cached data in the buffer.
      73             :          *
      74             :          * If reading, it's the CRC of the data read from the file,
      75             :          * excluding the one in the buffer.
      76             :          * If writing it's all the data wrote to the file,
      77             :          * excluding the one still in buffer yet to be written.
      78             :          */
      79             :         uint32_t crc_uncached;
      80             : 
      81             :         /**
      82             :          * CRC of the data written to the stream.
      83             :          *
      84             :          * This is an extra check of the data that is written to
      85             :          * file to ensure that it's consistent even in case
      86             :          * of memory errors.
      87             :          *
      88             :          * This extra check takes about 2 seconds for each GB of
      89             :          * content file with the Intel CRC instruction,
      90             :          * and about 4 seconds without it.
      91             :          * But usually this doesn't slow down the write process,
      92             :          * as the disk is the bottle-neck.
      93             :          *
      94             :          * Note that this CRC doesn't have the IV processing.
      95             :          *
      96             :          * Not used in reading.
      97             :          * In writing, it's all the data wrote calling sput() functions.
      98             :          */
      99             :         uint32_t crc_stream;
     100             : };
     101             : 
     102             : /**
     103             :  * Opaque STREAM type. Like ::FILE.
     104             :  */
     105             : typedef struct stream STREAM;
     106             : 
     107             : /**
     108             :  * Open a stream for reading. Like fopen("r").
     109             :  */
     110             : STREAM* sopen_read(const char* file, int flags);
     111             : 
     112             : /**
     113             :  * Open a stream for writing. Like fopen("w").
     114             :  */
     115             : STREAM* sopen_write(const char* file, int flags);
     116             : 
     117             : /**
     118             :  * Open a set of streams for writing. Like fopen("w").
     119             :  */
     120             : STREAM* sopen_multi_write(unsigned count, int flags);
     121             : 
     122             : /**
     123             :  * Specify the file to open.
     124             :  */
     125             : int sopen_multi_file(STREAM* s, unsigned i, const char* file);
     126             : 
     127             : /**
     128             :  * Close a stream. Like fclose().
     129             :  */
     130             : int sclose(STREAM* s);
     131             : 
     132             : /**
     133             :  * Return the handle of the file.
     134             :  * In case of multi file, the first one is returned.
     135             :  */
     136             : int shandle(STREAM* s);
     137             : 
     138             : /**
     139             :  * Read the stream until the end, and return the latest 4 chars.
     140             :  * The CRC of the file is also computed, and you can get it using scrc().
     141             :  * \return 0 on success, or EOF on error.
     142             :  */
     143             : int sdeplete(STREAM* s, unsigned char* last);
     144             : 
     145             : /**
     146             :  * Flush the write stream buffer.
     147             :  * \return 0 on success, or EOF on error.
     148             :  */
     149             : int sflush(STREAM* s);
     150             : 
     151             : /**
     152             :  * Get the file pointer.
     153             :  */
     154             : int64_t stell(STREAM* s);
     155             : 
     156             : /**
     157             :  * Get the CRC of the processed data.
     158             :  */
     159             : uint32_t scrc(STREAM* s);
     160             : 
     161             : /**
     162             :  * Get the CRC of the processed data in put.
     163             :  */
     164             : uint32_t scrc_stream(STREAM* s);
     165             : 
     166             : /**
     167             :  * Check if the buffer has enough data loaded.
     168             :  */
     169    35921982 : static inline int sptrlookup(STREAM* s, int size)
     170             : {
     171    35921982 :         return s->pos + size <= s->end;
     172             : }
     173             : 
     174             : /**
     175             :  * Get the current stream ptr.
     176             :  */
     177    35882872 : static inline unsigned char* sptrget(STREAM* s)
     178             : {
     179    35882872 :         return s->pos;
     180             : }
     181             : 
     182             : /**
     183             :  * Set the current stream ptr.
     184             :  */
     185    35882872 : static inline void sptrset(STREAM* s, unsigned char* ptr)
     186             : {
     187    35882872 :         s->pos = ptr;
     188    35882872 : }
     189             : 
     190             : /**
     191             :  * Check the error status. Like ferror().
     192             :  */
     193     3900249 : static inline int serror(STREAM* s)
     194             : {
     195     3900249 :         return s->state == STREAM_STATE_ERROR;
     196             : }
     197             : 
     198             : /**
     199             :  * Check the eof status. Like feof().
     200             :  */
     201           1 : static inline int seof(STREAM* s)
     202             : {
     203           1 :         return s->state == STREAM_STATE_EOF;
     204             : }
     205             : 
     206             : /**
     207             :  * Get the index of the handle that caused the error.
     208             :  */
     209             : static inline int serrorindex(STREAM* s)
     210             : {
     211             :         return s->state_index;
     212             : }
     213             : 
     214             : /**
     215             :  * Get the path of the handle that caused the error.
     216             :  */
     217           0 : static inline const char* serrorfile(STREAM* s)
     218             : {
     219           0 :         return s->handle[s->state_index].path;
     220             : }
     221             : 
     222             : /**
     223             :  * Sync the stream. Like fsync().
     224             :  */
     225             : int ssync(STREAM* s);
     226             : 
     227             : /****************************************************************************/
     228             : /* get */
     229             : 
     230             : /**
     231             :  * \internal Used by sgetc().
     232             :  * \note Don't call this directly, but use sgetc().
     233             :  */
     234             : int sgetc_uncached(STREAM* s);
     235             : 
     236             : /**
     237             :  * Read a char. Like fgetc().
     238             :  */
     239    89119166 : static inline int sgetc(STREAM* s)
     240             : {
     241    89119166 :         if (tommy_unlikely(s->pos == s->end))
     242      299594 :                 return sgetc_uncached(s);
     243    88819572 :         return *s->pos++;
     244             : }
     245             : 
     246             : /**
     247             :  * Unread a char.
     248             :  * Like ungetc() but you have to unget the same char read.
     249             :  */
     250       41624 : static inline void sungetc(int c, STREAM* s)
     251             : {
     252       41624 :         if (c != EOF)
     253       40685 :                 --s->pos;
     254       41624 : }
     255             : 
     256             : /**
     257             :  * Read a fixed amount of chars.
     258             :  * Return 0 on success, or -1 on error.
     259             :  */
     260             : int sread(STREAM* f, void* void_data, unsigned size);
     261             : 
     262             : /**
     263             :  * Get a char from a stream, ignoring one '\r'.
     264             :  */
     265        7321 : static inline int sgeteol(STREAM* f)
     266             : {
     267             :         int c;
     268             : 
     269        7321 :         c = sgetc(f);
     270        7321 :         if (c == '\r')
     271           0 :                 c = sgetc(f);
     272             : 
     273        7321 :         return c;
     274             : }
     275             : 
     276             : /**
     277             :  * Read all the spaces and tabs.
     278             :  * Return the number of spaces and tabs read.
     279             :  */
     280       24644 : static inline int sgetspace(STREAM* f)
     281             : {
     282       24644 :         int count = 0;
     283             :         int c;
     284             : 
     285       24644 :         c = sgetc(f);
     286       33998 :         while (c == ' ' || c == '\t') {
     287        9354 :                 ++count;
     288        9354 :                 c = sgetc(f);
     289             :         }
     290             : 
     291       24644 :         sungetc(c, f);
     292       24644 :         return count;
     293             : }
     294             : 
     295             : /**
     296             :  * Read until the first space or tab.
     297             :  * Stop at the first ' ', '\t', '\n' or EOF.
     298             :  * Return <0 if the buffer is too small, or the number of chars read.
     299             :  */
     300             : int sgettok(STREAM* f, char* str, int size);
     301             : 
     302             : /**
     303             :  * Read until the end of line.
     304             :  * Stop at the first '\n' or EOF. Note that '\n' is left in the stream.
     305             :  * Return <0 if the buffer is too small, or the number of chars read.
     306             :  */
     307             : int sgetline(STREAM* f, char* str, int size);
     308             : 
     309             : /**
     310             :  * Like sgetline() but remove ' ' and '\t' at the end.
     311             :  */
     312             : int sgetlasttok(STREAM* f, char* str, int size);
     313             : 
     314             : /**
     315             :  * Read a 32 bit number.
     316             :  * Stop at the first not digit char or EOF.
     317             :  * Return <0 if there isn't enough to read.
     318             :  */
     319             : int sgetu32(STREAM* f, uint32_t* value);
     320             : 
     321             : /****************************************************************************/
     322             : /* binary get */
     323             : 
     324             : /**
     325             :  * Read a binary 32 bit number in packet format.
     326             :  * Return <0 if there isn't enough to read.
     327             :  */
     328             : int sgetb32(STREAM* f, uint32_t* value);
     329             : 
     330             : /**
     331             :  * Read a binary 64 bit number in packet format.
     332             :  * Return <0 if there isn't enough to read.
     333             :  */
     334             : int sgetb64(STREAM* f, uint64_t* value);
     335             : 
     336             : /**
     337             :  * Read a binary 32 bit number in little endian format.
     338             :  * Return <0 if there isn't enough to read.
     339             :  */
     340             : int sgetble32(STREAM* f, uint32_t* value);
     341             : 
     342             : /**
     343             :  * Read a binary string.
     344             :  * Return -1 on error or if the buffer is too small, or the number of chars read.
     345             :  */
     346             : int sgetbs(STREAM* f, char* str, int size);
     347             : 
     348             : /****************************************************************************/
     349             : /* put */
     350             : 
     351             : /**
     352             :  * Write a char. Like fputc().
     353             :  * Return 0 on success or -1 on error.
     354             :  */
     355     4080574 : static inline int sputc(int c, STREAM* s)
     356             : {
     357     4080574 :         if (s->pos == s->end) {
     358       38733 :                 if (sflush(s) != 0)
     359           0 :                         return -1;
     360             :         }
     361             : 
     362             :         /**
     363             :          * Update the crc *before* writing the data in the buffer
     364             :          *
     365             :          * This must be done before the memory write,
     366             :          * to be able to detect memory errors on the buffer,
     367             :          * happening before we write it on the file.
     368             :          */
     369     4080574 :         if (s->flags & STREAM_FLAGS_CRC)
     370     4080574 :                 s->crc_stream = crc32c_plain_char(s->crc_stream, c);
     371             : 
     372     4080574 :         *s->pos++ = c;
     373             : 
     374     4080574 :         return 0;
     375             : }
     376             : 
     377             : /**
     378             :  * Write a end of line.
     379             :  * Return 0 on success or -1 on error.
     380             :  */
     381             : static inline int sputeol(STREAM* s)
     382             : {
     383             : #ifdef _WIN32
     384             :         if (sputc('\r', s) != 0)
     385             :                 return -1;
     386             : #endif
     387             :         return sputc('\n', s);
     388             : }
     389             : 
     390             : /**
     391             :  * Write a sized string.
     392             :  * Return 0 on success or -1 on error.
     393             :  */
     394             : int swrite(const void* data, unsigned size, STREAM* f);
     395             : 
     396             : /****************************************************************************/
     397             : /* binary put */
     398             : 
     399             : /**
     400             :  * Write a binary 32 bit number in packed format.
     401             :  * Return 0 on success or -1 on error.
     402             :  */
     403             : int sputb32(uint32_t value, STREAM* s);
     404             : 
     405             : /**
     406             :  * Write a binary 64 bit number in packed format.
     407             :  * Return 0 on success or -1 on error.
     408             :  */
     409             : int sputb64(uint64_t value, STREAM* s);
     410             : 
     411             : /**
     412             :  * Write a binary 32 bit number in little endian format.
     413             :  * Return 0 on success or -1 on error.
     414             :  */
     415             : int sputble32(uint32_t value, STREAM* s);
     416             : 
     417             : /**
     418             :  * Write a binary string.
     419             :  * Return 0 on success or -1 on error.
     420             :  */
     421             : int sputbs(const char* str, STREAM* s);
     422             : 
     423             : #endif
     424             : 

Generated by: LCOV version 1.0