| 1 | /* Copyright (C) 2001-2019 Artifex Software, Inc. |
| 2 | All Rights Reserved. |
| 3 | |
| 4 | This software is provided AS-IS with no warranty, either express or |
| 5 | implied. |
| 6 | |
| 7 | This software is distributed under license and may not be copied, |
| 8 | modified or distributed except as expressly authorized under the terms |
| 9 | of the license contained in the file LICENSE in this distribution. |
| 10 | |
| 11 | Refer to licensing information at http://www.artifex.com or contact |
| 12 | Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato, |
| 13 | CA 94945, U.S.A., +1(415)492-9861, for further information. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | jbig2dec |
| 18 | */ |
| 19 | |
| 20 | #ifndef _JBIG2_PRIV_H |
| 21 | #define _JBIG2_PRIV_H |
| 22 | |
| 23 | /* To enable Memento, either uncomment the following, or arrange to |
| 24 | * predefine MEMENTO whilst building. */ |
| 25 | /* #define MEMENTO */ |
| 26 | |
| 27 | /* If we are being compiled as part of a larger project that includes |
| 28 | * Memento, that project should define JBIG_EXTERNAL_MEMENTO_H to point |
| 29 | * to the include file to use. |
| 30 | */ |
| 31 | #ifdef JBIG_EXTERNAL_MEMENTO_H |
| 32 | #include JBIG_EXTERNAL_MEMENTO_H |
| 33 | #else |
| 34 | #include "memento.h" |
| 35 | #endif |
| 36 | |
| 37 | /* library internals */ |
| 38 | |
| 39 | typedef uint8_t byte; |
| 40 | |
| 41 | #define bool int |
| 42 | |
| 43 | #ifdef __cplusplus |
| 44 | #define template template_C |
| 45 | #define new new_C |
| 46 | #endif |
| 47 | |
| 48 | #ifndef TRUE |
| 49 | #define TRUE 1 |
| 50 | #endif |
| 51 | #ifndef FALSE |
| 52 | #define FALSE 0 |
| 53 | #endif |
| 54 | |
| 55 | #ifndef NULL |
| 56 | #define NULL ((void*)0) |
| 57 | #endif |
| 58 | |
| 59 | typedef struct _Jbig2Page Jbig2Page; |
| 60 | typedef struct _Jbig2Segment Jbig2Segment; |
| 61 | |
| 62 | typedef enum { |
| 63 | , |
| 64 | , |
| 65 | JBIG2_FILE_SEQUENTIAL_BODY, |
| 66 | JBIG2_FILE_RANDOM_HEADERS, |
| 67 | JBIG2_FILE_RANDOM_BODIES, |
| 68 | JBIG2_FILE_EOF |
| 69 | } Jbig2FileState; |
| 70 | |
| 71 | struct _Jbig2Ctx { |
| 72 | Jbig2Allocator *allocator; |
| 73 | Jbig2Options options; |
| 74 | const Jbig2Ctx *global_ctx; |
| 75 | Jbig2ErrorCallback error_callback; |
| 76 | void *error_callback_data; |
| 77 | |
| 78 | byte *buf; |
| 79 | size_t buf_size; |
| 80 | unsigned int buf_rd_ix; |
| 81 | unsigned int buf_wr_ix; |
| 82 | |
| 83 | Jbig2FileState state; |
| 84 | |
| 85 | uint8_t ; |
| 86 | uint32_t n_pages; |
| 87 | |
| 88 | int n_segments_max; |
| 89 | Jbig2Segment **segments; |
| 90 | int n_segments; /* index of last segment header parsed */ |
| 91 | int segment_index; /* index of last segment body parsed */ |
| 92 | |
| 93 | /* list of decoded pages, including the one in progress, |
| 94 | currently stored as a contiguous, 0-indexed array. */ |
| 95 | int current_page; |
| 96 | int max_page_index; |
| 97 | Jbig2Page *pages; |
| 98 | }; |
| 99 | |
| 100 | uint32_t jbig2_get_uint32(const byte *bptr); |
| 101 | |
| 102 | int32_t jbig2_get_int32(const byte *buf); |
| 103 | |
| 104 | uint16_t jbig2_get_uint16(const byte *bptr); |
| 105 | |
| 106 | int16_t jbig2_get_int16(const byte *buf); |
| 107 | |
| 108 | /* dynamic memory management */ |
| 109 | void *jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num); |
| 110 | |
| 111 | void jbig2_free(Jbig2Allocator *allocator, void *p); |
| 112 | |
| 113 | void *jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num); |
| 114 | |
| 115 | #define jbig2_new(ctx, t, size) ((t *)jbig2_alloc(ctx->allocator, size, sizeof(t))) |
| 116 | |
| 117 | #define jbig2_renew(ctx, p, t, size) ((t *)jbig2_realloc(ctx->allocator, (p), size, sizeof(t))) |
| 118 | |
| 119 | int jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t seg_idx, const char *fmt, ...); |
| 120 | |
| 121 | /* The word stream design is a compromise between simplicity and |
| 122 | trying to amortize the number of method calls. Each ::get_next_word |
| 123 | invocation pulls 4 bytes from the stream, packed big-endian into a |
| 124 | 32 bit word. The offset argument is provided as a convenience. It |
| 125 | begins at 0 and increments by 4 for each successive invocation. */ |
| 126 | typedef struct _Jbig2WordStream Jbig2WordStream; |
| 127 | |
| 128 | struct _Jbig2WordStream { |
| 129 | int (*get_next_word)(Jbig2WordStream *self, size_t offset, uint32_t *word); |
| 130 | }; |
| 131 | |
| 132 | Jbig2WordStream *jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size); |
| 133 | |
| 134 | void jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws); |
| 135 | |
| 136 | #endif /* _JBIG2_PRIV_H */ |
| 137 | |