123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #include "blockwise.h"
- #include "bitops.h"
- #include "tassert.h"
- #include <string.h>
- #ifndef MIN
- #define MIN(a, b) (((a) < (b)) ? (a) : (b))
- #endif
- void cf_blockwise_accumulate(uint8_t *partial, size_t *npartial, size_t nblock,
- const void *inp, size_t nbytes,
- cf_blockwise_in_fn process,
- void *ctx)
- {
- cf_blockwise_accumulate_final(partial, npartial, nblock,
- inp, nbytes,
- process, process, ctx);
- }
- void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial, size_t nblock,
- const void *inp, size_t nbytes,
- cf_blockwise_in_fn process,
- cf_blockwise_in_fn process_final,
- void *ctx)
- {
- const uint8_t *bufin = inp;
- assert(partial && *npartial < nblock);
- assert(inp || !nbytes);
- assert(process && ctx);
-
- if (*npartial && nbytes)
- {
- size_t space = nblock - *npartial;
- size_t taken = MIN(space, nbytes);
- memcpy(partial + *npartial, bufin, taken);
- bufin += taken;
- nbytes -= taken;
- *npartial += taken;
-
- if (*npartial == nblock)
- {
- if (nbytes == 0)
- process_final(ctx, partial);
- else
- process(ctx, partial);
- *npartial = 0;
- }
- }
-
-
- while (nbytes >= nblock)
- {
-
- assert(*npartial == 0);
- if (nbytes == nblock)
- process_final(ctx, bufin);
- else
- process(ctx, bufin);
- bufin += nblock;
- nbytes -= nblock;
- }
-
- while (nbytes)
- {
- size_t space = nblock - *npartial;
- size_t taken = MIN(space, nbytes);
- memcpy(partial + *npartial, bufin, taken);
- bufin += taken;
- nbytes -= taken;
- *npartial += taken;
-
- assert(*npartial < nblock);
- }
- }
- void cf_blockwise_xor(uint8_t *partial, size_t *npartial, size_t nblock,
- const void *inp, void *outp, size_t nbytes,
- cf_blockwise_out_fn process, void *ctx)
- {
- const uint8_t *inb = inp;
- uint8_t *outb = outp;
- assert(partial && *npartial < nblock);
- assert(inp || !nbytes);
- assert(process && ctx);
- while (nbytes)
- {
-
- if (*npartial == 0)
- {
- process(ctx, partial);
- *npartial = nblock;
- }
- size_t offset = nblock - *npartial;
- size_t taken = MIN(*npartial, nbytes);
- xor_bb(outb, inb, partial + offset, taken);
- *npartial -= taken;
- nbytes -= taken;
- outb += taken;
- inb += taken;
- }
- }
- void cf_blockwise_acc_byte(uint8_t *partial, size_t *npartial,
- size_t nblock,
- uint8_t byte, size_t nbytes,
- cf_blockwise_in_fn process,
- void *ctx)
- {
-
- int filled = 0;
- while (nbytes)
- {
- size_t start = *npartial;
- size_t count = MIN(nbytes, nblock - start);
- if (!filled)
- memset(partial + start, byte, count);
- if (start == 0 && count == nblock)
- filled = 1;
- if (start + count == nblock)
- {
- process(ctx, partial);
- *npartial = 0;
- } else {
- *npartial += count;
- }
- nbytes -= count;
- }
- }
- void cf_blockwise_acc_pad(uint8_t *partial, size_t *npartial,
- size_t nblock,
- uint8_t fbyte, uint8_t mbyte, uint8_t lbyte,
- size_t nbytes,
- cf_blockwise_in_fn process,
- void *ctx)
- {
- switch (nbytes)
- {
- case 0: break;
- case 1: fbyte ^= lbyte;
- cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1, process, ctx);
- break;
- case 2:
- cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1, process, ctx);
- cf_blockwise_accumulate(partial, npartial, nblock, &lbyte, 1, process, ctx);
- break;
- default:
- cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1, process, ctx);
-
- if (lbyte != mbyte)
- {
- cf_blockwise_acc_byte(partial, npartial, nblock, mbyte, nbytes - 2, process, ctx);
- cf_blockwise_accumulate(partial, npartial, nblock, &lbyte, 1, process, ctx);
- } else {
- cf_blockwise_acc_byte(partial, npartial, nblock, mbyte, nbytes - 1, process, ctx);
- }
- break;
- }
- }
|