Updated version number for development
[deark.git] / src / deark-private.h
blob487107292892c30856a4c9b48b9c90452f300d7e
1 // This file is part of Deark.
2 // Copyright (C) 2016 Jason Summers
3 // See the file COPYING for terms of use.
5 // Definitions not visible to the command-line utility.
7 #ifdef DEARK_PRIVATE_H_INC
8 #error "deark-private.h included multiple times"
9 #endif
10 #define DEARK_PRIVATE_H_INC
12 #ifndef DEARK_H_INC
13 #include "deark.h"
14 #endif
16 #define DE_MAX_SANE_OBJECT_SIZE 100000000
18 typedef i32 de_rune; // A Unicode codepoint
19 typedef u8 de_colorsample;
20 typedef u32 de_color;
22 enum de_encoding_enum {
23 DE_ENCODING_UNKNOWN = 0,
24 DE_ENCODING_ASCII,
25 DE_ENCODING_UTF8,
26 DE_ENCODING_UTF16LE,
27 DE_ENCODING_UTF16BE,
28 DE_ENCODING_LATIN1,
29 DE_ENCODING_LATIN2,
30 DE_ENCODING_WINDOWS1250,
31 DE_ENCODING_WINDOWS1251,
32 DE_ENCODING_WINDOWS1252,
33 DE_ENCODING_WINDOWS1253,
34 DE_ENCODING_WINDOWS1254,
35 DE_ENCODING_WINDOWS874,
36 DE_ENCODING_CP437,
37 DE_ENCODING_MACROMAN,
38 DE_ENCODING_ATARIST,
39 DE_ENCODING_PALM,
40 DE_ENCODING_RISCOS,
41 DE_ENCODING_PETSCII,
42 DE_ENCODING_DEC_SPECIAL_GRAPHICS
44 #define DE_ENCODING_CP437_G DE_ENCODING_CP437
46 #define DE_ENCSUBTYPE_CONTROLS 2
47 #define DE_ENCSUBTYPE_HYBRID 3
48 #define DE_ENCSUBTYPE_PRINTABLE 4
49 typedef enum de_encoding_enum de_encoding;
50 typedef int de_ext_encoding;
51 #define DE_EXTENC_MAKE(b, st) ((de_ext_encoding)(((int)(b) & 0xff) | ((int)(st)<<8)))
52 #define DE_EXTENC_GET_BASE(ee) ((de_encoding)((int)(ee) & 0xff))
53 #define DE_EXTENC_GET_SUBTYPE(ee) ((int)(ee) >> 8)
55 #define DE_CODEPOINT_HL 0x0001
56 #define DE_CODEPOINT_UNHL 0x0002
57 #define DE_CODEPOINT_RGBSAMPLE 0x0003
58 #define DE_CODEPOINT_MOVED 0xfde00
59 #define DE_CODEPOINT_MOVED_MAX 0xffffd
60 #define DE_CODEPOINT_INVALID 0x0fffffff // Generic invalid codepoint
61 #define DE_CODEPOINT_BYTE00 0x10000000 // More "invalid" codepoints
62 #define DE_CODEPOINT_BYTEFF 0x100000ff
64 #define DE_ARRAYCOUNT(x) (sizeof(x)/sizeof((x)[0]))
66 struct de_ucstring_struct;
67 typedef struct de_ucstring_struct de_ucstring;
68 struct dbuf_struct;
69 typedef struct dbuf_struct dbuf;
70 struct de_finfo_struct;
71 typedef struct de_finfo_struct de_finfo;
73 struct de_module_params_struct;
74 typedef struct de_module_params_struct de_module_params;
76 #define DE_DECLARE_MODULE(x) void x(deark *c, struct deark_module_info *mi)
78 // 'mparams' is used for sending data to, and receiving data from, a module.
79 typedef void (*de_module_run_fn)(deark *c, de_module_params *mparams);
81 typedef int (*de_module_identify_fn)(deark *c);
83 typedef void (*de_module_help_fn)(deark *c);
85 struct deark_module_info {
86 const char *id;
87 const char *desc;
88 const char *desc2; // Additional notes
89 de_module_run_fn run_fn;
90 de_module_identify_fn identify_fn;
91 de_module_help_fn help_fn;
92 #define DE_MODFLAG_HIDDEN 0x01 // Do not list
93 #define DE_MODFLAG_NONWORKING 0x02 // Do not list, and print a warning
94 #define DE_MODFLAG_NOEXTRACT 0x04 // Do not warn if no files are extracted
95 #define DE_MODFLAG_SECURITYWARNING 0x08
96 #define DE_MODFLAG_SHAREDDETECTION 0x10 // Module modifies deark::detection_data
97 #define DE_MODFLAG_DISABLEDETECT 0x100 // Ignore results of autodetection
98 u32 flags;
99 u32 unique_id; // or 0. Rarely used.
100 #define DE_MAX_MODULE_ALIASES 2
101 const char *id_alias[DE_MAX_MODULE_ALIASES];
103 typedef void (*de_module_getinfo_fn)(deark *c, struct deark_module_info *mi);
105 struct de_encconv_state;
106 typedef de_rune (*de_encconv_fn)(struct de_encconv_state *es, i32 a);
107 struct de_encconv_state {
108 de_ext_encoding ee;
109 de_encconv_fn fn;
110 const void *fn_pvt_data;
111 u8 buf[8];
114 struct de_ucstring_struct {
115 deark *c;
116 de_rune *str;
117 i64 len; // len and alloc are measured in characters, not bytes
118 i64 alloc;
119 char *tmp_string;
122 struct de_timestamp {
123 u8 is_valid;
124 #define DE_TZCODE_UNKNOWN 0 // should be treated as UTC in most cases
125 #define DE_TZCODE_UTC 1 // known to be UTC
126 #define DE_TZCODE_LOCAL 2 // likely to be some local time
127 u8 tzcode;
128 // Timestamp precision codes are in order of increasing precision, except
129 // for 0 (UNKNOWN).
130 #define DE_TSPREC_UNKNOWN 0 // default, usually treated as 1sec
131 #define DE_TSPREC_1DAY 10
132 #define DE_TSPREC_2SEC 20
133 #define DE_TSPREC_1SEC 30
134 #define DE_TSPREC_HIGH 40 // = better than 1 second
135 u8 precision;
136 i64 ts_FILETIME; // the timestamp, in Windows FILETIME format
139 typedef void (*de_writelistener_cb_type)(dbuf *f, void *userdata, const u8 *buf, i64 buf_len);
140 typedef void (*de_dbufcustomread_type)(dbuf *f, void *userdata, u8 *buf, i64 pos, i64 len);
141 typedef void (*de_dbufcustomwrite_type)(dbuf *f, void *userdata, const u8 *buf, i64 buf_len);
143 // dbuf is our generalized I/O object. Used for many purposes.
144 struct dbuf_struct {
145 #define DBUF_TYPE_NULL 0
146 #define DBUF_TYPE_IFILE 1
147 #define DBUF_TYPE_OFILE 2
148 #define DBUF_TYPE_MEMBUF 3
149 #define DBUF_TYPE_IDBUF 4 // nested dbuf, for input
150 #define DBUF_TYPE_STDOUT 5
151 #define DBUF_TYPE_STDIN 6
152 #define DBUF_TYPE_FIFO 7
153 #define DBUF_TYPE_ODBUF 8 // nested dbuf, for output
154 #define DBUF_TYPE_CUSTOM 9
155 int btype;
156 u8 is_managed;
158 deark *c;
159 FILE *fp;
160 i64 len;
162 i64 max_len_hard; // Serious error if this is exceeded
163 i64 len_limit; // Valid if has_len_limit is set. May only work for type MEMBUF.
164 int has_len_limit;
166 int file_pos_known;
167 i64 file_pos;
169 struct dbuf_struct *parent_dbuf; // used for DBUF_TYPE_DBUF
170 i64 offset_into_parent_dbuf; // used for DBUF_TYPE_DBUF
172 u8 write_memfile_to_zip_archive;
173 u8 writing_to_tar_archive;
174 char *name; // used for DBUF_TYPE_OFILE (utf-8)
176 i64 membuf_alloc;
177 u8 *membuf_buf;
179 void *userdata_for_writelistener;
180 de_writelistener_cb_type writelistener_cb;
181 void *userdata_for_customread;
182 de_dbufcustomread_type customread_fn; // used for DBUF_TYPE_CUSTOM
183 void *userdata_for_customwrite;
184 de_dbufcustomwrite_type customwrite_fn; // used for DBUF_TYPE_CUSTOM
186 #define DE_CACHE_POLICY_NONE 0
187 #define DE_CACHE_POLICY_ENABLED 1
188 int cache_policy;
189 i64 cache_bytes_used;
190 u8 *cache; // first 'cache_bytes_used' bytes of the file
192 // cache2 is a simple 1-byte cache, mainly to speed up de_get_bits_symbol().
193 i64 cache2_pos;
194 u8 cache2;
196 // Things copied from the de_finfo object at file creation
197 de_finfo *fi_copy;
200 // Image density (resolution) settings
201 struct de_density_info {
202 #define DE_DENSITY_UNKNOWN 0
203 #define DE_DENSITY_UNK_UNITS 1
204 #define DE_DENSITY_DPI 2
205 int code;
206 // Note: If units are unknown, xdens and ydens must be integers.
207 double xdens;
208 double ydens;
211 // Extended information & metadata about a file to be written.
212 struct de_finfo_struct {
213 de_ucstring *file_name_internal; // Modules should avoid using this field directly.
214 u8 original_filename_flag; // Indicates if .file_name_internal is a real file name
215 u8 is_directory; // Does the "file" represent a subdirectory?
216 u8 is_root_dir; // Is this definitely the unnamed root (".") dir?
217 u8 detect_root_dot_dir; // Directories named "." are special.
218 u8 orig_name_was_dot; // Internal use
219 u8 has_hotspot;
221 #define DE_MODEFLAG_NONEXE 0x01 // Make the output file non-executable.
222 #define DE_MODEFLAG_EXE 0x02 // Make the output file executable.
223 unsigned int mode_flags;
225 #define DE_TIMESTAMPIDX_MODIFY 0 // External timestamps...
226 #define DE_TIMESTAMPIDX_CREATE 1
227 #define DE_TIMESTAMPIDX_ACCESS 2
228 #define DE_TIMESTAMPIDX_ATTRCHANGE 3
229 #define DE_TIMESTAMPIDX_BACKUP 4
230 #define DE_TIMESTAMPIDX_COUNT 5
231 struct de_timestamp timestamp[DE_TIMESTAMPIDX_COUNT];
233 struct de_timestamp internal_mod_time; // E.g. for PNG tIME chunk
234 struct de_density_info density;
235 de_ucstring *name_other; // Modules can use this field as needed.
236 int hotspot_x, hotspot_y; // Measured from upper-left pixel (after handling 'flipped')
239 struct deark_bitmap_struct {
240 deark *c;
241 i64 width;
242 i64 height;
243 i64 unpadded_width;
244 int invalid_image_flag;
245 int bytes_per_pixel;
246 u8 *bitmap;
247 i64 bitmap_size; // bytes allocated for bitmap
248 int orig_colortype; // Optional; can be used by modules
249 int orig_bitdepth; // Optional; can be used by modules
251 typedef struct deark_bitmap_struct de_bitmap;
253 struct de_SAUCE_detection_data {
254 u8 has_SAUCE;
255 u8 data_type;
256 u8 file_type;
259 struct de_ID3_detection_data {
260 u8 detection_attempted;
261 u8 has_id3v2;
262 u32 bytes_at_start;
265 // This struct is a crude way for data to be shared by the various format
266 // identification functions. It generally should not be used outside of them --
267 // but it can be, provided it's only used as a cache.
268 struct de_detection_data_struct {
269 int best_confidence_so_far;
270 u8 is_macbinary;
271 u8 SAUCE_detection_attempted;
272 u8 zip_eocd_looked_for;
273 u8 zip_eocd_found;
274 i64 zip_eocd_pos; // valid if zip_eocd_found
275 struct de_SAUCE_detection_data sauce;
276 struct de_ID3_detection_data id3;
279 struct de_module_in_params {
280 const char *codes;
281 // Module-specific fields:
282 u32 flags;
283 de_encoding input_encoding;
284 u32 uint1;
285 i64 offset_in_parent;
286 dbuf *parent_dbuf;
287 de_finfo *fi;
290 struct de_module_out_params {
291 // Fields are module-specific.
292 u32 flags;
293 u32 uint1;
294 u32 uint2;
295 u32 uint3;
296 u32 uint4;
297 i64 int64_1;
298 // The caller is responsible for freeing pointer fields.
299 // The callee should not use these fields unless requested.
300 de_finfo *fi;
301 void *obj1;
304 struct de_module_params_struct {
305 struct de_module_in_params in_params;
306 struct de_module_out_params out_params;
309 struct deark_ext_option {
310 char *name;
311 char *val;
314 typedef void (*de_module_register_fn_type)(deark *c);
316 enum de_moddisp_enum {
317 DE_MODDISP_NONE = 0, // No active module, or unknown
318 DE_MODDISP_AUTODETECT, // Format was autodetected
319 DE_MODDISP_EXPLICIT, // User used -m to select the module
320 DE_MODDISP_INTERNAL // Another module is using this module
323 struct deark_struct {
324 int debug_level;
325 void *userdata;
327 ////////////////////////////////////////////////////
328 int module_nesting_level;
330 // Data specific to the current module.
332 // TODO: There really ought to be a stack of standard-module-local data
333 // objects, but that may be more trouble than it's worth.
334 // For now, we just need to use caution when changing these fields.
336 // The current primary input file.
337 // Modules may change this, provided they change it back when they're done.
338 dbuf *infile;
340 // A flag to remember whether we've printed the specific format of the
341 // top-level file.
342 int format_declared;
344 enum de_moddisp_enum module_disposition; // Why are we using this module?
346 // Always valid during identify(); can be NULL during run().
347 struct de_detection_data_struct *detection_data;
348 ////////////////////////////////////////////////////
350 int file_count; // The number of extractable files encountered so far.
352 // The number of files we've actually written (or listed), after taking
353 // first_output_file/max_output_files into account.
354 int num_files_extracted;
356 i64 total_output_size;
357 int error_count;
358 u8 serious_error_flag;
360 const char *input_filename;
361 const char *input_format_req; // Format requested
362 const char *modcodes_req;
363 i64 slice_start_req; // Used if we're only to look at part of the file.
364 i64 slice_size_req;
365 int slice_size_req_valid;
366 int suppress_detection_by_filename;
368 int output_style; // DE_OUTPUTSTYLE_*
369 int archive_fmt; // If output_style==DE_OUTPUTSTYLE_ARCHIVE
370 int input_style; // DE_INPUTSTYLE_*
371 u8 archive_to_stdout;
372 u8 allow_subdirs;
374 int extract_policy; // DE_EXTRACTPOLICY_*
375 int extract_level;
376 u8 list_mode;
377 u8 list_mode_include_file_id;
378 int first_output_file; // first file = 0
379 int max_output_files;
380 u8 user_set_max_output_files;
381 i64 max_image_dimension;
382 i64 max_output_file_size;
383 i64 max_total_output_size;
384 int show_infomessages;
385 int show_warnings;
386 int dbg_indent_amount;
387 u8 write_bom;
388 u8 write_density;
389 u8 ascii_html;
390 u8 keep_dir_entries;
391 u8 filenames_from_file;
392 u8 macformat_known;
393 u8 macformat;
394 u8 padpix;
395 int overwrite_mode;
396 u8 preserve_file_times;
397 u8 preserve_file_times_archives;
398 u8 preserve_file_times_internal;
399 u8 reproducible_output;
400 struct de_timestamp reproducible_timestamp;
401 int can_decode_fltpt;
402 int host_is_le;
403 u8 identify_only;
404 u8 modhelp_req;
405 de_encoding input_encoding;
406 i64 input_tz_offs_seconds;
408 de_msgfn_type msgfn; // Caller's message output function
409 de_specialmsgfn_type specialmsgfn;
410 de_fatalerrorfn_type fatalerrorfn;
411 const char *dprefix;
413 u8 deflate_decoder_id;
414 u8 tmpflag1;
415 u8 tmpflag2;
416 u8 pngcprlevel_valid;
417 unsigned int pngcmprlevel;
418 void *zip_data;
419 void *tar_data;
420 dbuf *extrlist_dbuf;
422 char *base_output_filename;
423 char *special_1st_filename;
424 char *output_archive_filename;
425 char *extrlist_filename;
427 const char *onlymods_string;
428 const char *disablemods_string;
429 const char *onlydetectmods_string;
430 const char *nodetectmods_string;
432 struct de_timestamp current_time;
434 de_module_register_fn_type module_register_fn;
436 int num_modules;
437 struct deark_module_info *module_info; // Pointer to an array
439 #define DE_MAX_EXT_OPTIONS 16
440 int num_ext_options;
441 struct deark_ext_option ext_option[DE_MAX_EXT_OPTIONS];
444 void de_fatalerror(deark *c);
445 void de_internal_err_fatal(deark *c, const char *fmt, ...)
446 de_gnuc_attribute ((format (printf, 2, 3)));
447 void de_internal_err_nonfatal(deark *c, const char *fmt, ...)
448 de_gnuc_attribute ((format (printf, 2, 3)));
450 deark *de_create_internal(void);
451 int de_run_module(deark *c, struct deark_module_info *mi, de_module_params *mparams,
452 enum de_moddisp_enum moddisp);
453 int de_run_module_by_id(deark *c, const char *id, de_module_params *mparams);
454 int de_run_module_by_id_on_slice(deark *c, const char *id, de_module_params *mparams,
455 dbuf *f, i64 pos, i64 len);
456 int de_run_module_by_id_on_slice2(deark *c, const char *id, const char *codes,
457 dbuf *f, i64 pos, i64 len);
458 int de_get_module_idx_by_id(deark *c, const char *module_id);
459 struct deark_module_info *de_get_module_by_id(deark *c, const char *module_id);
461 void de_strlcpy(char *dst, const char *src, size_t dstlen);
462 char *de_strchr(const char *s, int c);
463 #define de_strlen strlen
464 #define de_strcmp strcmp
465 #define de_strncmp strncmp
466 #define de_memcmp memcmp
467 #define de_memcpy memcpy
468 #define de_memmove memmove
469 #define de_memset memset
470 #define de_zeromem(a,b) memset((a),0,(b))
471 #define de_memchr memchr
472 #ifdef DE_WINDOWS
473 #define de_sscanf sscanf_s
474 #else
475 #define de_sscanf sscanf
476 #endif
477 #define de_strtod strtod
479 // de_dbg*, de_msg, de_warn, de_err: The output is a single line, to which a
480 // standard prefix like "Warning: " may be added. A newline will be added
481 // automatically.
482 // [For other output functions, see de_puts, de_printf (deark.h).]
484 void de_dbg(deark *c, const char *fmt, ...)
485 de_gnuc_attribute ((format (printf, 2, 3)));
486 void de_dbg2(deark *c, const char *fmt, ...)
487 de_gnuc_attribute ((format (printf, 2, 3)));
488 void de_dbg3(deark *c, const char *fmt, ...)
489 de_gnuc_attribute ((format (printf, 2, 3)));
490 void de_dbgx(deark *c, int lv, const char *fmt, ...)
491 de_gnuc_attribute ((format (printf, 3, 4)));
492 void de_info(deark *c, const char *fmt, ...)
493 de_gnuc_attribute ((format (printf, 2, 3)));
494 void de_msg(deark *c, const char *fmt, ...)
495 de_gnuc_attribute ((format (printf, 2, 3)));
496 void de_vwarn(deark *c, const char *fmt, va_list ap);
497 void de_warn(deark *c, const char *fmt, ...)
498 de_gnuc_attribute ((format (printf, 2, 3)));
499 void de_verr(deark *c, const char *fmt, va_list ap);
500 void de_err(deark *c, const char *fmt, ...)
501 de_gnuc_attribute ((format (printf, 2, 3)));
503 FILE* de_fopen_for_read(deark *c, const char *fn, i64 *len,
504 char *errmsg, size_t errmsg_len, unsigned int *returned_flags);
505 FILE* de_fopen_for_write(deark *c, const char *fn,
506 char *errmsg, size_t errmsg_len, int overwrite_mode,
507 unsigned int flags);
508 int de_fseek(FILE *fp, i64 offs, int whence);
509 i64 de_ftell(FILE *fp);
510 int de_fclose(FILE *fp);
511 void de_update_file_attribs(dbuf *f, u8 preserve_file_times);
513 void de_declare_fmt(deark *c, const char *fmtname);
514 void de_declare_fmtf(deark *c, const char *fmt, ...)
515 de_gnuc_attribute ((format (printf, 2, 3)));
516 de_encoding de_get_input_encoding(deark *c, de_module_params *mparams,
517 de_encoding dflt);
519 void de_dbg_indent(deark *c, int n);
520 void de_dbg_indent_save(deark *c, int *saved_indent_level);
521 void de_dbg_indent_restore(deark *c, int saved_indent_level);
522 void de_dbg_hexdump(deark *c, dbuf *f, i64 pos1, i64 nbytes_avail,
523 i64 max_nbytes_to_dump, const char *prefix, unsigned int flags);
524 void de_hexdump2(deark *c, dbuf *f, i64 pos1, i64 nbytes_avail,
525 i64 max_nbytes_to_dump, unsigned int flags);
526 void de_dbg_dimensions(deark *c, i64 w, i64 h);
527 void de_dbg_pal_entry(deark *c, i64 idx, de_color clr);
528 void de_dbg_pal_entry2(deark *c, i64 idx, de_color clr,
529 const char *txt_before, const char *txt_in, const char *txt_after);
530 char *de_get_colorsample_code(deark *c, de_color clr, char *csamp,
531 size_t csamplen);
533 const char *de_get_ext_option(deark *c, const char *name);
534 int de_get_ext_option_bool(deark *c, const char *name, int defaultval);
536 ///////////////////////////////////////////
538 const char *de_get_sz_ext(const char *sz);
539 int de_sz_has_ext(const char *sz, const char *ext);
540 const char *de_get_input_file_ext(deark *c);
541 int de_input_file_has_ext(deark *c, const char *ext);
542 int de_havemodcode(deark *c, de_module_params *mparams, int code);
544 ///////////////////////////////////////////
546 int de_archive_initialize(deark *c);
547 void de_get_reproducible_timestamp(deark *c, struct de_timestamp *ts);
549 int de_tar_create_file(deark *c);
550 void de_tar_start_member_file(deark *c, dbuf *f);
551 void de_tar_end_member_file(deark *c, dbuf *f);
552 void de_tar_close_file(deark *c);
554 ///////////////////////////////////////////
556 int de_zip_create_file(deark *c);
557 void de_zip_add_file_to_archive(deark *c, dbuf *f);
558 void de_zip_close_file(deark *c);
560 int de_write_png(deark *c, de_bitmap *img, dbuf *f, UI createflags);
562 ///////////////////////////////////////////
564 i64 de_geti8_direct(const u8 *m);
565 i64 de_getu16be_direct(const u8 *m);
566 i64 de_getu16le_direct(const u8 *m);
567 i64 de_getu32be_direct(const u8 *m);
568 i64 de_getu32le_direct(const u8 *m);
569 i64 de_geti64be_direct(const u8 *m);
570 i64 de_geti64le_direct(const u8 *m);
571 u64 de_getu64be_direct(const u8 *m);
572 u64 de_getu64le_direct(const u8 *m);
574 void dbuf_read(dbuf *f, u8 *buf, i64 pos, i64 len);
575 i64 dbuf_standard_read(dbuf *f, u8 *buf, i64 n, i64 *fpos);
577 u8 dbuf_getbyte(dbuf *f, i64 pos);
578 i64 dbuf_geti8(dbuf *f, i64 pos);
579 i64 dbuf_getu16be(dbuf *f, i64 pos);
580 i64 dbuf_getu16le(dbuf *f, i64 pos);
581 i64 dbuf_getu16x(dbuf *f, i64 pos, int is_le);
582 i64 dbuf_geti16be(dbuf *f, i64 pos);
583 i64 dbuf_geti16le(dbuf *f, i64 pos);
584 i64 dbuf_geti16x(dbuf *f, i64 pos, int is_le);
585 i64 dbuf_getu32be(dbuf *f, i64 pos);
586 i64 dbuf_getu32le(dbuf *f, i64 pos);
587 i64 dbuf_getu32x(dbuf *f, i64 pos, int is_le);
588 i64 dbuf_geti32be(dbuf *f, i64 pos);
589 i64 dbuf_geti32le(dbuf *f, i64 pos);
590 i64 dbuf_geti32x(dbuf *f, i64 pos, int is_le);
591 i64 dbuf_geti64be(dbuf *f, i64 pos);
592 i64 dbuf_geti64le(dbuf *f, i64 pos);
593 i64 dbuf_geti64x(dbuf *f, i64 pos, int is_le);
594 u64 dbuf_getu64be(dbuf *f, i64 pos);
595 u64 dbuf_getu64le(dbuf *f, i64 pos);
596 u64 dbuf_getu64x(dbuf *f, i64 pos, int is_le);
598 i64 dbuf_getint_ext(dbuf *f, i64 pos, unsigned int nbytes,
599 int is_le, int is_signed);
601 // The _p functions update a caller-supplied position.
602 u8 dbuf_getbyte_p(dbuf *f, i64 *ppos);
603 i64 dbuf_getu16be_p(dbuf *f, i64 *ppos);
604 i64 dbuf_getu16le_p(dbuf *f, i64 *ppos);
605 i64 dbuf_getu32le_p(dbuf *f, i64 *ppos);
606 i64 dbuf_getu32be_p(dbuf *f, i64 *ppos);
607 i64 dbuf_geti16be_p(dbuf *f, i64 *ppos);
608 i64 dbuf_geti16le_p(dbuf *f, i64 *ppos);
609 i64 dbuf_geti32be_p(dbuf *f, i64 *ppos);
610 i64 dbuf_geti32le_p(dbuf *f, i64 *ppos);
612 // Only format modules should use these convenience macros.
613 // (The DE_WINDOWS condition has no functional purpose; it's a hack to make
614 // some development tools work better.)
615 #if !defined(DE_NOT_IN_MODULE) || defined(DE_WINDOWS)
616 #define de_read(b,p,l) dbuf_read(c->infile,b,p,l);
617 #define de_getbyte(p) dbuf_getbyte(c->infile,p)
618 #define de_getu16be(p) dbuf_getu16be(c->infile,p)
619 #define de_getu16le(p) dbuf_getu16le(c->infile,p)
620 #define de_geti16be(p) dbuf_geti16be(c->infile,p)
621 #define de_geti16le(p) dbuf_geti16le(c->infile,p)
622 #define de_getu32be(p) dbuf_getu32be(c->infile,p)
623 #define de_getu32le(p) dbuf_getu32le(c->infile,p)
624 #define de_geti32be(p) dbuf_geti32be(c->infile,p)
625 #define de_geti32le(p) dbuf_geti32le(c->infile,p)
626 #define de_geti64be(p) dbuf_geti64be(c->infile,p)
627 #define de_geti64le(p) dbuf_geti64le(c->infile,p)
628 #define de_getbyte_p(p) dbuf_getbyte_p(c->infile,p)
629 #define de_getu16be_p(p) dbuf_getu16be_p(c->infile,p)
630 #define de_getu16le_p(p) dbuf_getu16le_p(c->infile,p)
631 #define de_getu32be_p(p) dbuf_getu32be_p(c->infile,p)
632 #define de_getu32le_p(p) dbuf_getu32le_p(c->infile,p)
633 #define de_geti16be_p(p) dbuf_geti16be_p(c->infile,p)
634 #define de_geti16le_p(p) dbuf_geti16le_p(c->infile,p)
635 #define de_geti32be_p(p) dbuf_geti32be_p(c->infile,p)
636 #define de_geti32le_p(p) dbuf_geti32le_p(c->infile,p)
637 #endif
639 // Read IEEE 754 floating point
640 double de_getfloat32x_direct(deark *c, const u8 *m, int is_le);
641 double dbuf_getfloat32x(dbuf *f, i64 pos, int is_le);
642 double de_getfloat64x_direct(deark *c, const u8 *m, int is_le);
643 double dbuf_getfloat64x(dbuf *f, i64 pos, int is_le);
645 int dbuf_read_ascii_number(dbuf *f, i64 pos, i64 fieldsize,
646 int base, i64 *value);
648 #define DE_GETRGBFLAG_BGR 0x1 // Assume BGR order instead of RGB
649 de_color dbuf_getRGB(dbuf *f, i64 pos, unsigned int flags);
651 // Convert and append encoded bytes from a dbuf to a ucstring.
652 // (see also ucstring_append_*)
653 void dbuf_read_to_ucstring_ex(dbuf *f, i64 pos, i64 len,
654 de_ucstring *s, unsigned int conv_flags, struct de_encconv_state *es);
655 void dbuf_read_to_ucstring(dbuf *f, i64 pos, i64 len,
656 de_ucstring *s, unsigned int conv_flags, de_ext_encoding encoding);
657 // The _n version has an extra max_len field, for convenience.
658 void dbuf_read_to_ucstring_n(dbuf *f, i64 pos, i64 len, i64 max_len,
659 de_ucstring *s, unsigned int conv_flags, de_ext_encoding encoding);
661 // At least one of 'ext' or 'fi' should be non-NULL.
662 #define DE_CREATEFLAG_IS_AUX 0x1
663 #define DE_CREATEFLAG_OPT_IMAGE 0x2
664 #define DE_CREATEFLAG_FLIP_IMAGE 0x4
665 dbuf *dbuf_create_output_file(deark *c, const char *ext, de_finfo *fi, unsigned int createflags);
667 dbuf *dbuf_create_unmanaged_file(deark *c, const char *fname, int overwrite_mode, unsigned int flags);
668 dbuf *dbuf_create_unmanaged_file_stdout(deark *c, const char *name);
669 dbuf *dbuf_open_input_file(deark *c, const char *fn);
670 dbuf *dbuf_open_input_stdin(deark *c);
671 dbuf *dbuf_open_input_subfile(dbuf *parent, i64 offset, i64 size);
672 dbuf *dbuf_create_custom_dbuf(deark *c, i64 apparent_size, unsigned int flags);
674 // Flag:
675 // 0x1: Set the maximum size to the 'initialsize'
676 dbuf *dbuf_create_membuf(deark *c, i64 initialsize, unsigned int flags);
678 // If f is NULL, this is a no-op.
679 void dbuf_close(dbuf *f);
681 void dbuf_set_writelistener(dbuf *f, de_writelistener_cb_type fn, void *userdata);
683 void dbuf_write(dbuf *f, const u8 *m, i64 len);
684 void dbuf_write_at(dbuf *f, i64 pos, const u8 *m, i64 len);
685 void dbuf_write_zeroes(dbuf *f, i64 len);
686 void dbuf_truncate(dbuf *f, i64 len);
687 void dbuf_write_run(dbuf *f, u8 n, i64 len);
689 void de_writeu16le_direct(u8 *m, i64 n);
690 void de_writeu16be_direct(u8 *m, i64 n);
691 void de_writeu32le_direct(u8 *m, i64 n);
692 void de_writeu32be_direct(u8 *m, i64 n);
693 void de_writeu64le_direct(u8 *m, u64 n);
694 void dbuf_writebyte(dbuf *f, u8 n);
695 void dbuf_writebyte_at(dbuf *f, i64 pos, u8 n);
696 void dbuf_writeu16le(dbuf *f, i64 n);
697 void dbuf_writeu16be(dbuf *f, i64 n);
698 void dbuf_writei16le(dbuf *f, i64 n);
699 void dbuf_writei16be(dbuf *f, i64 n);
700 void dbuf_writeu32le(dbuf *f, i64 n);
701 void dbuf_writeu32be(dbuf *f, i64 n);
702 void dbuf_writei32le(dbuf *f, i64 n);
703 void dbuf_writei32be(dbuf *f, i64 n);
704 void dbuf_writeu64le(dbuf *f, u64 n);
706 void dbuf_puts(dbuf *f, const char *sz);
707 void dbuf_printf(dbuf *f, const char *fmt, ...)
708 de_gnuc_attribute ((format (printf, 2, 3)));
709 void dbuf_flush(dbuf *f);
711 // Read a slice of one dbuf, and append it to another dbuf.
712 void dbuf_copy(dbuf *inf, i64 input_offset, i64 input_len, dbuf *outf);
713 void dbuf_copy_at(dbuf *inf, i64 input_offset, i64 input_len, dbuf *outf, i64 outpos);
715 struct de_stringreaderdata {
716 // The number of bytes used by the string in the file (ie includes trailing NUL),
717 // even if they aren't all stored in ->sz.
718 i64 bytes_consumed;
720 char *sz; // Stores some or all of the bytes read. Always NUL terminated.
721 size_t sz_strlen;
722 de_ucstring *str; // Unicode version of ->sz
723 char *sz_utf8; // UTF-8 version of ->str (+ NUL terminator) (optional)
724 size_t sz_utf8_strlen;
725 int was_truncated;
726 int found_nul;
729 struct de_stringreaderdata *dbuf_read_string(dbuf *f, i64 pos,
730 i64 max_bytes_to_scan, i64 max_bytes_to_keep,
731 unsigned int flags, de_ext_encoding ee);
732 void de_destroy_stringreaderdata(deark *c, struct de_stringreaderdata *srd);
734 // Compare bytes in a dbuf to s.
735 // Note that repeatedly comparing the same dbuf bytes might be inefficient.
736 int dbuf_memcmp(dbuf *f, i64 pos, const void *s, size_t n);
738 // Read a slice of a dbuf, and create a new file containing only that.
739 // At least one of 'ext' or 'fi' should be non-NULL.
740 int dbuf_create_file_from_slice(dbuf *inf, i64 pos, i64 data_size,
741 const char *ext, de_finfo *fi, unsigned int createflags);
743 int dbuf_has_utf8_bom(dbuf *f, i64 pos);
745 int dbuf_dump_to_file(dbuf *inf, const char *fn);
747 // Remove everything from the dbuf.
748 // May be valid only for memory buffers.
749 void dbuf_empty(dbuf *f);
751 void dbuf_set_length_limit(dbuf *f, i64 max_len);
752 const u8 *dbuf_get_membuf_direct_ptr(dbuf *f);
753 int dbuf_search_byte(dbuf *f, const u8 b, i64 startpos, i64 haystack_len,
754 i64 *foundpos);
755 int dbuf_search(dbuf *f, const u8 *needle, i64 needle_len, i64 startpos,
756 i64 haystack_len, i64 *foundpos);
757 int dbuf_get_utf16_NULterm_len(dbuf *f, i64 pos1, i64 bytes_avail,
758 i64 *bytes_consumed);
759 int dbuf_find_line(dbuf *f, i64 pos1, i64 *pcontent_len, i64 *ptotal_len);
761 struct de_fourcc {
762 u8 bytes[4];
763 u32 id;
764 char id_sanitized_sz[8]; // NUL-terminated printable ASCII
765 char id_dbgstr[32]; // Usable only with de_dbg()
767 #define DE_4CCFLAG_REVERSED 0x1
768 void dbuf_read_fourcc(dbuf *f, i64 pos, struct de_fourcc *fcc, int nbytes,
769 unsigned int flags);
771 #define DE_BUFFERED_READ_MIN_BLKSIZE 1024
772 struct de_bufferedreadctx {
773 void *userdata;
774 deark *c;
775 i64 offset;
776 i64 bytes_consumed;
777 u8 eof_flag;
779 typedef int (*de_buffered_read_cbfn)(struct de_bufferedreadctx *brctx,
780 const u8 *buf, i64 buf_len);
781 int dbuf_buffered_read(dbuf *f, i64 pos, i64 len,
782 de_buffered_read_cbfn cbfn, void *userdata);
784 int de_is_all_zeroes(const u8 *b, i64 n);
785 int dbuf_is_all_zeroes(dbuf *f, i64 pos, i64 len);
787 struct de_bitbuf_lowlevel {
788 u8 is_lsb;
789 UI nbits_in_bitbuf;
790 u64 bit_buf;
792 void de_bitbuf_lowlevel_add_byte(struct de_bitbuf_lowlevel *bbll, u8 n);
793 u64 de_bitbuf_lowlevel_get_bits(struct de_bitbuf_lowlevel *bbll, UI nbits);
794 void de_bitbuf_lowlevel_empty(struct de_bitbuf_lowlevel *bbll);
796 struct de_bitreader {
797 dbuf *f;
798 i64 curpos;
799 i64 endpos;
800 u8 eof_flag;
801 struct de_bitbuf_lowlevel bbll;
803 u64 de_bitreader_getbits(struct de_bitreader *bitrd, UI nbits);
804 void de_bitreader_skip_to_byte_boundary(struct de_bitreader *bitrd);
805 char *de_bitreader_describe_curpos(struct de_bitreader *bitrd, char *buf, size_t buf_len);
807 ///////////////////////////////////////////
809 void de_bitmap_write_to_file(de_bitmap *img, const char *token, unsigned int createflags);
810 void de_bitmap_write_to_file_finfo(de_bitmap *img, de_finfo *fi, unsigned int createflags);
812 void de_bitmap_setsample(de_bitmap *img, i64 x, i64 y,
813 i64 samplenum, de_colorsample v);
815 void de_bitmap_setpixel_gray(de_bitmap *img, i64 x, i64 y, de_colorsample v);
816 void de_bitmap_setpixel_rgb(de_bitmap *img, i64 x, i64 y, de_color color);
817 void de_bitmap_setpixel_rgba(de_bitmap *img, i64 x, i64 y, de_color color);
819 de_color de_bitmap_getpixel(de_bitmap *img, i64 x, i64 y);
821 de_bitmap *de_bitmap_create(deark *c, i64 width, i64 height, int bypp);
822 de_bitmap *de_bitmap_create2(deark *c, i64 npwidth, i64 pdwidth, i64 height, int bypp);
823 void de_bitmap_destroy(de_bitmap *b);
825 #define DE_COLOR_A(x) ((de_colorsample)(((x)>>24)&0xff))
826 #define DE_COLOR_R(x) ((de_colorsample)(((x)>>16)&0xff))
827 #define DE_COLOR_G(x) ((de_colorsample)(((x)>>8)&0xff))
828 #define DE_COLOR_B(x) ((de_colorsample)((x)&0xff))
829 #define DE_COLOR_K(x) ((de_colorsample)(((x)>>16)&0xff)) // Gray value. Arbitrarily use the Red channel.
831 #define DE_STOCKCOLOR_BLACK ((de_color)0xff000000U)
832 #define DE_STOCKCOLOR_WHITE ((de_color)0xffffffffU)
833 #define DE_STOCKCOLOR_TRANSPARENT ((de_color)0x00000000U)
835 #define DE_MAKE_RGBA(r,g,b,a) ((((de_color)(a))<<24)|((r)<<16)|((g)<<8)|(b))
836 #define DE_MAKE_RGB(r,g,b) ((((de_color)0xff)<<24)|((r)<<16)|((g)<<8)|(b))
837 #define DE_MAKE_GRAY(k) ((((de_color)0xff)<<24)|((k)<<16)|((k)<<8)|(k))
838 #define DE_SET_ALPHA(v,a) (((v)&0x00ffffff)|(((de_color)(a))<<24))
839 #define DE_MAKE_OPAQUE(v) (((de_color)(v))|0xff000000U)
841 // Return the index'th symbol in the bitmap row beginning at file position rowstart.
842 // A symbol has bps bits. bps must be 1, 2, 4, or 8.
843 u8 de_get_bits_symbol(dbuf *f, i64 bps, i64 rowstart, i64 index);
845 u8 de_get_bits_symbol_lsb(dbuf *f, i64 bps, i64 rowstart, i64 index);
847 u8 de_get_bits_symbol2(dbuf *f, int nbits, i64 bytepos, i64 bitpos);
849 // Conversion flags used by some functions.
850 #define DE_CVTF_WHITEISZERO 0x1
851 #define DE_CVTF_LSBFIRST 0x2
853 // Utility function for the common case of reading a packed bi-level row, and
854 // writing to a bitmap.
855 void de_convert_row_bilevel(dbuf *f, i64 fpos, de_bitmap *img,
856 i64 rownum, unsigned int flags);
858 void de_convert_image_bilevel(dbuf *f, i64 fpos, i64 rowspan,
859 de_bitmap *img, unsigned int flags);
861 void de_convert_and_write_image_bilevel(dbuf *f, i64 fpos,
862 i64 w, i64 h, i64 rowspan, unsigned int cvtflags,
863 de_finfo *fi, unsigned int createflags);
864 void de_convert_and_write_image_bilevel2(dbuf *f, i64 fpos,
865 i64 w, i64 h, i64 rowspan, unsigned int cvtflags,
866 de_finfo *fi, unsigned int createflags);
868 void de_read_palette_rgb(dbuf *f,
869 i64 fpos, i64 num_entries, i64 entryspan,
870 de_color *pal, i64 ncolors_in_pal,
871 unsigned int flags);
873 // Utility function that will work for many of the common kinds of paletted images.
874 void de_convert_image_paletted(dbuf *f, i64 fpos,
875 i64 bpp, i64 rowspan, const de_color *pal,
876 de_bitmap *img, unsigned int flags);
878 void de_convert_image_rgb(dbuf *f, i64 fpos,
879 i64 rowspan, i64 pixelspan, de_bitmap *img, unsigned int flags);
881 i64 de_min_int(i64 n1, i64 n2);
882 i64 de_max_int(i64 n1, i64 n2);
883 i64 de_pad_to_2(i64 x);
884 i64 de_pad_to_4(i64 x);
885 i64 de_pad_to_n(i64 x, i64 n);
886 i64 de_pow2(i64 x);
888 // Calculate the number of bits required to store n symbols.
889 // Intended to be used with bitmap graphics.
890 // Returns a minimum of 1, maximum of 32.
891 i64 de_log2_rounded_up(i64 n);
893 char *de_print_base2_fixed(char *buf, size_t buf_len, u64 n, UI bitcount);
895 // Test if the image dimensions are valid and supported.
896 int de_good_image_dimensions_noerr(deark *c, i64 w, i64 h);
898 // Test if the image dimensions are valid and supported. Report an error if not.
899 int de_good_image_dimensions(deark *c, i64 w, i64 h);
901 // Test if the number of images is sane. Report an error if not.
902 int de_good_image_count(deark *c, i64 n);
904 int de_is_grayscale_palette(const de_color *pal, i64 num_entries);
906 #define DE_BITMAPFLAG_WHITEISTRNS 0x1
907 #define DE_BITMAPFLAG_MERGE 0x2
909 void de_bitmap_flip(de_bitmap *img);
910 void de_bitmap_mirror(de_bitmap *img);
911 void de_bitmap_transpose(de_bitmap *img);
912 void de_bitmap_rect(de_bitmap *img,
913 i64 xpos, i64 ypos, i64 width, i64 height,
914 de_color clr, unsigned int flags);
915 void de_bitmap_copy_rect(de_bitmap *srcimg, de_bitmap *dstimg,
916 i64 srcxpos, i64 srcypos, i64 width, i64 height,
917 i64 dstxpos, i64 dstypos, unsigned int flags);
919 void de_bitmap_apply_mask(de_bitmap *fg, de_bitmap *mask,
920 unsigned int flags);
921 void de_bitmap_remove_alpha(de_bitmap *img);
922 void de_bitmap_optimize_alpha(de_bitmap *img, unsigned int flags);
924 void de_make_grayscale_palette(de_color *pal, i64 num_entries, unsigned int flags);
926 ///////////////////////////////////////////
928 char de_get_hexchar(int n);
929 u8 de_decode_hex_digit(u8 x, int *errorflag);
931 de_color de_palette_vga256(int index);
932 de_color de_palette_ega64(int index);
933 de_color de_palette_pc16(int index);
934 de_color de_palette_pcpaint_cga4(int palnum, int index);
936 const u8 *de_get_8x8ascii_font_ptr(void);
937 const u8 *de_get_vga_cp437_font_ptr(void);
939 void de_color_to_css(de_color color, char *buf, int buflen);
941 u8 de_sample_nbit_to_8bit(i64 n, unsigned int x);
942 u8 de_scale_63_to_255(u8 x);
943 u8 de_scale_1000_to_255(i64 x);
944 u8 de_scale_n_to_255(i64 n, i64 x);
945 de_color de_rgb565_to_888(u32 x);
946 de_color de_bgr555_to_888(u32 x);
947 de_color de_rgb555_to_888(u32 x);
949 void de_encconv_init(struct de_encconv_state *es, de_ext_encoding ee);
950 de_rune de_char_to_unicode_ex(i32 a, struct de_encconv_state *es);
951 de_rune de_char_to_unicode(deark *c, i32 a, de_ext_encoding ee);
952 void de_uchar_to_utf8(de_rune u1, u8 *utf8buf, i64 *p_utf8len);
953 void dbuf_write_uchar_as_utf8(dbuf *outf, de_rune u);
954 int de_utf8_to_uchar(const u8 *utf8buf, i64 buflen,
955 de_rune *p_uchar, i64 *p_utf8len);
956 int de_is_ascii(const u8 *buf, i64 buflen);
958 #define DE_CONVFLAG_STOP_AT_NUL 0x1
959 #define DE_CONVFLAG_MAKE_PRINTABLE 0x2
960 #define DE_CONVFLAG_WANT_UTF8 0x10
961 #define DE_CONVFLAG_ALLOW_HL 0x20
962 #define DE_CONVFLAG_PARTIAL_DATA 0x40
964 char de_byte_to_printable_char(u8 b);
966 // Convert encoded bytes to a NUL-terminated string that can be
967 // printed to the terminal.
968 // Consider using {dbuf_read_to_ucstring or dbuf_read_string or
969 // ucstring_append_bytes} followed by ucstring_getpsz* instead.
970 void de_bytes_to_printable_sz(const u8 *src, i64 src_len,
971 char *dst, i64 dst_len, unsigned int conv_flags, de_ext_encoding src_ee);
973 de_finfo *de_finfo_create(deark *c);
974 void de_finfo_destroy(deark *c, de_finfo *fi);
976 #define DE_SNFLAG_FULLPATH 0x01
977 #define DE_SNFLAG_STRIPTRAILINGSLASH 0x2
978 void de_finfo_set_name_from_ucstring(deark *c, de_finfo *fi, de_ucstring *s, unsigned int flags);
979 void de_finfo_set_name_from_sz(deark *c, de_finfo *fi, const char *name1, unsigned int flags,
980 de_ext_encoding ee);
982 de_ucstring *ucstring_create(deark *c);
983 de_ucstring *ucstring_clone(const de_ucstring *src);
984 void ucstring_destroy(de_ucstring *s);
985 void ucstring_empty(de_ucstring *s);
986 void ucstring_truncate(de_ucstring *s, i64 newlen);
987 void ucstring_truncate_at_NUL(de_ucstring *s);
988 void ucstring_strip_trailing_NUL(de_ucstring *s);
989 void ucstring_strip_trailing_spaces(de_ucstring *s);
990 void ucstring_append_char(de_ucstring *s, de_rune ch);
991 void ucstring_append_ucstring(de_ucstring *s1, const de_ucstring *s2);
992 void ucstring_vprintf(de_ucstring *s, de_ext_encoding ee, const char *fmt, va_list ap);
993 void ucstring_printf(de_ucstring *s, de_ext_encoding ee, const char *fmt, ...)
994 de_gnuc_attribute ((format (printf, 3, 4)));
995 int ucstring_isempty(const de_ucstring *s);
996 int ucstring_isnonempty(const de_ucstring *s);
998 // Convert and append an encoded array of bytes to the string.
999 void ucstring_append_bytes(de_ucstring *s, const u8 *buf, i64 buflen,
1000 unsigned int conv_flags, de_ext_encoding ee);
1001 void ucstring_append_bytes_ex(de_ucstring *s, const u8 *buf, i64 buflen,
1002 unsigned int conv_flags, struct de_encconv_state *es);
1004 void ucstring_append_sz(de_ucstring *s, const char *sz, de_ext_encoding ee);
1006 void ucstring_write_as_utf8(deark *c, de_ucstring *s, dbuf *outf, int add_bom_if_needed);
1007 int de_is_printable_uchar(de_rune ch);
1008 i64 ucstring_count_utf8_bytes(de_ucstring *s);
1010 // Supported encodings are DE_ENCODING_UTF8, DE_ENCODING_ASCII, DE_ENCODING_LATIN1.
1011 // flags: DE_CONVFLAG_*
1012 void ucstring_to_sz(de_ucstring *s, char *szbuf, size_t szbuf_len, unsigned int flags,
1013 de_ext_encoding ee);
1015 // "get printable string"
1016 // Returns a pointer to a NUL-terminated string, that is valid until the
1017 // next ucstring_* function is called on that string.
1018 const char *ucstring_getpsz(de_ucstring *s);
1019 // The _n version limits the number of bytes in the result.
1020 // max_bytes does not count the terminating NUL.
1021 const char *ucstring_getpsz_n(de_ucstring *s, i64 max_bytes);
1023 #define DE_DBG_MAX_STRLEN 500
1024 // Same as ..._n, with max_bytes=DE_DBG_MAX_STRLEN
1025 const char *ucstring_getpsz_d(de_ucstring *s);
1027 // Helper functions for printing the contents of bit-flags fields
1028 void ucstring_append_flags_item(de_ucstring *s, const char *str);
1029 void ucstring_append_flags_itemf(de_ucstring *s, const char *fmt, ...)
1030 de_gnuc_attribute ((format (printf, 2, 3)));
1032 struct de_strarray;
1033 struct de_strarray *de_strarray_create(deark *c, size_t max_elems);
1034 void de_strarray_destroy(struct de_strarray *sa);
1035 int de_strarray_push(struct de_strarray *sa, de_ucstring *s);
1036 int de_strarray_pop(struct de_strarray *sa);
1037 #define DE_MPFLAG_NOTRAILINGSLASH 0x1
1038 void de_strarray_make_path(struct de_strarray *sa, de_ucstring *path, unsigned int flags);
1040 void de_write_codepoint_to_html(deark *c, dbuf *f, de_rune ch);
1042 de_encoding de_encoding_name_to_code(const char *encname);
1043 de_encoding de_windows_codepage_to_encoding(deark *c, int wincodepage,
1044 char *encname, size_t encname_len, unsigned int flags);
1046 void de_copy_bits(const u8 *src, i64 srcbitnum,
1047 u8 *dst, i64 dstbitnum, i64 bitstocopy);
1049 void de_decode_base16(deark *c, dbuf *inf, i64 pos1, i64 len,
1050 dbuf *outf, unsigned int flags);
1052 struct de_inthashtable;
1053 struct de_inthashtable *de_inthashtable_create(deark *c);
1054 void de_inthashtable_destroy(deark *c, struct de_inthashtable *ht);
1055 int de_inthashtable_add_item(deark *c, struct de_inthashtable *ht, i64 key, void *value);
1056 int de_inthashtable_get_item(deark *c, struct de_inthashtable *ht, i64 key, void **pvalue);
1057 int de_inthashtable_item_exists(deark *c, struct de_inthashtable *ht, i64 key);
1058 int de_inthashtable_remove_item(deark *c, struct de_inthashtable *ht, i64 key, void **pvalue);
1059 int de_inthashtable_remove_any_item(deark *c, struct de_inthashtable *ht, i64 *pkey, void **pvalue);
1061 #define DE_CRCOBJ_CRC32_IEEE 0x10
1062 #define DE_CRCOBJ_ADLER32 0x11
1063 #define DE_CRCOBJ_CRC16_CCITT 0x20
1064 #define DE_CRCOBJ_CRC16_ARC 0x21
1066 struct de_crcobj;
1068 struct de_crcobj *de_crcobj_create(deark *c, UI type_and_flags);
1069 void de_crcobj_destroy(struct de_crcobj *crco);
1070 void de_crcobj_reset(struct de_crcobj *crco);
1071 u32 de_crcobj_getval(struct de_crcobj *crco);
1072 void de_crcobj_addbuf(struct de_crcobj *crco, const u8 *buf, i64 buf_len);
1073 void de_crcobj_addzeroes(struct de_crcobj *crco, i64 len);
1074 void de_crcobj_addslice(struct de_crcobj *crco, dbuf *f, i64 pos, i64 len);
1076 ///////////////////////////////////////////
1078 struct de_bitmap_font_char {
1079 i32 codepoint_nonunicode;
1081 // If font->has_unicode_codepoints is set, then ->codepoint_unicode
1082 // must be set to a Unicode codepoint, or to DE_INVALID_CODEPOINT.
1083 de_rune codepoint_unicode;
1085 int width, height;
1086 int v_offset; // Used if the glyphs do not all have the same height
1087 i16 extraspace_l, extraspace_r;
1088 i64 rowspan;
1089 u8 *bitmap;
1092 struct de_bitmap_font {
1093 int nominal_width, nominal_height;
1094 i64 index_of_replacement_char; // -1 if none
1096 // Flag: Are the char_array[]->codepoint_nonunicode codes set?
1097 // (This should be ignored if has_unicode_codepoints is not set.)
1098 u8 has_nonunicode_codepoints;
1100 // Flag: Are the char_array[]->codepoint_unicode codes set?
1101 u8 has_unicode_codepoints;
1103 // If the font has both unicode and non-unicode codpoints, this flag tells which
1104 // to prefer when displaying the font.
1105 u8 prefer_unicode;
1107 i64 num_chars;
1108 struct de_bitmap_font_char *char_array;
1111 struct de_bitmap_font *de_create_bitmap_font(deark *c);
1112 void de_destroy_bitmap_font(deark *c, struct de_bitmap_font *font);
1114 #define DE_PAINTFLAG_TRNSBKGD 0x01
1115 #define DE_PAINTFLAG_VGA9COL 0x02 // Render an extra column, like VGA does
1116 #define DE_PAINTFLAG_LEFTHALF 0x04 // Note: The "HALF" flags must fit into a byte,
1117 #define DE_PAINTFLAG_RIGHTHALF 0x08 // because they are stored in de_char_cell::size_flags.
1118 #define DE_PAINTFLAG_TOPHALF 0x10
1119 #define DE_PAINTFLAG_BOTTOMHALF 0x20
1120 void de_font_paint_character_idx(deark *c, de_bitmap *img,
1121 struct de_bitmap_font *font, i64 char_idx,
1122 i64 xpos, i64 ypos, de_color fgcol, de_color bgcol, unsigned int flags);
1123 void de_font_paint_character_cp(deark *c, de_bitmap *img,
1124 struct de_bitmap_font *font, i32 codepoint,
1125 i64 xpos, i64 ypos, de_color fgcol, de_color bgcol, unsigned int flags);
1127 void de_font_bitmap_font_to_image(deark *c, struct de_bitmap_font *font, de_finfo *fi, unsigned int createflags);
1128 int de_font_is_standard_vga_font(deark *c, u32 crc);
1130 ///////////////////////////////////////////
1132 // Note that this struct is assumed to be copyable with a simple struct copy.
1133 // It should not contain pointers.
1134 struct de_char_cell {
1135 i32 codepoint;
1136 de_rune codepoint_unicode;
1137 // The color fields are interpreted as follows:
1138 // A color value <=0x0000000f is a palette index.
1139 // A color value >=0xff000000 is an RGB color, e.g. from DE_MAKE_RGB().
1140 #define DE_IS_PAL_COLOR(x) ((u32)(x)<=0xfU)
1141 de_color fgcol;
1142 de_color bgcol;
1143 u8 underline;
1144 u8 strikethru;
1145 u8 blink;
1146 u8 size_flags;
1149 struct de_char_screen {
1150 i64 width;
1151 i64 height;
1152 struct de_char_cell **cell_rows; // Array of [height] row pointers
1155 struct de_char_context {
1156 u8 prefer_image_output;
1157 u8 prefer_9col_mode;
1158 u8 no_density;
1159 u8 suppress_custom_font_warning;
1160 u8 outfmt_known;
1161 int outfmt;
1162 i64 nscreens;
1163 struct de_char_screen **screens; // Array of [nscreens] screens
1164 de_color pal[16];
1165 struct de_bitmap_font *font; // Optional
1166 de_ucstring *title;
1167 de_ucstring *artist;
1168 de_ucstring *organization;
1169 struct de_timestamp creation_date;
1170 de_ucstring *comment; // NULL if there is no comment
1173 void de_char_output_to_file(deark *c, struct de_char_context *charctx);
1174 struct de_char_context *de_create_charctx(deark *c, unsigned int flags);
1175 void de_char_decide_output_format(deark *c, struct de_char_context *charctx);
1176 void de_destroy_charctx(deark *c, struct de_char_context *charctx);
1177 void de_free_charctx_screens(deark *c, struct de_char_context *charctx);
1178 void de_free_charctx(deark *c, struct de_char_context *charctx);
1180 ///////////////////////////////////////////
1182 // Our version of "struct tm".
1183 // Differences: Year is full year, removed some fields, added milliseconds field.
1184 struct de_struct_tm {
1185 int is_valid;
1186 int tm_fullyear, tm_mon, tm_mday;
1187 int tm_hour, tm_min, tm_sec;
1188 int tm_subsec; // in ten-millionths of a second
1191 void de_unix_time_to_timestamp(i64 ut, struct de_timestamp *ts, unsigned int flags);
1192 void de_mac_time_to_timestamp(i64 mt, struct de_timestamp *ts);
1193 void de_FILETIME_to_timestamp(i64 ft, struct de_timestamp *ts, unsigned int flags);
1194 void de_dos_datetime_to_timestamp(struct de_timestamp *ts,
1195 i64 ddate, i64 dtime);
1196 void de_describe_dos_attribs(deark *c, UI attr, de_ucstring *s, UI flags);
1197 void de_riscos_loadexec_to_timestamp(u32 load_addr,
1198 u32 exec_addr, struct de_timestamp *ts);
1199 void de_timestamp_set_subsec(struct de_timestamp *ts, double frac);
1200 i64 de_timestamp_get_subsec(const struct de_timestamp *ts);
1201 i64 de_timestamp_to_unix_time(const struct de_timestamp *ts);
1202 i64 de_timestamp_to_FILETIME(const struct de_timestamp *ts);
1203 void de_make_timestamp(struct de_timestamp *ts,
1204 i64 yr, i64 mo, i64 da,
1205 i64 hr, i64 mi, i64 se);
1206 void de_timestamp_cvt_to_utc(struct de_timestamp *ts, i64 offset_seconds);
1207 char *de_timestamp_to_string(const struct de_timestamp *ts,
1208 char *buf, size_t buf_len, unsigned int flags);
1209 char *de_dbg_timestamp_to_string(deark *c, const struct de_timestamp *ts,
1210 char *buf, size_t buf_len, unsigned int flags);
1211 void de_gmtime(const struct de_timestamp *ts, struct de_struct_tm *tm2);
1212 void de_current_time_to_timestamp(struct de_timestamp *ts);
1213 void de_cached_current_time_to_timestamp(deark *c, struct de_timestamp *ts);