Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2003 Artifex Software, Inc.
  5.  
  6.     This software is distributed under license and may not
  7.     be copied, modified or distributed except as expressly
  8.     authorized under the terms of the license contained in
  9.     the file LICENSE in this distribution.
  10.  
  11.     For further licensing information refer to http://artifex.com/ or
  12.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  13.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  14. */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "os_types.h"
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #include "jbig2.h"
  25. #include "jbig2_priv.h"
  26. #include "jbig2_metadata.h"
  27.  
  28. /* metadata key,value list object */
  29. Jbig2Metadata *jbig2_metadata_new(Jbig2Ctx *ctx, Jbig2Encoding encoding)
  30. {
  31.     Jbig2Metadata *md = jbig2_alloc(ctx->allocator, sizeof(Jbig2Metadata));
  32.  
  33.     if (md != NULL) {
  34.         md->encoding = encoding;
  35.         md->entries = 0;
  36.         md->max_entries = 4;
  37.         md->keys = jbig2_alloc(ctx->allocator, md->max_entries*sizeof(char*));
  38.         md->values = jbig2_alloc(ctx->allocator, md->max_entries*sizeof(char*));
  39.         if (md->keys == NULL || md->values == NULL) {
  40.             jbig2_metadata_free(ctx, md);
  41.             md = NULL;
  42.         }
  43.     }
  44.     return md;
  45. }
  46.  
  47. void jbig2_metadata_free(Jbig2Ctx *ctx, Jbig2Metadata *md)
  48. {
  49.     int i;
  50.  
  51.     if (md->keys) {
  52.       /* assume we own the pointers */
  53.       for (i = 0; i < md->entries; i++)
  54.         jbig2_free(ctx->allocator, md->keys[i]);
  55.       jbig2_free(ctx->allocator, md->keys);
  56.     }
  57.     if (md->values) {
  58.       for (i = 0; i < md->entries; i++)
  59.         jbig2_free(ctx->allocator, md->values[i]);
  60.       jbig2_free(ctx->allocator, md->values);
  61.     }
  62.     jbig2_free(ctx->allocator, md);
  63. }
  64.  
  65. static char *jbig2_strndup(Jbig2Ctx *ctx, const char *c, const int len)
  66. {
  67.     char *s = jbig2_alloc(ctx->allocator, len*sizeof(char));
  68.     if (s == NULL) {
  69.         jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
  70.             "unable to duplicate comment string");
  71.     } else {
  72.         memcpy(s, c, len);
  73.     }
  74.     return s;
  75. }
  76.  
  77. int jbig2_metadata_add(Jbig2Ctx *ctx, Jbig2Metadata *md,
  78.                         const char *key, const int key_length,
  79.                         const char *value, const int value_length)
  80. {
  81.     char **keys, **values;
  82.  
  83.     /* grow the array if necessary */
  84.     if (md->entries == md->max_entries) {
  85.         md->max_entries >>= 2;
  86.         keys = jbig2_realloc(ctx->allocator, md->keys, md->max_entries);
  87.         values = jbig2_realloc(ctx->allocator, md->values, md->max_entries);
  88.         if (keys == NULL || values == NULL) {
  89.             jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
  90.                 "unable to resize metadata structure");
  91.             return -1;
  92.         }
  93.         md->keys = keys;
  94.         md->values = values;
  95.     }
  96.  
  97.     /* copy the passed key,value pair */
  98.     md->keys[md->entries] = jbig2_strndup(ctx, key, key_length);
  99.     md->values[md->entries] = jbig2_strndup(ctx, value, value_length);
  100.     md->entries++;
  101.  
  102.     return 0;
  103. }
  104.  
  105.  
  106. /* decode an ascii comment segment 7.4.15.1 */
  107. int jbig2_comment_ascii(Jbig2Ctx *ctx, Jbig2Segment *segment,
  108.                                const uint8_t *segment_data)
  109. {
  110.     char *s = (char *)(segment_data + 4);
  111.     char *end = (char *)(segment_data + segment->data_length);
  112.     Jbig2Metadata *comment;
  113.     char *key, *value;
  114.     int key_length, value_length;
  115.  
  116.     jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  117.         "ASCII comment data");
  118.  
  119.     comment = jbig2_metadata_new(ctx, JBIG2_ENCODING_ASCII);
  120.     if (comment == NULL) {
  121.         jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  122.             "unable to allocate comment structure");
  123.         return -1;
  124.     }
  125.     /* loop over the segment data pulling out the key,value pairs */
  126.     while(*s && s < end) {
  127.         key_length = strlen(s) + 1;
  128.         key = s; s += key_length;
  129.         if (s >= end) goto too_short;
  130.         value_length = strlen(s) + 1;
  131.         value = s; s += value_length;
  132.         if (s >= end) goto too_short;
  133.         jbig2_metadata_add(ctx, comment, key, key_length, value, value_length);
  134.         jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  135.             "'%s'\t'%s'", key, value);
  136.     }
  137.  
  138.     /* TODO: associate with ctx, page, or referred-to segment(s) */
  139.     segment->result = comment;
  140.  
  141.     return 0;
  142.  
  143. too_short:
  144.     jbig2_metadata_free(ctx, comment);
  145.     return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  146.         "unexpected end of comment segment");
  147. }
  148.  
  149. /* decode a UCS-16 comment segement 7.4.15.2 */
  150. int jbig2_comment_unicode(Jbig2Ctx *ctx, Jbig2Segment *segment,
  151.                                const uint8_t *segment_data)
  152. {
  153.     return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  154.         "unhandled unicode comment segment");
  155. }
  156.