I/O wrapper for files or buffers. More...
Functions | |
Pcpstream * | ps_new_file (FILE *backendfd) |
Create a new stream, backed with an open file. More... | |
Pcpstream * | ps_new_inbuffer (Buffer *b) |
Create a new input stream, backed with filled a buffer. More... | |
Pcpstream * | ps_new_outbuffer () |
Create a new output stream, backed with a buffer. More... | |
size_t | ps_read (Pcpstream *stream, void *buf, size_t readbytes) |
Read bytes into the given buffer from the current stream. More... | |
size_t | ps_write (Pcpstream *stream, void *buf, size_t writebytes) |
Write bytes from the given buffer into the current stream. More... | |
size_t | ps_finish (Pcpstream *stream) |
Finalize writing the stream. More... | |
size_t | ps_print (Pcpstream *stream, const char *fmt,...) |
Write a formatted string to the stream. More... | |
size_t | ps_tell (Pcpstream *stream) |
Tell the current read or write offset. More... | |
Buffer * | ps_buffer (Pcpstream *stream) |
Access the Buffer backend pointer. More... | |
void | ps_close (Pcpstream *stream) |
Close the stream and frees allocated memory. More... | |
int | ps_end (Pcpstream *stream) |
Check if EOF have been reached. More... | |
int | ps_err (Pcpstream *stream) |
Check if an error occurred during a read or write operation. More... | |
void | ps_setdetermine (Pcpstream *stream, size_t blocksize) |
Enable auto Z85 encoding detection for an input stream. More... | |
void | ps_armor (Pcpstream *stream, size_t blocksize) |
Enable Z85 encoding for an output stream. More... | |
void | ps_unarmor (Pcpstream *stream) |
Disable Z85 encoding for an output stream. More... | |
int | ps_readline (Pcpstream *stream, Buffer *line) |
Read a line from the stream. More... | |
I/O wrapper for files or buffers.
Simple wrapper around FILE streams or Buffers, depending how the user initialized them. The Pcpstream object behaves always the same and it doesn't matter how it's backed.
We use it in the lib, e.g. in the crypto routines. That way we can support blockwise crypto on buffers or files.
Streams are, just like iostreams in c++, either output or input mode.
Sample usage:
void ps_armor | ( | Pcpstream * | stream, |
size_t | blocksize | ||
) |
Enable Z85 encoding for an output stream.
[in] | stream | The stream object. |
[in] | blocksize | The blocksize to for Z85 encoding. |
Access the Buffer backend pointer.
Use this function to access the underlying Buffer object of an output stream to access the contents written to it. Only usefull if the stream have been initialized with ps_new_outbuffer().
[in] | stream | The stream object. |
void ps_close | ( | Pcpstream * | stream | ) |
Close the stream and frees allocated memory.
If the backend of the stream was a FILE stream, close it, unless it is stdin, stdout or stderr.
If the backend was a Buffer, clear and free it.
[in] | stream | The stream to close. |
int ps_end | ( | Pcpstream * | stream | ) |
Check if EOF have been reached.
This function can be used to check if there are no more bytes to read. This will happen if we reach EOF with a FILE backed stream or buffer_done() with a Buffer backed stream.
[in] | stream | The stream object. |
int ps_err | ( | Pcpstream * | stream | ) |
Check if an error occurred during a read or write operation.
[in] | stream | The stream object. |
size_t ps_finish | ( | Pcpstream * | stream | ) |
Finalize writing the stream.
You NEED to call this function after you're done writing to the stream if you enabled Z85 armoring using ps_armor().
It writes the remaining bytes (encoded) to the stream destination.
[in] | stream | The input stream to write to. |
Pcpstream* ps_new_file | ( | FILE * | backendfd | ) |
Create a new stream, backed with an open file.
The stream used for in- or output.
[in] | backendfd | An open FILE stream. |
Create a new input stream, backed with filled a buffer.
This kind of stream can be used for reading only.
[in] | b | A Buffer object filled with data. |
Pcpstream* ps_new_outbuffer | ( | ) |
Create a new output stream, backed with a buffer.
The buffer used to write data to will be allocated and filled by the class. You can retrieve it later.
size_t ps_print | ( | Pcpstream * | stream, |
const char * | fmt, | ||
... | |||
) |
Write a formatted string to the stream.
Use an printf() style format string to print something out to a stream.
Sets err in case of an error. See ps_err().
[out] | stream | The input stream to read from. |
[in] | fmt | The printf() compatible format description. |
[in] | ... | A variable number of arguments for the format string. |
size_t ps_read | ( | Pcpstream * | stream, |
void * | buf, | ||
size_t | readbytes | ||
) |
Read bytes into the given buffer from the current stream.
This function reads 'readbytes' bytes from the stream into given buf. The buffer needs to be properly allocated by the caller in advance to have at least readbytes len.
Sets eof=1 if end of file or end of buffer has been reached.
Sets err=1 if an error occurred, fatals() maybe set, or errno.
[in] | stream | The input stream to read from. |
[out] | buf | The buffer where to put read bytes. |
[in] | readbytes | The number of bytes to read. |
Read a line from the stream.
[in] | stream | The stream object. |
[out] | line | Linecontent will be written to this Buffer. |
void ps_setdetermine | ( | Pcpstream * | stream, |
size_t | blocksize | ||
) |
Enable auto Z85 encoding detection for an input stream.
If you're not sure if your input is Z85 encoded, enable detection.
[in] | stream | The stream object. |
[in] | blocksize | The blocksize to for Z85 decoding (if encoded). |
size_t ps_tell | ( | Pcpstream * | stream | ) |
Tell the current read or write offset.
This function works like ftell() on a FILE stream or like Buffer->offset in the Buffer class.
[in] | stream | The input stream to read from. |
void ps_unarmor | ( | Pcpstream * | stream | ) |
Disable Z85 encoding for an output stream.
[in] | stream | The stream object. |
size_t ps_write | ( | Pcpstream * | stream, |
void * | buf, | ||
size_t | writebytes | ||
) |
Write bytes from the given buffer into the current stream.
This function writes 'writebytes' bytes from the given buf into the stream
Sets err in case of an error. See ps_err().
[out] | stream | The input stream to write to. |
[in] | buf | The buffer containing data to put into the stream. |
[in] | writebytes | The number of bytes to write. |