mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
added pcpstream, which wraps buffers or file i/o, so crypto code doesn't have to know if it works with a file or a buffer. makes the api much more flexible
This commit is contained in:
125
libpcp/pcpstream.c
Normal file
125
libpcp/pcpstream.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
This file is part of Pretty Curved Privacy (pcp1).
|
||||
|
||||
Copyright (C) 2013-2014 T.v.Dein.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
You can contact me by mail: <tom AT vondein DOT org>.
|
||||
*/
|
||||
|
||||
#include "pcpstream.h"
|
||||
|
||||
Pcpstream *ps_init(void) {
|
||||
Pcpstream *stream = ucmalloc(sizeof(Pcpstream));
|
||||
stream->b = NULL;
|
||||
stream->fd = NULL;
|
||||
stream->is_buffer = 0;
|
||||
stream->eof = 0;
|
||||
stream->err = 0;
|
||||
return stream;
|
||||
}
|
||||
|
||||
Pcpstream *ps_new_file(FILE *backendfd) {
|
||||
Pcpstream *stream = ps_init();
|
||||
stream->fd = backendfd;
|
||||
return stream;
|
||||
}
|
||||
|
||||
Pcpstream *ps_new_inbuffer(Buffer *b) {
|
||||
Pcpstream *stream = ps_init();
|
||||
stream->b = b;
|
||||
stream->is_buffer = 1;
|
||||
return stream;
|
||||
}
|
||||
|
||||
Pcpstream *ps_new_outbuffer() {
|
||||
Pcpstream *stream = ps_init();
|
||||
stream->b = buffer_new(32, "Pcpstream");
|
||||
stream->is_buffer = 1;
|
||||
return stream;
|
||||
}
|
||||
|
||||
size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes) {
|
||||
size_t gotbytes = 0;
|
||||
|
||||
if(stream->is_buffer) {
|
||||
gotbytes = buffer_get_chunk(stream->b, buf, readbytes);
|
||||
if(gotbytes == 0) {
|
||||
/* this should not happen with buffers */
|
||||
stream->eof = 1;
|
||||
stream->err = 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
gotbytes = fread(buf, 1, readbytes, stream->fd);
|
||||
if(gotbytes == 0) {
|
||||
if(feof(stream->fd) != 0)
|
||||
stream->eof = 1;
|
||||
if(ferror(stream->fd) != 0)
|
||||
stream->err = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return gotbytes;
|
||||
}
|
||||
|
||||
size_t ps_write(Pcpstream *stream, void *buf, size_t writebytes) {
|
||||
size_t donebytes = 0;
|
||||
|
||||
if(stream->is_buffer) {
|
||||
buffer_add(stream->b, buf, writebytes);
|
||||
donebytes = writebytes;
|
||||
}
|
||||
else {
|
||||
donebytes = fwrite(buf, 1, writebytes, stream->fd);
|
||||
if(ferror(stream->fd) != 0 || donebytes < writebytes)
|
||||
stream->err = 1;
|
||||
}
|
||||
|
||||
return writebytes;
|
||||
}
|
||||
|
||||
void ps_close(Pcpstream *stream) {
|
||||
if(stream->is_buffer) {
|
||||
buffer_clear(stream->b);
|
||||
free(stream);
|
||||
}
|
||||
else {
|
||||
/* only close files, not terminal devices */
|
||||
if(fileno(stream->fd) > 2)
|
||||
fclose(stream->fd);
|
||||
free(stream);
|
||||
}
|
||||
}
|
||||
|
||||
int ps_end(Pcpstream *stream) {
|
||||
return stream->eof;
|
||||
}
|
||||
|
||||
int ps_err(Pcpstream *stream) {
|
||||
return stream->err;
|
||||
}
|
||||
|
||||
size_t ps_tell(Pcpstream *stream) {
|
||||
if(stream->is_buffer) {
|
||||
if(stream->b->end > stream->b->offset)
|
||||
return stream->b->end; /* write buffer */
|
||||
else
|
||||
return stream->b->offset; /* read buffer */
|
||||
}
|
||||
else {
|
||||
return ftell(stream->fd);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user