small_page_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* xdelta3 - delta compression tools and library
  2. Copyright 2016 Joshua MacDonald
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #define PAGE_SIZE 4096
  15. #define SPACE_MAX 131072 // how much memory per process
  16. #define OUTPUT_MAX 1024 // max size for output
  17. #define XD3_ALLOCSIZE 256 // internal size for various buffers
  18. #define IOPT_SIZE 128 // instruction buffer
  19. // SPACE_MAX of 32K is sufficient for most inputs with XD3_COMPLEVEL_1
  20. // XD3_COMPLEVEL_9 requires about 4x more space than XD3_COMPLEVEL_1
  21. #include "xdelta3.h"
  22. #include "xdelta3.c"
  23. typedef struct _context {
  24. uint8_t *buffer;
  25. int allocated;
  26. } context_t;
  27. static int max_allocated = 0;
  28. void*
  29. process_alloc (void* opaque, usize_t items, usize_t size)
  30. {
  31. context_t *ctx = (context_t*) opaque;
  32. usize_t t = items * size;
  33. void *ret;
  34. if (ctx->allocated + t > SPACE_MAX)
  35. {
  36. return NULL;
  37. }
  38. ret = ctx->buffer + ctx->allocated;
  39. ctx->allocated += t;
  40. return ret;
  41. }
  42. void
  43. process_free (void* opaque, void *ptr)
  44. {
  45. }
  46. int
  47. process_page (int is_encode,
  48. int (*func) (xd3_stream *),
  49. const uint8_t *input,
  50. usize_t input_size,
  51. const uint8_t *source,
  52. uint8_t *output,
  53. usize_t *output_size,
  54. usize_t output_size_max,
  55. int flags) {
  56. /* On my x86 this is 1072 of objects on the stack */
  57. xd3_stream stream;
  58. xd3_config config;
  59. xd3_source src;
  60. context_t *ctx = calloc(SPACE_MAX, 1);
  61. int ret;
  62. memset (&config, 0, sizeof(config));
  63. if (ctx == NULL)
  64. {
  65. printf("calloc failed\n");
  66. return -1;
  67. }
  68. ctx->buffer = (uint8_t*)ctx;
  69. ctx->allocated = sizeof(*ctx);
  70. config.flags = flags;
  71. config.winsize = PAGE_SIZE;
  72. config.sprevsz = PAGE_SIZE;
  73. config.srcwin_maxsz = PAGE_SIZE;
  74. config.iopt_size = IOPT_SIZE;
  75. config.alloc = &process_alloc;
  76. config.freef = &process_free;
  77. config.opaque = (void*) ctx;
  78. src.blksize = PAGE_SIZE;
  79. src.onblk = PAGE_SIZE;
  80. src.curblk = source;
  81. src.curblkno = 0;
  82. if ((ret = xd3_config_stream (&stream, &config)) != 0 ||
  83. (ret = xd3_set_source_and_size (&stream, &src, PAGE_SIZE)) != 0 ||
  84. (ret = xd3_process_stream (is_encode,
  85. &stream,
  86. func, 1,
  87. input, input_size,
  88. output, output_size,
  89. output_size_max)) != 0)
  90. {
  91. if (stream.msg != NULL)
  92. {
  93. fprintf(stderr, "stream message: %s\n", stream.msg);
  94. }
  95. }
  96. xd3_free_stream (&stream);
  97. if (max_allocated < ctx->allocated)
  98. {
  99. max_allocated = ctx->allocated;
  100. fprintf(stderr, "max allocated %d\n", max_allocated);
  101. }
  102. free(ctx);
  103. return ret;
  104. }
  105. int test(int stride, int encode_flags)
  106. {
  107. uint8_t frompg[PAGE_SIZE];
  108. uint8_t topg[PAGE_SIZE];
  109. uint8_t output[OUTPUT_MAX];
  110. uint8_t reout[PAGE_SIZE];
  111. usize_t output_size;
  112. usize_t re_size;
  113. int i, j, ret;
  114. for (i = 0; i < PAGE_SIZE; i++)
  115. {
  116. topg[i] = frompg[i] = (rand() >> 3 ^ rand() >> 6 ^ rand() >> 9);
  117. }
  118. // change 1 byte every stride
  119. if (stride > 0)
  120. {
  121. for (j = stride; j <= PAGE_SIZE; j += stride)
  122. {
  123. topg[j - 1] ^= 0xff;
  124. }
  125. }
  126. if ((ret = process_page (1, xd3_encode_input,
  127. topg, PAGE_SIZE,
  128. frompg, output,
  129. &output_size, OUTPUT_MAX,
  130. encode_flags)) != 0)
  131. {
  132. fprintf (stderr, "encode failed: stride %u flags 0x%x\n",
  133. stride, encode_flags);
  134. return ret;
  135. }
  136. if ((ret = process_page (0, xd3_decode_input,
  137. output, output_size,
  138. frompg, reout,
  139. &re_size, PAGE_SIZE,
  140. 0)) != 0)
  141. {
  142. fprintf (stderr, "decode failed: stride %u output_size %u flags 0x%x\n",
  143. stride, output_size, encode_flags);
  144. return ret;
  145. }
  146. if (output_size > OUTPUT_MAX || re_size != PAGE_SIZE)
  147. {
  148. fprintf (stderr, "internal error: %u != %u\n", output_size, re_size);
  149. return -1;
  150. }
  151. for (i = 0; i < PAGE_SIZE; i++)
  152. {
  153. if (reout[i] != topg[i])
  154. {
  155. fprintf (stderr, "encode-decode error: position %d\n", i);
  156. return -1;
  157. }
  158. }
  159. fprintf(stderr, "stride %d flags 0x%x size %u ",
  160. stride, encode_flags, output_size);
  161. fprintf(stderr, "%s\n", (ret == 0) ? "OK" : "FAIL");
  162. return 0;
  163. }
  164. int main()
  165. {
  166. int stride;
  167. int level;
  168. for (level = 1; level < 10; level = (level == 1 ? 3 : level + 3))
  169. {
  170. int lflag = level << XD3_COMPLEVEL_SHIFT;
  171. for (stride = 2; stride <= PAGE_SIZE; stride += 2)
  172. {
  173. test(stride, lflag);
  174. test(stride, lflag | XD3_SEC_DJW);
  175. }
  176. }
  177. return 0;
  178. }