dclib-xdump.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  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. #define _GNU_SOURCE 1
  34. #include <stdio.h>
  35. #include <stddef.h>
  36. #include "dclib-xdump.h"
  37. //
  38. ///////////////////////////////////////////////////////////////////////////////
  39. /////////////// data ///////////////
  40. ///////////////////////////////////////////////////////////////////////////////
  41. const ccp XDumpCommandName[XDUMPC__N] =
  42. {
  43. "DUMP",
  44. "DIFF",
  45. "SCAN",
  46. };
  47. //-----------------------------------------------------------------------------
  48. const ccp XDumpFormatName[XDUMPF__N] =
  49. {
  50. "AUTO",
  51. "INT_1",
  52. "INT_2",
  53. "INT_3",
  54. "INT_4",
  55. "INT_5",
  56. "INT_6",
  57. "INT_7",
  58. "INT_8",
  59. "FLOAT",
  60. "DOUBLE",
  61. };
  62. const char XDumpFormatOption[XDUMPF__N+1] = "A12345678FD";
  63. //
  64. ///////////////////////////////////////////////////////////////////////////////
  65. /////////////// output helpers ///////////////
  66. ///////////////////////////////////////////////////////////////////////////////
  67. static bool PrintFormat
  68. (
  69. struct XDump_t *xd, // internal copy of params
  70. int force // >0: ignore 'format_printed'
  71. // >1: print always
  72. )
  73. {
  74. DASSERT(xd);
  75. if ( !xd->f || !xd->format_name )
  76. return false;
  77. if ( force > 1
  78. || ( force > 0 && xd->print_format )
  79. || ( xd->print_format && !xd->format_printed )
  80. )
  81. {
  82. xd->format_printed = true;
  83. fprintf(xd->f,"%s%*s%sFORMAT: %s",
  84. xd->prefix, xd->indent,"",
  85. xd->mode_c ? "//" : "#",
  86. xd->format_name );
  87. if ( xd->format > XDUMPF_INT_1 )
  88. fprintf(xd->f," %s%s",
  89. xd->endian == DC_BIG_ENDIAN ? "BE" : "LE",
  90. xd->eol );
  91. else
  92. fputs(xd->eol,xd->f);
  93. return true;
  94. }
  95. return false;
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////
  98. int XDumpStandardOutput
  99. (
  100. struct XDump_t *xd, // internal copy of params
  101. ccp prefix, // NULL or additional prefix (used by diff)
  102. ccp dump, // dump to print
  103. ccp text // NULL or text to print
  104. )
  105. {
  106. DASSERT(xd);
  107. DASSERT(dump);
  108. if (xd->f)
  109. {
  110. fprintf(xd->f,"%s%*s%s",
  111. xd->prefix, xd->indent,"", prefix ? prefix : "" );
  112. if (xd->print_addr)
  113. {
  114. if (xd->mode_c)
  115. fprintf(xd->f,"/*%*llx*/ ",xd->addr_fw,xd->addr);
  116. else
  117. fprintf(xd->f,"%*llx:%s",
  118. xd->addr_fw, xd->addr,
  119. xd->extra_space >= 0 ? " " : "" );
  120. }
  121. if (xd->print_number)
  122. {
  123. if (xd->min_number_fw)
  124. fprintf(xd->f,"%-*s",xd->min_number_fw,dump);
  125. else
  126. fputs(dump,xd->f);
  127. }
  128. if ( text && xd->print_text )
  129. {
  130. if (xd->mode_c)
  131. fprintf(xd->f," // %s",text);
  132. else
  133. fprintf(xd->f,"%s :%s:",
  134. xd->extra_space >= 0 ? " " : "", text );
  135. }
  136. fputs(xd->eol,xd->f);
  137. }
  138. return 0;
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////
  141. static void print_text ( char *buf, const u8 *data, int n )
  142. {
  143. while ( n-- > 0 )
  144. {
  145. const u8 ch = *data++;
  146. *buf++ = ch >= ' ' && ch < 0x7f ? ch : '.';
  147. }
  148. *buf = 0;
  149. }
  150. //
  151. ///////////////////////////////////////////////////////////////////////////////
  152. /////////////// XParam_t ///////////////
  153. ///////////////////////////////////////////////////////////////////////////////
  154. // [[XParam_t]]
  155. typedef struct XParam_t
  156. {
  157. const u8 *d; // pointer to input data
  158. const u8 *end; // end of input data
  159. uint size; // size of data to dump
  160. bool term; // true: data complete
  161. bool recalc_align; // true: recalculate alignment
  162. bool next_line; // true: continue at next line
  163. const u64 *addr_break; // NULL or pointer to next break
  164. uint skip; // number of columns to skip
  165. uint line_size; // current line size (source bytes)
  166. uint max_col; // max number of columns to print
  167. int extra_space; // extra space counter
  168. // buffers
  169. char text_buf[XDUMP_GOOD_TEXT_BUF_SIZE];
  170. char num_buf[XDUMP_GOOD_NUM_BUF_SIZE];
  171. }
  172. XParam_t;
  173. ///////////////////////////////////////////////////////////////////////////////
  174. static void SetupXParam
  175. ( XParam_t *p, XDump_t *xd, cvp data, uint size, bool term )
  176. {
  177. DASSERT(p);
  178. DASSERT(xd);
  179. DASSERT(data);
  180. if ( size > INT_MAX )
  181. {
  182. size = INT_MAX; // limit because of return value
  183. term = false;
  184. }
  185. memset( p, 0, offsetof(XParam_t,num_buf) + 15 & ~7 );
  186. p->size = size;
  187. p->term = term;
  188. p->d = data;
  189. p->end = p->d + size;
  190. p->recalc_align = xd->mode_align;
  191. p->addr_break = xd->addr_break;
  192. }
  193. ///////////////////////////////////////////////////////////////////////////////
  194. static uint SetupXParamLine ( XParam_t *p, XDump_t *xd )
  195. {
  196. DASSERT(p);
  197. DASSERT(xd);
  198. if ( p->recalc_align )
  199. {
  200. p->recalc_align = false;
  201. p->skip = ( xd->addr % xd->bytes_per_line ) / xd->bytes_per_col;
  202. const int delta = p->skip * xd->bytes_per_col;
  203. xd->addr -= delta;
  204. if (xd->print_number)
  205. p->size += delta;
  206. }
  207. p->next_line = false;
  208. p->line_size = xd->bytes_per_line < p->size
  209. ? xd->bytes_per_line
  210. : p->term ? p->size : 0;
  211. if ( p->addr_break )
  212. {
  213. while ( *p->addr_break && xd->addr >= *p->addr_break )
  214. p->addr_break++;
  215. if (!*p->addr_break)
  216. p->addr_break = 0;
  217. else
  218. {
  219. const s64 max = *p->addr_break - xd->addr;
  220. if ( p->line_size > max )
  221. p->line_size = max;
  222. }
  223. }
  224. p->max_col = p->line_size / xd->bytes_per_col;
  225. p->line_size = p->max_col * xd->bytes_per_col;
  226. p->extra_space = xd->col_space ? xd->col_space+1 : 0;
  227. if (xd->print_text)
  228. {
  229. if (p->skip)
  230. {
  231. const uint skip_size = p->skip * xd->bytes_per_col;
  232. if ( skip_size < p->line_size )
  233. {
  234. memset(p->text_buf,' ',skip_size);
  235. print_text(p->text_buf+skip_size,p->d,p->line_size-skip_size);
  236. }
  237. else
  238. *p->text_buf = 0; // should never happen!
  239. }
  240. else
  241. print_text(p->text_buf,p->d,p->line_size);
  242. }
  243. return p->max_col;
  244. }
  245. ///////////////////////////////////////////////////////////////////////////////
  246. static bool PrintNullSummary ( XDump_t *xd )
  247. {
  248. if ( !xd->null_lines || !xd->out_func )
  249. return false;
  250. char buf[100];
  251. const uint skipped = xd->null_lines * xd->bytes_per_line;
  252. snprintf(buf,sizeof(buf),"%s#NULL: 0x%x bytes",
  253. xd->extra_space >= 0 ? " " : "", skipped );
  254. xd->addr -= skipped;
  255. xd->out_func(xd,0,buf,0);
  256. xd->addr += skipped;
  257. xd->null_lines = 0;
  258. return true;
  259. }
  260. ///////////////////////////////////////////////////////////////////////////////
  261. static bool CheckXParamNull ( XParam_t *p, XDump_t *xd )
  262. {
  263. DASSERT(p);
  264. DASSERT(xd);
  265. static u8 nullbuf[XDUMP_MAX_BYTES_PER_LINE] = {};
  266. const bool skip = p->line_size == xd->bytes_per_line
  267. && !memcmp(p->d,nullbuf,xd->bytes_per_line);
  268. if (skip)
  269. {
  270. xd->null_lines++;
  271. xd->addr += p->line_size;
  272. p->d += p->line_size;
  273. p->size -= p->line_size;
  274. return true;
  275. }
  276. if ( xd->null_lines && ( !skip || p->size <= xd->bytes_per_line ))
  277. PrintNullSummary(xd);
  278. return skip;
  279. }
  280. ///////////////////////////////////////////////////////////////////////////////
  281. static void AbortXParamLine ( XParam_t *p, XDump_t *xd, uint col )
  282. {
  283. const uint col1 = col + 1;
  284. if ( col1 < p->max_col )
  285. {
  286. p->max_col = col1;
  287. p->line_size = col1 * xd->bytes_per_col;
  288. p->text_buf[p->line_size] = 0;
  289. p->recalc_align = xd->mode_align;
  290. p->next_line = true;
  291. }
  292. }
  293. //
  294. ///////////////////////////////////////////////////////////////////////////////
  295. /////////////// output functions ///////////////
  296. ///////////////////////////////////////////////////////////////////////////////
  297. static int DumpInt32
  298. (
  299. // returns <0 on error, or number of dumped bytes
  300. XDump_t *xd, // current params
  301. const void *p_data, // data to dump
  302. uint p_size, // size of 'p_data'
  303. bool p_term // false: print only whole lines
  304. // true: print complete data
  305. )
  306. {
  307. DASSERT(xd);
  308. if ( !p_data || !xd->out_func )
  309. return 0;
  310. XParam_t p;
  311. SetupXParam(&p,xd,p_data,p_size,p_term);
  312. while(SetupXParamLine(&p,xd))
  313. {
  314. if ( xd->mode_ignore && CheckXParamNull(&p,xd) )
  315. continue;
  316. if (xd->print_number)
  317. {
  318. char *num = p.num_buf;
  319. uint col;
  320. for ( col = 0; col < xd->cols_per_line; col++ )
  321. {
  322. if (!--p.extra_space)
  323. {
  324. p.extra_space = xd->col_space;
  325. *num++ = ' ';
  326. }
  327. if ( p.skip > 0 )
  328. {
  329. p.skip--;
  330. memset(num,' ',xd->num_format_fw);
  331. num += xd->num_format_fw;
  332. }
  333. else if ( col < p.max_col )
  334. {
  335. u32 val;
  336. switch(xd->bytes_per_col)
  337. {
  338. case 2: val = xd->endian_func->rd16(p.d); p.d += 2; break;
  339. case 3: val = xd->endian_func->rd24(p.d); p.d += 3; break;
  340. case 4: val = xd->endian_func->rd32(p.d); p.d += 4; break;
  341. default: val = *p.d++; break;
  342. }
  343. num += sprintf(num,xd->num_format1,val);
  344. if ( xd->have_trigger && val == (u32)xd->trigger )
  345. AbortXParamLine(&p,xd,col);
  346. }
  347. else if ( p.term && !p.next_line && !xd->mode_c )
  348. {
  349. if ( p.d < p.end )
  350. num += sprintf(num, "%*c%u", xd->num_format_fw-1,
  351. '>', (int)(p.end-p.d) );
  352. else if ( xd->print_endmarker )
  353. num += sprintf(num,"%*c", xd->num_format_fw, '/' );
  354. else if ( xd->print_text )
  355. {
  356. memset(num,' ',xd->num_format_fw);
  357. num += xd->num_format_fw;
  358. }
  359. p.term = false; // print only once
  360. }
  361. else if ( xd->print_text )
  362. {
  363. memset(num,' ',xd->num_format_fw);
  364. num += xd->num_format_fw;
  365. }
  366. }
  367. if (!xd->print_text)
  368. while ( num > p.num_buf && num[-1] == ' ' )
  369. num--;
  370. *num = 0;
  371. ASSERT( num < p.num_buf + sizeof(p.num_buf));
  372. xd->last_number_fw = num - p.num_buf;
  373. }
  374. else
  375. {
  376. p.d += p.line_size - p.skip * xd->bytes_per_col;
  377. p.skip = 0;
  378. }
  379. int stat = xd->out_func(xd,0,p.num_buf,p.text_buf);
  380. if ( stat < 0 )
  381. return stat;
  382. xd->addr += p.line_size;
  383. p.size -= p.line_size;
  384. }
  385. const uint written = p.d - (u8*)p_data;
  386. xd->written += written;
  387. return written;
  388. }
  389. ///////////////////////////////////////////////////////////////////////////////
  390. static int DumpInt64
  391. (
  392. // returns <0 on error, or number of dumped bytes
  393. XDump_t *xd, // current params
  394. const void *p_data, // data to dump
  395. uint p_size, // size of 'p_data'
  396. bool p_term // false: print only whole lines
  397. // true: print complete data
  398. )
  399. {
  400. DASSERT(xd);
  401. if ( !p_data || !xd->out_func )
  402. return 0;
  403. XParam_t p;
  404. SetupXParam(&p,xd,p_data,p_size,p_term);
  405. while(SetupXParamLine(&p,xd))
  406. {
  407. if ( xd->mode_ignore && CheckXParamNull(&p,xd) )
  408. continue;
  409. if (xd->print_number)
  410. {
  411. char *num = p.num_buf;
  412. uint col;
  413. for ( col = 0; col < xd->cols_per_line; col++ )
  414. {
  415. if (!--p.extra_space)
  416. {
  417. p.extra_space = xd->col_space;
  418. *num++ = ' ';
  419. }
  420. if ( p.skip > 0 )
  421. {
  422. p.skip--;
  423. memset(num,' ',xd->num_format_fw);
  424. num += xd->num_format_fw;
  425. }
  426. else if ( col < p.max_col )
  427. {
  428. u64 val;
  429. switch(xd->bytes_per_col)
  430. {
  431. case 5: val = xd->endian_func->rd40(p.d); p.d += 5; break;
  432. case 6: val = xd->endian_func->rd48(p.d); p.d += 6; break;
  433. case 7: val = xd->endian_func->rd56(p.d); p.d += 7; break;
  434. default: val = xd->endian_func->rd64(p.d); p.d += 8; break;
  435. }
  436. num += sprintf(num,xd->num_format1,val);
  437. if ( xd->have_trigger && val == (u32)xd->trigger )
  438. AbortXParamLine(&p,xd,col);
  439. }
  440. else if ( p.term && !p.next_line && !xd->mode_c )
  441. {
  442. if ( p.d < p.end )
  443. num += sprintf(num, "%*c%u", xd->num_format_fw-1,
  444. '>', (int)(p.end-p.d) );
  445. else if ( xd->print_endmarker )
  446. num += sprintf(num,"%*c", xd->num_format_fw, '/' );
  447. else if (xd->print_text)
  448. {
  449. memset(num,' ',xd->num_format_fw);
  450. num += xd->num_format_fw;
  451. }
  452. p.term = false; // print only once
  453. }
  454. else if (xd->print_text)
  455. {
  456. memset(num,' ',xd->num_format_fw);
  457. num += xd->num_format_fw;
  458. }
  459. }
  460. *num = 0;
  461. ASSERT( num < p.num_buf + sizeof(p.num_buf));
  462. xd->last_number_fw = num - p.num_buf;
  463. }
  464. else
  465. {
  466. p.d += p.line_size - p.skip * xd->bytes_per_col;
  467. p.skip = 0;
  468. }
  469. int stat = xd->out_func(xd,0,p.num_buf,p.text_buf);
  470. if ( stat < 0 )
  471. return stat;
  472. xd->addr += p.line_size;
  473. p.size -= p.line_size;
  474. }
  475. const uint written = p.d - (u8*)p_data;
  476. xd->written += written;
  477. return written;
  478. }
  479. ///////////////////////////////////////////////////////////////////////////////
  480. static int DumpFloat
  481. (
  482. // returns <0 on error, or number of dumped bytes
  483. XDump_t *xd, // current params
  484. const void *p_data, // data to dump
  485. uint p_size, // size of 'p_data'
  486. bool p_term // false: print only whole lines
  487. // true: print complete data
  488. )
  489. {
  490. DASSERT(xd);
  491. if ( !p_data || !xd->out_func )
  492. return 0;
  493. XParam_t p;
  494. SetupXParam(&p,xd,p_data,p_size,p_term);
  495. while(SetupXParamLine(&p,xd))
  496. {
  497. if ( xd->mode_ignore && CheckXParamNull(&p,xd) )
  498. continue;
  499. if (xd->print_number)
  500. {
  501. char *num = p.num_buf;
  502. uint col;
  503. for ( col = 0; col < xd->cols_per_line; col++ )
  504. {
  505. if (!--p.extra_space)
  506. {
  507. p.extra_space = xd->col_space;
  508. *num++ = ' ';
  509. }
  510. if ( p.skip > 0 )
  511. {
  512. p.skip--;
  513. memset(num,' ',xd->num_format_fw);
  514. num += xd->num_format_fw;
  515. }
  516. else if ( col < p.max_col )
  517. {
  518. double val;
  519. ccp format;
  520. if ( xd->bytes_per_col == 4 )
  521. {
  522. val = xd->endian_func->rdf4(p.d);
  523. format = fabs(val) <= 9999.0 && fabs(val) >= 1e-3
  524. ? xd->num_format1 : xd->num_format2;
  525. p.d += 4;
  526. }
  527. else
  528. {
  529. val = xd->endian_func->rdf8(p.d);
  530. format = fabs(val) <= 999999999.0 && fabs(val) >= 1e-6
  531. ? xd->num_format1 : xd->num_format2;
  532. p.d += 8;
  533. }
  534. num += sprintf(num,format,val);
  535. }
  536. else if ( p.term && !p.next_line && !xd->mode_c )
  537. {
  538. if ( p.d < p.end )
  539. num += sprintf(num, "%*c%u", xd->num_format_fw-1,
  540. '>', (int)(p.end-p.d) );
  541. else if ( xd->print_text )
  542. num += sprintf(num,"%*c", xd->num_format_fw, '/' );
  543. else if (xd->print_text)
  544. {
  545. memset(num,' ',xd->num_format_fw);
  546. num += xd->num_format_fw;
  547. }
  548. p.term = false; // print only once
  549. }
  550. else if (xd->print_text)
  551. {
  552. memset(num,' ',xd->num_format_fw);
  553. num += xd->num_format_fw;
  554. }
  555. }
  556. *num = 0;
  557. ASSERT( num < p.num_buf + sizeof(p.num_buf));
  558. xd->last_number_fw = num - p.num_buf;
  559. }
  560. else
  561. {
  562. p.d += p.line_size - p.skip * xd->bytes_per_col;
  563. p.skip = 0;
  564. }
  565. int stat = xd->out_func(xd,0,p.num_buf,p.text_buf);
  566. if ( stat < 0 )
  567. return stat;
  568. xd->addr += p.line_size;
  569. p.size -= p.line_size;
  570. }
  571. const uint written = p.d - (u8*)p_data;
  572. xd->written += written;
  573. return written;
  574. }
  575. //
  576. ///////////////////////////////////////////////////////////////////////////////
  577. /////////////// setup ///////////////
  578. ///////////////////////////////////////////////////////////////////////////////
  579. void InitializeXDump ( XDump_t *xd )
  580. {
  581. DASSERT(xd);
  582. memset(xd,0,sizeof(*xd));
  583. xd->f = stdout;
  584. xd->print_format =
  585. xd->print_addr =
  586. xd->print_number =
  587. xd->print_endmarker =
  588. xd->print_text =
  589. xd->print_diff_sep =
  590. xd->print_summary = true;
  591. }
  592. ///////////////////////////////////////////////////////////////////////////////
  593. ccp hexdump_prefix = "";
  594. ccp hexdump_eol = "\n";
  595. bool hexdump_align = false;
  596. //-----------------------------------------------------------------------------
  597. void InitializeXDumpEx
  598. (
  599. XDump_t *xd,
  600. FILE *f,
  601. int indent,
  602. u64 addr,
  603. int addr_fw,
  604. int row_len
  605. )
  606. {
  607. DASSERT(xd);
  608. InitializeXDump(xd);
  609. xd->f = f;
  610. xd->prefix = hexdump_prefix;
  611. xd->eol = hexdump_eol;
  612. xd->mode_align = hexdump_align;
  613. xd->indent = indent;
  614. xd->start_addr = addr;
  615. xd->min_addr_fw = addr_fw;
  616. xd->min_width = row_len;
  617. xd->format = XDUMPF_INT_1;
  618. xd->print_format = false;
  619. xd->print_summary = false;
  620. xd->print_diff_sep = false;
  621. hexdump_prefix = EmptyString;
  622. hexdump_eol = "\n";
  623. hexdump_align = false;
  624. }
  625. ///////////////////////////////////////////////////////////////////////////////
  626. void SetupXDump ( XDump_t *xd, XDumpCommand_t cmd )
  627. {
  628. DASSERT(xd);
  629. char buf[100];
  630. xd->cmd = (uint)cmd < XDUMPC__N ? cmd : XDUMPC_DIFF;
  631. if ( xd->endian != DC_SECOND_ENDIAN )
  632. xd->endian = DC_DEFAULT_ENDIAN;
  633. xd->endian_func = xd->endian == DC_LITTLE_ENDIAN ? &le_func : &be_func;
  634. //--- address field width
  635. xd->addr = xd->start_addr;
  636. xd->addr_fw = xd->min_addr_fw;
  637. if ( !xd->min_addr_fw || xd->addr_fw < xd->min_addr_fw )
  638. {
  639. uint len = snprintf(buf,sizeof(buf),"%llx",xd->addr);
  640. if ( xd->addr_fw < len )
  641. xd->addr_fw = len;
  642. const u64 size = xd->assumed_size ? xd->assumed_size : 0x100;
  643. len = snprintf(buf,sizeof(buf),"%llx", xd->addr + size - 1 );
  644. if ( xd->addr_fw < len )
  645. xd->addr_fw = len;
  646. if (xd->max_addr_fw)
  647. {
  648. if ( xd->addr_fw > xd->max_addr_fw )
  649. xd->addr_fw = xd->max_addr_fw;
  650. if ( xd->addr_fw < xd->min_addr_fw )
  651. xd->addr_fw = xd->min_addr_fw;
  652. }
  653. }
  654. //--- misc
  655. xd->indent = NormalizeIndent(xd->indent);
  656. if (!xd->out_func)
  657. xd->out_func = XDumpStandardOutput;
  658. if (!xd->prefix)
  659. xd->prefix = EmptyString;
  660. if (!xd->eol)
  661. xd->eol = "\n";
  662. xd->num_format2 = 0;
  663. xd->format_printed = 0;
  664. //--- format
  665. if ( (uint)xd->format >= XDUMPF__N )
  666. xd->format = XDUMPF_AUTO;
  667. if ( xd->format == XDUMPF_AUTO && xd->cmd != XDUMPC_SCAN )
  668. xd->format = XDUMPF_INT_1;
  669. switch(xd->format)
  670. {
  671. case XDUMPF_INT_2:
  672. xd->bytes_per_col = 2;
  673. xd->dump_func = DumpInt32;
  674. xd->format_name = xd->mode_dec ? "DEC2" : "HEX2";
  675. xd->max_format_fw = 7;
  676. xd->num_format1
  677. = xd->mode_c ? ( xd->mode_dec ? "%5u," : "0x%04x," )
  678. : xd->mode_zero ? ( xd->mode_dec ? " %05u" : " %04x" )
  679. : ( xd->mode_dec ? " %5u" : " %4x" );
  680. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1);
  681. break;
  682. case XDUMPF_INT_3:
  683. xd->bytes_per_col = 3;
  684. xd->dump_func = DumpInt32;
  685. xd->format_name = xd->mode_dec ? "DEC3" : "HEX3";
  686. xd->max_format_fw = 9;
  687. xd->num_format1
  688. = xd->mode_c ? ( xd->mode_dec ? "%8u," : "0x%06x," )
  689. : xd->mode_zero ? ( xd->mode_dec ? " %08u" : " %06x" )
  690. : ( xd->mode_dec ? " %8u" : " %6x" );
  691. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1);
  692. break;
  693. case XDUMPF_INT_4:
  694. xd->bytes_per_col = 4;
  695. xd->dump_func = DumpInt32;
  696. xd->format_name = xd->mode_dec ? "DEC4" : "HEX4";
  697. xd->max_format_fw = 11;
  698. xd->num_format1
  699. = xd->mode_c ? ( xd->mode_dec ? "%10u," : "0x%08x," )
  700. : xd->mode_zero ? ( xd->mode_dec ? " %010u" : " %08x" )
  701. : ( xd->mode_dec ? " %10u" : " %8x" );
  702. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1);
  703. break;
  704. case XDUMPF_INT_5:
  705. xd->bytes_per_col = 5;
  706. xd->dump_func = DumpInt64;
  707. xd->format_name = xd->mode_dec ? "DEC5" : "HEX5";
  708. xd->max_format_fw = 13;
  709. xd->num_format1
  710. = xd->mode_c ? ( xd->mode_dec ? "%12llu," : "0x%010llx," )
  711. : xd->mode_zero ? ( xd->mode_dec ? " %012llu" : " %010llx" )
  712. : ( xd->mode_dec ? " %12llu" : " %10llx" );
  713. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1ull);
  714. break;
  715. case XDUMPF_INT_6:
  716. xd->bytes_per_col = 6;
  717. xd->dump_func = DumpInt64;
  718. xd->format_name = xd->mode_dec ? "DEC6" : "HEX6";
  719. xd->max_format_fw = 16;
  720. xd->num_format1
  721. = xd->mode_c ? ( xd->mode_dec ? "%15llu," : "0x%012llx," )
  722. : xd->mode_zero ? ( xd->mode_dec ? " %015llu" : " %012llx" )
  723. : ( xd->mode_dec ? " %15llu" : " %12llx" );
  724. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1ull);
  725. break;
  726. case XDUMPF_INT_7:
  727. xd->bytes_per_col = 7;
  728. xd->dump_func = DumpInt64;
  729. xd->format_name = xd->mode_dec ? "DEC7" : "HEX7";
  730. xd->max_format_fw = 18;
  731. xd->num_format1
  732. = xd->mode_c ? ( xd->mode_dec ? "%17llu," : "0x%014llx," )
  733. : xd->mode_zero ? ( xd->mode_dec ? " %017llu" : " %014llx" )
  734. : ( xd->mode_dec ? " %17llu" : " %14llx" );
  735. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1ull);
  736. break;
  737. case XDUMPF_INT_8:
  738. xd->bytes_per_col = 8;
  739. xd->dump_func = DumpInt64;
  740. xd->format_name = xd->mode_dec ? "DEC8" : "HEX8";
  741. xd->max_format_fw = 20;
  742. xd->num_format1
  743. = xd->mode_c ? ( xd->mode_dec ? "%19llu," : "0x%016llx," )
  744. : xd->mode_zero ? ( xd->mode_dec ? " %019llu" : " %016llx" )
  745. : ( xd->mode_dec ? " %19llu" : " %16llx" );
  746. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1ull);
  747. break;
  748. case XDUMPF_FLOAT:
  749. xd->bytes_per_col = 4;
  750. xd->dump_func = DumpFloat;
  751. xd->format_name = "FLOAT";
  752. xd->max_format_fw = 12;
  753. xd->num_format1 = xd->mode_c ? "%11.5g," : " %11.5f";
  754. xd->num_format2 = xd->mode_c ? "%11.5g," : " %11.5g";
  755. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1.1);
  756. break;
  757. case XDUMPF_DOUBLE:
  758. xd->bytes_per_col = 8;
  759. xd->dump_func = DumpFloat;
  760. xd->format_name = "DOUBLE";
  761. xd->max_format_fw = 21;
  762. xd->num_format1 = xd->mode_c ? "%20.8g," : " %20.8f";
  763. xd->num_format2 = xd->mode_c ? "%20.8g," : " %20.8g";
  764. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1.1);
  765. break;
  766. default:
  767. //case XDUMPF_AUTO:
  768. //case XDUMPF_INT_1:
  769. xd->bytes_per_col = 1;
  770. xd->dump_func = DumpInt32;
  771. xd->format_name = xd->mode_dec ? "DEC1" : "HEX1";
  772. xd->max_format_fw = 5;
  773. xd->num_format1
  774. = xd->mode_c ? ( xd->mode_dec ? "%3u," : "0x%02x," )
  775. : xd->mode_zero ? ( xd->mode_dec ? " %03u" : " %02x" )
  776. : ( xd->mode_dec ? " %3u" : " %2x" );
  777. xd->num_format_fw = snprintf(buf,sizeof(buf),xd->num_format1,1);
  778. break;
  779. }
  780. //--- bytes per line
  781. uint min = xd->min_width ? xd->min_width : 16;
  782. uint max = xd->max_width ? xd->max_width : min + 2*xd->bytes_per_col/3;
  783. if ( max > XDUMP_MAX_BYTES_PER_LINE )
  784. max = XDUMP_MAX_BYTES_PER_LINE;
  785. if ( min > max || !xd->min_width )
  786. min = max;
  787. uint ncols = ( min + xd->bytes_per_col - 1 ) / xd->bytes_per_col;
  788. uint ncols_max = max / xd->bytes_per_col;
  789. if ( ncols > ncols_max )
  790. ncols = ncols_max;
  791. xd->cols_per_line = ncols ? ncols : 1;
  792. xd->bytes_per_line = xd->cols_per_line * xd->bytes_per_col;
  793. //--- extra space
  794. if ( xd->extra_space < 0 || xd->cols_per_line <= 3 )
  795. xd->col_space = 0;
  796. else
  797. {
  798. xd->col_space = xd->extra_space;
  799. if (!xd->col_space)
  800. {
  801. xd->col_space = 4 / xd->bytes_per_col;
  802. if ( xd->col_space < 2 )
  803. xd->col_space = 2;
  804. }
  805. if ( xd->col_space >= xd->cols_per_line )
  806. xd->col_space = 0;
  807. }
  808. }
  809. //
  810. ///////////////////////////////////////////////////////////////////////////////
  811. /////////////// print xdump params ///////////////
  812. ///////////////////////////////////////////////////////////////////////////////
  813. void PrintXDumpParam
  814. (
  815. FILE *f, // output file, abort if NULL
  816. int indent, // indention
  817. const ColorSet_t *colset, // NULL or color set
  818. const XDump_t *xd // data to print
  819. )
  820. {
  821. if ( !f || !xd )
  822. return;
  823. indent = NormalizeIndent(indent);
  824. if (!colset)
  825. colset = GetColorSet0();
  826. fprintf(f,
  827. "%*s%s" "Command: %s%s%s\n"
  828. "%*s%s" "Format: %s%s, %s%s%s => %s%s\n"
  829. "%*s%s" "Endian: %s%s%s\n",
  830. indent,"", colset->name, colset->value,
  831. (uint)xd->cmd < XDUMPC__N ? XDumpCommandName[xd->cmd] : "?",
  832. colset->reset,
  833. indent,"", colset->name, colset->value,
  834. (uint)xd->format < XDUMPF__N ? XDumpFormatName[xd->format] : "?",
  835. xd->mode_dec ? "decimal" : "hexadecimal",
  836. xd->mode_zero ? " with leading zeros" : "",
  837. xd->mode_c ? ", print in C syntax" : "",
  838. xd->format_name ? xd->format_name : "?",
  839. colset->reset,
  840. indent,"", colset->name, colset->value,
  841. xd->endian == DC_LITTLE_ENDIAN ? "LITTLE"
  842. : xd->endian == DC_BIG_ENDIAN ? "BIG" : "?",
  843. colset->reset );
  844. if (xd->num_format2)
  845. fprintf(f,
  846. "%*s%s" "Num format: %s'%s' & '%s' (fw=%u,max=%u)%s\n",
  847. indent,"", colset->name, colset->value,
  848. xd->num_format1 ? xd->num_format1 : "?",
  849. xd->num_format2, xd->num_format_fw, xd->max_format_fw,
  850. colset->reset );
  851. else
  852. fprintf(f,
  853. "%*s%s" "Num format: %s'%s' (fw=%u,max=%u)%s\n",
  854. indent,"", colset->name, colset->value,
  855. xd->num_format1 ? xd->num_format1 : "?",
  856. xd->num_format_fw, xd->max_format_fw,
  857. colset->reset );
  858. if (xd->assumed_size)
  859. fprintf(f, "%*s%s"
  860. "Address: %sstart=0x%llx, current=0x%llx, size=0x%llx%s%s\n",
  861. indent,"", colset->name, colset->value,
  862. xd->start_addr, xd->addr, xd->assumed_size,
  863. xd->mode_align ? ", aligned" : "",
  864. colset->reset );
  865. else
  866. fprintf(f, "%*s%s"
  867. "Address: %sstart=0x%llx, current=0x%llx%s%s\n",
  868. indent,"", colset->name, colset->value,
  869. xd->start_addr, xd->addr,
  870. xd->mode_align ? ", aligned" : "",
  871. colset->reset );
  872. if (xd->have_trigger)
  873. fprintf(f, "%*s%s"
  874. "Line trigger: %s%llu = 0x%llx%s%s\n",
  875. indent,"", colset->name, colset->value,
  876. xd->trigger, xd->trigger,
  877. xd->trigger == '\n' ? " (LF)" :
  878. xd->trigger == '\r' ? " (CR)" :
  879. xd->trigger == '\t' ? " (TAB)" :
  880. xd->trigger == ' ' ? " (SPACE)" :
  881. xd->trigger == 0 ? " (NULL)" :
  882. "",
  883. colset->reset );
  884. fprintf(f,
  885. "%*s%s" "Address fw: %smin=%u, max=%u, active=%u%s\n",
  886. indent,"", colset->name, colset->value,
  887. xd->min_addr_fw, xd->max_addr_fw, xd->addr_fw,
  888. colset->reset );
  889. fprintf(f,
  890. "%*s%s" "Print: %s%sformat, %saddress, %snumbers, %stext, %ssummary, %sseparator%s\n",
  891. indent,"", colset->name, colset->value,
  892. xd->print_format ? "" : "no ",
  893. xd->print_addr ? "" : "no ",
  894. xd->print_number ? "" : "no ",
  895. xd->print_text ? "" : "no ",
  896. xd->print_summary ? "" : "no ",
  897. xd->print_diff_sep ? "" : "no ",
  898. colset->reset );
  899. fprintf(f,
  900. "%*s%s" "Output file: %s%s, indent=%d, prefix=%s, eol=%s%s\n",
  901. indent,"", colset->name, colset->value,
  902. xd->f == stdout ? "STDOUT"
  903. : xd->f == stderr ? "STDERR"
  904. : xd->f ? "FILE" : "NONE",
  905. xd->indent,
  906. !xd->prefix ? "NULL" : !*xd->prefix ? "EMPTY" : "DEFINED",
  907. !xd->eol ? "NULL"
  908. : !*xd->eol ? "EMPTY"
  909. : !strcmp(xd->eol,"\n") ? "LF"
  910. : !strcmp(xd->eol,"\r") ? "CR"
  911. : !strcmp(xd->eol,"\r\n") ? "CR+LF"
  912. : "DEFINED",
  913. colset->reset );
  914. fprintf(f,
  915. "%*s%s" "Bytes/line: %smin=%u, max=%u%s\n"
  916. "%*s%s" " =>: %sbytes/column = %u, columns/line = %u, bytes/line = %u%s\n"
  917. "%*s%s" "Extra space: %severy %u column => every %u column%s\n",
  918. indent,"", colset->name, colset->value,
  919. xd->min_width, xd->max_width,
  920. colset->reset,
  921. indent,"", colset->name, colset->value,
  922. xd->bytes_per_col, xd->cols_per_line, xd->bytes_per_line,
  923. colset->reset,
  924. indent,"", colset->name, colset->value,
  925. xd->extra_space, xd->col_space,
  926. colset->reset );
  927. if (xd->mode_ignore)
  928. fprintf(f,
  929. "%*s" "%sNULL bytes: %sPrint a summary for lines with NULL bytes only.%s\n",
  930. indent,"", colset->name, colset->value, colset->reset );
  931. fprintf(f,
  932. "%*s%s" "Functions: %sdump=%s, output=%s, endian=%s%s\n",
  933. indent,"", colset->name, colset->value,
  934. !xd->dump_func ? "-" : "SET",
  935. !xd->out_func ? "-"
  936. : xd->out_func == XDumpStandardOutput
  937. ? "STANDARD" : "SET",
  938. !xd->endian_func ? "-"
  939. : xd->endian_func == &le_func ? "LITTLE"
  940. : xd->endian_func == &be_func ? "BIG"
  941. : "SET",
  942. colset->reset );
  943. fprintf(f,
  944. "%*s%s" "User param: %sint=%d, ptr=%s%s\n",
  945. indent,"", colset->name, colset->value,
  946. xd->user_int, xd->user_ptr ? "SET" : "-",
  947. colset->reset );
  948. }
  949. //
  950. ///////////////////////////////////////////////////////////////////////////////
  951. /////////////// dump interface ///////////////
  952. ///////////////////////////////////////////////////////////////////////////////
  953. static void SetupRemainder
  954. ( XDump_t *xd, const XDump_t *src, XDumpCommand_t cmd, uint size )
  955. {
  956. DASSERT(xd);
  957. DASSERT(src);
  958. memcpy(xd,src,sizeof(*xd));
  959. xd->format = XDUMPF_INT_1;
  960. xd->start_addr = xd->addr;
  961. xd->assumed_size = size;
  962. xd->min_width = size;
  963. xd->max_width = size;
  964. xd->extra_space = 0;
  965. xd->mode_align = false;
  966. xd->mode_ignore = false;
  967. if (xd->print_text)
  968. xd->min_number_fw = xd->last_number_fw;
  969. SetupXDump(xd,cmd);
  970. }
  971. ///////////////////////////////////////////////////////////////////////////////
  972. static int XDumpHelper
  973. (
  974. // returns <0 on error, or number of dumped bytes
  975. XDump_t *xd, // params; SetupXDump() already done!
  976. const void *data, // data to dump
  977. uint size, // size of 'data'
  978. bool term // false: print only whole lines
  979. // true: print complete data
  980. )
  981. {
  982. DASSERT(xd);
  983. DASSERT(data||!size);
  984. PrintFormat(xd,0);
  985. int stat = xd->dump_func(xd,data,size,term);
  986. if ( stat < 0 )
  987. return stat;
  988. if (term)
  989. {
  990. PrintNullSummary(xd);
  991. if ( stat < size )
  992. {
  993. XDump_t local;
  994. SetupRemainder(&local,xd,XDUMPC_DUMP,size-stat);
  995. PrintFormat(&local,0);
  996. int stat2 = local.dump_func(&local,data+stat,local.min_width,term);
  997. xd->addr = local.addr;
  998. xd->written = local.written;
  999. if ( stat2 < 0 )
  1000. return stat2;
  1001. stat += stat2;
  1002. }
  1003. if ( xd->f && xd->print_summary )
  1004. {
  1005. fprintf(xd->f,"%s%*s",xd->prefix,xd->indent,"");
  1006. if ( xd->print_addr )
  1007. {
  1008. if (xd->mode_c)
  1009. fprintf(xd->f,"//%*llx// [%llu Bytes]%s",
  1010. xd->addr_fw, xd->addr,
  1011. xd->written, xd->eol );
  1012. else
  1013. fprintf(xd->f,"%*llx:%s [%llu Bytes]%s",
  1014. xd->addr_fw, xd->addr,
  1015. xd->extra_space >= 0 ? " " : "",
  1016. xd->written, xd->eol );
  1017. }
  1018. else
  1019. {
  1020. if (xd->mode_c)
  1021. fprintf(xd->f,"// [%llu Bytes]%s",
  1022. xd->written, xd->eol );
  1023. else
  1024. fprintf(xd->f,"%s [%llu Bytes]%s",
  1025. xd->extra_space >= 0 ? " " : "",
  1026. xd->written, xd->eol );
  1027. }
  1028. }
  1029. }
  1030. return stat;
  1031. }
  1032. ///////////////////////////////////////////////////////////////////////////////
  1033. int XDump
  1034. (
  1035. // returns <0 on error, or number of dumped bytes
  1036. XDump_t *xd, // params; if NULL: use local version
  1037. const void *data, // data to dump
  1038. uint size, // size of 'data'
  1039. bool term // false: print only whole lines
  1040. // true: print complete data
  1041. )
  1042. {
  1043. XDump_t local_xd;
  1044. if (!xd)
  1045. {
  1046. InitializeXDump(&local_xd);
  1047. xd = &local_xd;
  1048. }
  1049. SetupXDump(xd,XDUMPC_DUMP);
  1050. return XDumpHelper(xd,data,size,term);
  1051. }
  1052. ///////////////////////////////////////////////////////////////////////////////
  1053. int XDumpByFile
  1054. (
  1055. // returns <0 on error, or number of dumped bytes
  1056. XDump_t *xd, // params; if NULL: use local version
  1057. FILE *f, // input file
  1058. u64 max_size // >0: limit size to print
  1059. )
  1060. {
  1061. DASSERT(f);
  1062. XDump_t local_xd;
  1063. if (!xd)
  1064. {
  1065. InitializeXDump(&local_xd);
  1066. xd = &local_xd;
  1067. }
  1068. SetupXDump(xd,XDUMPC_DUMP);
  1069. char buf[0x4100];
  1070. uint start = 0;
  1071. if (!max_size)
  1072. max_size = M1(max_size);
  1073. uint count = 0;
  1074. while ( max_size > 0 )
  1075. {
  1076. uint max_read = ( sizeof(buf) - start ) / 0x1000 * 0x1000;
  1077. if ( max_read > max_size )
  1078. max_read = max_size;
  1079. size_t stat = fread(buf+start,1,max_read,f);
  1080. if (!stat)
  1081. break;
  1082. max_size -= stat;
  1083. stat += start;
  1084. int dstat = XDumpHelper(xd,buf,stat,false);
  1085. if ( dstat < 0 )
  1086. return dstat;
  1087. count += dstat;
  1088. start = stat - dstat;
  1089. if (start)
  1090. memmove(buf,buf+dstat,start);
  1091. }
  1092. int dstat = XDumpHelper(xd,buf,start,true);
  1093. return dstat < 0 ? dstat : count + dstat;
  1094. }
  1095. ///////////////////////////////////////////////////////////////////////////////
  1096. int XDump16 // wrapper for a standard hexdump
  1097. (
  1098. // returns <0 on error, or number of dumped bytes
  1099. FILE *f_out, // output file
  1100. int indent, // indention
  1101. u64 addr, // start address
  1102. const void *data, // data
  1103. uint size // size of 'data'
  1104. )
  1105. {
  1106. XDump_t xd;
  1107. InitializeXDump(&xd);
  1108. xd.f = f_out;
  1109. xd.indent = indent;
  1110. xd.start_addr = addr;
  1111. xd.assumed_size = size;
  1112. return XDump(&xd,data,size,true);
  1113. }
  1114. //
  1115. ///////////////////////////////////////////////////////////////////////////////
  1116. /////////////// diff interface ///////////////
  1117. ///////////////////////////////////////////////////////////////////////////////
  1118. typedef struct XDiff_t
  1119. {
  1120. const void *data; // data to compare
  1121. uint size; // size of 'data'
  1122. bool term; // 'data' reached end
  1123. XDump_t xd; // dump parameters
  1124. //--- buffers to catch output
  1125. char num_buf[XDUMP_GOOD_NUM_BUF_SIZE+4];
  1126. char text_buf[XDUMP_GOOD_TEXT_BUF_SIZE];
  1127. }
  1128. XDiff_t;
  1129. ///////////////////////////////////////////////////////////////////////////////
  1130. static int XDiffCatchOutput
  1131. (
  1132. struct XDump_t *xd, // internal copy of params
  1133. ccp prefix, // NULL or additional prefix (used by diff)
  1134. ccp dump, // dump to print
  1135. ccp text // text to print
  1136. )
  1137. {
  1138. DASSERT(xd);
  1139. DASSERT(dump);
  1140. DASSERT(text);
  1141. XDiff_t *diff = xd->user_ptr;
  1142. DASSERT(diff);
  1143. StringCopyS(diff->num_buf,sizeof(diff->num_buf),dump);
  1144. StringCopyS(diff->text_buf,sizeof(diff->text_buf),text);
  1145. return 0;
  1146. }
  1147. ///////////////////////////////////////////////////////////////////////////////
  1148. static void SetupXDiff
  1149. (
  1150. XDiff_t *xdiff, // data to setup
  1151. XDump_t *xd, // NULL or template
  1152. const void *data, // data to compare
  1153. uint size, // size of 'data'
  1154. bool term // 'data' reached end
  1155. )
  1156. {
  1157. DASSERT(xdiff);
  1158. xdiff->data = data;
  1159. xdiff->size = size;
  1160. xdiff->term = term;
  1161. *xdiff->num_buf = *xdiff->text_buf = 0;
  1162. if (xd)
  1163. memcpy(&xdiff->xd,xd,sizeof(xdiff->xd));
  1164. else
  1165. InitializeXDump(&xdiff->xd);
  1166. xd = &xdiff->xd;
  1167. SetupXDump(xd,XDUMPC_DIFF);
  1168. xd->user_ptr = xdiff;
  1169. xd->out_func = XDiffCatchOutput;
  1170. xd->mode_ignore = false;
  1171. }
  1172. ///////////////////////////////////////////////////////////////////////////////
  1173. static bool XDiffText
  1174. (
  1175. // returns true if differ
  1176. XDump_t *xd, // original params; SetupXDump() already done!
  1177. XDiff_t *d1, // first diff file
  1178. XDiff_t *d2, // second diff file
  1179. bool colorize // true: use red and green on output
  1180. )
  1181. {
  1182. DASSERT(xd);
  1183. DASSERT(d1);
  1184. DASSERT(d2);
  1185. char *n1 = d1->num_buf;
  1186. char *n2 = d2->num_buf;
  1187. int len1 = strlen(n1);
  1188. int len2 = strlen(n2);
  1189. if ( len1 < len2 )
  1190. {
  1191. memset( n1 + len1, ' ', len2-len1 );
  1192. n1[len2] = 0;
  1193. len1 = len2;
  1194. }
  1195. else if ( len1 > len2 )
  1196. {
  1197. memset( n2 + len2, ' ', len1-len2 );
  1198. n2[len1] = 0;
  1199. }
  1200. if ( xd->min_number_fw < len1 )
  1201. xd->min_number_fw = len1;
  1202. if (!strcmp(n1,n2))
  1203. return false;
  1204. for(;;)
  1205. {
  1206. while ( *n1 == ' ' && *n2 == ' ' )
  1207. n1++, n2++;
  1208. if ( !*n1 || !*n2 )
  1209. break;
  1210. char *x = n2;
  1211. while ( (u8)*n2 > ' ' && *n1 == *n2 && *n2 != '/' && *n2 != '/' )
  1212. n1++, n2++;
  1213. if ( n2 > x && (u8)*n2 <= ' ' )
  1214. {
  1215. while ( x < n2 )
  1216. *x++ = ' ';
  1217. x[-1] = '.';
  1218. }
  1219. else
  1220. while ( (u8)*n1 > ' ' || (u8)*n2 > ' ' )
  1221. n1++, n2++;
  1222. }
  1223. xd->out_func(xd,"< ",d1->num_buf,d1->text_buf);
  1224. xd->out_func(xd,"> ",d2->num_buf,d2->text_buf);
  1225. if ( xd->f && xd->print_diff_sep )
  1226. fputs(xd->eol,xd->f);
  1227. xd->diff_count++;
  1228. return true;
  1229. }
  1230. ///////////////////////////////////////////////////////////////////////////////
  1231. #undef OPTIMIZED_SEARCH
  1232. #define OPTIMIZED_SEARCH 0
  1233. static int XDiffHelper
  1234. (
  1235. // returns <0 on error, or number of dumped bytes
  1236. XDump_t *xd, // original params; SetupXDump() already done!
  1237. XDiff_t *d1, // first diff file
  1238. XDiff_t *d2, // second diff file
  1239. uint limit_lines, // >0: limit number of differ-lines
  1240. bool colorize // true: use red and green on output
  1241. )
  1242. {
  1243. DASSERT(xd);
  1244. DASSERT(d1);
  1245. DASSERT(d2);
  1246. DASSERT( d1->xd.bytes_per_col == d1->xd.bytes_per_col );
  1247. DASSERT( d1->xd.bytes_per_line == d1->xd.bytes_per_line );
  1248. DASSERT( d1->xd.format == d1->xd.format );
  1249. //--- size calculations & setup
  1250. uint count = 0;
  1251. int size1 = d1->size;
  1252. int size2 = d2->size;
  1253. if ( !d1->term && !d2->term )
  1254. {
  1255. if ( size1 < size2 )
  1256. size2 = size1;
  1257. else if ( size1 > size2 )
  1258. size1 = size2;
  1259. }
  1260. if (!d1->term)
  1261. size1 = size1 / xd->bytes_per_line * xd->bytes_per_line;
  1262. if (!d2->term)
  1263. size2 = size2 / xd->bytes_per_line * xd->bytes_per_line;
  1264. const u8 *p1 = d1->data;
  1265. const u8 *p2 = d2->data;
  1266. const u64 *save_addr_break = xd->addr_break;
  1267. const u64 *addr_break = save_addr_break;
  1268. xd->addr_break = 0;
  1269. //--- main loop
  1270. int done = 0;
  1271. while ( size1 > 0 && size2 > 0
  1272. || !done && ( size1 > 0 || size2 > 0 ) )
  1273. {
  1274. //--- optimized search
  1275. #if OPTIMIZED_SEARCH
  1276. uint same = CountEqual(p1,p2, size1 < size2 ? size1 : size2 );
  1277. if ( same >= xd->bytes_per_line )
  1278. {
  1279. same = same / xd->bytes_per_line * xd->bytes_per_line;
  1280. p1 += same;
  1281. p2 += same;
  1282. xd->addr += same;
  1283. count += same;
  1284. size1 -= same;
  1285. size2 -= same;
  1286. }
  1287. #endif
  1288. //---
  1289. int dumpsize1 = size1 < (int)xd->bytes_per_line ? size1 : xd->bytes_per_line;
  1290. int dumpsize2 = size2 < (int)xd->bytes_per_line ? size2 : xd->bytes_per_line;
  1291. bool have_break = false;
  1292. if (addr_break)
  1293. {
  1294. while ( *addr_break && xd->addr >= *addr_break )
  1295. addr_break++;
  1296. if (!*addr_break)
  1297. addr_break = 0;
  1298. else
  1299. {
  1300. const s64 max = *addr_break - xd->addr;
  1301. if ( dumpsize1 > max || dumpsize2 > max )
  1302. {
  1303. have_break = true;
  1304. if ( dumpsize1 > max ) dumpsize1 = max;
  1305. if ( dumpsize2 > max ) dumpsize2 = max;
  1306. }
  1307. }
  1308. }
  1309. #if !OPTIMIZED_SEARCH
  1310. const uint diffsize = dumpsize1 < dumpsize2 ? dumpsize1 : dumpsize2;
  1311. #endif
  1312. uint donesize = dumpsize1 > dumpsize2 ? dumpsize1 : dumpsize2;
  1313. //DEL fprintf(stderr,">>> %d,%d done=%d, donesize=%d, diffsize=%d\n",size1,size2,done,donesize,diffsize);
  1314. #if !OPTIMIZED_SEARCH
  1315. if ( diffsize > 0
  1316. && ( diffsize < xd->bytes_per_line || memcmp(p1,p2,diffsize) ))
  1317. #endif
  1318. {
  1319. if ( dumpsize1 != dumpsize2 && !xd->diff_size )
  1320. {
  1321. xd->diff_size = dumpsize1 - dumpsize2;
  1322. if (xd->diff_size)
  1323. xd->diff_count++;
  1324. }
  1325. if ( dumpsize1 >= (int)xd->bytes_per_col )
  1326. {
  1327. const int stat = xd->dump_func(&d1->xd,p1,dumpsize1,true);
  1328. if ( stat >= 0 && stat < dumpsize1 && donesize > stat )
  1329. donesize = stat;
  1330. }
  1331. else if ( dumpsize1 > 0 )
  1332. {
  1333. SetupRemainder(&d1->xd,&d1->xd,XDUMPC_DIFF,dumpsize1);
  1334. d1->xd.dump_func(&d1->xd,p1,dumpsize1,true);
  1335. }
  1336. else
  1337. {
  1338. done++;
  1339. strcpy(d1->num_buf,"/");
  1340. *d1->text_buf = 0;
  1341. }
  1342. if ( dumpsize2 >= (int)xd->bytes_per_col )
  1343. {
  1344. const int stat = xd->dump_func(&d2->xd,p2,dumpsize2,true);
  1345. if ( stat >= 0 && stat < dumpsize2 && donesize > stat )
  1346. donesize = stat;
  1347. }
  1348. else if ( dumpsize2 > 0 )
  1349. {
  1350. SetupRemainder(&d2->xd,&d2->xd,XDUMPC_DIFF,dumpsize2);
  1351. d2->xd.dump_func(&d2->xd,p2,dumpsize2,true);
  1352. }
  1353. else
  1354. {
  1355. done++;
  1356. strcpy(d2->num_buf,"/");
  1357. *d2->text_buf = 0;
  1358. }
  1359. XDiffText(xd,d1,d2,colorize);
  1360. if ( limit_lines && !--limit_lines )
  1361. break;
  1362. }
  1363. p1 += donesize;
  1364. p2 += donesize;
  1365. xd->addr += donesize;
  1366. count += donesize;
  1367. size1 -= donesize;
  1368. size2 -= donesize;
  1369. if (have_break)
  1370. {
  1371. if (!d1->term)
  1372. size1 = size1 / xd->bytes_per_line * xd->bytes_per_line;
  1373. if (!d2->term)
  1374. size2 = size2 / xd->bytes_per_line * xd->bytes_per_line;
  1375. }
  1376. }
  1377. xd->addr_break = save_addr_break;
  1378. return count;
  1379. }
  1380. ///////////////////////////////////////////////////////////////////////////////
  1381. int XDiff
  1382. (
  1383. // returns <0 on error, or number of dumped bytes
  1384. XDump_t *xd, // params; if NULL: use local version
  1385. const void *data1, // data1 to compare
  1386. uint size1, // size of 'data1'
  1387. bool term1, // true: 'data1' completed
  1388. const void *data2, // data2 to compare
  1389. uint size2, // size of 'data2'
  1390. bool term2, // true: 'data2' completed
  1391. uint limit_lines, // >0: limit number of differ-lines
  1392. bool colorize // true: use red and green on output
  1393. )
  1394. {
  1395. // fast test for identical data
  1396. if ( term1 && term2 && size1 == size2 && ( !size1 || !memcmp(data1,data2,size1) ))
  1397. return 0;
  1398. XDump_t local_xd;
  1399. if (!xd)
  1400. {
  1401. InitializeXDump(&local_xd);
  1402. xd = &local_xd;
  1403. }
  1404. SetupXDump(xd,XDUMPC_DIFF);
  1405. XDiff_t diff1, diff2;
  1406. SetupXDiff(&diff1,xd,data1,size1,term1);
  1407. SetupXDiff(&diff2,xd,data2,size2,term2);
  1408. return XDiffHelper(xd,&diff1,&diff2,limit_lines,colorize);
  1409. }
  1410. ///////////////////////////////////////////////////////////////////////////////
  1411. int XDiffByFile
  1412. (
  1413. // returns <0 on error, or number of dumped bytes
  1414. XDump_t *xd, // params; if NULL: use local version
  1415. FILE *f1, // first input file
  1416. FILE *f2, // second input file
  1417. u64 max_size, // >0: limit comparing size
  1418. uint limit_lines, // >0: limit number of differ-lines
  1419. bool colorize // true: use red and green on output
  1420. )
  1421. {
  1422. DASSERT(f1);
  1423. DASSERT(f2);
  1424. XDump_t local_xd;
  1425. if (!xd)
  1426. {
  1427. InitializeXDump(&local_xd);
  1428. xd = &local_xd;
  1429. }
  1430. SetupXDump(xd,XDUMPC_DIFF);
  1431. char buf1[0x4100];
  1432. char buf2[sizeof(buf1)];
  1433. XDiff_t diff1, diff2;
  1434. SetupXDiff(&diff1,xd,buf1,0,false);
  1435. SetupXDiff(&diff2,xd,buf2,0,false);
  1436. uint start1 = 0, start2 = 0;
  1437. if (!max_size)
  1438. max_size = M1(max_size);
  1439. uint count = 0;
  1440. while ( max_size > 0 )
  1441. {
  1442. uint max_read1 = ( sizeof(buf1) - start1 ) / 0x1000 * 0x1000;
  1443. if ( max_read1 > max_size )
  1444. max_read1 = max_size;
  1445. size_t stat1 = fread(buf1+start1,1,max_read1,f1);
  1446. uint max_read2 = ( sizeof(buf2) - start2 ) / 0x1000 * 0x1000;
  1447. if ( max_read2 > max_size )
  1448. max_read2 = max_size;
  1449. size_t stat2 = fread(buf2+start2,1,max_read2,f2);
  1450. uint stat = stat1 < stat2 ? stat1 : stat2;
  1451. max_size -= stat;
  1452. diff1.size = stat1 + start1;
  1453. diff1.term = !stat1;
  1454. diff2.size = stat2 + start2;
  1455. diff2.term = !stat2;
  1456. int dstat = XDiffHelper(xd,&diff1,&diff2,limit_lines,colorize);
  1457. if ( dstat <= 0 )
  1458. return dstat;
  1459. count += dstat;
  1460. if (xd->diff_size)
  1461. return count;
  1462. start1 = stat1 + start1 - dstat;
  1463. if (start1)
  1464. memmove(buf1,buf1+dstat,start1);
  1465. start2 = stat2 + start2 - dstat;
  1466. if (start2)
  1467. memmove(buf2,buf2+dstat,start2);
  1468. }
  1469. return count;
  1470. }
  1471. ///////////////////////////////////////////////////////////////////////////////
  1472. int XDiff16
  1473. (
  1474. // returns <0 on error, or number of dumped bytes
  1475. FILE *f_out, // output file
  1476. int indent, // indention
  1477. u64 addr, // start address
  1478. const void *data1, // data
  1479. uint size1, // size of 'data'
  1480. const void *data2, // data
  1481. uint size2, // size of 'data'
  1482. uint limit_lines, // >0: limit number of differ-lines
  1483. bool colorize // true: use red and green on output
  1484. )
  1485. {
  1486. XDump_t xd;
  1487. InitializeXDump(&xd);
  1488. xd.f = f_out;
  1489. xd.indent = indent;
  1490. xd.start_addr = addr;
  1491. xd.assumed_size = size1 > size2 ? size1 : size2;
  1492. return XDiff( &xd, data1,size1,true,
  1493. data2,size2,true, limit_lines, colorize );
  1494. }
  1495. //
  1496. ///////////////////////////////////////////////////////////////////////////////
  1497. /////////////// scan interface ///////////////
  1498. ///////////////////////////////////////////////////////////////////////////////
  1499. // [[XScan_t]]
  1500. typedef struct XScan_t
  1501. {
  1502. FastBuf_t *dest; // not NULL: append scanned data here & ignore 'fout'
  1503. FILE *fout; // valid output file
  1504. u64 written; // number of written bytes
  1505. bool auto_format; // true: detect '#FORMAT:'
  1506. XDumpFormat_t format; // current scan format
  1507. dcEndian_t endian; // current endian
  1508. uint num_base; // 8:octal, 10:decimal, 16:hexadecimal
  1509. ccp num_allowed; // string with allowed first characters
  1510. ccp prefix; // not NULL: skip this prefix
  1511. uint prefix_len; // length of prefix
  1512. const endian_func_t *endian_func; // endian functions
  1513. }
  1514. XScan_t;
  1515. ///////////////////////////////////////////////////////////////////////////////
  1516. static void UpdateXScan ( XScan_t *scan )
  1517. {
  1518. DASSERT(scan);
  1519. if ( !scan->dest && !scan->fout )
  1520. scan->fout = stdout;
  1521. if (scan->prefix)
  1522. {
  1523. ccp prefix = scan->prefix;
  1524. while ( *prefix > 0 && *prefix <= ' ' )
  1525. prefix++;
  1526. scan->prefix = *prefix ? prefix : 0;
  1527. scan->prefix_len = strlen(prefix);
  1528. }
  1529. else
  1530. scan->prefix_len = 0;
  1531. scan->endian_func = scan->endian == DC_LITTLE_ENDIAN ? &le_func : &be_func;
  1532. if ( scan->format == XDUMPF_AUTO )
  1533. scan->format = XDUMPF_INT_1;
  1534. if ( scan->num_base < 2 || scan->num_base > 36 )
  1535. scan->num_base = 16;
  1536. scan->num_allowed
  1537. = scan->format == XDUMPF_FLOAT || scan->format == XDUMPF_DOUBLE
  1538. ? "+-."
  1539. : "+-~!";
  1540. }
  1541. ///////////////////////////////////////////////////////////////////////////////
  1542. static void SetupXScan
  1543. (
  1544. XScan_t *scan, // data to setup
  1545. XDump_t *xd, // NULL or template
  1546. FastBuf_t *dest // not NULL: append scanned data here
  1547. )
  1548. {
  1549. DASSERT(scan);
  1550. memset(scan,0,sizeof(*scan));
  1551. if (xd)
  1552. {
  1553. scan->fout = xd->f;
  1554. scan->auto_format = xd->format == XDUMPF_AUTO;
  1555. scan->format = xd->format;
  1556. scan->endian = xd->endian;
  1557. scan->num_base = xd->mode_dec ? 10 : 16;
  1558. scan->prefix = xd->prefix;
  1559. }
  1560. else
  1561. {
  1562. scan->fout = stdout;
  1563. scan->auto_format = true;
  1564. scan->format = XDUMPF_INT_1;
  1565. scan->endian = DC_DEFAULT_ENDIAN;
  1566. scan->num_base = 16;
  1567. scan->prefix = 0;
  1568. }
  1569. scan->dest = dest;
  1570. UpdateXScan(scan);
  1571. }
  1572. ///////////////////////////////////////////////////////////////////////////////
  1573. static void XScanFormat
  1574. (
  1575. // returns <0 on error, or number of dumped bytes
  1576. XScan_t *scan, // valid scan parameters
  1577. ccp src, // source pointer
  1578. ccp eol // end of line
  1579. )
  1580. {
  1581. DASSERT(scan);
  1582. DASSERT(src);
  1583. DASSERT(eol);
  1584. DASSERT( src <= eol );
  1585. noPRINT(stderr,"FORMAT: %.*s\n",(int)(eol-src),src);
  1586. enum { MD_HEX, MD_DEC, MD_OCT, MD_NUM, MD_ENDIAN };
  1587. static const KeywordTab_t keytab[] =
  1588. {
  1589. { MD_HEX, "HEX", 0, XDUMPF_AUTO },
  1590. { MD_HEX, "HEX-1", "HEX1", XDUMPF_INT_1 },
  1591. { MD_HEX, "HEX-2", "HEX2", XDUMPF_INT_2 },
  1592. { MD_HEX, "HEX-3", "HEX3", XDUMPF_INT_3 },
  1593. { MD_HEX, "HEX-4", "HEX4", XDUMPF_INT_4 },
  1594. { MD_HEX, "HEX-5", "HEX5", XDUMPF_INT_5 },
  1595. { MD_HEX, "HEX-6", "HEX6", XDUMPF_INT_6 },
  1596. { MD_HEX, "HEX-7", "HEX7", XDUMPF_INT_7 },
  1597. { MD_HEX, "HEX-8", "HEX8", XDUMPF_INT_8 },
  1598. { MD_DEC, "DEC", 0, XDUMPF_AUTO },
  1599. { MD_DEC, "DEC-1", "DEC1", XDUMPF_INT_1 },
  1600. { MD_DEC, "DEC-2", "DEC2", XDUMPF_INT_2 },
  1601. { MD_DEC, "DEC-3", "DEC3", XDUMPF_INT_3 },
  1602. { MD_DEC, "DEC-4", "DEC4", XDUMPF_INT_4 },
  1603. { MD_DEC, "DEC-5", "DEC5", XDUMPF_INT_5 },
  1604. { MD_DEC, "DEC-6", "DEC6", XDUMPF_INT_6 },
  1605. { MD_DEC, "DEC-7", "DEC7", XDUMPF_INT_7 },
  1606. { MD_DEC, "DEC-8", "DEC8", XDUMPF_INT_8 },
  1607. { MD_OCT, "OCT", 0, XDUMPF_AUTO },
  1608. { MD_OCT, "OCT-1", "OCT1", XDUMPF_INT_1 },
  1609. { MD_OCT, "OCT-2", "OCT2", XDUMPF_INT_2 },
  1610. { MD_OCT, "OCT-3", "OCT3", XDUMPF_INT_3 },
  1611. { MD_OCT, "OCT-4", "OCT4", XDUMPF_INT_4 },
  1612. { MD_OCT, "OCT-5", "OCT5", XDUMPF_INT_5 },
  1613. { MD_OCT, "OCT-6", "OCT6", XDUMPF_INT_6 },
  1614. { MD_OCT, "OCT-7", "OCT7", XDUMPF_INT_7 },
  1615. { MD_OCT, "OCT-8", "OCT8", XDUMPF_INT_8 },
  1616. { MD_NUM, "1", 0, XDUMPF_INT_1 },
  1617. { MD_NUM, "2", 0, XDUMPF_INT_2 },
  1618. { MD_NUM, "3", 0, XDUMPF_INT_3 },
  1619. { MD_NUM, "4", 0, XDUMPF_INT_4 },
  1620. { MD_NUM, "5", 0, XDUMPF_INT_5 },
  1621. { MD_NUM, "6", 0, XDUMPF_INT_6 },
  1622. { MD_NUM, "7", 0, XDUMPF_INT_7 },
  1623. { MD_NUM, "8", 0, XDUMPF_INT_8 },
  1624. { MD_NUM, "FLOAT", 0, XDUMPF_FLOAT },
  1625. { MD_NUM, "DOUBLE", 0, XDUMPF_DOUBLE },
  1626. { MD_ENDIAN, "LE", "LITTLE", DC_LITTLE_ENDIAN },
  1627. { MD_ENDIAN, "BE", "BIG", DC_BIG_ENDIAN },
  1628. {0,0,0,0}
  1629. };
  1630. for(;;)
  1631. {
  1632. while ( src < eol && !isalnum((int)*src) )
  1633. src++;
  1634. char namebuf[20], *name = namebuf;
  1635. while ( src < eol && (u8)*src > ' ' )
  1636. {
  1637. if ( name < namebuf + sizeof(namebuf) - 1 )
  1638. *name++ = toupper((int)*src);
  1639. src++;
  1640. }
  1641. *name = 0;
  1642. if ( name == namebuf )
  1643. break;
  1644. noPRINT(stderr,"SCAN FORMAT: %s\n",namebuf);
  1645. const KeywordTab_t *key = ScanKeyword(0,namebuf,keytab);
  1646. if (!key)
  1647. continue;
  1648. XDumpFormat_t format = key->opt;
  1649. switch(key->id)
  1650. {
  1651. case MD_HEX:
  1652. scan->num_base = 16;
  1653. break;
  1654. case MD_DEC:
  1655. scan->num_base = 10;
  1656. break;
  1657. case MD_OCT:
  1658. scan->num_base = 8;
  1659. break;
  1660. case MD_NUM:
  1661. break;
  1662. case MD_ENDIAN:
  1663. scan->endian = key->opt;
  1664. format = XDUMPF_AUTO;
  1665. break;
  1666. }
  1667. if ( format != XDUMPF_AUTO )
  1668. scan->format = format;
  1669. noPRINT(stderr,"F: %lld %lld => base=%d, format=%d, endian=%d\n",
  1670. key->id, key->opt,
  1671. scan->num_base, scan->format, scan->endian );
  1672. }
  1673. UpdateXScan(scan);
  1674. }
  1675. ///////////////////////////////////////////////////////////////////////////////
  1676. static size_t XScanHelper
  1677. (
  1678. // returns <0 on error, or number of dumped bytes
  1679. XScan_t *scan, // valid scan parameters
  1680. ccp source, // source pointer
  1681. ccp end, // end of source
  1682. bool term, // true: accept uncomplete last line
  1683. u64 max_size // >0: limit output size
  1684. )
  1685. {
  1686. DASSERT(scan);
  1687. DASSERT( scan->dest || scan->fout );
  1688. DASSERT(source);
  1689. DASSERT(end);
  1690. DASSERT( source <= end );
  1691. if (!max_size)
  1692. max_size = M1(max_size);
  1693. max_size -= scan->written;
  1694. ccp src = source;
  1695. u8 destbuf[0x4000+10];
  1696. u8 *destmax = destbuf + sizeof(destbuf) - 10;
  1697. u8 *dest = destbuf;
  1698. while ( src < end && max_size > 0 )
  1699. {
  1700. //--- skip all leading contral characters
  1701. while ( src < end && (u8)*src <= ' ' )
  1702. src++;
  1703. //--- find end of line
  1704. bool found_eol = term;
  1705. ccp eol = src;
  1706. while ( eol < end )
  1707. {
  1708. const u8 ch = *eol;
  1709. if ( !ch || ch == '\n' || ch == '\r' )
  1710. {
  1711. found_eol = true;
  1712. break;
  1713. }
  1714. eol++;
  1715. }
  1716. if (!found_eol)
  1717. break;
  1718. PRINT0("LINE: |%.*s|\n",(int)(eol-src),src);
  1719. //--- skip prefix
  1720. if ( scan->prefix_len
  1721. && scan->prefix_len <= eol-src
  1722. && !memcmp(scan->prefix,src,scan->prefix_len) )
  1723. {
  1724. src += scan->prefix_len;
  1725. while ( src < eol && (u8)*src <= ' ' )
  1726. src++;
  1727. }
  1728. if ( src == eol )
  1729. continue;
  1730. //--- check for "#FORMAT:"
  1731. if (!strncasecmp(src,"#FORMAT:",8))
  1732. {
  1733. if (scan->auto_format)
  1734. XScanFormat(scan,src+8,eol);
  1735. src = eol;
  1736. continue;
  1737. }
  1738. //--- skip address column
  1739. ccp ptr = src;
  1740. while ( ptr < eol && isalnum((int)*ptr) )
  1741. ptr++;
  1742. noPRINT(stderr,"ADDR: |%.*s|\n",(int)(ptr-src),src);
  1743. if ( ptr > src && ptr < eol && *ptr == ':' )
  1744. src = ptr + 1;
  1745. //--- scan numbers until unknown character
  1746. noPRINT(stderr,"SCAN: |%.*s| base=%d\n",(int)(eol-src),src,scan->num_base);
  1747. for(;;)
  1748. {
  1749. while ( src < eol && ( (u8)*src <= ' ' || *src == ',' ) )
  1750. src++;
  1751. if ( src == eol
  1752. || (u8)TableNumbers[(u8)*src] >= scan->num_base
  1753. && !strchr(scan->num_allowed,*src) )
  1754. {
  1755. break;
  1756. }
  1757. if ( dest >= destmax )
  1758. {
  1759. size_t len = dest - destbuf;
  1760. if ( len > max_size )
  1761. len = max_size;
  1762. if (scan->dest)
  1763. AppendFastBuf(scan->dest,destbuf,len);
  1764. else
  1765. fwrite(destbuf,1,len,scan->fout);
  1766. max_size -= len;
  1767. scan->written += len;
  1768. dest = destbuf;
  1769. }
  1770. //u64 num;
  1771. //double d;
  1772. char *end;
  1773. switch(scan->format)
  1774. {
  1775. case XDUMPF_INT_2:
  1776. scan->endian_func->wr16(dest,str2ll(src,&end,scan->num_base));
  1777. dest += 2;
  1778. break;
  1779. case XDUMPF_INT_3:
  1780. scan->endian_func->wr24(dest,str2ll(src,&end,scan->num_base));
  1781. dest += 3;
  1782. break;
  1783. case XDUMPF_INT_4:
  1784. scan->endian_func->wr32(dest,str2ll(src,&end,scan->num_base));
  1785. dest += 4;
  1786. break;
  1787. case XDUMPF_INT_5:
  1788. scan->endian_func->wr40(dest,str2ll(src,&end,scan->num_base));
  1789. dest += 5;
  1790. break;
  1791. case XDUMPF_INT_6:
  1792. scan->endian_func->wr48(dest,str2ll(src,&end,scan->num_base));
  1793. dest += 6;
  1794. break;
  1795. case XDUMPF_INT_7:
  1796. scan->endian_func->wr56(dest,str2ll(src,&end,scan->num_base));
  1797. dest += 7;
  1798. break;
  1799. case XDUMPF_INT_8:
  1800. scan->endian_func->wr64(dest,str2ll(src,&end,scan->num_base));
  1801. dest += 8;
  1802. break;
  1803. case XDUMPF_FLOAT:
  1804. scan->endian_func->wrf4(dest,strtod(src,&end));
  1805. dest += 4;
  1806. break;
  1807. case XDUMPF_DOUBLE:
  1808. scan->endian_func->wrf8(dest,strtod(src,&end));
  1809. dest += 8;
  1810. break;
  1811. default:
  1812. *dest++ = str2ll(src,&end,scan->num_base);
  1813. break;
  1814. }
  1815. if ( src == end )
  1816. break;
  1817. src = end;
  1818. }
  1819. //--- prepare next line
  1820. src = eol;
  1821. }
  1822. if ( dest > destbuf )
  1823. {
  1824. size_t len = dest - destbuf;
  1825. if ( len > max_size )
  1826. len = max_size;
  1827. if (scan->dest)
  1828. AppendFastBuf(scan->dest,destbuf,len);
  1829. else
  1830. fwrite(destbuf,1,len,scan->fout);
  1831. scan->written += len;
  1832. }
  1833. return src - source;
  1834. }
  1835. ///////////////////////////////////////////////////////////////////////////////
  1836. int XScan
  1837. (
  1838. // returns <0 on error, or number of dumped bytes
  1839. XDump_t *xd, // params; if NULL: use local version
  1840. FastBuf_t *dest, // not NULL: append scanned data here
  1841. const void *data, // data to scan
  1842. uint size, // size of 'data'
  1843. bool term, // true: accept uncomplete last line
  1844. u64 max_size // >0: limit output size
  1845. )
  1846. {
  1847. if ( !data || !size )
  1848. return 0;
  1849. XScan_t scan;
  1850. SetupXScan(&scan,xd,dest);
  1851. return XScanHelper(&scan,data,data+size,term,max_size);
  1852. }
  1853. ///////////////////////////////////////////////////////////////////////////////
  1854. s64 XScanByFile
  1855. (
  1856. // returns <0 on error, or number of written bytes
  1857. XDump_t *xd, // params; if NULL: use local version
  1858. FastBuf_t *dest, // not NULL: append scanned data here
  1859. FILE *f, // input file
  1860. u64 max_size // >0: limit output size
  1861. )
  1862. {
  1863. if (!f)
  1864. return 0;
  1865. if (!max_size)
  1866. max_size = M1(max_size);
  1867. XScan_t scan;
  1868. SetupXScan(&scan,xd,dest);
  1869. char buf[0x4100];
  1870. uint start = 0;
  1871. while ( scan.written < max_size )
  1872. {
  1873. const uint max_read = ( sizeof(buf) - start ) / 0x1000 * 0x1000;
  1874. size_t stat = fread(buf+start,1,max_read,f);
  1875. if (!stat)
  1876. break;
  1877. stat += start;
  1878. const bool eof = feof(f) != 0;
  1879. int scanstat = XScanHelper(&scan,buf,buf+stat,eof,max_size);
  1880. if ( scanstat < 0 )
  1881. return scanstat;
  1882. start = stat - scanstat;
  1883. if (start)
  1884. memmove(buf,buf+scanstat,start);
  1885. if (eof)
  1886. break;
  1887. }
  1888. if (start)
  1889. {
  1890. int scanstat = XScanHelper(&scan,buf,buf+start,true,max_size);
  1891. if ( scanstat < 0 )
  1892. return scanstat;
  1893. }
  1894. return scan.written;
  1895. }
  1896. //
  1897. ///////////////////////////////////////////////////////////////////////////////
  1898. /////////////// END ///////////////
  1899. ///////////////////////////////////////////////////////////////////////////////