Refactored the convert_row_bilevel function
[deark.git] / src / deark-private.h
blob990d6026211a19c0e47998b424cd789cc1427185
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;
220 u8 has_riscos_data; // attribs, load_addr, exec_addr
222 #define DE_MODEFLAG_NONEXE 0x01 // Make the output file non-executable.
223 #define DE_MODEFLAG_EXE 0x02 // Make the output file executable.
224 unsigned int mode_flags;
226 #define DE_TIMESTAMPIDX_MODIFY 0 // External timestamps...
227 #define DE_TIMESTAMPIDX_CREATE 1
228 #define DE_TIMESTAMPIDX_ACCESS 2
229 #define DE_TIMESTAMPIDX_ATTRCHANGE 3
230 #define DE_TIMESTAMPIDX_BACKUP 4
231 #define DE_TIMESTAMPIDX_COUNT 5
232 struct de_timestamp timestamp[DE_TIMESTAMPIDX_COUNT];
234 struct de_timestamp internal_mod_time; // E.g. for PNG tIME chunk
235 struct de_density_info density;
236 de_ucstring *name_other; // Modules can use this field as needed.
237 int hotspot_x, hotspot_y; // Measured from upper-left pixel (after handling 'flipped')
238 u32 riscos_attribs;
239 u32 load_addr, exec_addr;
242 struct deark_bitmap_struct {
243 deark *c;
244 i64 width;
245 i64 height;
246 i64 unpadded_width;
247 int invalid_image_flag;
248 int bytes_per_pixel;
249 u8 *bitmap;
250 i64 bitmap_size; // bytes allocated for bitmap
251 int orig_colortype; // Optional; can be used by modules
252 int orig_bitdepth; // Optional; can be used by modules
254 typedef struct deark_bitmap_struct de_bitmap;
256 struct de_SAUCE_detection_data {
257 u8 has_SAUCE;
258 u8 data_type;
259 u8 file_type;
262 struct de_ID3_detection_data {
263 u8 detection_attempted;
264 u8 has_id3v2;
265 u32 bytes_at_start;
268 // This struct is a crude way for data to be shared by the various format
269 // identification functions. It generally should not be used outside of them --
270 // but it can be, provided it's only used as a cache.
271 struct de_detection_data_struct {
272 int best_confidence_so_far;
273 u8 is_macbinary;
274 u8 SAUCE_detection_attempted;
275 u8 zip_eocd_looked_for;
276 u8 zip_eocd_found;
277 i64 zip_eocd_pos; // valid if zip_eocd_found
278 struct de_SAUCE_detection_data sauce;
279 struct de_ID3_detection_data id3;
282 struct de_module_in_params {
283 const char *codes;
284 // Module-specific fields:
285 u32 flags;
286 de_encoding input_encoding;
287 u32 uint1;
288 i64 offset_in_parent;
289 dbuf *parent_dbuf;
290 de_finfo *fi;
293 struct de_module_out_params {
294 // Fields are module-specific.
295 u32 flags;
296 u32 uint1;
297 u32 uint2;
298 u32 uint3;
299 u32 uint4;
300 i64 int64_1;
301 // The caller is responsible for freeing pointer fields.
302 // The callee should not use these fields unless requested.
303 de_finfo *fi;
304 void *obj1;
307 struct de_module_params_struct {
308 struct de_module_in_params in_params;
309 struct de_module_out_params out_params;
312 struct deark_ext_option {
313 char *name;
314 char *val;
317 typedef void (*de_module_register_fn_type)(deark *c);
319 enum de_moddisp_enum {
320 DE_MODDISP_NONE = 0, // No active module, or unknown
321 DE_MODDISP_AUTODETECT, // Format was autodetected
322 DE_MODDISP_EXPLICIT, // User used -m to select the module
323 DE_MODDISP_INTERNAL // Another module is using this module
326 struct deark_struct {
327 int debug_level;
328 void *userdata;
330 ////////////////////////////////////////////////////
331 int module_nesting_level;
333 // Data specific to the current module.
335 // TODO: There really ought to be a stack of standard-module-local data
336 // objects, but that may be more trouble than it's worth.
337 // For now, we just need to use caution when changing these fields.
339 // The current primary input file.
340 // Modules may change this, provided they change it back when they're done.
341 dbuf *infile;
343 // A flag to remember whether we've printed the specific format of the
344 // top-level file.
345 int format_declared;
347 enum de_moddisp_enum module_disposition; // Why are we using this module?
349 // Always valid during identify(); can be NULL during run().
350 struct de_detection_data_struct *detection_data;
351 ////////////////////////////////////////////////////
353 int file_count; // The number of extractable files encountered so far.
355 // The number of files we've actually written (or listed), after taking
356 // first_output_file/max_output_files into account.
357 int num_files_extracted;
359 i64 total_output_size;
360 int error_count;
361 u8 serious_error_flag;
363 const char *input_filename;
364 const char *input_format_req; // Format requested
365 const char *modcodes_req;
366 i64 slice_start_req; // Used if we're only to look at part of the file.
367 i64 slice_size_req;
368 int slice_size_req_valid;
369 int suppress_detection_by_filename;
371 int output_style; // DE_OUTPUTSTYLE_*
372 int archive_fmt; // If output_style==DE_OUTPUTSTYLE_ARCHIVE
373 int input_style; // DE_INPUTSTYLE_*
374 u8 archive_to_stdout;
375 u8 allow_subdirs;
377 int extract_policy; // DE_EXTRACTPOLICY_*
378 int extract_level;
379 u8 list_mode;
380 u8 list_mode_include_file_id;
381 int first_output_file; // first file = 0
382 int max_output_files;
383 u8 user_set_max_output_files;
384 i64 max_image_dimension;
385 i64 max_output_file_size;
386 i64 max_total_output_size;
387 int show_infomessages;
388 int show_warnings;
389 int dbg_indent_amount;
390 u8 write_bom;
391 u8 write_density;
392 u8 ascii_html;
393 u8 keep_dir_entries;
394 u8 filenames_from_file;
395 u8 macformat_known;
396 u8 macformat;
397 u8 padpix;
398 int overwrite_mode;
399 u8 preserve_file_times;
400 u8 preserve_file_times_archives;
401 u8 preserve_file_times_internal;
402 u8 reproducible_output;
403 struct de_timestamp reproducible_timestamp;
404 int can_decode_fltpt;
405 int host_is_le;
406 u8 identify_only;
407 u8 modhelp_req;
408 de_encoding input_encoding;
409 i64 input_tz_offs_seconds;
411 de_msgfn_type msgfn; // Caller's message output function
412 de_specialmsgfn_type specialmsgfn;
413 de_fatalerrorfn_type fatalerrorfn;
414 const char *dprefix;
416 u8 deflate_decoder_id;
417 u8 tmpflag1;
418 u8 tmpflag2;
419 u8 pngcprlevel_valid;
420 unsigned int pngcmprlevel;
421 void *zip_data;
422 void *tar_data;
423 dbuf *extrlist_dbuf;
425 char *base_output_filename;
426 char *special_1st_filename;
427 char *output_archive_filename;
428 char *extrlist_filename;
430 const char *onlymods_string;
431 const char *disablemods_string;
432 const char *onlydetectmods_string;
433 const char *nodetectmods_string;
435 struct de_timestamp current_time;
437 de_module_register_fn_type module_register_fn;
439 int num_modules;
440 struct deark_module_info *module_info; // Pointer to an array
442 #define DE_MAX_EXT_OPTIONS 16
443 int num_ext_options;
444 struct deark_ext_option ext_option[DE_MAX_EXT_OPTIONS];
447 void de_fatalerror(deark *c);
448 void de_internal_err_fatal(deark *c, const char *fmt, ...)
449 de_gnuc_attribute ((format (printf, 2, 3)));
450 void de_internal_err_nonfatal(deark *c, const char *fmt, ...)
451 de_gnuc_attribute ((format (printf, 2, 3)));
453 deark *de_create_internal(void);
454 int de_run_module(deark *c, struct deark_module_info *mi, de_module_params *mparams,
455 enum de_moddisp_enum moddisp);
456 int de_run_module_by_id(deark *c, const char *id, de_module_params *mparams);
457 int de_run_module_by_id_on_slice(deark *c, const char *id, de_module_params *mparams,
458 dbuf *f, i64 pos, i64 len);
459 int de_run_module_by_id_on_slice2(deark *c, const char *id, const char *codes,
460 dbuf *f, i64 pos, i64 len);
461 int de_get_module_idx_by_id(deark *c, const char *module_id);
462 struct deark_module_info *de_get_module_by_id(deark *c, const char *module_id);
464 void de_strlcpy(char *dst, const char *src, size_t dstlen);
465 char *de_strchr(const char *s, int c);
466 #define de_strlen strlen
467 #define de_strcmp strcmp
468 #define de_strncmp strncmp
469 #define de_memcmp memcmp
470 #define de_memcpy memcpy
471 #define de_memmove memmove
472 #define de_memset memset
473 #define de_zeromem(a,b) memset((a),0,(b))
474 #define de_memchr memchr
475 #ifdef DE_WINDOWS
476 #define de_sscanf sscanf_s
477 #else
478 #define de_sscanf sscanf
479 #endif
480 #define de_strtod strtod
482 // de_dbg*, de_msg, de_warn, de_err: The output is a single line, to which a
483 // standard prefix like "Warning: " may be added. A newline will be added
484 // automatically.
485 // [For other output functions, see de_puts, de_printf (deark.h).]
487 void de_dbg(deark *c, const char *fmt, ...)
488 de_gnuc_attribute ((format (printf, 2, 3)));
489 void de_dbg2(deark *c, const char *fmt, ...)
490 de_gnuc_attribute ((format (printf, 2, 3)));
491 void de_dbg3(deark *c, const char *fmt, ...)
492 de_gnuc_attribute ((format (printf, 2, 3)));
493 void de_dbgx(deark *c, int lv, const char *fmt, ...)
494 de_gnuc_attribute ((format (printf, 3, 4)));
495 void de_info(deark *c, const char *fmt, ...)
496 de_gnuc_attribute ((format (printf, 2, 3)));
497 void de_msg(deark *c, const char *fmt, ...)
498 de_gnuc_attribute ((format (printf, 2, 3)));
499 void de_vwarn(deark *c, const char *fmt, va_list ap);
500 void de_warn(deark *c, const char *fmt, ...)
501 de_gnuc_attribute ((format (printf, 2, 3)));
502 void de_verr(deark *c, const char *fmt, va_list ap);
503 void de_err(deark *c, const char *fmt, ...)
504 de_gnuc_attribute ((format (printf, 2, 3)));
506 FILE* de_fopen_for_read(deark *c, const char *fn, i64 *len,
507 char *errmsg, size_t errmsg_len, unsigned int *returned_flags);
508 FILE* de_fopen_for_write(deark *c, const char *fn,
509 char *errmsg, size_t errmsg_len, int overwrite_mode,
510 unsigned int flags);
511 int de_fseek(FILE *fp, i64 offs, int whence);
512 i64 de_ftell(FILE *fp);
513 int de_fclose(FILE *fp);
514 void de_update_file_attribs(dbuf *f, u8 preserve_file_times);
516 void de_declare_fmt(deark *c, const char *fmtname);
517 void de_declare_fmtf(deark *c, const char *fmt, ...)
518 de_gnuc_attribute ((format (printf, 2, 3)));
519 de_encoding de_get_input_encoding(deark *c, de_module_params *mparams,
520 de_encoding dflt);
522 void de_dbg_indent(deark *c, int n);
523 void de_dbg_indent_save(deark *c, int *saved_indent_level);
524 void de_dbg_indent_restore(deark *c, int saved_indent_level);
525 void de_dbg_hexdump(deark *c, dbuf *f, i64 pos1, i64 nbytes_avail,
526 i64 max_nbytes_to_dump, const char *prefix, unsigned int flags);
527 void de_hexdump2(deark *c, dbuf *f, i64 pos1, i64 nbytes_avail,
528 i64 max_nbytes_to_dump, unsigned int flags);
529 void de_dbg_dimensions(deark *c, i64 w, i64 h);
530 void de_dbg_pal_entry(deark *c, i64 idx, de_color clr);
531 void de_dbg_pal_entry2(deark *c, i64 idx, de_color clr,
532 const char *txt_before, const char *txt_in, const char *txt_after);
533 char *de_get_colorsample_code(deark *c, de_color clr, char *csamp,
534 size_t csamplen);
536 const char *de_get_ext_option(deark *c, const char *name);
537 int de_get_ext_option_bool(deark *c, const char *name, int defaultval);
539 ///////////////////////////////////////////
541 const char *de_get_sz_ext(const char *sz);
542 int de_sz_has_ext(const char *sz, const char *ext);
543 const char *de_get_input_file_ext(deark *c);
544 int de_input_file_has_ext(deark *c, const char *ext);
545 int de_havemodcode(deark *c, de_module_params *mparams, int code);
547 ///////////////////////////////////////////
549 int de_archive_initialize(deark *c);
550 void de_get_reproducible_timestamp(deark *c, struct de_timestamp *ts);
552 int de_tar_create_file(deark *c);
553 void de_tar_start_member_file(deark *c, dbuf *f);
554 void de_tar_end_member_file(deark *c, dbuf *f);
555 void de_tar_close_file(deark *c);
557 ///////////////////////////////////////////
559 int de_zip_create_file(deark *c);
560 void de_zip_add_file_to_archive(deark *c, dbuf *f);
561 void de_zip_close_file(deark *c);
563 int de_write_png(deark *c, de_bitmap *img, dbuf *f, UI createflags);
565 ///////////////////////////////////////////
567 i64 de_geti8_direct(const u8 *m);
568 i64 de_getu16be_direct(const u8 *m);
569 i64 de_getu16le_direct(const u8 *m);
570 i64 de_getu32be_direct(const u8 *m);
571 i64 de_getu32le_direct(const u8 *m);
572 i64 de_geti64be_direct(const u8 *m);
573 i64 de_geti64le_direct(const u8 *m);
574 u64 de_getu64be_direct(const u8 *m);
575 u64 de_getu64le_direct(const u8 *m);
577 void dbuf_read(dbuf *f, u8 *buf, i64 pos, i64 len);
578 i64 dbuf_standard_read(dbuf *f, u8 *buf, i64 n, i64 *fpos);
580 u8 dbuf_getbyte(dbuf *f, i64 pos);
581 i64 dbuf_geti8(dbuf *f, i64 pos);
582 i64 dbuf_getu16be(dbuf *f, i64 pos);
583 i64 dbuf_getu16le(dbuf *f, i64 pos);
584 i64 dbuf_getu16x(dbuf *f, i64 pos, int is_le);
585 i64 dbuf_geti16be(dbuf *f, i64 pos);
586 i64 dbuf_geti16le(dbuf *f, i64 pos);
587 i64 dbuf_geti16x(dbuf *f, i64 pos, int is_le);
588 i64 dbuf_getu32be(dbuf *f, i64 pos);
589 i64 dbuf_getu32le(dbuf *f, i64 pos);
590 i64 dbuf_getu32x(dbuf *f, i64 pos, int is_le);
591 i64 dbuf_geti32be(dbuf *f, i64 pos);
592 i64 dbuf_geti32le(dbuf *f, i64 pos);
593 i64 dbuf_geti32x(dbuf *f, i64 pos, int is_le);
594 i64 dbuf_geti64be(dbuf *f, i64 pos);
595 i64 dbuf_geti64le(dbuf *f, i64 pos);
596 i64 dbuf_geti64x(dbuf *f, i64 pos, int is_le);
597 u64 dbuf_getu64be(dbuf *f, i64 pos);
598 u64 dbuf_getu64le(dbuf *f, i64 pos);
599 u64 dbuf_getu64x(dbuf *f, i64 pos, int is_le);
601 i64 dbuf_getint_ext(dbuf *f, i64 pos, unsigned int nbytes,
602 int is_le, int is_signed);
604 // The _p functions update a caller-supplied position.
605 u8 dbuf_getbyte_p(dbuf *f, i64 *ppos);
606 i64 dbuf_getu16be_p(dbuf *f, i64 *ppos);
607 i64 dbuf_getu16le_p(dbuf *f, i64 *ppos);
608 i64 dbuf_getu32le_p(dbuf *f, i64 *ppos);
609 i64 dbuf_getu32be_p(dbuf *f, i64 *ppos);
610 i64 dbuf_geti16be_p(dbuf *f, i64 *ppos);
611 i64 dbuf_geti16le_p(dbuf *f, i64 *ppos);
612 i64 dbuf_geti32be_p(dbuf *f, i64 *ppos);
613 i64 dbuf_geti32le_p(dbuf *f, i64 *ppos);
615 // Only format modules should use these convenience macros.
616 // (The DE_WINDOWS condition has no functional purpose; it's a hack to make
617 // some development tools work better.)
618 #if !defined(DE_NOT_IN_MODULE) || defined(DE_WINDOWS)
619 #define de_read(b,p,l) dbuf_read(c->infile,b,p,l);
620 #define de_getbyte(p) dbuf_getbyte(c->infile,p)
621 #define de_getu16be(p) dbuf_getu16be(c->infile,p)
622 #define de_getu16le(p) dbuf_getu16le(c->infile,p)
623 #define de_geti16be(p) dbuf_geti16be(c->infile,p)
624 #define de_geti16le(p) dbuf_geti16le(c->infile,p)
625 #define de_getu32be(p) dbuf_getu32be(c->infile,p)
626 #define de_getu32le(p) dbuf_getu32le(c->infile,p)
627 #define de_geti32be(p) dbuf_geti32be(c->infile,p)
628 #define de_geti32le(p) dbuf_geti32le(c->infile,p)
629 #define de_geti64be(p) dbuf_geti64be(c->infile,p)
630 #define de_geti64le(p) dbuf_geti64le(c->infile,p)
631 #define de_getbyte_p(p) dbuf_getbyte_p(c->infile,p)
632 #define de_getu16be_p(p) dbuf_getu16be_p(c->infile,p)
633 #define de_getu16le_p(p) dbuf_getu16le_p(c->infile,p)
634 #define de_getu32be_p(p) dbuf_getu32be_p(c->infile,p)
635 #define de_getu32le_p(p) dbuf_getu32le_p(c->infile,p)
636 #define de_geti16be_p(p) dbuf_geti16be_p(c->infile,p)
637 #define de_geti16le_p(p) dbuf_geti16le_p(c->infile,p)
638 #define de_geti32be_p(p) dbuf_geti32be_p(c->infile,p)
639 #define de_geti32le_p(p) dbuf_geti32le_p(c->infile,p)
640 #endif
642 // Read IEEE 754 floating point
643 double de_getfloat32x_direct(deark *c, const u8 *m, int is_le);
644 double dbuf_getfloat32x(dbuf *f, i64 pos, int is_le);
645 double de_getfloat64x_direct(deark *c, const u8 *m, int is_le);
646 double dbuf_getfloat64x(dbuf *f, i64 pos, int is_le);
648 int dbuf_read_ascii_number(dbuf *f, i64 pos, i64 fieldsize,
649 int base, i64 *value);
651 #define DE_GETRGBFLAG_BGR 0x1 // Assume BGR order instead of RGB
652 de_color dbuf_getRGB(dbuf *f, i64 pos, unsigned int flags);
654 // Convert and append encoded bytes from a dbuf to a ucstring.
655 // (see also ucstring_append_*)
656 void dbuf_read_to_ucstring_ex(dbuf *f, i64 pos, i64 len,
657 de_ucstring *s, unsigned int conv_flags, struct de_encconv_state *es);
658 void dbuf_read_to_ucstring(dbuf *f, i64 pos, i64 len,
659 de_ucstring *s, unsigned int conv_flags, de_ext_encoding encoding);
660 // The _n version has an extra max_len field, for convenience.
661 void dbuf_read_to_ucstring_n(dbuf *f, i64 pos, i64 len, i64 max_len,
662 de_ucstring *s, unsigned int conv_flags, de_ext_encoding encoding);
664 // At least one of 'ext' or 'fi' should be non-NULL.
665 #define DE_CREATEFLAG_IS_AUX 0x1
666 #define DE_CREATEFLAG_OPT_IMAGE 0x2
667 #define DE_CREATEFLAG_FLIP_IMAGE 0x4
668 dbuf *dbuf_create_output_file(deark *c, const char *ext, de_finfo *fi, unsigned int createflags);
670 dbuf *dbuf_create_unmanaged_file(deark *c, const char *fname, int overwrite_mode, unsigned int flags);
671 dbuf *dbuf_create_unmanaged_file_stdout(deark *c, const char *name);
672 dbuf *dbuf_open_input_file(deark *c, const char *fn);
673 dbuf *dbuf_open_input_stdin(deark *c);
674 dbuf *dbuf_open_input_subfile(dbuf *parent, i64 offset, i64 size);
675 dbuf *dbuf_create_custom_dbuf(deark *c, i64 apparent_size, unsigned int flags);
677 // Flag:
678 // 0x1: Set the maximum size to the 'initialsize'
679 dbuf *dbuf_create_membuf(deark *c, i64 initialsize, unsigned int flags);
681 // If f is NULL, this is a no-op.
682 void dbuf_close(dbuf *f);
684 void dbuf_set_writelistener(dbuf *f, de_writelistener_cb_type fn, void *userdata);
686 void dbuf_write(dbuf *f, const u8 *m, i64 len);
687 void dbuf_write_at(dbuf *f, i64 pos, const u8 *m, i64 len);
688 void dbuf_write_zeroes(dbuf *f, i64 len);
689 void dbuf_truncate(dbuf *f, i64 len);
690 void dbuf_write_run(dbuf *f, u8 n, i64 len);
692 void de_writeu16le_direct(u8 *m, i64 n);
693 void de_writeu16be_direct(u8 *m, i64 n);
694 void de_writeu32le_direct(u8 *m, i64 n);
695 void de_writeu32be_direct(u8 *m, i64 n);
696 void de_writeu64le_direct(u8 *m, u64 n);
697 void dbuf_writebyte(dbuf *f, u8 n);
698 void dbuf_writebyte_at(dbuf *f, i64 pos, u8 n);
699 void dbuf_writeu16le(dbuf *f, i64 n);
700 void dbuf_writeu16be(dbuf *f, i64 n);
701 void dbuf_writei16le(dbuf *f, i64 n);
702 void dbuf_writei16be(dbuf *f, i64 n);
703 void dbuf_writeu32le(dbuf *f, i64 n);
704 void dbuf_writeu32be(dbuf *f, i64 n);
705 void dbuf_writei32le(dbuf *f, i64 n);
706 void dbuf_writei32be(dbuf *f, i64 n);
707 void dbuf_writeu64le(dbuf *f, u64 n);
709 void dbuf_puts(dbuf *f, const char *sz);
710 void dbuf_printf(dbuf *f, const char *fmt, ...)
711 de_gnuc_attribute ((format (printf, 2, 3)));
712 void dbuf_flush(dbuf *f);
714 // Read a slice of one dbuf, and append it to another dbuf.
715 void dbuf_copy(dbuf *inf, i64 input_offset, i64 input_len, dbuf *outf);
716 void dbuf_copy_at(dbuf *inf, i64 input_offset, i64 input_len, dbuf *outf, i64 outpos);
718 struct de_stringreaderdata {
719 // The number of bytes used by the string in the file (ie includes trailing NUL),
720 // even if they aren't all stored in ->sz.
721 i64 bytes_consumed;
723 char *sz; // Stores some or all of the bytes read. Always NUL terminated.
724 size_t sz_strlen;
725 de_ucstring *str; // Unicode version of ->sz
726 char *sz_utf8; // UTF-8 version of ->str (+ NUL terminator) (optional)
727 size_t sz_utf8_strlen;
728 int was_truncated;
729 int found_nul;
732 struct de_stringreaderdata *dbuf_read_string(dbuf *f, i64 pos,
733 i64 max_bytes_to_scan, i64 max_bytes_to_keep,
734 unsigned int flags, de_ext_encoding ee);
735 void de_destroy_stringreaderdata(deark *c, struct de_stringreaderdata *srd);
737 // Compare bytes in a dbuf to s.
738 // Note that repeatedly comparing the same dbuf bytes might be inefficient.
739 int dbuf_memcmp(dbuf *f, i64 pos, const void *s, size_t n);
741 // Read a slice of a dbuf, and create a new file containing only that.
742 // At least one of 'ext' or 'fi' should be non-NULL.
743 int dbuf_create_file_from_slice(dbuf *inf, i64 pos, i64 data_size,
744 const char *ext, de_finfo *fi, unsigned int createflags);
746 int dbuf_has_utf8_bom(dbuf *f, i64 pos);
748 int dbuf_dump_to_file(dbuf *inf, const char *fn);
750 // Remove everything from the dbuf.
751 // May be valid only for memory buffers.
752 void dbuf_empty(dbuf *f);
754 void dbuf_set_length_limit(dbuf *f, i64 max_len);
755 const u8 *dbuf_get_membuf_direct_ptr(dbuf *f);
756 int dbuf_search_byte(dbuf *f, const u8 b, i64 startpos, i64 haystack_len,
757 i64 *foundpos);
758 int dbuf_search(dbuf *f, const u8 *needle, i64 needle_len, i64 startpos,
759 i64 haystack_len, i64 *foundpos);
760 int dbuf_get_utf16_NULterm_len(dbuf *f, i64 pos1, i64 bytes_avail,
761 i64 *bytes_consumed);
762 int dbuf_find_line(dbuf *f, i64 pos1, i64 *pcontent_len, i64 *ptotal_len);
764 struct de_fourcc {
765 u8 bytes[4];
766 u32 id;
767 char id_sanitized_sz[8]; // NUL-terminated printable ASCII
768 char id_dbgstr[32]; // Usable only with de_dbg()
770 #define DE_4CCFLAG_REVERSED 0x1
771 void dbuf_read_fourcc(dbuf *f, i64 pos, struct de_fourcc *fcc, int nbytes,
772 unsigned int flags);
774 #define DE_BUFFERED_READ_MIN_BLKSIZE 1024
775 struct de_bufferedreadctx {
776 void *userdata;
777 deark *c;
778 i64 offset;
779 i64 bytes_consumed;
780 u8 eof_flag;
782 typedef int (*de_buffered_read_cbfn)(struct de_bufferedreadctx *brctx,
783 const u8 *buf, i64 buf_len);
784 int dbuf_buffered_read(dbuf *f, i64 pos, i64 len,
785 de_buffered_read_cbfn cbfn, void *userdata);
787 int de_is_all_zeroes(const u8 *b, i64 n);
788 int dbuf_is_all_zeroes(dbuf *f, i64 pos, i64 len);
790 struct de_bitbuf_lowlevel {
791 u8 is_lsb;
792 UI nbits_in_bitbuf;
793 u64 bit_buf;
795 void de_bitbuf_lowlevel_add_byte(struct de_bitbuf_lowlevel *bbll, u8 n);
796 u64 de_bitbuf_lowlevel_get_bits(struct de_bitbuf_lowlevel *bbll, UI nbits);
797 void de_bitbuf_lowlevel_empty(struct de_bitbuf_lowlevel *bbll);
799 struct de_bitreader {
800 dbuf *f;
801 i64 curpos;
802 i64 endpos;
803 u8 eof_flag;
804 struct de_bitbuf_lowlevel bbll;
806 u64 de_bitreader_getbits(struct de_bitreader *bitrd, UI nbits);
807 void de_bitreader_skip_to_byte_boundary(struct de_bitreader *bitrd);
808 char *de_bitbuf_describe_curpos(struct de_bitbuf_lowlevel *bbll, i64 pos,
809 char *buf, size_t buf_len);
810 char *de_bitreader_describe_curpos(struct de_bitreader *bitrd, char *buf, size_t buf_len);
812 ///////////////////////////////////////////
814 void de_bitmap_write_to_file(de_bitmap *img, const char *token, unsigned int createflags);
815 void de_bitmap_write_to_file_finfo(de_bitmap *img, de_finfo *fi, unsigned int createflags);
817 void de_bitmap_setsample(de_bitmap *img, i64 x, i64 y,
818 i64 samplenum, de_colorsample v);
820 void de_bitmap_setpixel_gray(de_bitmap *img, i64 x, i64 y, de_colorsample v);
821 void de_bitmap_setpixel_rgb(de_bitmap *img, i64 x, i64 y, de_color color);
822 void de_bitmap_setpixel_rgba(de_bitmap *img, i64 x, i64 y, de_color color);
824 de_color de_bitmap_getpixel(de_bitmap *img, i64 x, i64 y);
826 de_bitmap *de_bitmap_create(deark *c, i64 width, i64 height, int bypp);
827 de_bitmap *de_bitmap_create2(deark *c, i64 npwidth, i64 pdwidth, i64 height, int bypp);
828 void de_bitmap_destroy(de_bitmap *b);
830 #define DE_COLOR_A(x) ((de_colorsample)(((x)>>24)&0xff))
831 #define DE_COLOR_R(x) ((de_colorsample)(((x)>>16)&0xff))
832 #define DE_COLOR_G(x) ((de_colorsample)(((x)>>8)&0xff))
833 #define DE_COLOR_B(x) ((de_colorsample)((x)&0xff))
834 #define DE_COLOR_K(x) ((de_colorsample)(((x)>>16)&0xff)) // Gray value. Arbitrarily use the Red channel.
836 #define DE_STOCKCOLOR_BLACK ((de_color)0xff000000U)
837 #define DE_STOCKCOLOR_WHITE ((de_color)0xffffffffU)
838 #define DE_STOCKCOLOR_TRANSPARENT ((de_color)0x00000000U)
840 #define DE_MAKE_RGBA(r,g,b,a) ((((de_color)(a))<<24)|((r)<<16)|((g)<<8)|(b))
841 #define DE_MAKE_RGB(r,g,b) ((((de_color)0xff)<<24)|((r)<<16)|((g)<<8)|(b))
842 #define DE_MAKE_GRAY(k) ((((de_color)0xff)<<24)|((k)<<16)|((k)<<8)|(k))
843 #define DE_SET_ALPHA(v,a) (((v)&0x00ffffff)|(((de_color)(a))<<24))
844 #define DE_MAKE_OPAQUE(v) (((de_color)(v))|0xff000000U)
846 // Return the index'th symbol in the bitmap row beginning at file position rowstart.
847 // A symbol has bps bits. bps must be 1, 2, 4, or 8.
848 u8 de_get_bits_symbol(dbuf *f, i64 bps, i64 rowstart, i64 index);
850 u8 de_get_bits_symbol_lsb(dbuf *f, i64 bps, i64 rowstart, i64 index);
852 u8 de_get_bits_symbol2(dbuf *f, int nbits, i64 bytepos, i64 bitpos);
854 // Conversion flags used by some functions.
855 #define DE_CVTF_WHITEISZERO 0x1
856 #define DE_CVTF_LSBFIRST 0x2
857 #define DE_CVTF_ONLYWHITE 0x4
859 void de_unpack_pixels_bilevel_from_byte(de_bitmap *img, i64 xpos, i64 ypos,
860 u8 val, UI npixels, unsigned int flags);
861 void de_convert_pixels_bilevel(dbuf *f, i64 fpos, de_bitmap *img,
862 i64 xpos, i64 ypos, i64 npixels, unsigned int flags);
863 void de_convert_row_bilevel(dbuf *f, i64 fpos, de_bitmap *img,
864 i64 rownum, unsigned int flags);
865 void de_convert_image_bilevel(dbuf *f, i64 fpos, i64 rowspan,
866 de_bitmap *img, unsigned int flags);
868 void de_convert_and_write_image_bilevel(dbuf *f, i64 fpos,
869 i64 w, i64 h, i64 rowspan, unsigned int cvtflags,
870 de_finfo *fi, unsigned int createflags);
871 void de_convert_and_write_image_bilevel2(dbuf *f, i64 fpos,
872 i64 w, i64 h, i64 rowspan, unsigned int cvtflags,
873 de_finfo *fi, unsigned int createflags);
875 void de_read_palette_rgb(dbuf *f,
876 i64 fpos, i64 num_entries, i64 entryspan,
877 de_color *pal, i64 ncolors_in_pal,
878 unsigned int flags);
880 // Utility function that will work for many of the common kinds of paletted images.
881 void de_convert_image_paletted(dbuf *f, i64 fpos,
882 i64 bpp, i64 rowspan, const de_color *pal,
883 de_bitmap *img, unsigned int flags);
885 void de_convert_image_rgb(dbuf *f, i64 fpos,
886 i64 rowspan, i64 pixelspan, de_bitmap *img, unsigned int flags);
888 i64 de_min_int(i64 n1, i64 n2);
889 i64 de_max_int(i64 n1, i64 n2);
890 i64 de_pad_to_2(i64 x);
891 i64 de_pad_to_4(i64 x);
892 i64 de_pad_to_n(i64 x, i64 n);
893 i64 de_pow2(i64 x);
895 // Calculate the number of bits required to store n symbols.
896 // Intended to be used with bitmap graphics.
897 // Returns a minimum of 1, maximum of 32.
898 i64 de_log2_rounded_up(i64 n);
900 char *de_print_base2_fixed(char *buf, size_t buf_len, u64 n, UI bitcount);
902 // Test if the image dimensions are valid and supported.
903 int de_good_image_dimensions_noerr(deark *c, i64 w, i64 h);
905 // Test if the image dimensions are valid and supported. Report an error if not.
906 int de_good_image_dimensions(deark *c, i64 w, i64 h);
908 // Test if the number of images is sane. Report an error if not.
909 int de_good_image_count(deark *c, i64 n);
911 int de_is_grayscale_palette(const de_color *pal, i64 num_entries);
913 #define DE_BITMAPFLAG_WHITEISTRNS 0x1
914 #define DE_BITMAPFLAG_MERGE 0x2
916 void de_bitmap_flip(de_bitmap *img);
917 void de_bitmap_mirror(de_bitmap *img);
918 void de_bitmap_transpose(de_bitmap *img);
919 void de_bitmap_rect(de_bitmap *img,
920 i64 xpos, i64 ypos, i64 width, i64 height,
921 de_color clr, unsigned int flags);
922 void de_bitmap_copy_rect(de_bitmap *srcimg, de_bitmap *dstimg,
923 i64 srcxpos, i64 srcypos, i64 width, i64 height,
924 i64 dstxpos, i64 dstypos, unsigned int flags);
926 void de_bitmap_apply_mask(de_bitmap *fg, de_bitmap *mask,
927 unsigned int flags);
928 void de_bitmap_remove_alpha(de_bitmap *img);
929 void de_bitmap_optimize_alpha(de_bitmap *img, unsigned int flags);
931 void de_make_grayscale_palette(de_color *pal, i64 num_entries, unsigned int flags);
933 ///////////////////////////////////////////
935 char de_get_hexchar(int n);
936 u8 de_decode_hex_digit(u8 x, int *errorflag);
938 de_color de_palette_vga256(int index);
939 de_color de_palette_ega64(int index);
940 de_color de_palette_pc16(int index);
941 de_color de_palette_pcpaint_cga4(int palnum, int index);
943 const u8 *de_get_8x8ascii_font_ptr(void);
944 const u8 *de_get_vga_cp437_font_ptr(void);
946 void de_color_to_css(de_color color, char *buf, int buflen);
948 u8 de_sample_nbit_to_8bit(i64 n, unsigned int x);
949 u8 de_scale_63_to_255(u8 x);
950 u8 de_scale_1000_to_255(i64 x);
951 u8 de_scale_n_to_255(i64 n, i64 x);
952 de_color de_rgb565_to_888(u32 x);
953 de_color de_bgr555_to_888(u32 x);
954 de_color de_rgb555_to_888(u32 x);
956 void de_encconv_init(struct de_encconv_state *es, de_ext_encoding ee);
957 de_rune de_char_to_unicode_ex(i32 a, struct de_encconv_state *es);
958 de_rune de_char_to_unicode(deark *c, i32 a, de_ext_encoding ee);
959 void de_uchar_to_utf8(de_rune u1, u8 *utf8buf, i64 *p_utf8len);
960 void dbuf_write_uchar_as_utf8(dbuf *outf, de_rune u);
961 int de_utf8_to_uchar(const u8 *utf8buf, i64 buflen,
962 de_rune *p_uchar, i64 *p_utf8len);
963 int de_is_ascii(const u8 *buf, i64 buflen);
965 #define DE_CONVFLAG_STOP_AT_NUL 0x1
966 #define DE_CONVFLAG_MAKE_PRINTABLE 0x2
967 #define DE_CONVFLAG_WANT_UTF8 0x10
968 #define DE_CONVFLAG_ALLOW_HL 0x20
969 #define DE_CONVFLAG_PARTIAL_DATA 0x40
971 char de_byte_to_printable_char(u8 b);
973 // Convert encoded bytes to a NUL-terminated string that can be
974 // printed to the terminal.
975 // Consider using {dbuf_read_to_ucstring or dbuf_read_string or
976 // ucstring_append_bytes} followed by ucstring_getpsz* instead.
977 void de_bytes_to_printable_sz(const u8 *src, i64 src_len,
978 char *dst, i64 dst_len, unsigned int conv_flags, de_ext_encoding src_ee);
980 de_finfo *de_finfo_create(deark *c);
981 void de_finfo_destroy(deark *c, de_finfo *fi);
983 #define DE_SNFLAG_FULLPATH 0x01
984 #define DE_SNFLAG_STRIPTRAILINGSLASH 0x2
985 void de_finfo_set_name_from_ucstring(deark *c, de_finfo *fi, de_ucstring *s, unsigned int flags);
986 void de_finfo_set_name_from_sz(deark *c, de_finfo *fi, const char *name1, unsigned int flags,
987 de_ext_encoding ee);
989 de_ucstring *ucstring_create(deark *c);
990 de_ucstring *ucstring_clone(const de_ucstring *src);
991 void ucstring_destroy(de_ucstring *s);
992 void ucstring_empty(de_ucstring *s);
993 void ucstring_truncate(de_ucstring *s, i64 newlen);
994 void ucstring_truncate_at_NUL(de_ucstring *s);
995 void ucstring_strip_trailing_NUL(de_ucstring *s);
996 void ucstring_strip_trailing_spaces(de_ucstring *s);
997 void ucstring_append_char(de_ucstring *s, de_rune ch);
998 void ucstring_append_ucstring(de_ucstring *s1, const de_ucstring *s2);
999 void ucstring_vprintf(de_ucstring *s, de_ext_encoding ee, const char *fmt, va_list ap);
1000 void ucstring_printf(de_ucstring *s, de_ext_encoding ee, const char *fmt, ...)
1001 de_gnuc_attribute ((format (printf, 3, 4)));
1002 int ucstring_isempty(const de_ucstring *s);
1003 int ucstring_isnonempty(const de_ucstring *s);
1005 // Convert and append an encoded array of bytes to the string.
1006 void ucstring_append_bytes(de_ucstring *s, const u8 *buf, i64 buflen,
1007 unsigned int conv_flags, de_ext_encoding ee);
1008 void ucstring_append_bytes_ex(de_ucstring *s, const u8 *buf, i64 buflen,
1009 unsigned int conv_flags, struct de_encconv_state *es);
1011 void ucstring_append_sz(de_ucstring *s, const char *sz, de_ext_encoding ee);
1013 void ucstring_write_as_utf8(deark *c, de_ucstring *s, dbuf *outf, int add_bom_if_needed);
1014 int de_is_printable_uchar(de_rune ch);
1015 i64 ucstring_count_utf8_bytes(de_ucstring *s);
1017 // Supported encodings are DE_ENCODING_UTF8, DE_ENCODING_ASCII, DE_ENCODING_LATIN1.
1018 // flags: DE_CONVFLAG_*
1019 void ucstring_to_sz(de_ucstring *s, char *szbuf, size_t szbuf_len, unsigned int flags,
1020 de_ext_encoding ee);
1022 // "get printable string"
1023 // Returns a pointer to a NUL-terminated string, that is valid until the
1024 // next ucstring_* function is called on that string.
1025 const char *ucstring_getpsz(de_ucstring *s);
1026 // The _n version limits the number of bytes in the result.
1027 // max_bytes does not count the terminating NUL.
1028 const char *ucstring_getpsz_n(de_ucstring *s, i64 max_bytes);
1030 #define DE_DBG_MAX_STRLEN 500
1031 // Same as ..._n, with max_bytes=DE_DBG_MAX_STRLEN
1032 const char *ucstring_getpsz_d(de_ucstring *s);
1034 // Helper functions for printing the contents of bit-flags fields
1035 void ucstring_append_flags_item(de_ucstring *s, const char *str);
1036 void ucstring_append_flags_itemf(de_ucstring *s, const char *fmt, ...)
1037 de_gnuc_attribute ((format (printf, 2, 3)));
1039 struct de_strarray;
1040 struct de_strarray *de_strarray_create(deark *c, size_t max_elems);
1041 void de_strarray_destroy(struct de_strarray *sa);
1042 int de_strarray_push(struct de_strarray *sa, de_ucstring *s);
1043 int de_strarray_pop(struct de_strarray *sa);
1044 #define DE_MPFLAG_NOTRAILINGSLASH 0x1
1045 void de_strarray_make_path(struct de_strarray *sa, de_ucstring *path, unsigned int flags);
1047 void de_write_codepoint_to_html(deark *c, dbuf *f, de_rune ch);
1049 de_encoding de_encoding_name_to_code(const char *encname);
1050 de_encoding de_windows_codepage_to_encoding(deark *c, int wincodepage,
1051 char *encname, size_t encname_len, unsigned int flags);
1053 void de_copy_bits(const u8 *src, i64 srcbitnum,
1054 u8 *dst, i64 dstbitnum, i64 bitstocopy);
1056 void de_decode_base16(deark *c, dbuf *inf, i64 pos1, i64 len,
1057 dbuf *outf, unsigned int flags);
1059 struct de_inthashtable;
1060 struct de_inthashtable *de_inthashtable_create(deark *c);
1061 void de_inthashtable_destroy(deark *c, struct de_inthashtable *ht);
1062 int de_inthashtable_add_item(deark *c, struct de_inthashtable *ht, i64 key, void *value);
1063 int de_inthashtable_get_item(deark *c, struct de_inthashtable *ht, i64 key, void **pvalue);
1064 int de_inthashtable_item_exists(deark *c, struct de_inthashtable *ht, i64 key);
1065 int de_inthashtable_remove_item(deark *c, struct de_inthashtable *ht, i64 key, void **pvalue);
1066 int de_inthashtable_remove_any_item(deark *c, struct de_inthashtable *ht, i64 *pkey, void **pvalue);
1068 #define DE_CRCOBJ_CRC32_IEEE 0x10
1069 #define DE_CRCOBJ_ADLER32 0x11
1070 #define DE_CRCOBJ_CRC16_CCITT 0x20
1071 #define DE_CRCOBJ_CRC16_ARC 0x21
1073 struct de_crcobj;
1075 struct de_crcobj *de_crcobj_create(deark *c, UI type_and_flags);
1076 void de_crcobj_destroy(struct de_crcobj *crco);
1077 void de_crcobj_reset(struct de_crcobj *crco);
1078 u32 de_crcobj_getval(struct de_crcobj *crco);
1079 void de_crcobj_addbuf(struct de_crcobj *crco, const u8 *buf, i64 buf_len);
1080 void de_crcobj_addzeroes(struct de_crcobj *crco, i64 len);
1081 void de_crcobj_addslice(struct de_crcobj *crco, dbuf *f, i64 pos, i64 len);
1083 ///////////////////////////////////////////
1085 struct de_bitmap_font_char {
1086 i32 codepoint_nonunicode;
1088 // If font->has_unicode_codepoints is set, then ->codepoint_unicode
1089 // must be set to a Unicode codepoint, or to DE_INVALID_CODEPOINT.
1090 de_rune codepoint_unicode;
1092 int width, height;
1093 int v_offset; // Used if the glyphs do not all have the same height
1094 i16 extraspace_l, extraspace_r;
1095 i64 rowspan;
1096 u8 *bitmap;
1099 struct de_bitmap_font {
1100 int nominal_width, nominal_height;
1101 i64 index_of_replacement_char; // -1 if none
1103 // Flag: Are the char_array[]->codepoint_nonunicode codes set?
1104 // (This should be ignored if has_unicode_codepoints is not set.)
1105 u8 has_nonunicode_codepoints;
1107 // Flag: Are the char_array[]->codepoint_unicode codes set?
1108 u8 has_unicode_codepoints;
1110 // If the font has both unicode and non-unicode codpoints, this flag tells which
1111 // to prefer when displaying the font.
1112 u8 prefer_unicode;
1114 i64 num_chars;
1115 struct de_bitmap_font_char *char_array;
1118 struct de_bitmap_font *de_create_bitmap_font(deark *c);
1119 void de_destroy_bitmap_font(deark *c, struct de_bitmap_font *font);
1121 #define DE_PAINTFLAG_TRNSBKGD 0x01
1122 #define DE_PAINTFLAG_VGA9COL 0x02 // Render an extra column, like VGA does
1123 #define DE_PAINTFLAG_LEFTHALF 0x04 // Note: The "HALF" flags must fit into a byte,
1124 #define DE_PAINTFLAG_RIGHTHALF 0x08 // because they are stored in de_char_cell::size_flags.
1125 #define DE_PAINTFLAG_TOPHALF 0x10
1126 #define DE_PAINTFLAG_BOTTOMHALF 0x20
1127 void de_font_paint_character_idx(deark *c, de_bitmap *img,
1128 struct de_bitmap_font *font, i64 char_idx,
1129 i64 xpos, i64 ypos, de_color fgcol, de_color bgcol, unsigned int flags);
1130 void de_font_paint_character_cp(deark *c, de_bitmap *img,
1131 struct de_bitmap_font *font, i32 codepoint,
1132 i64 xpos, i64 ypos, de_color fgcol, de_color bgcol, unsigned int flags);
1134 void de_font_bitmap_font_to_image(deark *c, struct de_bitmap_font *font, de_finfo *fi, unsigned int createflags);
1135 int de_font_is_standard_vga_font(deark *c, u32 crc);
1137 ///////////////////////////////////////////
1139 // Note that this struct is assumed to be copyable with a simple struct copy.
1140 // It should not contain pointers.
1141 struct de_char_cell {
1142 i32 codepoint;
1143 de_rune codepoint_unicode;
1144 // The color fields are interpreted as follows:
1145 // A color value <=0x0000000f is a palette index.
1146 // A color value >=0xff000000 is an RGB color, e.g. from DE_MAKE_RGB().
1147 #define DE_IS_PAL_COLOR(x) ((u32)(x)<=0xfU)
1148 de_color fgcol;
1149 de_color bgcol;
1150 u8 underline;
1151 u8 strikethru;
1152 u8 blink;
1153 u8 size_flags;
1156 struct de_char_screen {
1157 i64 width;
1158 i64 height;
1159 struct de_char_cell **cell_rows; // Array of [height] row pointers
1162 struct de_char_context {
1163 u8 prefer_image_output;
1164 u8 prefer_9col_mode;
1165 u8 no_density;
1166 u8 suppress_custom_font_warning;
1167 u8 outfmt_known;
1168 int outfmt;
1169 i64 nscreens;
1170 struct de_char_screen **screens; // Array of [nscreens] screens
1171 de_color pal[16];
1172 struct de_bitmap_font *font; // Optional
1173 de_ucstring *title;
1174 de_ucstring *artist;
1175 de_ucstring *organization;
1176 struct de_timestamp creation_date;
1177 de_ucstring *comment; // NULL if there is no comment
1180 void de_char_output_to_file(deark *c, struct de_char_context *charctx);
1181 struct de_char_context *de_create_charctx(deark *c, unsigned int flags);
1182 void de_char_decide_output_format(deark *c, struct de_char_context *charctx);
1183 void de_destroy_charctx(deark *c, struct de_char_context *charctx);
1184 void de_free_charctx_screens(deark *c, struct de_char_context *charctx);
1185 void de_free_charctx(deark *c, struct de_char_context *charctx);
1187 ///////////////////////////////////////////
1189 // Our version of "struct tm".
1190 // Differences: Year is full year, removed some fields, added milliseconds field.
1191 struct de_struct_tm {
1192 int is_valid;
1193 int tm_fullyear, tm_mon, tm_mday;
1194 int tm_hour, tm_min, tm_sec;
1195 int tm_subsec; // in ten-millionths of a second
1198 void de_unix_time_to_timestamp(i64 ut, struct de_timestamp *ts, unsigned int flags);
1199 void de_mac_time_to_timestamp(i64 mt, struct de_timestamp *ts);
1200 void de_FILETIME_to_timestamp(i64 ft, struct de_timestamp *ts, unsigned int flags);
1201 void de_dos_datetime_to_timestamp(struct de_timestamp *ts,
1202 i64 ddate, i64 dtime);
1203 void de_describe_dos_attribs(deark *c, UI attr, de_ucstring *s, UI flags);
1204 void de_riscos_loadexec_to_timestamp(u32 load_addr,
1205 u32 exec_addr, struct de_timestamp *ts);
1206 void de_timestamp_set_subsec(struct de_timestamp *ts, double frac);
1207 i64 de_timestamp_get_subsec(const struct de_timestamp *ts);
1208 i64 de_timestamp_to_unix_time(const struct de_timestamp *ts);
1209 i64 de_timestamp_to_FILETIME(const struct de_timestamp *ts);
1210 void de_make_timestamp(struct de_timestamp *ts,
1211 i64 yr, i64 mo, i64 da,
1212 i64 hr, i64 mi, i64 se);
1213 void de_timestamp_cvt_to_utc(struct de_timestamp *ts, i64 offset_seconds);
1214 char *de_timestamp_to_string(const struct de_timestamp *ts,
1215 char *buf, size_t buf_len, unsigned int flags);
1216 char *de_dbg_timestamp_to_string(deark *c, const struct de_timestamp *ts,
1217 char *buf, size_t buf_len, unsigned int flags);
1218 void de_gmtime(const struct de_timestamp *ts, struct de_struct_tm *tm2);
1219 void de_current_time_to_timestamp(struct de_timestamp *ts);
1220 void de_cached_current_time_to_timestamp(deark *c, struct de_timestamp *ts);