progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / symm / counter-def.h
index 8ec798d..3c949c7 100644 (file)
 #  include "paranoia.h"
 #endif
 
+#ifndef CATACOMB_RSVR_H
+#  include "rsvr.h"
+#endif
+
 /*----- Macros ------------------------------------------------------------*/
 
 /* --- @COUNTER_DEF@ --- *
@@ -167,61 +171,66 @@ void pre##_counterinit(pre##_counterctx *ctx,                             \
  *             use the cipher as a random data generator.              \
  */                                                                    \
                                                                        \
+static const rsvr_policy pre##_counterpolicy =                         \
+  { 0, PRE##_BLKSZ, PRE##_BLKSZ };                                     \
+                                                                       \
 void pre##_counterencrypt(pre##_counterctx *ctx,                       \
                          const void *src, void *dest,                  \
                          size_t sz)                                    \
 {                                                                      \
-  const octet *s = src;                                                        \
+  rsvr_plan plan;                                                      \
+  const octet *s = src, *p;                                            \
   octet *d = dest;                                                     \
-  unsigned off = ctx->off;                                             \
-  uint32 t[PRE##_BLKSZ/4], u[PRE##_BLKSZ/4];                           \
-  octet y;                                                             \
-                                                                       \
-  /* --- Empty blocks are trivial --- */                               \
-                                                                       \
-  if (!sz) return;                                                     \
-                                                                       \
-  /* --- If I can deal with the block from my buffer, do that --- */   \
-                                                                       \
-  if (sz < PRE##_BLKSZ - off) goto small;                              \
-                                                                       \
-  /* --- Finish off what's left in my buffer --- */                    \
-                                                                       \
-  if (!d) sz -= PRE##_BLKSZ - off;                                     \
-  else while (off < PRE##_BLKSZ)                                       \
-    { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; }              \
-                                                                       \
-  /* --- Main encryption loop --- */                                   \
-                                                                       \
-  for (;;) {                                                           \
-    pre##_eblk(&ctx->ctx, ctx->c, t);                                  \
-    BLKC_STEP(PRE, ctx->c);                                            \
-    if (sz < PRE##_BLKSZ) break;                                       \
-    if (!d) /* do nothing */;                                          \
-    else if (!s) { BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ; }          \
-    else {                                                             \
-      BLKC_LOAD(PRE, u, s); s += PRE##_BLKSZ;                          \
-      BLKC_XSTORE(PRE, d, t, u); d += PRE##_BLKSZ;                     \
+  uint32 t[PRE##_BLKSZ/4];                                             \
+                                                                       \
+  /* Construct a plan and prepare to follow through. */                        \
+  rsvr_mkplan(&plan, &pre##_counterpolicy, ctx->off, sz);              \
+                                                                       \
+  /* Initial portion, fulfilled from the buffer.  If the chunk is small        \
+   * enough, then this will be the only portion.  If the buffer is     \
+   * currently empty, then we must prepare it.                         \
+   */                                                                  \
+  if (plan.head) {                                                     \
+    if (!ctx->off) {                                                   \
+      pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);                \
+      BLKC_STORE(PRE, ctx->b, t);                                      \
     }                                                                  \
-    sz -= PRE##_BLKSZ;                                                 \
+    p = ctx->b + ctx->off; ctx->off += plan.head;                      \
+    if (!d) /* nothing to do */;                                       \
+    else if (!s) { memcpy(d, p, plan.head); d += plan.head; }          \
+    else while (plan.head--) *d++ = *s++ ^ *p++;                       \
   }                                                                    \
                                                                        \
-  BLKC_STORE(PRE, ctx->b, t);                                          \
-  off = 0;                                                             \
-                                                                       \
-  /* --- Tidying up the tail end --- */                                        \
-                                                                       \
-  if (sz) {                                                            \
-  small:                                                               \
-    if (!d) off += sz;                                                 \
-    else do { y = s ? *s++ : 0; *d++ = ctx->b[off++] ^ y; sz--; }      \
-    while (sz);                                                                \
+  /* If the buffer is all used, then reset it ready for next time. */  \
+  ctx->off -= plan.from_rsvr;                                          \
+                                                                       \
+  /* Handle multiple whole blocks. */                                  \
+  if (!d)                                                              \
+    BLKC_ADD(PRE, ctx->c, plan.from_input/PRE##_BLKSZ);                        \
+  else if (!s) while (plan.from_input) {                               \
+    pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);          \
+    BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ;                           \
+    plan.from_input -= PRE##_BLKSZ;                                    \
+  } else while (plan.from_input) {                                     \
+    pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);          \
+    BLKC_XLOAD(PRE, t, s); s += PRE##_BLKSZ;                           \
+    BLKC_STORE(PRE, d, t); d += PRE##_BLKSZ;                           \
+    plan.from_input -= PRE##_BLKSZ;                                    \
   }                                                                    \
                                                                        \
-  /* --- Done --- */                                                   \
-                                                                       \
-  ctx->off = off;                                                      \
-  return;                                                              \
+  /* Final portion.  Note that the buffer must be empty if there is a  \
+   * tail, since otherwise the input data would have been part of the  \
+   * head portion instad. */                                           \
+  if (!plan.tail)                                                      \
+    BLKC_STORE(PRE, ctx->b, t);                                                \
+  else {                                                               \
+    pre##_eblk(&ctx->ctx, ctx->c, t); BLKC_STEP(PRE, ctx->c);          \
+    BLKC_STORE(PRE, ctx->b, t);                                                \
+    p = ctx->b; ctx->off += plan.tail;                                 \
+    if (!d) /* nothing to do */;                                       \
+    else if (!s) { memcpy(d, p, plan.tail); d += plan.tail; }          \
+    else while (plan.tail--) *d++ = *s++ ^ *p++;                       \
+  }                                                                    \
 }                                                                      \
                                                                        \
 /* --- Generic cipher interface --- */                                 \