mirror of
https://github.com/amadvance/snapraid.git
synced 2026-10-26 02:49:02 -04:00
415 lines
9.1 KiB
C
415 lines
9.1 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2011 Andrea Mazzoleni
|
|
|
|
#ifndef __STREAM_H
|
|
#define __STREAM_H
|
|
|
|
#include "support.h"
|
|
#include "util.h"
|
|
|
|
/****************************************************************************/
|
|
/* stream */
|
|
|
|
#define STREAM_FLAGS_ADVISE_MASK 0xFF /**< Low bits containing one of ADVISE_*. */
|
|
#define STREAM_FLAGS_CRC 0x100 /**< Enable the CRC computation. */
|
|
#define STREAM_FLAGS_NOFOLLOW 0x200 /**< Don't follow the final symbolic link on open. */
|
|
|
|
/**
|
|
* Size of the buffer of the stream.
|
|
*
|
|
* It's not a constant for testing purpose.
|
|
*/
|
|
extern unsigned STREAM_SIZE;
|
|
|
|
#define STREAM_STATE_READ 0 /**< The stream is in a normal state of read. */
|
|
#define STREAM_STATE_WRITE 1 /**< The stream is in a normal state of write. */
|
|
#define STREAM_STATE_ERROR -1 /**< An error was encountered. */
|
|
#define STREAM_STATE_EOF 2 /**< The end of file was encountered. */
|
|
|
|
struct stream_handle {
|
|
int f; /**< Handle of the file. */
|
|
char path[PATH_MAX]; /**< Path of the file. */
|
|
};
|
|
|
|
struct stream {
|
|
unsigned char* buffer; /**< Buffer of the stream. */
|
|
off_t buffer_size; /**< Size of the buffer */
|
|
unsigned char* pos; /**< Current position in the buffer. */
|
|
unsigned char* end; /**< End position of the buffer. */
|
|
struct stream_handle* handle; /**< Set of handles. */
|
|
off_t offset; /**< Offset into the file. */
|
|
off_t offset_uncached; /**< Offset into the file excluding the cached data. */
|
|
off_t size; /**< File size, only in read */
|
|
|
|
int flags; /**< Flags passed on open */
|
|
int state; /**< State of the stream. One of STREAM_STATE. */
|
|
int state_index; /**< Index of the handle causing a state change. */
|
|
unsigned handle_size; /**< Number of handles. */
|
|
|
|
/**
|
|
* CRC of the data read or written in the file.
|
|
*
|
|
* If reading, it's the CRC of all data read from the file,
|
|
* including the one in the buffer.
|
|
* If writing it's all the data wrote to the file,
|
|
* excluding the one still in buffer yet to be written.
|
|
*/
|
|
uint32_t crc;
|
|
|
|
/**
|
|
* CRC of the file excluding the cached data in the buffer.
|
|
*
|
|
* If reading, it's the CRC of the data read from the file,
|
|
* excluding the one in the buffer.
|
|
* If writing it's all the data wrote to the file,
|
|
* excluding the one still in buffer yet to be written.
|
|
*/
|
|
uint32_t crc_uncached;
|
|
|
|
struct advise_struct* advise; /**< Cache advice state for each handle. */
|
|
};
|
|
|
|
/**
|
|
* Opaque STREAM type. Like ::FILE.
|
|
*/
|
|
typedef struct stream STREAM;
|
|
|
|
/**
|
|
* Open a stream for reading. Like fopen("r").
|
|
*/
|
|
STREAM* sopen_read(const char* file, int flags);
|
|
|
|
/**
|
|
* Open a stream for writing. Like fopen("w").
|
|
*/
|
|
STREAM* sopen_write(const char* file, int flags);
|
|
|
|
/**
|
|
* Open a set of streams for writing. Like fopen("w").
|
|
*/
|
|
STREAM* sopen_multi_write(unsigned count, int flags);
|
|
|
|
/**
|
|
* Specify the file to open.
|
|
*/
|
|
int sopen_multi_file(STREAM* s, unsigned i, const char* file);
|
|
|
|
/**
|
|
* Close a stream. Like fclose().
|
|
*/
|
|
int sclose(STREAM* s);
|
|
|
|
/**
|
|
* Return the handle of the file.
|
|
* In case of multi file, the first one is returned.
|
|
*/
|
|
int shandle(STREAM* s);
|
|
|
|
/**
|
|
* Read the stream until the end, and return the latest 4 chars.
|
|
* The CRC of the file is also computed, and you can get it using scrc().
|
|
* \return 0 on success, or EOF on error.
|
|
*/
|
|
int sdeplete(STREAM* s, unsigned char* last);
|
|
|
|
/**
|
|
* Flush the write stream buffer.
|
|
* \return 0 on success, or EOF on error.
|
|
*/
|
|
int sflush(STREAM* s);
|
|
|
|
/**
|
|
* Get the file pointer.
|
|
*/
|
|
int64_t stell(STREAM* s);
|
|
|
|
/**
|
|
* Get the CRC of the processed data.
|
|
*/
|
|
uint32_t scrc(STREAM* s);
|
|
|
|
/**
|
|
* Check if the buffer has enough data loaded.
|
|
*/
|
|
static inline int sptrlookup(STREAM* s, size_t size)
|
|
{
|
|
/* compute remaining distance to avoid pointer arithmetic out of buffer bounds */
|
|
return size <= (size_t)(s->end - s->pos);
|
|
}
|
|
|
|
/**
|
|
* Get the current stream ptr.
|
|
*/
|
|
static inline unsigned char* sptrget(STREAM* s)
|
|
{
|
|
return s->pos;
|
|
}
|
|
|
|
/**
|
|
* Set the current stream ptr.
|
|
*/
|
|
static inline void sptrset(STREAM* s, unsigned char* ptr)
|
|
{
|
|
s->pos = ptr;
|
|
}
|
|
|
|
/**
|
|
* Check the error status. Like ferror().
|
|
*/
|
|
static inline int serror(STREAM* s)
|
|
{
|
|
return s->state == STREAM_STATE_ERROR;
|
|
}
|
|
|
|
/**
|
|
* Check the eof status. Like feof().
|
|
*/
|
|
static inline int seof(STREAM* s)
|
|
{
|
|
return s->state == STREAM_STATE_EOF;
|
|
}
|
|
|
|
/**
|
|
* Get the index of the handle that caused the error.
|
|
*/
|
|
static inline int serrorindex(STREAM* s)
|
|
{
|
|
return s->state_index;
|
|
}
|
|
|
|
/**
|
|
* Get the path of the handle that caused the error.
|
|
*/
|
|
static inline const char* serrorfile(STREAM* s)
|
|
{
|
|
return s->handle[s->state_index].path;
|
|
}
|
|
|
|
/**
|
|
* Sync the stream. Like fsync().
|
|
*/
|
|
int ssync(STREAM* s);
|
|
|
|
/**
|
|
* Change the modification time of all files in the stream.
|
|
*/
|
|
int smtime(STREAM* s, int64_t mtime_sec, int mtime_nsec);
|
|
|
|
/****************************************************************************/
|
|
/* get */
|
|
|
|
/**
|
|
* \internal Used by sgetc().
|
|
* \note Don't call this directly, but use sgetc().
|
|
*/
|
|
int sgetc_uncached(STREAM* s);
|
|
|
|
/**
|
|
* Read a char. Like fgetc().
|
|
*/
|
|
static inline int sgetc(STREAM* s)
|
|
{
|
|
if (tommy_unlikely(s->pos == s->end))
|
|
return sgetc_uncached(s);
|
|
return *s->pos++;
|
|
}
|
|
|
|
/**
|
|
* Unread a char.
|
|
* Like ungetc() but you have to unget the same char read.
|
|
*/
|
|
static inline void sungetc(int c, STREAM* s)
|
|
{
|
|
if (c != EOF)
|
|
--s->pos;
|
|
}
|
|
|
|
/**
|
|
* \internal Used by sread() when the requested data crosses the buffered range.
|
|
* \note Don't call this directly, but use sread().
|
|
*/
|
|
int sread_uncached(STREAM* f, void* void_data, size_t size);
|
|
|
|
/**
|
|
* Read a fixed amount of chars.
|
|
* Return 0 on success, or -1 on error.
|
|
*/
|
|
static inline int sread(STREAM* f, void* void_data, size_t size)
|
|
{
|
|
if (tommy_unlikely(!sptrlookup(f, size)))
|
|
return sread_uncached(f, void_data, size);
|
|
|
|
memcpy(void_data, f->pos, size);
|
|
f->pos += size;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Get a char from a stream, ignoring one '\r'.
|
|
*/
|
|
static inline int sgeteol(STREAM* f)
|
|
{
|
|
int c;
|
|
|
|
c = sgetc(f);
|
|
if (c == '\r')
|
|
c = sgetc(f);
|
|
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* Read all the spaces and tabs.
|
|
* Return the number of spaces and tabs read.
|
|
*/
|
|
static inline int sgetspace(STREAM* f)
|
|
{
|
|
int count = 0;
|
|
int c;
|
|
|
|
c = sgetc(f);
|
|
while (c == ' ' || c == '\t') {
|
|
++count;
|
|
c = sgetc(f);
|
|
}
|
|
|
|
sungetc(c, f);
|
|
return count;
|
|
}
|
|
|
|
/**
|
|
* Read until the first space or tab.
|
|
* Stop at the first ' ', '\t', '\n' or EOF.
|
|
* Return <0 if the buffer is too small, or the number of chars read.
|
|
*/
|
|
ssize_t sgettok(STREAM* f, char* str, int size);
|
|
|
|
/**
|
|
* Read until the end of line.
|
|
* Stop at the first '\n' or EOF. Note that '\n' is left in the stream.
|
|
* Return <0 if the buffer is too small, or the number of chars read.
|
|
*/
|
|
ssize_t sgetline(STREAM* f, char* str, int size);
|
|
|
|
/**
|
|
* Like sgetline() but remove ' ' and '\t' at the end.
|
|
*/
|
|
ssize_t sgetlasttok(STREAM* f, char* str, int size);
|
|
|
|
/**
|
|
* Read a 32 bit number.
|
|
* Stop at the first not digit char or EOF.
|
|
* Return <0 if there isn't enough to read.
|
|
*/
|
|
int sgetu32(STREAM* f, uint32_t* value);
|
|
|
|
/****************************************************************************/
|
|
/* binary get */
|
|
|
|
/**
|
|
* Read a binary 32 bit number in packet format.
|
|
* Return <0 if there isn't enough to read.
|
|
*/
|
|
int sgetb32(STREAM* f, uint32_t* value);
|
|
|
|
/**
|
|
* Read a binary 64 bit number in packet format.
|
|
* Return <0 if there isn't enough to read.
|
|
*/
|
|
int sgetb64(STREAM* f, uint64_t* value);
|
|
|
|
/**
|
|
* Read a binary 32 bit number in little endian format.
|
|
* Return <0 if there isn't enough to read.
|
|
*/
|
|
int sgetble32(STREAM* f, uint32_t* value);
|
|
|
|
/**
|
|
* Read a binary string.
|
|
* Return -1 on error or if the buffer is too small, or the number of chars read.
|
|
*/
|
|
int sgetbs(STREAM* f, char* str, size_t size);
|
|
|
|
/****************************************************************************/
|
|
/* put */
|
|
|
|
/**
|
|
* Write a char. Like fputc().
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
static inline int sputc(int c, STREAM* s)
|
|
{
|
|
if (s->pos == s->end) {
|
|
if (sflush(s) != 0)
|
|
return -1;
|
|
}
|
|
|
|
*s->pos++ = c;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Write a end of line.
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
static inline int sputeol(STREAM* s)
|
|
{
|
|
#ifdef _WIN32
|
|
if (sputc('\r', s) != 0)
|
|
return -1;
|
|
#endif
|
|
return sputc('\n', s);
|
|
}
|
|
|
|
/**
|
|
* \internal Used by swrite() when the data crosses the buffered range.
|
|
* \note Don't call this directly, but use swrite().
|
|
*/
|
|
int swrite_uncached(const void* data, size_t size, STREAM* f);
|
|
|
|
/**
|
|
* Write a sized buffer.
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
static __always_inline int swrite(const void* data, size_t size, STREAM* f)
|
|
{
|
|
if (tommy_unlikely(!sptrlookup(f, size)))
|
|
return swrite_uncached(data, size, f);
|
|
|
|
memcpy(f->pos, data, size);
|
|
f->pos += size;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/****************************************************************************/
|
|
/* binary put */
|
|
|
|
/**
|
|
* Write a binary 32 bit number in packed format.
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
int sputb32(uint32_t value, STREAM* s);
|
|
|
|
/**
|
|
* Write a binary 64 bit number in packed format.
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
int sputb64(uint64_t value, STREAM* s);
|
|
|
|
/**
|
|
* Write a binary 32 bit number in little endian format.
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
int sputble32(uint32_t value, STREAM* s);
|
|
|
|
/**
|
|
* Write a binary string.
|
|
* Return 0 on success or -1 on error.
|
|
*/
|
|
int sputbs(const char* str, STREAM* s);
|
|
|
|
#endif
|
|
|