stm_buffer.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "fitz-internal.h"
  2. fz_buffer *
  3. fz_new_buffer(fz_context *ctx, int size)
  4. {
  5. fz_buffer *b;
  6. size = size > 1 ? size : 16;
  7. b = fz_malloc_struct(ctx, fz_buffer);
  8. b->refs = 1;
  9. fz_try(ctx)
  10. {
  11. b->data = fz_malloc(ctx, size);
  12. }
  13. fz_catch(ctx)
  14. {
  15. fz_free(ctx, b);
  16. fz_rethrow(ctx);
  17. }
  18. b->cap = size;
  19. b->len = 0;
  20. b->unused_bits = 0;
  21. return b;
  22. }
  23. fz_buffer *
  24. fz_keep_buffer(fz_context *ctx, fz_buffer *buf)
  25. {
  26. if (buf)
  27. {
  28. if (buf->refs == 1 && buf->cap > buf->len+1)
  29. fz_resize_buffer(ctx, buf, buf->len);
  30. buf->refs ++;
  31. }
  32. return buf;
  33. }
  34. void
  35. fz_drop_buffer(fz_context *ctx, fz_buffer *buf)
  36. {
  37. if (!buf)
  38. return;
  39. if (--buf->refs == 0)
  40. {
  41. fz_free(ctx, buf->data);
  42. fz_free(ctx, buf);
  43. }
  44. }
  45. void
  46. fz_resize_buffer(fz_context *ctx, fz_buffer *buf, int size)
  47. {
  48. buf->data = fz_resize_array(ctx, buf->data, size, 1);
  49. buf->cap = size;
  50. if (buf->len > buf->cap)
  51. buf->len = buf->cap;
  52. }
  53. void
  54. fz_grow_buffer(fz_context *ctx, fz_buffer *buf)
  55. {
  56. int newsize = (buf->cap * 3) / 2;
  57. if (newsize == 0)
  58. newsize = 256;
  59. fz_resize_buffer(ctx, buf, newsize);
  60. }
  61. static void
  62. fz_ensure_buffer(fz_context *ctx, fz_buffer *buf, int min)
  63. {
  64. int newsize = buf->cap;
  65. while (newsize < min)
  66. {
  67. newsize = (newsize * 3) / 2;
  68. }
  69. fz_resize_buffer(ctx, buf, newsize);
  70. }
  71. void
  72. fz_trim_buffer(fz_context *ctx, fz_buffer *buf)
  73. {
  74. if (buf->cap > buf->len+1)
  75. fz_resize_buffer(ctx, buf, buf->len);
  76. }
  77. int
  78. fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap)
  79. {
  80. if (datap)
  81. *datap = (buf ? buf->data : NULL);
  82. return (buf ? buf->len : 0);
  83. }
  84. void
  85. fz_buffer_cat(fz_context *ctx, fz_buffer *buf, fz_buffer *extra)
  86. {
  87. if (buf->cap - buf->len < extra->len)
  88. {
  89. buf->data = fz_resize_array(ctx, buf->data, buf->len + extra->len, 1);
  90. buf->cap = buf->len + extra->len;
  91. }
  92. memcpy(buf->data + buf->len, extra->data, extra->len);
  93. buf->len += extra->len;
  94. }
  95. void fz_write_buffer(fz_context *ctx, fz_buffer *buf, unsigned char *data, int len)
  96. {
  97. if (buf->len + len > buf->cap)
  98. fz_ensure_buffer(ctx, buf, buf->len + len);
  99. memcpy(buf->data + buf->len, data, len);
  100. buf->len += len;
  101. buf->unused_bits = 0;
  102. }
  103. void fz_write_buffer_byte(fz_context *ctx, fz_buffer *buf, int val)
  104. {
  105. if (buf->len > buf->cap)
  106. fz_grow_buffer(ctx, buf);
  107. buf->data[buf->len++] = val;
  108. buf->unused_bits = 0;
  109. }
  110. void fz_write_buffer_rune(fz_context *ctx, fz_buffer *buf, int c)
  111. {
  112. char data[10];
  113. int len = fz_runetochar(data, c);
  114. if (buf->len + len > buf->cap)
  115. fz_ensure_buffer(ctx, buf, buf->len + len);
  116. memcpy(buf->data + buf->len, data, len);
  117. buf->len += len;
  118. buf->unused_bits = 0;
  119. }
  120. void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits)
  121. {
  122. int shift;
  123. /* Throughout this code, the invariant is that we need to write the
  124. * bottom 'bits' bits of 'val' into the stream. On entry we assume
  125. * that val & ((1<<bits)-1) == val, but we do not rely on this after
  126. * having written the first partial byte. */
  127. if (bits == 0)
  128. return;
  129. /* buf->len always covers all the bits in the buffer, including
  130. * any unused ones in the last byte, which will always be 0.
  131. * buf->unused_bits = the number of unused bits in the last byte.
  132. */
  133. /* Find the amount we need to shift val up by so that it will be in
  134. * the correct position to be inserted into any existing data byte. */
  135. shift = (buf->unused_bits - bits);
  136. /* Extend the buffer as required before we start; that way we never
  137. * fail part way during writing. If shift < 0, then we'll need -shift
  138. * more bits. */
  139. if (shift < 0)
  140. {
  141. int extra = (7-shift)>>3; /* Round up to bytes */
  142. fz_ensure_buffer(ctx, buf, buf->len + extra);
  143. }
  144. /* Write any bits that will fit into the existing byte */
  145. if (buf->unused_bits)
  146. {
  147. buf->data[buf->len-1] |= (shift >= 0 ? (((unsigned int)val)<<shift) : (((unsigned int)val)>>-shift));
  148. if (shift >= 0)
  149. {
  150. /* If we were shifting up, we're done. */
  151. buf->unused_bits -= bits;
  152. return;
  153. }
  154. /* The number of bits left to write is the number that didn't
  155. * fit in this first byte. */
  156. bits = -shift;
  157. }
  158. /* Write any whole bytes */
  159. while (bits >= 8)
  160. {
  161. bits -= 8;
  162. buf->data[buf->len++] = val>>bits;
  163. }
  164. /* Write trailing bits (with 0's in unused bits) */
  165. if (bits > 0)
  166. {
  167. bits = 8-bits;
  168. buf->data[buf->len++] = val<<bits;
  169. }
  170. buf->unused_bits = bits;
  171. }
  172. void fz_write_buffer_pad(fz_context *ctx, fz_buffer *buf)
  173. {
  174. buf->unused_bits = 0;
  175. }
  176. int
  177. fz_buffer_printf(fz_context *ctx, fz_buffer *buffer, const char *fmt, ...)
  178. {
  179. int ret;
  180. va_list args;
  181. va_start(args, fmt);
  182. ret = fz_buffer_vprintf(ctx, buffer, fmt, args);
  183. va_end(args);
  184. return ret;
  185. }
  186. int
  187. fz_buffer_vprintf(fz_context *ctx, fz_buffer *buffer, const char *fmt, va_list old_args)
  188. {
  189. int len;
  190. do
  191. {
  192. int slack = buffer->cap - buffer->len;
  193. if (slack > 0)
  194. {
  195. va_list args;
  196. #ifdef _MSC_VER /* Microsoft Visual C */
  197. args = old_args;
  198. #else
  199. va_copy(args, old_args);
  200. #endif
  201. len = vsnprintf((char *)buffer->data + buffer->len, slack, fmt, args);
  202. #ifndef _MSC_VER
  203. va_end(args);
  204. #endif
  205. /* len = number of chars written, not including the terminating
  206. * NULL, so len+1 > slack means "truncated". MSVC differs here
  207. * and returns -1 for truncated. */
  208. if (len >= 0 && len+1 <= slack)
  209. break;
  210. }
  211. /* Grow the buffer and retry */
  212. fz_grow_buffer(ctx, buffer);
  213. }
  214. while (1);
  215. buffer->len += len;
  216. return len;
  217. }
  218. void
  219. fz_buffer_cat_pdf_string(fz_context *ctx, fz_buffer *buffer, const char *text)
  220. {
  221. int len = 2;
  222. const char *s = text;
  223. char *d;
  224. char c;
  225. while ((c = *s++) != 0)
  226. {
  227. switch (c)
  228. {
  229. case '\n':
  230. case '\r':
  231. case '\t':
  232. case '\b':
  233. case '\f':
  234. case '(':
  235. case ')':
  236. case '\\':
  237. len++;
  238. break;
  239. }
  240. len++;
  241. }
  242. while(buffer->cap - buffer->len < len)
  243. fz_grow_buffer(ctx, buffer);
  244. s = text;
  245. d = (char *)buffer->data + buffer->len;
  246. *d++ = '(';
  247. while ((c = *s++) != 0)
  248. {
  249. switch (c)
  250. {
  251. case '\n':
  252. *d++ = '\\';
  253. *d++ = 'n';
  254. break;
  255. case '\r':
  256. *d++ = '\\';
  257. *d++ = 'r';
  258. break;
  259. case '\t':
  260. *d++ = '\\';
  261. *d++ = 't';
  262. break;
  263. case '\b':
  264. *d++ = '\\';
  265. *d++ = 'b';
  266. break;
  267. case '\f':
  268. *d++ = '\\';
  269. *d++ = 'f';
  270. break;
  271. case '(':
  272. *d++ = '\\';
  273. *d++ = '(';
  274. break;
  275. case ')':
  276. *d++ = '\\';
  277. *d++ = ')';
  278. break;
  279. case '\\':
  280. *d++ = '\\';
  281. *d++ = '\\';
  282. break;
  283. default:
  284. *d++ = c;
  285. }
  286. }
  287. *d++ = ')';
  288. buffer->len += len;
  289. }
  290. #ifdef TEST_BUFFER_WRITE
  291. #define TEST_LEN 1024
  292. void
  293. fz_test_buffer_write(fz_context *ctx)
  294. {
  295. fz_buffer *master = fz_new_buffer(ctx, TEST_LEN);
  296. fz_buffer *copy = fz_new_buffer(ctx, TEST_LEN);
  297. fz_stream *stm;
  298. int i, j, k;
  299. /* Make us a dummy buffer */
  300. for (i = 0; i < TEST_LEN; i++)
  301. {
  302. master->data[i] = rand();
  303. }
  304. master->len = TEST_LEN;
  305. /* Now copy that buffer several times, checking it for validity */
  306. stm = fz_open_buffer(ctx, master);
  307. for (i = 0; i < 256; i++)
  308. {
  309. memset(copy->data, i, TEST_LEN);
  310. copy->len = 0;
  311. j = TEST_LEN * 8;
  312. do
  313. {
  314. k = (rand() & 31)+1;
  315. if (k > j)
  316. k = j;
  317. fz_write_buffer_bits(ctx, copy, fz_read_bits(stm, k), k);
  318. j -= k;
  319. }
  320. while (j);
  321. if (memcmp(copy->data, master->data, TEST_LEN) != 0)
  322. fprintf(stderr, "Copied buffer is different!\n");
  323. fz_seek(stm, 0, 0);
  324. }
  325. fz_close(stm);
  326. fz_drop_buffer(ctx, master);
  327. fz_drop_buffer(ctx, copy);
  328. }
  329. #endif