mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
Revert "rm html stuff from repo, changes too often. enhanced pcpstream"
This reverts commit 82b05b768e.
This commit is contained in:
4
TODO
4
TODO
@@ -17,10 +17,6 @@ vault checksum: add keysigs as well
|
|||||||
|
|
||||||
enable formats for secret key exports as well
|
enable formats for secret key exports as well
|
||||||
|
|
||||||
PCPSTREAM changes:
|
|
||||||
- enable determine armor mode of input, parse headers, comments
|
|
||||||
and newlines away, decode and return the binary result with ps_read()
|
|
||||||
as before.
|
|
||||||
|
|
||||||
|
|
||||||
Python binding, e.g.:
|
Python binding, e.g.:
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ extern "C" {
|
|||||||
#include "pcp/config.h"
|
#include "pcp/config.h"
|
||||||
#include "pcp/base85.h"
|
#include "pcp/base85.h"
|
||||||
#include "pcp/buffer.h"
|
#include "pcp/buffer.h"
|
||||||
#include "pcp/config.h"
|
|
||||||
#include "pcp/crypto.h"
|
#include "pcp/crypto.h"
|
||||||
#include "pcp/defines.h"
|
#include "pcp/defines.h"
|
||||||
#include "pcp/digital_crc32.h"
|
#include "pcp/digital_crc32.h"
|
||||||
|
|||||||
@@ -28,7 +28,6 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "z85.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \defgroup Pcpstream PCPSTREAMS
|
* \defgroup Pcpstream PCPSTREAMS
|
||||||
@@ -59,15 +58,9 @@
|
|||||||
struct _pcp_stream_t {
|
struct _pcp_stream_t {
|
||||||
FILE *fd; /**< The backend FILE stream */
|
FILE *fd; /**< The backend FILE stream */
|
||||||
Buffer *b; /**< The backend Buffer object */
|
Buffer *b; /**< The backend Buffer object */
|
||||||
Buffer *z; /**< Buffer Cache for Z85 en/de-coding */
|
|
||||||
Buffer *t; /**< Temporary Buffer */
|
|
||||||
uint8_t is_buffer; /**< Set to 1 if the backend is a Buffer */
|
uint8_t is_buffer; /**< Set to 1 if the backend is a Buffer */
|
||||||
uint8_t eof; /**< Set to 1 if EOF reached */
|
uint8_t eof; /**< Set to 1 if EOF reached */
|
||||||
uint8_t err; /**< Set to 1 if an error occured */
|
uint8_t err; /**< Set to 1 if an error occured */
|
||||||
uint8_t armor; /**< Set to 1 if Z85 en/de-coding is requested */
|
|
||||||
uint8_t determine; /**< Set to 1 to automatically determine armor mode */
|
|
||||||
uint8_t firstread; /**< Internal flag, will be set after first read() */
|
|
||||||
size_t linewr; /**< Used for Z85 writing, remember how many chars we lastly wrote on the current line */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** The name used everywhere */
|
/** The name used everywhere */
|
||||||
|
|||||||
@@ -23,14 +23,11 @@
|
|||||||
|
|
||||||
Pcpstream *ps_init(void) {
|
Pcpstream *ps_init(void) {
|
||||||
Pcpstream *stream = ucmalloc(sizeof(Pcpstream));
|
Pcpstream *stream = ucmalloc(sizeof(Pcpstream));
|
||||||
stream->z = buffer_new(32, "Z85stream");
|
|
||||||
stream->b = NULL;
|
stream->b = NULL;
|
||||||
stream->fd = NULL;
|
stream->fd = NULL;
|
||||||
stream->is_buffer = 0;
|
stream->is_buffer = 0;
|
||||||
stream->eof = 0;
|
stream->eof = 0;
|
||||||
stream->err = 0;
|
stream->err = 0;
|
||||||
stream->armor = 0;
|
|
||||||
stream->determine = 0;
|
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,47 +51,15 @@ Pcpstream *ps_new_outbuffer() {
|
|||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ps_armor(Pcpstream *stream) {
|
|
||||||
stream->armor = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ps_determine(Pcpstream *stream) {
|
|
||||||
stream->determine = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ps_end(Pcpstream *stream) {
|
|
||||||
return stream->eof;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ps_err(Pcpstream *stream) {
|
|
||||||
return stream->err;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes) {
|
size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes) {
|
||||||
size_t gotbytes = 0;
|
size_t gotbytes = 0;
|
||||||
|
|
||||||
if(stream->is_buffer) {
|
if(stream->is_buffer) {
|
||||||
/* a buffer stream */
|
/* check if there's enough space in our buffer */
|
||||||
if(buffer_left(stream->b) < readbytes)
|
if(buffer_left(stream->b) < readbytes)
|
||||||
readbytes = buffer_left(stream->b);
|
readbytes = buffer_left(stream->b);
|
||||||
|
|
||||||
if(stream->armor == 1) {
|
|
||||||
size_t i = 0;
|
|
||||||
uint8_t c;
|
|
||||||
while (i < readbytes) {
|
|
||||||
c = buffer_get8(stream->b);
|
|
||||||
if(c != '\r' && c != '\n') {
|
|
||||||
buffer_add8(stream->z, c);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
memcpy(buf, buffer_get(stream->z), buffer_size(stream->z));
|
|
||||||
gotbytes = buffer_size(stream->z);
|
|
||||||
buffer_clear(stream->z);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
gotbytes = buffer_get_chunk(stream->b, buf, readbytes);
|
gotbytes = buffer_get_chunk(stream->b, buf, readbytes);
|
||||||
|
|
||||||
if(gotbytes == 0) {
|
if(gotbytes == 0) {
|
||||||
/* this should not happen with buffers */
|
/* this should not happen with buffers */
|
||||||
stream->eof = 1;
|
stream->eof = 1;
|
||||||
@@ -102,27 +67,7 @@ size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* a FILE stream */
|
|
||||||
if(stream->armor == 1) {
|
|
||||||
size_t i = 0;
|
|
||||||
uint8_t c;
|
|
||||||
while (i < readbytes) {
|
|
||||||
gotbytes = fread(&c, 1, 1, stream->fd);
|
|
||||||
if(gotbytes == 0)
|
|
||||||
break;
|
|
||||||
if(c != '\r' && c != '\n') {
|
|
||||||
buffer_add8(stream->z, c);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
memcpy(buf, buffer_get(stream->z), buffer_size(stream->z));
|
|
||||||
gotbytes = buffer_size(stream->z);
|
|
||||||
_dump("buf", buf, gotbytes);
|
|
||||||
buffer_clear(stream->z);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
gotbytes = fread(buf, 1, readbytes, stream->fd);
|
gotbytes = fread(buf, 1, readbytes, stream->fd);
|
||||||
|
|
||||||
if(gotbytes == 0) {
|
if(gotbytes == 0) {
|
||||||
if(feof(stream->fd) != 0)
|
if(feof(stream->fd) != 0)
|
||||||
stream->eof = 1;
|
stream->eof = 1;
|
||||||
@@ -131,82 +76,22 @@ size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(gotbytes > 0 && stream->determine && stream->firstread == 0) {
|
|
||||||
/* check if we need to decode input */
|
|
||||||
fprintf(stderr, "determine\n");
|
|
||||||
if(_buffer_is_binary(buf, gotbytes) == 0) {
|
|
||||||
fprintf(stderr, "is armored\n");
|
|
||||||
stream->armor = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->firstread = 1;
|
|
||||||
|
|
||||||
if(gotbytes > 0 && stream->armor == 1) {
|
|
||||||
/* z85 decode buf */
|
|
||||||
size_t binlen;
|
|
||||||
unsigned char *bin = pcp_z85_decode(buf, &binlen);
|
|
||||||
if(bin == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(buf, bin, binlen);
|
|
||||||
|
|
||||||
_dump("decoded", buf, binlen);
|
|
||||||
|
|
||||||
free(bin);
|
|
||||||
return binlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
return gotbytes;
|
return gotbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ps_write(Pcpstream *stream, void *buf, size_t writebytes) {
|
size_t ps_write(Pcpstream *stream, void *buf, size_t writebytes) {
|
||||||
size_t donebytes = 0;
|
size_t donebytes = 0;
|
||||||
|
|
||||||
if(stream->armor == 1) {
|
|
||||||
/* z85 encode buf */
|
|
||||||
size_t padlen, zlen, i, pos;
|
|
||||||
unsigned char *padded = pcp_padfour(buf, writebytes, &padlen);
|
|
||||||
|
|
||||||
zlen = (padlen * 5 / 4);
|
|
||||||
char *z85 = ucmalloc(zlen);
|
|
||||||
|
|
||||||
zmq_z85_encode(z85, padded, padlen);
|
|
||||||
|
|
||||||
_dump(" orig", buf, writebytes);
|
|
||||||
_dump(" z85", z85, zlen);
|
|
||||||
|
|
||||||
pos = stream->linewr;
|
|
||||||
for(i=0; i<zlen; ++i) {
|
|
||||||
if(pos >= 71) {
|
|
||||||
buffer_add8(stream->z, '\r');
|
|
||||||
buffer_add8(stream->z, '\n');
|
|
||||||
pos = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
pos++;
|
|
||||||
buffer_add8(stream->z, z85[i]);
|
|
||||||
}
|
|
||||||
stream->linewr = pos;
|
|
||||||
_dump("n added", buffer_get(stream->z), buffer_size(stream->z));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
buffer_add(stream->z, buf, writebytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(stream->is_buffer) {
|
if(stream->is_buffer) {
|
||||||
buffer_add(stream->b, buffer_get(stream->z), buffer_size(stream->z));
|
buffer_add(stream->b, buf, writebytes);
|
||||||
donebytes = buffer_size(stream->z);
|
donebytes = writebytes;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
donebytes = fwrite(buffer_get(stream->z), 1, buffer_size(stream->z), stream->fd);
|
donebytes = fwrite(buf, 1, writebytes, stream->fd);
|
||||||
if(ferror(stream->fd) != 0 || donebytes < buffer_size(stream->z))
|
if(ferror(stream->fd) != 0 || donebytes < writebytes)
|
||||||
stream->err = 1;
|
stream->err = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
writebytes = buffer_size(stream->z);
|
|
||||||
buffer_clear(stream->z);
|
|
||||||
return writebytes;
|
return writebytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,6 +125,14 @@ void ps_close(Pcpstream *stream) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ps_end(Pcpstream *stream) {
|
||||||
|
return stream->eof;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ps_err(Pcpstream *stream) {
|
||||||
|
return stream->err;
|
||||||
|
}
|
||||||
|
|
||||||
size_t ps_tell(Pcpstream *stream) {
|
size_t ps_tell(Pcpstream *stream) {
|
||||||
if(stream->is_buffer) {
|
if(stream->is_buffer) {
|
||||||
if(stream->b->end > stream->b->offset)
|
if(stream->b->end > stream->b->offset)
|
||||||
@@ -255,4 +148,3 @@ size_t ps_tell(Pcpstream *stream) {
|
|||||||
Buffer *ps_buffer(Pcpstream *stream) {
|
Buffer *ps_buffer(Pcpstream *stream) {
|
||||||
return stream->b;
|
return stream->b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
AM_CFLAGS = -I../include/pcp -I../src -I../libpcp/scrypt/crypto -Wall -g
|
AM_CFLAGS = -I../include/pcp -I../src -I../libpcp/scrypt/crypto -Wall -g
|
||||||
check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest buffertest sample streamtest
|
check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest buffertest sample
|
||||||
|
|
||||||
gencheader_LDADD = ../libpcp/.libs/libpcp1.a
|
gencheader_LDADD = ../libpcp/.libs/libpcp1.a
|
||||||
gencheader_SOURCES = gencheader.c
|
gencheader_SOURCES = gencheader.c
|
||||||
@@ -34,8 +34,7 @@ buffertest_SOURCES = buffertest.c
|
|||||||
sample_LDADD = ../libpcp/.libs/libpcp1.a
|
sample_LDADD = ../libpcp/.libs/libpcp1.a
|
||||||
sample_SOURCES = sample.c
|
sample_SOURCES = sample.c
|
||||||
|
|
||||||
streamtest_LDADD = ../libpcp/.libs/libpcp1.a
|
|
||||||
streamtest_SOURCES = streamtest.c
|
|
||||||
|
|
||||||
col_LDADD = ../libpcp/.libs/libpcp1.a
|
col_LDADD = ../libpcp/.libs/libpcp1.a
|
||||||
col_SOURCES = collisions.c ../src/compat_getopt.c
|
col_SOURCES = collisions.c ../src/compat_getopt.c
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ int main() {
|
|||||||
FILE *out, *in;
|
FILE *out, *in;
|
||||||
unsigned char clear[8] = "ABCDEFGH";
|
unsigned char clear[8] = "ABCDEFGH";
|
||||||
unsigned char key[8] = "IxD8Lq1K";
|
unsigned char key[8] = "IxD8Lq1K";
|
||||||
unsigned char crypt[11] = {0};
|
unsigned char crypt[8] = {0};
|
||||||
int blocks = 24;
|
int blocks = 8;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
if((out = fopen("teststream.out", "wb+")) == NULL) {
|
if((out = fopen("teststream.out", "wb+")) == NULL) {
|
||||||
@@ -18,7 +18,6 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
Pcpstream *pout = ps_new_file(out);
|
Pcpstream *pout = ps_new_file(out);
|
||||||
ps_armor(pout);
|
|
||||||
|
|
||||||
/* "encrypt" a couple of times into the file */
|
/* "encrypt" a couple of times into the file */
|
||||||
for(i=0; i<blocks; i++) {
|
for(i=0; i<blocks; i++) {
|
||||||
@@ -35,7 +34,6 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
Pcpstream *pin = ps_new_file(in);
|
Pcpstream *pin = ps_new_file(in);
|
||||||
ps_determine(pin);
|
|
||||||
|
|
||||||
/* we'll use this stream to put the "decrypted" data in.
|
/* we'll use this stream to put the "decrypted" data in.
|
||||||
note, that this could be a file as well. */
|
note, that this could be a file as well. */
|
||||||
@@ -43,7 +41,7 @@ int main() {
|
|||||||
|
|
||||||
/* read and "decrypt" */
|
/* read and "decrypt" */
|
||||||
for(i=0; i<blocks; i++) {
|
for(i=0; i<blocks; i++) {
|
||||||
ps_read(pin, crypt, 10);
|
ps_read(pin, crypt, 8);
|
||||||
_xorbuf(key, crypt, 8);
|
_xorbuf(key, crypt, 8);
|
||||||
ps_write(pclear, crypt, 8);
|
ps_write(pclear, crypt, 8);
|
||||||
}
|
}
|
||||||
@@ -56,7 +54,6 @@ int main() {
|
|||||||
/* and verify if it's "decrypted" (re-use crypt) */
|
/* and verify if it's "decrypted" (re-use crypt) */
|
||||||
for(i=0; i<blocks; i++) {
|
for(i=0; i<blocks; i++) {
|
||||||
buffer_get_chunk(result, crypt, 8);
|
buffer_get_chunk(result, crypt, 8);
|
||||||
_dump("chunk", crypt, 8);
|
|
||||||
if(memcmp(crypt, clear, 8) != 0) {
|
if(memcmp(crypt, clear, 8) != 0) {
|
||||||
fprintf(stderr, "Oops, block %d doesn't match\n", i);
|
fprintf(stderr, "Oops, block %d doesn't match\n", i);
|
||||||
goto error;
|
goto error;
|
||||||
|
|||||||
Reference in New Issue
Block a user