jbig2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /* Copyright (C) 2001-2023 Artifex Software, Inc.
  2. All Rights Reserved.
  3. This software is provided AS-IS with no warranty, either express or
  4. implied.
  5. This software is distributed under license and may not be copied,
  6. modified or distributed except as expressly authorized under the terms
  7. of the license contained in the file LICENSE in this distribution.
  8. Refer to licensing information at http://www.artifex.com or contact
  9. Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  10. CA 94129, USA, for further information.
  11. */
  12. /*
  13. jbig2dec
  14. */
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #include "os_types.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include <limits.h>
  24. #include "jbig2.h"
  25. #include "jbig2_priv.h"
  26. #include "jbig2_image.h"
  27. #include "jbig2_page.h"
  28. #include "jbig2_segment.h"
  29. static void *
  30. jbig2_default_alloc(Jbig2Allocator *allocator, size_t size)
  31. {
  32. return malloc(size);
  33. }
  34. static void
  35. jbig2_default_free(Jbig2Allocator *allocator, void *p)
  36. {
  37. free(p);
  38. }
  39. static void *
  40. jbig2_default_realloc(Jbig2Allocator *allocator, void *p, size_t size)
  41. {
  42. return realloc(p, size);
  43. }
  44. static Jbig2Allocator jbig2_default_allocator = {
  45. jbig2_default_alloc,
  46. jbig2_default_free,
  47. jbig2_default_realloc
  48. };
  49. void *
  50. jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num)
  51. {
  52. /* Check for integer multiplication overflow when computing
  53. the full size of the allocation. */
  54. if (num > 0 && size > SIZE_MAX / num)
  55. return NULL;
  56. return allocator->alloc(allocator, size * num);
  57. }
  58. /* jbig2_free and jbig2_realloc moved to the bottom of this file */
  59. static void
  60. jbig2_default_error(void *data, const char *msg, Jbig2Severity severity, uint32_t seg_idx)
  61. {
  62. /* report only fatal errors by default */
  63. if (severity == JBIG2_SEVERITY_FATAL) {
  64. fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
  65. if (seg_idx != JBIG2_UNKNOWN_SEGMENT_NUMBER)
  66. fprintf(stderr, " (segment 0x%02x)", seg_idx);
  67. fprintf(stderr, "\n");
  68. fflush(stderr);
  69. }
  70. }
  71. int
  72. jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, uint32_t segment_number, const char *fmt, ...)
  73. {
  74. char buf[1024];
  75. va_list ap;
  76. int n;
  77. va_start(ap, fmt);
  78. n = vsnprintf(buf, sizeof(buf), fmt, ap);
  79. va_end(ap);
  80. if (n < 0 || n == sizeof(buf))
  81. strncpy(buf, "failed to generate error string", sizeof(buf));
  82. ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number);
  83. return -1;
  84. }
  85. Jbig2Ctx *
  86. jbig2_ctx_new_imp(Jbig2Allocator *allocator, Jbig2Options options, Jbig2GlobalCtx *global_ctx, Jbig2ErrorCallback error_callback, void *error_callback_data, int jbig2_version_major, int jbig2_version_minor)
  87. {
  88. Jbig2Ctx *result;
  89. if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
  90. Jbig2Ctx fakectx;
  91. fakectx.error_callback = error_callback;
  92. fakectx.error_callback_data = error_callback_data;
  93. jbig2_error(&fakectx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "incompatible jbig2dec header (%d.%d) and library (%d.%d) versions",
  94. jbig2_version_major, jbig2_version_minor, JBIG2_VERSION_MAJOR, JBIG2_VERSION_MINOR);
  95. return NULL;
  96. }
  97. if (allocator == NULL)
  98. allocator = &jbig2_default_allocator;
  99. if (error_callback == NULL)
  100. error_callback = &jbig2_default_error;
  101. result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
  102. if (result == NULL) {
  103. error_callback(error_callback_data, "failed to allocate initial context", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
  104. return NULL;
  105. }
  106. result->allocator = allocator;
  107. result->options = options;
  108. result->global_ctx = (const Jbig2Ctx *)global_ctx;
  109. result->error_callback = error_callback;
  110. result->error_callback_data = error_callback_data;
  111. result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_SEQUENTIAL_HEADER : JBIG2_FILE_HEADER;
  112. result->buf = NULL;
  113. result->n_segments = 0;
  114. result->n_segments_max = 16;
  115. result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max);
  116. if (result->segments == NULL) {
  117. error_callback(error_callback_data, "failed to allocate initial segments", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
  118. jbig2_free(allocator, result);
  119. return NULL;
  120. }
  121. result->segment_index = 0;
  122. result->current_page = 0;
  123. result->max_page_index = 4;
  124. result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
  125. if (result->pages == NULL) {
  126. error_callback(error_callback_data, "failed to allocated initial pages", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
  127. jbig2_free(allocator, result->segments);
  128. jbig2_free(allocator, result);
  129. return NULL;
  130. }
  131. {
  132. uint32_t index;
  133. for (index = 0; index < result->max_page_index; index++) {
  134. result->pages[index].state = JBIG2_PAGE_FREE;
  135. result->pages[index].number = 0;
  136. result->pages[index].width = 0;
  137. result->pages[index].height = 0xffffffff;
  138. result->pages[index].x_resolution = 0;
  139. result->pages[index].y_resolution = 0;
  140. result->pages[index].stripe_size = 0;
  141. result->pages[index].striped = 0;
  142. result->pages[index].end_row = 0;
  143. result->pages[index].flags = 0;
  144. result->pages[index].image = NULL;
  145. }
  146. }
  147. return result;
  148. }
  149. #define get_uint16(bptr)\
  150. (((bptr)[0] << 8) | (bptr)[1])
  151. #define get_int16(bptr)\
  152. (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
  153. /* coverity[ -tainted_data_return ] */
  154. /* coverity[ -tainted_data_argument : arg-0 ] */
  155. int16_t
  156. jbig2_get_int16(const byte *bptr)
  157. {
  158. return get_int16(bptr);
  159. }
  160. /* coverity[ -tainted_data_return ] */
  161. /* coverity[ -tainted_data_argument : arg-0 ] */
  162. uint16_t
  163. jbig2_get_uint16(const byte *bptr)
  164. {
  165. return get_uint16(bptr);
  166. }
  167. /* coverity[ -tainted_data_return ] */
  168. /* coverity[ -tainted_data_argument : arg-0 ] */
  169. int32_t
  170. jbig2_get_int32(const byte *bptr)
  171. {
  172. return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
  173. }
  174. /* coverity[ -tainted_data_return ] */
  175. /* coverity[ -tainted_data_argument : arg-0 ] */
  176. uint32_t
  177. jbig2_get_uint32(const byte *bptr)
  178. {
  179. return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
  180. }
  181. static size_t
  182. jbig2_find_buffer_size(size_t desired)
  183. {
  184. const size_t initial_buf_size = 1024;
  185. size_t size = initial_buf_size;
  186. if (desired == SIZE_MAX)
  187. return SIZE_MAX;
  188. while (size < desired)
  189. size <<= 1;
  190. return size;
  191. }
  192. /**
  193. * jbig2_data_in: submit data for decoding
  194. * @ctx: The jbig2dec decoder context
  195. * @data: a pointer to the data buffer
  196. * @size: the size of the data buffer in bytes
  197. *
  198. * Copies the specified data into internal storage and attempts
  199. * to (continue to) parse it as part of a jbig2 data stream.
  200. *
  201. * Return code: 0 on success
  202. * -1 if there is a parsing error
  203. **/
  204. int
  205. jbig2_data_in(Jbig2Ctx *ctx, const unsigned char *data, size_t size)
  206. {
  207. if (ctx->buf == NULL) {
  208. size_t buf_size = jbig2_find_buffer_size(size);
  209. ctx->buf = jbig2_new(ctx, byte, buf_size);
  210. if (ctx->buf == NULL) {
  211. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate buffer when reading data");
  212. }
  213. ctx->buf_size = buf_size;
  214. ctx->buf_rd_ix = 0;
  215. ctx->buf_wr_ix = 0;
  216. } else if (size > ctx->buf_size - ctx->buf_wr_ix) {
  217. size_t already = ctx->buf_wr_ix - ctx->buf_rd_ix;
  218. if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) && size <= ctx->buf_size - already) {
  219. memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix, already);
  220. } else {
  221. byte *buf;
  222. size_t buf_size;
  223. if (already > SIZE_MAX - size) {
  224. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "read data causes buffer to grow too large");
  225. }
  226. buf_size = jbig2_find_buffer_size(size + already);
  227. buf = jbig2_new(ctx, byte, buf_size);
  228. if (buf == NULL) {
  229. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate bigger buffer when reading data");
  230. }
  231. memcpy(buf, ctx->buf + ctx->buf_rd_ix, already);
  232. jbig2_free(ctx->allocator, ctx->buf);
  233. ctx->buf = buf;
  234. ctx->buf_size = buf_size;
  235. }
  236. ctx->buf_wr_ix -= ctx->buf_rd_ix;
  237. ctx->buf_rd_ix = 0;
  238. }
  239. memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
  240. ctx->buf_wr_ix += size;
  241. /* data has now been added to buffer */
  242. for (;;) {
  243. const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
  244. Jbig2Segment *segment;
  245. size_t header_size;
  246. int code;
  247. switch (ctx->state) {
  248. case JBIG2_FILE_HEADER:
  249. /* D.4.1 */
  250. if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
  251. return 0;
  252. if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
  253. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "not a JBIG2 file header");
  254. /* D.4.2 */
  255. ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
  256. /* Check for T.88 amendment 2 */
  257. if (ctx->file_header_flags & 0x04)
  258. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates use of 12 adaptive template pixels (NYI)");
  259. /* Check for T.88 amendment 3 */
  260. if (ctx->file_header_flags & 0x08)
  261. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates use of colored region segments (NYI)");
  262. if (ctx->file_header_flags & 0xFC) {
  263. jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags);
  264. }
  265. /* D.4.3 */
  266. if (!(ctx->file_header_flags & 2)) { /* number of pages is known */
  267. if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
  268. return 0;
  269. ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
  270. ctx->buf_rd_ix += 13;
  271. if (ctx->n_pages == 1)
  272. jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a single page document");
  273. else
  274. jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a %d page document", ctx->n_pages);
  275. } else { /* number of pages not known */
  276. ctx->n_pages = 0;
  277. ctx->buf_rd_ix += 9;
  278. }
  279. /* determine the file organization based on the flags - D.4.2 again */
  280. if (ctx->file_header_flags & 1) {
  281. ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  282. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates sequential organization");
  283. } else {
  284. ctx->state = JBIG2_FILE_RANDOM_HEADERS;
  285. jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates random-access organization");
  286. }
  287. break;
  288. case JBIG2_FILE_SEQUENTIAL_HEADER:
  289. case JBIG2_FILE_RANDOM_HEADERS:
  290. segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size);
  291. if (segment == NULL)
  292. return 0; /* need more data */
  293. ctx->buf_rd_ix += header_size;
  294. if (ctx->n_segments >= ctx->n_segments_max) {
  295. Jbig2Segment **segments;
  296. if (ctx->n_segments_max == UINT32_MAX) {
  297. ctx->state = JBIG2_FILE_EOF;
  298. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many segments in jbig2 image");
  299. }
  300. else if (ctx->n_segments_max > (UINT32_MAX >> 2)) {
  301. ctx->n_segments_max = UINT32_MAX;
  302. }
  303. segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, (ctx->n_segments_max <<= 2));
  304. if (segments == NULL) {
  305. ctx->state = JBIG2_FILE_EOF;
  306. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate space for more segments");
  307. }
  308. ctx->segments = segments;
  309. }
  310. ctx->segments[ctx->n_segments++] = segment;
  311. if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) {
  312. if ((segment->flags & 63) == 51) /* end of file */
  313. ctx->state = JBIG2_FILE_RANDOM_BODIES;
  314. } else /* JBIG2_FILE_SEQUENTIAL_HEADER */
  315. ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
  316. break;
  317. case JBIG2_FILE_SEQUENTIAL_BODY:
  318. case JBIG2_FILE_RANDOM_BODIES:
  319. segment = ctx->segments[ctx->segment_index];
  320. /* immediate generic regions may have unknown size */
  321. if (segment->data_length == 0xffffffff && (segment->flags & 63) == 38) {
  322. byte *s, *e, *p;
  323. int mmr;
  324. byte mmr_marker[2] = { 0x00, 0x00 };
  325. byte arith_marker[2] = { 0xff, 0xac };
  326. byte *desired_marker;
  327. s = p = ctx->buf + ctx->buf_rd_ix;
  328. e = ctx->buf + ctx->buf_wr_ix;
  329. if (e - p < 18)
  330. return 0; /* need more data */
  331. mmr = p[17] & 1;
  332. p += 18;
  333. desired_marker = mmr ? mmr_marker : arith_marker;
  334. /* look for two byte marker */
  335. if (e - p < 2)
  336. return 0; /* need more data */
  337. while (p[0] != desired_marker[0] || p[1] != desired_marker[1]) {
  338. p++;
  339. if (e - p < 2)
  340. return 0; /* need more data */
  341. }
  342. p += 2;
  343. /* the marker is followed by a four byte row count */
  344. if (e - p < 4)
  345. return 0; /* need more data */
  346. segment->rows = jbig2_get_uint32(p);
  347. p += 4;
  348. segment->data_length = (size_t) (p - s);
  349. jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "unknown length determined to be %lu", (long) segment->data_length);
  350. }
  351. else if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
  352. return 0; /* need more data */
  353. code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  354. ctx->buf_rd_ix += segment->data_length;
  355. ctx->segment_index++;
  356. if (ctx->state == JBIG2_FILE_RANDOM_BODIES) {
  357. if (ctx->segment_index == ctx->n_segments)
  358. ctx->state = JBIG2_FILE_EOF;
  359. } else { /* JBIG2_FILE_SEQUENTIAL_BODY */
  360. ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  361. }
  362. if (code < 0) {
  363. ctx->state = JBIG2_FILE_EOF;
  364. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode; treating as end of file");
  365. }
  366. break;
  367. case JBIG2_FILE_EOF:
  368. if (ctx->buf_rd_ix == ctx->buf_wr_ix)
  369. return 0;
  370. return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "garbage beyond end of file");
  371. }
  372. }
  373. }
  374. Jbig2Allocator *
  375. jbig2_ctx_free(Jbig2Ctx *ctx)
  376. {
  377. Jbig2Allocator *ca;
  378. uint32_t i;
  379. if (ctx == NULL)
  380. return NULL;
  381. ca = ctx->allocator;
  382. jbig2_free(ca, ctx->buf);
  383. if (ctx->segments != NULL) {
  384. for (i = 0; i < ctx->n_segments; i++)
  385. jbig2_free_segment(ctx, ctx->segments[i]);
  386. jbig2_free(ca, ctx->segments);
  387. }
  388. if (ctx->pages != NULL) {
  389. for (i = 0; i <= ctx->current_page; i++)
  390. if (ctx->pages[i].image != NULL)
  391. jbig2_image_release(ctx, ctx->pages[i].image);
  392. jbig2_free(ca, ctx->pages);
  393. }
  394. jbig2_free(ca, ctx);
  395. return ca;
  396. }
  397. Jbig2GlobalCtx *
  398. jbig2_make_global_ctx(Jbig2Ctx *ctx)
  399. {
  400. return (Jbig2GlobalCtx *) ctx;
  401. }
  402. Jbig2Allocator *
  403. jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx)
  404. {
  405. return jbig2_ctx_free((Jbig2Ctx *) global_ctx);
  406. }
  407. /* I'm not committed to keeping the word stream interface. It's handy
  408. when you think you may be streaming your input, but if you're not
  409. (as is currently the case), it just adds complexity.
  410. */
  411. typedef struct {
  412. Jbig2WordStream super;
  413. const byte *data;
  414. size_t size;
  415. } Jbig2WordStreamBuf;
  416. static int
  417. jbig2_word_stream_buf_get_next_word(Jbig2Ctx *ctx, Jbig2WordStream *self, size_t offset, uint32_t *word)
  418. {
  419. Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
  420. uint32_t val = 0;
  421. int ret = 0;
  422. if (self == NULL || word == NULL) {
  423. return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read next word of stream because stream or output missing");
  424. }
  425. if (offset >= z->size) {
  426. *word = 0;
  427. return 0;
  428. }
  429. if (offset < z->size) {
  430. val = (uint32_t) z->data[offset] << 24;
  431. ret++;
  432. }
  433. if (offset + 1 < z->size) {
  434. val |= (uint32_t) z->data[offset + 1] << 16;
  435. ret++;
  436. }
  437. if (offset + 2 < z->size) {
  438. val |= (uint32_t) z->data[offset + 2] << 8;
  439. ret++;
  440. }
  441. if (offset + 3 < z->size) {
  442. val |= z->data[offset + 3];
  443. ret++;
  444. }
  445. *word = val;
  446. return ret;
  447. }
  448. Jbig2WordStream *
  449. jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size)
  450. {
  451. Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
  452. if (result == NULL) {
  453. jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate word stream");
  454. return NULL;
  455. }
  456. result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
  457. result->data = data;
  458. result->size = size;
  459. return &result->super;
  460. }
  461. void
  462. jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws)
  463. {
  464. jbig2_free(ctx->allocator, ws);
  465. }
  466. /* When Memento is in use, the ->free and ->realloc calls get
  467. * turned into ->Memento_free and ->Memento_realloc, which is
  468. * obviously problematic. Undefine free and realloc here to
  469. * avoid this. */
  470. #ifdef MEMENTO
  471. #undef free
  472. #undef realloc
  473. #endif
  474. void
  475. jbig2_free(Jbig2Allocator *allocator, void *p)
  476. {
  477. allocator->free(allocator, p);
  478. }
  479. void *
  480. jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num)
  481. {
  482. /* check for integer multiplication overflow */
  483. if (num > 0 && size >= SIZE_MAX / num)
  484. return NULL;
  485. return allocator->realloc(allocator, p, size * num);
  486. }