EBOOK-TOOLS
epublib.h
Go to the documentation of this file.
1 #ifndef EPUBLIB_H
2 #define EPUBLIB_H 1
3 // generally needed includes
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 
8 // For opening the zip file
9 #include <zip.h>
10 #include <zlib.h>
11 
12 // For parsing xml
13 #include <libxml/xmlreader.h>
14 
15 // For list stuff
16 #include "linklist.h"
17 #include "epub_shared.h"
18 
19 // General definitions
20 #ifdef _WIN32
21 # define PATH_SEPARATOR '\\'
22 #else
23 # define PATH_SEPARATOR '/'
24 #endif
25 
26 #ifdef __GNUC__
27 # define PRINTF_FORMAT(si, ftc) __attribute__ ((format (printf, si, ftc)))
28 #else
29 # define PRINTF_FORMAT(si, ftc)
30 #endif
31 
32 // MSVC-specific definitions
33 #ifdef _MSC_VER
34 # define strdup _strdup
35 #endif
36 
37 ///////////////////////////////////////////////////////////
38 // OCF definions
39 ///////////////////////////////////////////////////////////
40 #define METAINFO_DIR "META-INF"
41 #define MIMETYPE_FILENAME "mimetype"
42 #define CONTAINER_FILENAME "container.xml"
43 #define MANIFEST_FILENAME "manifest.xml"
44 #define METADATA_FILENAME "metadata.xml"
45 #define SIGNATURES_FILENAME "signatures.xml"
46 #define ENCRYPTION_FILENAME "encryption.xml"
47 #define RIGHTS_FILENAME "rights.xml"
48 
49 // An OCF root
50 struct root {
51  xmlChar *mediatype; // media type (mime)
52  xmlChar *fullpath; // full path to the root
53 };
54 
55 
56 struct ocf {
57  char *datapath; // The path that the data files relative to
58  char *filename; // The ebook filename
59  struct zip *arch; // The epub zip
60  char *mimetype; // For debugging
61  listPtr roots; // list of OCF roots
62  struct epub *epub; // back pointer
63 };
64 
65 struct meta {
66  xmlChar *name;
67  xmlChar *content;
68 };
69 
70 struct id {
71  xmlChar *id;
72  xmlChar *scheme;
73  xmlChar *string;
74 };
75 
76 struct date {
77  xmlChar *date;
78  xmlChar *event;
79 };
80 
81 struct creator {
82  xmlChar *name;
83  xmlChar *fileAs;
84  xmlChar *role;
85 };
86 
87 struct metadata {
104 };
105 
106 struct manifest {
107  xmlChar *nspace;
108  xmlChar *modules;
109  xmlChar *id;
110  xmlChar *href;
111  xmlChar *type;
112  xmlChar *fallback;
113  xmlChar *fbStyle;
114 
115 };
116 
117 struct guide {
118  xmlChar *type;
119  xmlChar *title;
120  xmlChar *href;
121 };
122 
123 struct site {
124  xmlChar *title;
125  xmlChar *href;
126 };
127 
128 struct tour {
129  xmlChar *id;
130  xmlChar *title;
132 };
133 
134 // Struct for navLabel and navInfo
135 struct tocLabel {
136  xmlChar *lang;
137  xmlChar *dir;
138  xmlChar *text;
139 };
140 
141 // struct for navPoint, pageTarget, navTarget
142 struct tocItem {
143  xmlChar *id;
144  xmlChar *src;
145  xmlChar *class;
146  xmlChar *type; //pages
148  int depth;
150  int value;
151 };
152 
153 // struct for navMap, pageList, navList
154 struct tocCategory {
155  xmlChar *id;
156  xmlChar *class;
157  listPtr info; //tocLabel
158  listPtr label; //tocLabel
159  listPtr items; //tocItem
160 };
161 
162 // General toc struct
163 struct toc {
164  struct tocCategory *navMap;
168 };
169 
170 struct spine {
171  xmlChar *idref;
172  int linear; //bool
173 };
174 
175 struct opf {
176  char *name;
177  xmlChar *tocName;
178  struct epub *epub;
180  struct toc *toc; // must in opf 2.0
184 
185  // might be NULL
188 };
189 
190 struct epuberr {
191  char lastStr[1025];
192  const char *str;
193  int len;
194  int type; /* for str: 0 = lastStr, 1 = external */
195 };
196 extern const char _epub_error_oom[];
197 #define _epub_err_set_const_str(_err, _err_string) \
198  do { \
199  (_err)->str = _err_string; \
200  (_err)->type = 1; \
201  } while (0)
202 #define _epub_err_set_str(_err, _err_string, _err_string_len) \
203  do { \
204  strncpy((_err)->lastStr, _err_string, _err_string_len); \
205  (_err)->len = _err_string_len; \
206  (_err)->str = (_err)->lastStr; \
207  (_err)->type = 0; \
208  } while (0)
209 #define _epub_err_set_oom(_epub_err) _epub_err_set_const_str(_epub_err, _epub_error_oom)
210 
211 // general structs
212 struct epub {
213  struct ocf *ocf;
214  struct opf *opf;
215  struct epuberr error;
216  int debug;
217 
218 };
219 
220 enum {
226 };
227 
228 struct eiterator {
230  struct epub *epub;
231  int opt;
233  char *cache;
234 };
235 
236 struct tit_info {
237  char *label;
238  int depth;
239  char *link;
240 };
241 
242 struct titerator {
244  struct epub *epub;
245  int opt;
247  struct tit_info cache;
248  int valid;
249 };
250 
251 // Ocf functions
252 struct ocf *_ocf_parse(struct epub *epub, const char *filename);
253 void _ocf_dump(struct ocf *ocf);
254 void _ocf_close(struct ocf *ocf);
255 struct zip *_ocf_open(struct ocf *ocf, const char *fileName);
256 int _ocf_get_file(struct ocf *ocf, const char *filename, char **fileStr);
257 int _ocf_get_data_file(struct ocf *ocf, const char *filename, char **fileStr);
258 int _ocf_check_file(struct ocf *ocf, const char *filename);
259 char *_ocf_root_by_type(struct ocf *ocf, const char *type);
260 char *_ocf_root_fullpath_by_type(struct ocf *ocf, const char *type);
261 
262 // Parsing ocf
263 int _ocf_parse_container(struct ocf *ocf);
264 int _ocf_parse_mimetype(struct ocf *ocf);
265 
266 // parsing opf
267 struct opf *_opf_parse(struct epub *epub, char *opfStr);
268 void _opf_dump(struct opf *opf);
269 void _opf_close(struct opf *opf);
270 
271 void _opf_parse_metadata(struct opf *opf, xmlTextReaderPtr reader);
272 void _opf_parse_spine(struct opf *opf, xmlTextReaderPtr reader);
273 void _opf_parse_manifest(struct opf *opf, xmlTextReaderPtr reader);
274 void _opf_parse_guide(struct opf *opf, xmlTextReaderPtr reader);
275 void _opf_parse_tours(struct opf *opf, xmlTextReaderPtr reader);
276 
277 // parse toc
278 void _opf_parse_toc(struct opf *opf, char *tocStr, int size);
279 void _opf_parse_navlist(struct opf *opf, xmlTextReaderPtr reader);
280 void _opf_parse_navmap(struct opf *opf, xmlTextReaderPtr reader);
281 void _opf_parse_pagelist(struct opf *opf, xmlTextReaderPtr reader);
282 struct tocLabel *_opf_parse_navlabel(struct opf *opf, xmlTextReaderPtr reader);
283 void _opf_free_toc_category(struct tocCategory *tc);
284 void _opf_free_toc(struct toc *toc);
285 struct toc *_opf_init_toc();
287 
288 xmlChar *_opf_label_get_by_lang(struct opf *opf, listPtr label, char *lang);
289 xmlChar *_opf_label_get_by_doc_lang(struct opf *opf, listPtr label);
290 
291 struct manifest *_opf_manifest_get_by_id(struct opf *opf, xmlChar* id);
292 
293 // epub functions
294 struct epub *epub_open(const char *filename, int debug);
295 void _epub_print_debug(struct epub *epub, int debug, const char *format, ...) PRINTF_FORMAT(3, 4);
296 char *epub_last_errStr(struct epub *epub);
297 
298 // List operations
299 void _list_free_root(struct root *data);
300 
301 void _list_free_creator(struct creator *data);
302 void _list_free_date(struct date *date);
303 void _list_free_id(struct id *id);
304 void _list_free_meta(struct meta *meta);
305 
306 void _list_free_spine(struct spine *spine);
308 void _list_free_guide(struct guide *guide);
309 void _list_free_tours(struct tour *tour);
310 
311 void _list_free_toc_label(struct tocLabel *tl);
312 void _list_free_toc_item(struct tocItem *ti);
313 
314 int _list_cmp_root_by_mediatype(struct root *root1, struct root *root2);
315 int _list_cmp_manifest_by_id(struct manifest *m1, struct manifest *m2);
316 int _list_cmp_toc_by_playorder(struct tocItem *t1, struct tocItem *t2);
317 int _list_cmp_label_by_lang(struct tocLabel *t1, struct tocLabel *t2);
318 
319 void _list_dump_root(struct root *root);
320 
321 void _list_dump_string(char *string);
322 void _list_dump_creator(struct creator *data);
323 void _list_dump_date(struct date *date);
324 void _list_dump_id(struct id *id);
325 void _list_dump_meta(struct meta *meta);
326 
327 void _list_dump_spine(struct spine *spine);
328 void _list_dump_guide(struct guide *guide);
329 void _list_dump_tour(struct tour *tour);
330 
331 #endif /* epublib_h */
void _ocf_close(struct ocf *ocf)
xmlChar * fileAs
Definition: epublib.h:83
xmlChar * fullpath
Definition: epublib.h:52
void _list_dump_guide(struct guide *guide)
struct epub * epub
Definition: epublib.h:178
xmlChar * lang
Definition: epublib.h:136
void _list_free_manifest(struct manifest *manifest)
listPtr lang
Definition: epublib.h:99
void _list_dump_id(struct id *id)
Definition: epublib.h:123
int _ocf_parse_container(struct ocf *ocf)
enum eiterator_type type
Definition: epublib.h:229
struct epub * epub
Definition: epublib.h:244
void _list_free_toc_label(struct tocLabel *tl)
void _ocf_dump(struct ocf *ocf)
char * mimetype
Definition: epublib.h:60
struct tocCategory * navMap
Definition: epublib.h:164
const char _epub_error_oom[]
void _opf_close(struct opf *opf)
void _list_free_id(struct id *id)
xmlChar * tocName
Definition: epublib.h:177
xmlChar * event
Definition: epublib.h:78
void _list_free_tours(struct tour *tour)
struct epub * epub
Definition: epublib.h:230
void _list_dump_string(char *string)
int playOrder
Definition: epublib.h:149
Definition: epublib.h:163
int len
Definition: epublib.h:193
int opt
Definition: epublib.h:231
struct tocCategory * navList
Definition: epublib.h:166
int _ocf_parse_mimetype(struct ocf *ocf)
void _list_free_guide(struct guide *guide)
int linearCount
Definition: epublib.h:183
struct manifest * _opf_manifest_get_by_id(struct opf *opf, xmlChar *id)
xmlChar * _opf_label_get_by_lang(struct opf *opf, listPtr label, char *lang)
char * datapath
Definition: epublib.h:57
listPtr publisher
Definition: epublib.h:93
int valid
Definition: epublib.h:248
char * _ocf_root_fullpath_by_type(struct ocf *ocf, const char *type)
void _opf_parse_spine(struct opf *opf, xmlTextReaderPtr reader)
void _list_dump_tour(struct tour *tour)
xmlChar * name
Definition: epublib.h:82
xmlChar * content
Definition: epublib.h:67
void _opf_parse_toc(struct opf *opf, char *tocStr, int size)
void _list_dump_date(struct date *date)
xmlChar * name
Definition: epublib.h:66
xmlChar * fbStyle
Definition: epublib.h:113
char * name
Definition: epublib.h:176
xmlChar * nspace
Definition: epublib.h:107
int _ocf_check_file(struct ocf *ocf, const char *filename)
void char * epub_last_errStr(struct epub *epub)
void _opf_free_toc_category(struct tocCategory *tc)
xmlChar * title
Definition: epublib.h:119
xmlChar * type
Definition: epublib.h:118
Definition: epublib.h:170
xmlChar * id
Definition: epublib.h:109
char lastStr[1025]
Definition: epublib.h:191
listPtr subject
Definition: epublib.h:92
struct opf * opf
Definition: epublib.h:214
int _list_cmp_root_by_mediatype(struct root *root1, struct root *root2)
Definition: epublib.h:50
xmlChar * _opf_label_get_by_doc_lang(struct opf *opf, listPtr label)
void _opf_parse_pagelist(struct opf *opf, xmlTextReaderPtr reader)
xmlChar * scheme
Definition: epublib.h:72
Definition: epublib.h:56
char * cache
Definition: epublib.h:233
struct zip * _ocf_open(struct ocf *ocf, const char *fileName)
listPtr relation
Definition: epublib.h:100
xmlChar * idref
Definition: epublib.h:171
enum titerator_type type
Definition: epublib.h:243
xmlChar * href
Definition: epublib.h:125
xmlChar * modules
Definition: epublib.h:108
xmlChar * title
Definition: epublib.h:124
Definition: epublib.h:175
xmlChar * id
Definition: epublib.h:155
Definition: epublib.h:70
listPtr spine
Definition: epublib.h:182
Definition: epublib.h:65
struct metadata * metadata
Definition: epublib.h:179
listPtr description
Definition: epublib.h:94
struct toc * toc
Definition: epublib.h:180
listnodePtr curr
Definition: epublib.h:232
#define PRINTF_FORMAT(si, ftc)
Definition: epublib.h:29
xmlChar * string
Definition: epublib.h:73
listPtr sites
Definition: epublib.h:131
int _list_cmp_toc_by_playorder(struct tocItem *t1, struct tocItem *t2)
int _list_cmp_manifest_by_id(struct manifest *m1, struct manifest *m2)
struct toc * _opf_init_toc()
char * link
Definition: epublib.h:239
xmlChar * id
Definition: epublib.h:71
xmlChar * id
Definition: epublib.h:143
xmlChar * type
Definition: epublib.h:111
int linear
Definition: epublib.h:172
void _opf_parse_manifest(struct opf *opf, xmlTextReaderPtr reader)
char * _ocf_root_by_type(struct ocf *ocf, const char *type)
listPtr id
Definition: epublib.h:88
int value
Definition: epublib.h:150
listPtr title
Definition: epublib.h:89
listnodePtr next
Definition: epublib.h:246
listPtr date
Definition: epublib.h:95
Definition: epublib.h:117
xmlChar * date
Definition: epublib.h:77
xmlChar * src
Definition: epublib.h:144
xmlChar * fallback
Definition: epublib.h:112
struct tocLabel * _opf_parse_navlabel(struct opf *opf, xmlTextReaderPtr reader)
Definition: epublib.h:76
listPtr source
Definition: epublib.h:98
struct epub * epub
Definition: epublib.h:62
void _list_free_meta(struct meta *meta)
listPtr format
Definition: epublib.h:97
xmlChar * text
Definition: epublib.h:138
void _list_free_spine(struct spine *spine)
void _opf_parse_tours(struct opf *opf, xmlTextReaderPtr reader)
struct ocf * ocf
Definition: epublib.h:213
void _list_dump_spine(struct spine *spine)
xmlChar * title
Definition: epublib.h:130
void _opf_parse_navlist(struct opf *opf, xmlTextReaderPtr reader)
int depth
Definition: epublib.h:238
struct tit_info cache
Definition: epublib.h:247
struct epuberr error
Definition: epublib.h:215
xmlChar * href
Definition: epublib.h:120
titerator_type
Ebook Table Of Content Iterator types.
Definition: epub_shared.h:49
listPtr meta
Definition: epublib.h:103
listPtr label
Definition: epublib.h:147
xmlChar * href
Definition: epublib.h:110
listPtr roots
Definition: epublib.h:61
xmlChar * dir
Definition: epublib.h:137
void _opf_parse_guide(struct opf *opf, xmlTextReaderPtr reader)
listPtr coverage
Definition: epublib.h:101
void _list_free_root(struct root *data)
int type
Definition: epublib.h:194
xmlChar * mediatype
Definition: epublib.h:51
void _opf_free_toc(struct toc *toc)
listPtr label
Definition: epublib.h:158
listPtr contrib
Definition: epublib.h:91
private struct containting information about the epub file
Definition: epublib.h:212
void _list_dump_root(struct root *root)
void _opf_parse_navmap(struct opf *opf, xmlTextReaderPtr reader)
Definition: epublib.h:128
listPtr guide
Definition: epublib.h:186
void _list_free_creator(struct creator *data)
xmlChar * role
Definition: epublib.h:84
listPtr tours
Definition: epublib.h:187
char * filename
Definition: epublib.h:58
void _epub_print_debug(struct epub *epub, int debug, const char *format,...) PRINTF_FORMAT(3
xmlChar * type
Definition: epublib.h:146
int opt
Definition: epublib.h:245
char * label
Definition: epublib.h:237
struct zip * arch
Definition: epublib.h:59
listPtr type
Definition: epublib.h:96
int depth
Definition: epublib.h:148
void _opf_parse_metadata(struct opf *opf, xmlTextReaderPtr reader)
void _list_dump_creator(struct creator *data)
int _list_cmp_label_by_lang(struct tocLabel *t1, struct tocLabel *t2)
struct tocCategory * pageList
Definition: epublib.h:165
const char * str
Definition: epublib.h:192
listPtr info
Definition: epublib.h:157
struct ocf * _ocf_parse(struct epub *epub, const char *filename)
listPtr creator
Definition: epublib.h:90
void _list_free_date(struct date *date)
struct opf * _opf_parse(struct epub *epub, char *opfStr)
void _list_free_toc_item(struct tocItem *ti)
private iterator struct
Definition: epublib.h:228
listPtr playOrder
Definition: epublib.h:167
eiterator_type
Ebook Iterator types.
Definition: epub_shared.h:39
void _opf_dump(struct opf *opf)
struct epub * epub_open(const char *filename, int debug)
int debug
Definition: epublib.h:216
listPtr items
Definition: epublib.h:159
void _list_dump_meta(struct meta *meta)
listPtr rights
Definition: epublib.h:102
int _ocf_get_data_file(struct ocf *ocf, const char *filename, char **fileStr)
int _ocf_get_file(struct ocf *ocf, const char *filename, char **fileStr)
xmlChar * id
Definition: epublib.h:129
listPtr manifest
Definition: epublib.h:181
struct tocCategory * _opf_init_toc_category()