dclib-xdump.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #ifndef DCLIB_XDUMP_H
  34. #define DCLIB_XDUMP_H 1
  35. #include "dclib-basics.h"
  36. //
  37. ///////////////////////////////////////////////////////////////////////////////
  38. /////////////// consts and structs ///////////////
  39. ///////////////////////////////////////////////////////////////////////////////
  40. #define XDUMP_MAX_BYTES_PER_LINE 600
  41. #define XDUMP_GOOD_NUM_BUF_SIZE (6*XDUMP_MAX_BYTES_PER_LINE+1)
  42. #define XDUMP_GOOD_TEXT_BUF_SIZE (XDUMP_MAX_BYTES_PER_LINE+1)
  43. #define XDUMP_MAX_LINE_WIDTH (XDUMP_GOOD_NUM_BUF_SIZE+XDUMP_GOOD_TEXT_BUF_SIZE+30)
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // [[XDumpCommand_t]]
  46. typedef enum XDumpCommand_t
  47. {
  48. XDUMPC_DUMP,
  49. XDUMPC_DIFF,
  50. XDUMPC_SCAN,
  51. XDUMPC__N
  52. }
  53. __attribute__ ((packed)) XDumpCommand_t;
  54. extern const ccp XDumpCommandName[XDUMPC__N];
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // [[XDumpFormat_t]]
  57. typedef enum XDumpFormat_t
  58. {
  59. XDUMPF_AUTO,
  60. XDUMPF_INT_1,
  61. XDUMPF_INT_2,
  62. XDUMPF_INT_3,
  63. XDUMPF_INT_4,
  64. XDUMPF_INT_5,
  65. XDUMPF_INT_6,
  66. XDUMPF_INT_7,
  67. XDUMPF_INT_8,
  68. XDUMPF_FLOAT,
  69. XDUMPF_DOUBLE,
  70. XDUMPF__N,
  71. }
  72. __attribute__ ((packed)) XDumpFormat_t;
  73. extern const ccp XDumpFormatName[XDUMPF__N];
  74. extern const char XDumpFormatOption[XDUMPF__N+1];
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // [[XDumpOutputFunc]]
  77. struct XDump_t;
  78. typedef int (*XDumpOutputFunc)
  79. (
  80. struct XDump_t *xd, // internal copy of params
  81. ccp prefix, // NULL or additional prefix (used by diff)
  82. ccp dump, // dump to print
  83. ccp text // text to print
  84. );
  85. int XDumpStandardOutput
  86. (
  87. struct XDump_t *xd, // internal copy of params
  88. ccp prefix, // NULL or additional prefix (used by diff)
  89. ccp dump, // dump to print
  90. ccp text // text to print
  91. );
  92. ///////////////////////////////////////////////////////////////////////////////
  93. // [[XDumpDumpFunc]]
  94. typedef int (*XDumpDumpFunc)
  95. (
  96. // returns <0 on error, or number of dumped bytes
  97. struct XDump_t *xd, // current params
  98. const void *data, // data to dump
  99. uint size, // size of 'data'
  100. bool term // false: print only whole lines
  101. // true: print complete data
  102. );
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // [[XDump_t]]
  105. typedef struct XDump_t
  106. {
  107. //--- options
  108. XDumpCommand_t cmd; // command
  109. XDumpFormat_t format; // scan or output format
  110. dcEndian_t endian; // endian
  111. bool mode_dec; // print integers as decimal instead of hex
  112. bool mode_zero; // print leading zeros
  113. bool mode_c; // print in c format
  114. bool mode_align; // align addresses by 'addr % bytes_per_line'
  115. bool mode_ignore; // ignore lines with NULL bytes only
  116. bool print_format; // print an info line about the format
  117. bool print_addr; // print address field
  118. bool print_number; // print number field
  119. bool print_endmarker;// print "/" as end of data marker
  120. bool print_text; // print ascii text field
  121. bool print_summary; // print a summary at end of dump
  122. bool print_diff_sep; // print an empty line after differ lines
  123. bool have_trigger; // true: trigger is active
  124. u64 trigger; // the trigger value
  125. u64 start_addr; // start address
  126. u64 assumed_size; // assumed output size, used for 'addr_fw'
  127. uint min_addr_fw; // min field width of address
  128. uint max_addr_fw; // max field width of address; 0=AUTO
  129. uint min_number_fw; // >0: minimal field width for number dump
  130. uint min_width; // minimal output width in bytes per line
  131. uint max_width; // maximal output width in bytes per line
  132. int extra_space; // >0: print an extra space after each # column
  133. // =0: auto
  134. FILE *f; // not NULL: output file
  135. int indent; // indention before 'prefix'
  136. ccp prefix; // not NULL: prefix each line by this
  137. ccp eol; // NULL or eol
  138. XDumpOutputFunc out_func; // NULL or a private function
  139. //--- user parameters, not used, not changed
  140. int user_int;
  141. void *user_ptr;
  142. //--- runtime parameters
  143. ccp num_format1; // pointer to first numeric format
  144. ccp num_format2; // pointer to second numeric format
  145. uint num_format_fw; // current field width of a number
  146. uint max_format_fw; // max possible fw for format
  147. ccp format_name; // name of the format
  148. u64 written; // total written bytes
  149. u64 addr; // current address
  150. uint addr_fw; // field width of address
  151. const u64 *addr_break; // NULL or null terminated list:
  152. // break line at this addresses
  153. uint bytes_per_col; // bytes per column
  154. uint cols_per_line; // columns per line
  155. uint bytes_per_line; // bytes per line
  156. uint col_space; // >0: print an extra space after each # column
  157. uint null_lines; // number of currently skipped NULL lines
  158. bool format_printed; // true: format line printed
  159. uint last_number_fw; // last field with of number column
  160. uint diff_count; // number of lines with differences
  161. uint diff_size; // <0: size1>size2 / >0: size1>size2
  162. //--- functions
  163. XDumpDumpFunc dump_func; // dump processing function
  164. const endian_func_t *endian_func; // endian functions
  165. }
  166. XDump_t;
  167. //-----------------------------------------------------------------------------
  168. extern ccp hexdump_prefix;
  169. extern ccp hexdump_eol;
  170. extern bool hexdump_align;
  171. //-----------------------------------------------------------------------------
  172. void InitializeXDump ( XDump_t *xd );
  173. void InitializeXDumpEx
  174. (
  175. XDump_t *xd,
  176. FILE *f,
  177. int indent,
  178. u64 addr,
  179. int addr_fw,
  180. int row_len
  181. );
  182. void SetupXDump ( XDump_t *xd, XDumpCommand_t cmd );
  183. static inline void ResetXDump ( XDump_t *xd ) {}
  184. void PrintXDumpParam
  185. (
  186. FILE *f, // output file, abort if NULL
  187. int indent, // indention
  188. const ColorSet_t *colset, // NULL or color set
  189. const XDump_t *xd // data to print
  190. );
  191. //
  192. ///////////////////////////////////////////////////////////////////////////////
  193. /////////////// dump interface ///////////////
  194. ///////////////////////////////////////////////////////////////////////////////
  195. int XDump
  196. (
  197. // returns <0 on error, or number of dumped bytes
  198. XDump_t *xd, // params; if NULL: use local version
  199. const void *data, // data to dump
  200. uint size, // size of 'data'
  201. bool term // false: print only whole lines
  202. // true: print complete data
  203. );
  204. ///////////////////////////////////////////////////////////////////////////////
  205. int XDumpByFile
  206. (
  207. // returns <0 on error, or number of dumped bytes
  208. XDump_t *xd, // params; if NULL: use local version
  209. FILE *f, // input file
  210. u64 max_size // >0: limit size to print
  211. );
  212. ///////////////////////////////////////////////////////////////////////////////
  213. int XDump16 // wrapper for a standard hexdump
  214. (
  215. // returns <0 on error, or number of dumped bytes
  216. FILE *f_out, // output file
  217. int indent, // indention
  218. u64 addr, // start address
  219. const void *data, // data
  220. uint size // size of 'data'
  221. );
  222. //
  223. ///////////////////////////////////////////////////////////////////////////////
  224. /////////////// diff interface ///////////////
  225. ///////////////////////////////////////////////////////////////////////////////
  226. int XDiff
  227. (
  228. // returns <0 on error, or number of dumped bytes
  229. XDump_t *xd, // params; if NULL: use local version
  230. const void *data1, // data1 to compare
  231. uint size1, // size of 'data1'
  232. bool term1, // true: 'data1' completed
  233. const void *data2, // data2 to compare
  234. uint size2, // size of 'data2'
  235. bool term2, // true: 'data2' completed
  236. uint limit_lines, // >0: limit number of differ-lines
  237. bool colorize // true: use red and green on output
  238. );
  239. ///////////////////////////////////////////////////////////////////////////////
  240. int XDiffByFile
  241. (
  242. // returns <0 on error, or number of dumped bytes
  243. XDump_t *xd, // params; if NULL: use local version
  244. FILE *f1, // first input file
  245. FILE *f2, // second input file
  246. u64 max_size, // >0: limit comparing size
  247. uint limit_lines, // >0: limit number of differ-lines
  248. bool colorize // true: use red and green on output
  249. );
  250. ///////////////////////////////////////////////////////////////////////////////
  251. int XDiff16
  252. (
  253. // returns <0 on error, or number of dumped bytes
  254. FILE *f_out, // output file
  255. int indent, // indention
  256. u64 addr, // start address
  257. const void *data1, // data
  258. uint size1, // size of 'data'
  259. const void *data2, // data
  260. uint size2, // size of 'data'
  261. uint limit_lines, // >0: limit number of differ-lines
  262. bool colorize // true: use red and green on output
  263. );
  264. //
  265. ///////////////////////////////////////////////////////////////////////////////
  266. /////////////// scan interface ///////////////
  267. ///////////////////////////////////////////////////////////////////////////////
  268. int XScan
  269. (
  270. // returns <0 on error, or number of dumped bytes
  271. XDump_t *xd, // params; if NULL: use local version
  272. FastBuf_t *dest, // not NULL: append scanned data here
  273. const void *data, // data to scan
  274. uint size, // size of 'data'
  275. bool term, // true: accept uncomplete last line
  276. u64 max_size // >0: limit output size
  277. );
  278. ///////////////////////////////////////////////////////////////////////////////
  279. s64 XScanByFile
  280. (
  281. // returns <0 on error, or number of written bytes
  282. XDump_t *xd, // params; if NULL: use local version
  283. FastBuf_t *dest, // not NULL: append scanned data here
  284. FILE *f, // input file
  285. u64 max_size // >0: limit output size
  286. );
  287. //
  288. ///////////////////////////////////////////////////////////////////////////////
  289. /////////////// E N D ///////////////
  290. ///////////////////////////////////////////////////////////////////////////////
  291. #endif // DCLIB_XDUMP_H