LCOV - code coverage report
Current view: top level - cmdline - stream.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 39 43 90.7 %
Date: 2015-05-12 Functions: 11 12 91.7 %

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

Generated by: LCOV version 1.10