Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2002-2005 Artifex Software, Inc.
  5.  
  6.     This software is provided AS-IS with no warranty,
  7.     either express or implied.
  8.  
  9.     This software is distributed under license and may not
  10.     be copied, modified or distributed except as expressly
  11.     authorized under the terms of the license contained in
  12.     the file LICENSE in this distribution.
  13.  
  14.     For further licensing information refer to http://artifex.com/ or
  15.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  16.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  17. */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "os_types.h"
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28.  
  29. #include "jbig2.h"
  30. #include "jbig2_priv.h"
  31.  
  32. static void *
  33. jbig2_default_alloc (Jbig2Allocator *allocator, size_t size)
  34. {
  35.   return malloc (size);
  36. }
  37.  
  38. static void
  39. jbig2_default_free (Jbig2Allocator *allocator, void *p)
  40. {
  41.   free (p);
  42. }
  43.  
  44. static void *
  45. jbig2_default_realloc (Jbig2Allocator *allocator, void *p, size_t size)
  46. {
  47.   return realloc (p, size);
  48. }
  49.  
  50. static Jbig2Allocator jbig2_default_allocator =
  51. {
  52.   jbig2_default_alloc,
  53.   jbig2_default_free,
  54.   jbig2_default_realloc
  55. };
  56.  
  57. void *
  58. jbig2_alloc (Jbig2Allocator *allocator, size_t size)
  59. {
  60.   return allocator->alloc (allocator, size);
  61. }
  62.  
  63. void
  64. jbig2_free (Jbig2Allocator *allocator, void *p)
  65. {
  66.   allocator->free (allocator, p);
  67. }
  68.  
  69. void *
  70. jbig2_realloc (Jbig2Allocator *allocator, void *p, size_t size)
  71. {
  72.   return allocator->realloc (allocator, p, size);
  73. }
  74.  
  75. static int
  76. jbig2_default_error(void *data, const char *msg,
  77.                     Jbig2Severity severity, int32_t seg_idx)
  78. {
  79.     /* report only fatal errors by default */
  80.     if (severity == JBIG2_SEVERITY_FATAL) {
  81.         fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
  82.         if (seg_idx != -1) fprintf(stderr, " (segment 0x%02x)", seg_idx);
  83.         fprintf(stderr, "\n");
  84.         fflush(stderr);
  85.     }
  86.  
  87.     return 0;
  88. }
  89.  
  90. int
  91. jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t segment_number,
  92.              const char *fmt, ...)
  93. {
  94.   char buf[1024];
  95.   va_list ap;
  96.   int n;
  97.   int code;
  98.  
  99.   va_start (ap, fmt);
  100.   n = vsnprintf (buf, sizeof(buf), fmt, ap);
  101.   va_end (ap);
  102.   if (n < 0 || n == sizeof(buf))
  103.     strncpy (buf, "jbig2_error: error in generating error string", sizeof(buf));
  104.   code = ctx->error_callback (ctx->error_callback_data, buf, severity, segment_number);
  105.   if (severity == JBIG2_SEVERITY_FATAL)
  106.     code = -1;
  107.   return code;
  108. }
  109.  
  110. Jbig2Ctx *
  111. jbig2_ctx_new (Jbig2Allocator *allocator,
  112.                Jbig2Options options,
  113.                Jbig2GlobalCtx *global_ctx,
  114.                Jbig2ErrorCallback error_callback,
  115.                void *error_callback_data)
  116. {
  117.   Jbig2Ctx *result;
  118.  
  119.   if (allocator == NULL)
  120.       allocator = &jbig2_default_allocator;
  121.   if (error_callback == NULL)
  122.       error_callback = &jbig2_default_error;
  123.  
  124.   result = (Jbig2Ctx *)jbig2_alloc(allocator, sizeof(Jbig2Ctx));
  125.   if (result == NULL) {
  126.     error_callback(error_callback_data, "initial context allocation failed!",
  127.                     JBIG2_SEVERITY_FATAL, -1);
  128.     return result;
  129.   }
  130.  
  131.   result->allocator = allocator;
  132.   result->options = options;
  133.   result->global_ctx = (const Jbig2Ctx *)global_ctx;
  134.   result->error_callback = error_callback;
  135.   result->error_callback_data = error_callback_data;
  136.  
  137.   result->state = (options & JBIG2_OPTIONS_EMBEDDED) ?
  138.     JBIG2_FILE_SEQUENTIAL_HEADER :
  139.     JBIG2_FILE_HEADER;
  140.  
  141.   result->buf = NULL;
  142.  
  143.   result->n_segments = 0;
  144.   result->n_segments_max = 16;
  145.   result->segments = (Jbig2Segment **)jbig2_alloc(allocator, result->n_segments_max * sizeof(Jbig2Segment *));
  146.   result->segment_index = 0;
  147.  
  148.   result->current_page = 0;
  149.   result->max_page_index = 4;
  150.   result->pages = (Jbig2Page *)jbig2_alloc(allocator, result->max_page_index * sizeof(Jbig2Page));
  151.   {
  152.     int index;
  153.     for (index = 0; index < result->max_page_index; index++) {
  154.         result->pages[index].state = JBIG2_PAGE_FREE;
  155.         result->pages[index].number = 0;
  156.         result->pages[index].image = NULL;
  157.     }
  158.   }
  159.  
  160.   return result;
  161. }
  162.  
  163. int32_t
  164. jbig2_get_int32 (const byte *buf)
  165. {
  166.   return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  167. }
  168.  
  169. int16_t
  170. jbig2_get_int16 (const byte *buf)
  171. {
  172.   return (buf[0] << 8) | buf[1];
  173. }
  174.  
  175.  
  176. /**
  177.  * jbig2_data_in: submit data for decoding
  178.  * @ctx: The jbig2dec decoder context
  179.  * @data: a pointer to the data buffer
  180.  * @size: the size of the data buffer in bytes
  181.  *
  182.  * Copies the specified data into internal storage and attempts
  183.  * to (continue to) parse it as part of a jbig2 data stream.
  184.  *
  185.  * Return code: 0 on success
  186.  *             -1 if there is a parsing error, or whatever
  187.  *                the error handling callback returns
  188.  **/
  189. int
  190. jbig2_data_in (Jbig2Ctx *ctx, const unsigned char *data, size_t size)
  191. {
  192.   const size_t initial_buf_size = 1024;
  193.  
  194.   if (ctx->buf == NULL)
  195.     {
  196.       size_t buf_size = initial_buf_size;
  197.  
  198.       do
  199.         buf_size <<= 1;
  200.       while (buf_size < size);
  201.       ctx->buf = (byte *)jbig2_alloc(ctx->allocator, buf_size);
  202.       ctx->buf_size = buf_size;
  203.       ctx->buf_rd_ix = 0;
  204.       ctx->buf_wr_ix = 0;
  205.     }
  206.   else if (ctx->buf_wr_ix + size > ctx->buf_size)
  207.     {
  208.       if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) &&
  209.           ctx->buf_wr_ix - ctx->buf_rd_ix + size <= ctx->buf_size)
  210.         {
  211.           memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix,
  212.                   ctx->buf_wr_ix - ctx->buf_rd_ix);
  213.         }
  214.       else
  215.         {
  216.           byte *buf;
  217.           size_t buf_size = initial_buf_size;
  218.  
  219.           do
  220.             buf_size <<= 1;
  221.           while (buf_size < ctx->buf_wr_ix - ctx->buf_rd_ix + size);
  222.           buf = (byte *)jbig2_alloc(ctx->allocator, buf_size);
  223.           memcpy(buf, ctx->buf + ctx->buf_rd_ix,
  224.                   ctx->buf_wr_ix - ctx->buf_rd_ix);
  225.           jbig2_free(ctx->allocator, ctx->buf);
  226.           ctx->buf = buf;
  227.           ctx->buf_size = buf_size;
  228.         }
  229.       ctx->buf_wr_ix -= ctx->buf_rd_ix;
  230.       ctx->buf_rd_ix = 0;
  231.     }
  232.   memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
  233.   ctx->buf_wr_ix += size;
  234.  
  235.   /* data has now been added to buffer */
  236.  
  237.   for (;;)
  238.     {
  239.       const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
  240.       Jbig2Segment *segment;
  241.       size_t header_size;
  242.       int code;
  243.  
  244.       switch (ctx->state)
  245.         {
  246.         case JBIG2_FILE_HEADER:
  247.           /* D.4.1 */
  248.           if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
  249.             return 0;
  250.           if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
  251.             return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
  252.                                "Not a JBIG2 file header");
  253.           /* D.4.2 */
  254.           ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
  255.           if (ctx->file_header_flags & 0xFC) {
  256.             jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
  257.                 "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags);
  258.           }
  259.           /* D.4.3 */
  260.           if (!(ctx->file_header_flags & 2)) /* number of pages is known */
  261.             {
  262.               if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
  263.                 return 0;
  264.               ctx->n_pages = jbig2_get_int32(ctx->buf + ctx->buf_rd_ix + 9);
  265.               ctx->buf_rd_ix += 13;
  266.               if (ctx->n_pages == 1)
  267.                 jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a single page document");
  268.               else
  269.                 jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a %d page document", ctx->n_pages);
  270.             }
  271.           else /* number of pages not known */
  272.             {
  273.               ctx->n_pages=0;
  274.               ctx->buf_rd_ix += 9;
  275.             }
  276.           /* determine the file organization based on the flags - D.4.2 again */
  277.           if (ctx->file_header_flags & 1)
  278.             {
  279.               ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  280.               jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates sequential organization");
  281.             }
  282.           else
  283.             {
  284.               ctx->state = JBIG2_FILE_RANDOM_HEADERS;
  285.               jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates random-access organization");
  286.  
  287.             }
  288.           break;
  289.         case JBIG2_FILE_SEQUENTIAL_HEADER:
  290.         case JBIG2_FILE_RANDOM_HEADERS:
  291.           segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix,
  292.                                           ctx->buf_wr_ix - ctx->buf_rd_ix,
  293.                                           &header_size);
  294.           if (segment == NULL)
  295.             return 0; /* need more data */
  296.           ctx->buf_rd_ix += header_size;
  297.  
  298.           if (ctx->n_segments == ctx->n_segments_max)
  299.             ctx->segments = (Jbig2Segment **)jbig2_realloc(ctx->allocator,
  300.                 ctx->segments, (ctx->n_segments_max <<= 2) * sizeof(Jbig2Segment *));
  301.  
  302.           ctx->segments[ctx->n_segments++] = segment;
  303.           if (ctx->state == JBIG2_FILE_RANDOM_HEADERS)
  304.             {
  305.               if ((segment->flags & 63) == 51) /* end of file */
  306.                 ctx->state = JBIG2_FILE_RANDOM_BODIES;
  307.             }
  308.           else /* JBIG2_FILE_SEQUENTIAL_HEADER */
  309.             ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
  310.           break;
  311.         case JBIG2_FILE_SEQUENTIAL_BODY:
  312.         case JBIG2_FILE_RANDOM_BODIES:
  313.           segment = ctx->segments[ctx->segment_index];
  314.           if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
  315.             return 0; /* need more data */
  316.           code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
  317.           ctx->buf_rd_ix += segment->data_length;
  318.           ctx->segment_index++;
  319.           if (ctx->state == JBIG2_FILE_RANDOM_BODIES)
  320.             {
  321.               if (ctx->segment_index == ctx->n_segments)
  322.                 ctx->state = JBIG2_FILE_EOF;
  323.             }
  324.           else /* JBIG2_FILE_SEQUENCIAL_BODY */
  325.             {
  326.               ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
  327.             }
  328.           if (code < 0)
  329.             {
  330.               ctx->state = JBIG2_FILE_EOF;
  331.               return code;
  332.             }
  333.           break;
  334.         case JBIG2_FILE_EOF:
  335.           if (ctx->buf_rd_ix == ctx->buf_wr_ix)
  336.             return 0;
  337.           return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
  338.                       "Garbage beyond end of file");
  339.         }
  340.     }
  341.   return 0;
  342. }
  343.  
  344. void
  345. jbig2_ctx_free (Jbig2Ctx *ctx)
  346. {
  347.   Jbig2Allocator *ca = ctx->allocator;
  348.   int i;
  349.  
  350.   jbig2_free(ca, ctx->buf);
  351.   if (ctx->segments != NULL) {
  352.     for (i = 0; i < ctx->n_segments; i++)
  353.       jbig2_free_segment(ctx, ctx->segments[i]);
  354.     jbig2_free(ca, ctx->segments);
  355.   }
  356.  
  357.   if (ctx->pages != NULL) {
  358.     for (i = 0; i <= ctx->current_page; i++)
  359.       if (ctx->pages[i].image != NULL)
  360.         jbig2_image_release(ctx, ctx->pages[i].image);
  361.     jbig2_free(ca, ctx->pages);
  362.   }
  363.  
  364.   jbig2_free(ca, ctx);
  365. }
  366.  
  367. Jbig2GlobalCtx *jbig2_make_global_ctx (Jbig2Ctx *ctx)
  368. {
  369.   return (Jbig2GlobalCtx *)ctx;
  370. }
  371.  
  372. void jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx)
  373. {
  374.   jbig2_ctx_free((Jbig2Ctx *)global_ctx);
  375. }
  376.  
  377.  
  378. /* I'm not committed to keeping the word stream interface. It's handy
  379.    when you think you may be streaming your input, but if you're not
  380.    (as is currently the case), it just adds complexity.
  381. */
  382.  
  383. typedef struct {
  384.   Jbig2WordStream super;
  385.   const byte *data;
  386.   size_t size;
  387. } Jbig2WordStreamBuf;
  388.  
  389. static uint32_t
  390. jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset)
  391. {
  392.   Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *)self;
  393.   const byte *data = z->data;
  394.   uint32_t result;
  395.  
  396.   if (offset + 4 < z->size)
  397.     result = (data[offset] << 24) | (data[offset + 1] << 16) |
  398.       (data[offset + 2] << 8) | data[offset + 3];
  399.   else if (offset >= z->size)
  400.     return 0;
  401.   else
  402.     {
  403.       int i;
  404.  
  405.       result = 0;
  406.       for (i = 0; i < z->size - offset; i++)
  407.         result |= data[offset + i] << ((3 - i) << 3);
  408.     }
  409.   return result;
  410. }
  411.  
  412. Jbig2WordStream *
  413. jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size)
  414. {
  415.   Jbig2WordStreamBuf *result = (Jbig2WordStreamBuf *)jbig2_alloc(ctx->allocator, sizeof(Jbig2WordStreamBuf));
  416.  
  417.   result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
  418.   result->data = data;
  419.   result->size = size;
  420.  
  421.   return &result->super;
  422. }
  423.  
  424. void
  425. jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws)
  426. {
  427.   jbig2_free(ctx->allocator, ws);
  428. }
  429.