summaryrefslogtreecommitdiff
path: root/naive
diff options
context:
space:
mode:
authorThomas Jespersen <laumann.thomas@gmail.com>2014-12-15 22:27:37 +0100
committerThomas Jespersen <laumann.thomas@gmail.com>2014-12-15 22:27:37 +0100
commitef042199166fc8336f28fee263a5c067bafb72cf (patch)
tree3909770fc2f21ebf883a1c1acb82a6de92bb857a /naive
parent3f14c179d9a735e319e737e7a105a13b487c034e (diff)
Rewrite naive encoding WIP
Diffstat (limited to 'naive')
-rw-r--r--naive/naive.c85
1 files changed, 51 insertions, 34 deletions
diff --git a/naive/naive.c b/naive/naive.c
index 72cf1ec..b2810b4 100644
--- a/naive/naive.c
+++ b/naive/naive.c
@@ -34,48 +34,65 @@ uint64_t zmort(uint32_t n)
return x << 1;
}
-
-static void naive_encode_blk256(naive_stream *s, blk256_t *dblk)
+int naive_encode_init(naive_stream *s)
{
- int i;
- for (i = 0; i < 4; i++) {
- s->buf[i<<1] = zmort(lo(dblk->x[i]));
- s->buf[(i<<1)+1] = zmort(hi(dblk->x[i]));
- }
+ memset(s, 0x0, sizeof(naive_stream));
+ return 0;
}
-naive_stream *naive_encode_init(int fd)
+int naive_encode(naive_stream *strm)
{
- naive_stream *s = (naive_stream*)malloc(sizeof(naive_stream));
- s->out = fd;
- return s;
-}
+ if (strm->avail_in * 2 > strm->avail_out)
+ return NAIVE_NO_BUF_SPC;
-void naive_stream_free(naive_stream *s)
-{
- if (!s)
- return;
- free(s);
-}
+ blk256_t buf;
+ uint64_t enc[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+ int i;
+ for (; strm->avail_in >= BLK256SIZ; strm->avail_in -= BLK256SIZ, strm->total_in += BLK256SIZ) {
+ memcpy(&buf, strm->inp, BLK256SIZ);
+ for (i = 0; i < 4; i++) {
+ enc[i<<1] = zmort(lo(buf->x[i]));
+ enc[(i<<1)+1] = zmort(hi(buf->x[i]));
+ }
+ memcpy(strm->outp, &enc, BLK256SIZ*2);
-int naive_encode_file(naive_stream *s, FILE *in)
-{
- blk256_t db;
- size_t n, n_prev;
+ strm->total_out += BLK256SIZ*2;
+ strm->avail_out -= BLK256SIZ*2;
+ }
- memset(db.x, 0, BLK256SIZ);
+ /* Less than 256 bits left */
+ byte b;
+ unsigned int r;
+ for (; strm->avail_in > 0; strm->avail_in--, strm->total_in++) {
+ b = strm->inp;
+ r = (unsigned int)zmort((uint32_t)b);
- while ((n = fread(db.x, BLK256SIZ, 1, in)) > 0) {
- naive_encode_blk256(s, &db);
- write(s->out, s->buf, sizeof(uint64_t)<<3);
- memset(db.x, 0, BLK256SIZ);
- n_prev = n;
- }
- printf("prev block: %lu\n", n_prev);
- printf("last block: %lu\n", n);
- naive_encode_blk256(s, &db);
- write(s->out, s->buf, n);
+ *strm->outp++ = (byte)(r >> 8);
+ *strm->outp++ = (byte)r;
- return 0;
+ strm->total_out += 2;
+ strm->avail_out -= 2;
+ }
+ return NAIVE_OK;
}
+/* int naive_encode_file(naive_stream *s, FILE *in) */
+/* { */
+/* blk256_t db; */
+/* size_t n, n_prev; */
+
+/* memset(db.x, 0, BLK256SIZ); */
+
+/* while ((n = fread(db.x, BLK256SIZ, 1, in)) > 0) { */
+/* naive_encode_blk256(s, &db); */
+/* write(s->out, s->buf, sizeof(uint64_t)<<3); */
+/* memset(db.x, 0, BLK256SIZ); */
+/* n_prev = n; */
+/* } */
+/* printf("prev block: %lu\n", n_prev); */
+/* printf("last block: %lu\n", n); */
+/* naive_encode_blk256(s, &db); */
+/* write(s->out, s->buf, n); */
+
+/* return 0; */
+/* } */