jbig2_priv.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef _JBIG2_PRIV_H
  16. #define _JBIG2_PRIV_H
  17. /* To enable Memento predefine MEMENTO while building by setting
  18. CFLAGS=-DMEMENTO. */
  19. /* If we are being compiled as part of a larger project that includes
  20. * Memento, that project should define JBIG_EXTERNAL_MEMENTO_H to point
  21. * to the include file to use.
  22. */
  23. #ifdef JBIG_EXTERNAL_MEMENTO_H
  24. #include JBIG_EXTERNAL_MEMENTO_H
  25. #else
  26. #include "memento.h"
  27. #endif
  28. /* library internals */
  29. /* If we don't have a definition for inline, make it nothing so the code will compile */
  30. #ifndef inline
  31. #define inline
  32. #endif
  33. typedef uint8_t byte;
  34. #define bool int
  35. #ifdef __cplusplus
  36. #define template template_C
  37. #define new new_C
  38. #endif
  39. #ifndef TRUE
  40. #define TRUE 1
  41. #endif
  42. #ifndef FALSE
  43. #define FALSE 0
  44. #endif
  45. #ifndef NULL
  46. #define NULL ((void*)0)
  47. #endif
  48. #if !defined (INT32_MIN)
  49. #define INT32_MIN (-0x7fffffff - 1)
  50. #endif
  51. #if !defined (INT32_MAX)
  52. #define INT32_MAX 0x7fffffff
  53. #endif
  54. #if !defined (UINT32_MAX)
  55. #define UINT32_MAX 0xffffffffu
  56. #endif
  57. typedef struct _Jbig2Page Jbig2Page;
  58. typedef struct _Jbig2Segment Jbig2Segment;
  59. typedef enum {
  60. JBIG2_FILE_HEADER,
  61. JBIG2_FILE_SEQUENTIAL_HEADER,
  62. JBIG2_FILE_SEQUENTIAL_BODY,
  63. JBIG2_FILE_RANDOM_HEADERS,
  64. JBIG2_FILE_RANDOM_BODIES,
  65. JBIG2_FILE_EOF
  66. } Jbig2FileState;
  67. struct _Jbig2Ctx {
  68. Jbig2Allocator *allocator;
  69. Jbig2Options options;
  70. const Jbig2Ctx *global_ctx;
  71. Jbig2ErrorCallback error_callback;
  72. void *error_callback_data;
  73. byte *buf;
  74. size_t buf_size;
  75. size_t buf_rd_ix;
  76. size_t buf_wr_ix;
  77. Jbig2FileState state;
  78. uint8_t file_header_flags;
  79. uint32_t n_pages;
  80. uint32_t n_segments_max;
  81. Jbig2Segment **segments;
  82. uint32_t n_segments; /* index of last segment header parsed */
  83. uint32_t segment_index; /* index of last segment body parsed */
  84. /* list of decoded pages, including the one in progress,
  85. currently stored as a contiguous, 0-indexed array. */
  86. uint32_t current_page;
  87. uint32_t max_page_index;
  88. Jbig2Page *pages;
  89. };
  90. uint32_t jbig2_get_uint32(const byte *bptr);
  91. int32_t jbig2_get_int32(const byte *buf);
  92. uint16_t jbig2_get_uint16(const byte *bptr);
  93. int16_t jbig2_get_int16(const byte *buf);
  94. /* dynamic memory management */
  95. void *jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num);
  96. void jbig2_free(Jbig2Allocator *allocator, void *p);
  97. void *jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num);
  98. #define jbig2_new(ctx, t, size) ((t *)jbig2_alloc(ctx->allocator, size, sizeof(t)))
  99. #define jbig2_renew(ctx, p, t, size) ((t *)jbig2_realloc(ctx->allocator, (p), size, sizeof(t)))
  100. int jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, uint32_t seg_idx, const char *fmt, ...)
  101. #ifdef __GNUC__
  102. __attribute__ ((format (__printf__, 4, 5)))
  103. #endif
  104. ;
  105. /* The word stream design is a compromise between simplicity and
  106. trying to amortize the number of method calls. Each ::get_next_word
  107. invocation pulls 4 bytes from the stream, packed big-endian into a
  108. 32 bit word. The offset argument is provided as a convenience. It
  109. begins at 0 and increments by 4 for each successive invocation. */
  110. typedef struct _Jbig2WordStream Jbig2WordStream;
  111. struct _Jbig2WordStream {
  112. int (*get_next_word)(Jbig2Ctx *ctx, Jbig2WordStream *self, size_t offset, uint32_t *word);
  113. };
  114. Jbig2WordStream *jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);
  115. void jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws);
  116. /* restrict is standard in C99, but not in all C++ compilers. */
  117. #if defined (__STDC_VERSION_) && (__STDC_VERSION__ >= 199901L) /* C99 */
  118. #define JBIG2_RESTRICT restrict
  119. #elif defined(_MSC_VER) && (_MSC_VER >= 1600) /* MSVC 10 or newer */
  120. #define JBIG2_RESTRICT __restrict
  121. #elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC 3 or newer */
  122. #define JBIG2_RESTRICT __restrict
  123. #else /* Unknown or ancient */
  124. #define JBIG2_RESTRICT
  125. #endif
  126. #endif /* _JBIG2_PRIV_H */