1 /* Copyright (C) 2007 Josh MacDonald */
7 #define SPACE_MAX 131072 // how much memory per process
8 #define OUTPUT_MAX 1024 // max size for output
9 #define XD3_ALLOCSIZE 256 // internal size for various buffers
10 #define IOPT_SIZE 128 // instruction buffer
12 // SPACE_MAX of 32K is sufficient for most inputs with XD3_COMPLEVEL_1
13 // XD3_COMPLEVEL_9 requires about 4x more space than XD3_COMPLEVEL_1
18 typedef struct _context {
23 static int max_allocated = 0;
26 process_alloc (void* opaque, usize_t items, usize_t size)
28 context_t *ctx = (context_t*) opaque;
29 usize_t t = items * size;
32 if (ctx->allocated + t > SPACE_MAX)
37 ret = ctx->buffer + ctx->allocated;
43 process_free (void* opaque, void *ptr)
48 process_page (int is_encode,
49 int (*func) (xd3_stream *),
52 const uint8_t *source,
55 usize_t output_size_max,
58 /* On my x86 this is 1072 of objects on the stack */
62 context_t *ctx = calloc(SPACE_MAX, 1);
65 memset (&config, 0, sizeof(config));
69 printf("calloc failed\n");
73 ctx->buffer = (uint8_t*)ctx;
74 ctx->allocated = sizeof(*ctx);
77 config.winsize = PAGE_SIZE;
78 config.sprevsz = PAGE_SIZE;
79 config.srcwin_maxsz = PAGE_SIZE;
80 config.iopt_size = IOPT_SIZE;
81 config.alloc = &process_alloc;
82 config.freef = &process_free;
83 config.opaque = (void*) ctx;
85 src.blksize = PAGE_SIZE;
86 src.onblk = PAGE_SIZE;
90 if ((ret = xd3_config_stream (&stream, &config)) != 0 ||
91 (ret = xd3_set_source_and_size (&stream, &src, PAGE_SIZE)) != 0 ||
92 (ret = xd3_process_stream (is_encode,
97 output_size_max)) != 0)
99 if (stream.msg != NULL)
101 fprintf(stderr, "stream message: %s\n", stream.msg);
105 xd3_free_stream (&stream);
106 if (max_allocated < ctx->allocated)
108 max_allocated = ctx->allocated;
109 fprintf(stderr, "max allocated %d\n", max_allocated);
116 int test(int stride, int encode_flags)
118 uint8_t frompg[PAGE_SIZE];
119 uint8_t topg[PAGE_SIZE];
120 uint8_t output[OUTPUT_MAX];
121 uint8_t reout[PAGE_SIZE];
126 for (i = 0; i < PAGE_SIZE; i++)
128 topg[i] = frompg[i] = (rand() >> 3 ^ rand() >> 6 ^ rand() >> 9);
131 // change 1 byte every stride
134 for (j = stride; j <= PAGE_SIZE; j += stride)
140 if ((ret = process_page (1, xd3_encode_input,
143 &output_size, OUTPUT_MAX,
146 fprintf (stderr, "encode failed: stride %u flags 0x%x\n",
147 stride, encode_flags);
151 if ((ret = process_page (0, xd3_decode_input,
157 fprintf (stderr, "decode failed: stride %u output_size %u flags 0x%x\n",
158 stride, output_size, encode_flags);
162 if (output_size > OUTPUT_MAX || re_size != PAGE_SIZE)
164 fprintf (stderr, "internal error: %u != %u\n", output_size, re_size);
168 for (i = 0; i < PAGE_SIZE; i++)
170 if (reout[i] != topg[i])
172 fprintf (stderr, "encode-decode error: position %d\n", i);
177 fprintf(stderr, "stride %d flags 0x%x size %u ",
178 stride, encode_flags, output_size);
179 fprintf(stderr, "%s\n", (ret == 0) ? "OK" : "FAIL");
189 for (level = 1; level < 10; level = (level == 1 ? 3 : level + 3))
191 int lflag = level << XD3_COMPLEVEL_SHIFT;
193 for (stride = 2; stride <= PAGE_SIZE; stride += 2)
196 test(stride, lflag | XD3_SEC_DJW);