| 1 |
/* |
| 2 |
* djvuload.c - functions to load from single-page DjVuBitonal files |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
static uint32 read_uint32_most_significant_byte_first(FILE *f) |
| 11 |
{ |
| 12 |
uint32 r = fgetc(f) << 24; |
| 13 |
r |= fgetc(f) << 16; |
| 14 |
r |= fgetc(f) << 8; |
| 15 |
r |= fgetc(f); |
| 16 |
return r; |
| 17 |
} |
| 18 |
|
| 19 |
typedef struct IFFChunk |
| 20 |
{ |
| 21 |
uint32 id; |
| 22 |
uint32 length; |
| 23 |
uint32 skipped; |
| 24 |
struct IFFChunk *parent; |
| 25 |
} IFFChunk; |
| 26 |
|
| 27 |
static void skip_in_chunk(IFFChunk *chunk, unsigned len) |
| 28 |
{ |
| 29 |
while (chunk) |
| 30 |
{ |
| 31 |
chunk->skipped += len; |
| 32 |
chunk = chunk->parent; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
static void skip_to_next_sibling_chunk(FILE *file, IFFChunk *chunk) |
| 37 |
{ |
| 38 |
skip_in_chunk(chunk->parent, chunk->length + 8); |
| 39 |
fseek(file, (chunk->length + 1) & ~1, SEEK_CUR); |
| 40 |
chunk->id = read_uint32_most_significant_byte_first(file); |
| 41 |
chunk->length = read_uint32_most_significant_byte_first(file); |
| 42 |
chunk->skipped = 0; |
| 43 |
} |
| 44 |
|
| 45 |
static void get_child_chunk(FILE *file, IFFChunk *chunk, IFFChunk *parent) |
| 46 |
{ |
| 47 |
chunk->id = read_uint32_most_significant_byte_first(file); |
| 48 |
chunk->length = read_uint32_most_significant_byte_first(file); |
| 49 |
chunk->skipped = 0; |
| 50 |
chunk->parent = parent; |
| 51 |
skip_in_chunk(parent, 8); |
| 52 |
} |
| 53 |
|
| 54 |
static int find_sibling_chunk(FILE *file, IFFChunk *chunk, uint32 id) |
| 55 |
{ |
| 56 |
while (chunk->id != id) |
| 57 |
{ |
| 58 |
if (chunk->parent && chunk->parent->skipped >= chunk->parent->length) |
| 59 |
return 0; |
| 60 |
skip_to_next_sibling_chunk(file, chunk); |
| 61 |
} |
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
#define CHUNK_ID_AT_AND_T 0x41542654 |
| 66 |
#define CHUNK_ID_FORM 0x464F524D |
| 67 |
#define ID_DJVU 0x444A5655 |
| 68 |
#define CHUNK_ID_Sjbz 0x536A627A |
| 69 |
|
| 70 |
MDJVU_IMPLEMENT int mdjvu_locate_jb2_chunk(mdjvu_file_t file, int32 *plength, mdjvu_error_t *perr) |
| 71 |
{ |
| 72 |
IFFChunk FORM, Sjbz; |
| 73 |
FILE *f = (FILE *) file; |
| 74 |
uint32 i = read_uint32_most_significant_byte_first(f); |
| 75 |
if (perr) *perr = NULL; |
| 76 |
if (i != CHUNK_ID_AT_AND_T) |
| 77 |
{ |
| 78 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_corrupted_djvu); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
get_child_chunk(f, &FORM, NULL); |
| 83 |
if (!find_sibling_chunk(f, &FORM, CHUNK_ID_FORM)) |
| 84 |
{ |
| 85 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_corrupted_djvu); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
if (read_uint32_most_significant_byte_first(f) != ID_DJVU) |
| 90 |
{ |
| 91 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_wrong_djvu_type); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
skip_in_chunk(&FORM, 4); |
| 96 |
|
| 97 |
get_child_chunk(f, &Sjbz, &FORM); |
| 98 |
if (!find_sibling_chunk(f, &Sjbz, CHUNK_ID_Sjbz)) |
| 99 |
{ |
| 100 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_djvu_no_Sjbz); |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
*plength = (int32) Sjbz.length; |
| 105 |
|
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
MDJVU_IMPLEMENT mdjvu_image_t mdjvu_file_load_djvu_page(mdjvu_file_t file, mdjvu_error_t *perr) |
| 110 |
{ |
| 111 |
int32 length; |
| 112 |
if (!mdjvu_locate_jb2_chunk(file, &length, perr)) |
| 113 |
return NULL; |
| 114 |
return mdjvu_file_load_jb2(file, length, perr); |
| 115 |
} |
| 116 |
|
| 117 |
MDJVU_IMPLEMENT mdjvu_image_t mdjvu_load_djvu_page(const char *path, mdjvu_error_t *perr) |
| 118 |
{ |
| 119 |
mdjvu_image_t result; |
| 120 |
FILE *f = fopen(path, "rb"); |
| 121 |
if (perr) *perr = NULL; |
| 122 |
if (!f) |
| 123 |
{ |
| 124 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_fopen_read); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
result = mdjvu_file_load_djvu_page((mdjvu_file_t) f, perr); |
| 128 |
fclose(f); |
| 129 |
return result; |
| 130 |
} |
| 131 |
|