Signed-off-by: Dmitry Eremin-Solenikov dbaryshkov@gmail.com --- macros.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
diff --git a/macros.h b/macros.h index 990d32ee3b36..615f06c4c7de 100644 --- a/macros.h +++ b/macros.h @@ -215,6 +215,45 @@ do { \ ; \ } while (0)
+/* Takes the compression function f as argument. NOTE: also clobbers + length and data. */ +#define MD_BLOCK_UPDATE(state, bctx, bsize, length, data, f, incr) \ + do { \ + if ((bctx)->index) \ + { \ + /* Try to fill partial block */ \ + unsigned __md_left = bsize - (bctx)->index; \ + if ((length) < __md_left) \ + { \ + memcpy((bctx)->buffer + (bctx)->index, (data), (length)); \ + (bctx)->index += (length); \ + goto __md_done; /* Finished */ \ + } \ + else \ + { \ + memcpy((bctx)->buffer + (bctx)->index, (data), __md_left); \ + \ + f((state), (bctx)->buffer); \ + (incr); \ + \ + (data) += __md_left; \ + (length) -= __md_left; \ + } \ + } \ + while ((length) >= bsize) \ + { \ + f((state), (data)); \ + (incr); \ + \ + (data) += bsize; \ + (length) -= bsize; \ + } \ + memcpy ((bctx)->buffer, (data), (length)); \ + (bctx)->index = (length); \ + __md_done: \ + ; \ + } while (0) + /* Pads the block to a block boundary with the bit pattern 1 0*, leaving size octets for the length field at the end. If needed, compresses the block and starts a new one. */ @@ -242,4 +281,31 @@ do { \ \ } while (0)
+/* Pads the block to a block boundary with the bit pattern 1 0*, + leaving size octets for the length field at the end. If needed, + compresses the block and starts a new one. */ +#define MD_BLOCK_PAD(sctx, bctx, bsize, size, f) \ + do { \ + unsigned __md_i; \ + __md_i = (bctx)->index; \ + \ + /* Set the first char of padding to 0x80. This is safe since there \ + is always at least one byte free */ \ + \ + assert(__md_i < bsize); \ + (bctx)->buffer[__md_i++] = 0x80; \ + \ + if (__md_i > (bsize - (size))) \ + { /* No room for length in this block. Process it and \ + pad with another one */ \ + memset((bctx)->buffer + __md_i, 0, bsize - __md_i); \ + \ + f((sctx), (bctx)->buffer); \ + __md_i = 0; \ + } \ + memset((bctx)->buffer + __md_i, 0, \ + bsize - (size) - __md_i); \ + \ + } while (0) + #endif /* NETTLE_MACROS_H_INCLUDED */