entfile.pas 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. {
  2. Copyright (c) 1998-2013 by Free Pascal development team
  3. Routines to read/write entry based files (ppu, pcp)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit entfile;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. systems,globtype,constexp,cstreams;
  22. const
  23. { buffer sizes }
  24. maxentrysize = 1024;
  25. entryfilebufsize = 16384;
  26. {ppu entries}
  27. mainentryid = 1;
  28. subentryid = 2;
  29. {special}
  30. iberror = 0;
  31. ibpputable = 243;
  32. ibstartrequireds = 244;
  33. ibendrequireds = 245;
  34. ibstartcontained = 246;
  35. ibendcontained = 247;
  36. ibstartdefs = 248;
  37. ibenddefs = 249;
  38. ibstartsyms = 250;
  39. ibendsyms = 251;
  40. ibendinterface = 252;
  41. ibendimplementation = 253;
  42. // ibendbrowser = 254;
  43. ibend = 255;
  44. {general}
  45. ibmodulename = 1;
  46. ibsourcefiles = 2;
  47. ibloadunit = 3;
  48. ibinitunit = 4;
  49. iblinkunitofiles = 5;
  50. iblinkunitstaticlibs = 6;
  51. iblinkunitsharedlibs = 7;
  52. iblinkotherofiles = 8;
  53. iblinkotherstaticlibs = 9;
  54. iblinkothersharedlibs = 10;
  55. ibImportSymbols = 11;
  56. ibsymref = 12;
  57. ibdefref = 13;
  58. // ibendsymtablebrowser = 14;
  59. // ibbeginsymtablebrowser = 15;
  60. {$IFDEF MACRO_DIFF_HINT}
  61. ibusedmacros = 16;
  62. {$ENDIF}
  63. ibderefdata = 17;
  64. ibexportedmacros = 18;
  65. ibderefmap = 19;
  66. {syms}
  67. ibtypesym = 20;
  68. ibprocsym = 21;
  69. ibstaticvarsym = 22;
  70. ibconstsym = 23;
  71. ibenumsym = 24;
  72. // ibtypedconstsym = 25;
  73. ibabsolutevarsym = 26;
  74. ibpropertysym = 27;
  75. ibfieldvarsym = 28;
  76. ibunitsym = 29;
  77. iblabelsym = 30;
  78. ibsyssym = 31;
  79. ibnamespacesym = 32;
  80. iblocalvarsym = 33;
  81. ibparavarsym = 34;
  82. ibmacrosym = 35;
  83. {definitions}
  84. iborddef = 40;
  85. ibpointerdef = 41;
  86. ibarraydef = 42;
  87. ibprocdef = 43;
  88. ibshortstringdef = 44;
  89. ibrecorddef = 45;
  90. ibfiledef = 46;
  91. ibformaldef = 47;
  92. ibobjectdef = 48;
  93. ibenumdef = 49;
  94. ibsetdef = 50;
  95. ibprocvardef = 51;
  96. ibfloatdef = 52;
  97. ibclassrefdef = 53;
  98. iblongstringdef = 54;
  99. ibansistringdef = 55;
  100. ibwidestringdef = 56;
  101. ibvariantdef = 57;
  102. ibundefineddef = 58;
  103. ibunicodestringdef = 59;
  104. {implementation/ObjData}
  105. ibnodetree = 80;
  106. ibasmsymbols = 81;
  107. ibresources = 82;
  108. ibcreatedobjtypes = 83;
  109. ibwpofile = 84;
  110. ibmoduleoptions = 85;
  111. ibunitimportsyms = 86;
  112. ibmainname = 90;
  113. ibsymtableoptions = 91;
  114. ibrecsymtableoptions = 91;
  115. ibpackagefiles = 92;
  116. ibpackagename = 93;
  117. { target-specific things }
  118. iblinkotherframeworks = 100;
  119. ibjvmnamespace = 101;
  120. {$ifdef generic_cpu}
  121. { We need to use the correct size of aint and pint for
  122. the target CPU }
  123. const
  124. CpuAddrBitSize : array[tsystemcpu] of longint =
  125. (
  126. { 0 } 32 {'none'},
  127. { 1 } 32 {'i386'},
  128. { 2 } 32 {'m68k'},
  129. { 3 } 32 {'alpha'},
  130. { 4 } 32 {'powerpc'},
  131. { 5 } 32 {'sparc'},
  132. { 6 } 32 {'vis'},
  133. { 7 } 64 {'ia64'},
  134. { 8 } 64 {'x86_64'},
  135. { 9 } 32 {'mipseb'},
  136. { 10 } 32 {'arm'},
  137. { 11 } 64 {'powerpc64'},
  138. { 12 } 16 {'avr'},
  139. { 13 } 32 {'mipsel'},
  140. { 14 } 32 {'jvm'},
  141. { 15 } 16 {'i8086'},
  142. { 16 } 64 {'aarch64'},
  143. { 17 } 32 {'wasm'},
  144. { 18 } 64 {'sparc64'}
  145. );
  146. CpuAluBitSize : array[tsystemcpu] of longint =
  147. (
  148. { 0 } 32 {'none'},
  149. { 1 } 32 {'i386'},
  150. { 2 } 32 {'m68k'},
  151. { 3 } 32 {'alpha'},
  152. { 4 } 32 {'powerpc'},
  153. { 5 } 32 {'sparc'},
  154. { 6 } 32 {'vis'},
  155. { 7 } 64 {'ia64'},
  156. { 8 } 64 {'x86_64'},
  157. { 9 } 32 {'mipseb'},
  158. { 10 } 32 {'arm'},
  159. { 11 } 64 {'powerpc64'},
  160. { 12 } 8 {'avr'},
  161. { 13 } 32 {'mipsel'},
  162. { 14 } 64 {'jvm'},
  163. { 15 } 16 {'i8086'},
  164. { 16 } 64 {'aarch64'},
  165. { 17 } 64 {'wasm'},
  166. { 18 } 64 {'sparc64'}
  167. );
  168. {$endif generic_cpu}
  169. type
  170. { bestreal is defined based on the target architecture }
  171. entryreal=bestreal;
  172. { common part of the header for all kinds of entry files }
  173. tentryheader=record
  174. id : array[1..3] of char;
  175. ver : array[1..3] of char;
  176. compiler : word;
  177. cpu : word;
  178. target : word;
  179. flags : longint;
  180. size : longint; { size of the ppufile without header }
  181. end;
  182. pentryheader=^tentryheader;
  183. tentry=packed record
  184. size : longint;
  185. id : byte;
  186. nr : byte;
  187. end;
  188. tentryfile=class
  189. private
  190. function getposition:longint;
  191. procedure setposition(value:longint);
  192. protected
  193. buf : pchar;
  194. bufstart,
  195. bufsize,
  196. bufidx : integer;
  197. entrybufstart,
  198. entrystart,
  199. entryidx : integer;
  200. entry : tentry;
  201. closed,
  202. tempclosed : boolean;
  203. closepos : integer;
  204. protected
  205. f : TCStream;
  206. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  207. fisfile : boolean;
  208. fname : string;
  209. fsize : integer;
  210. procedure newheader;virtual;abstract;
  211. function readheader:longint;virtual;abstract;
  212. function outputallowed:boolean;virtual;
  213. procedure resetfile;virtual;abstract;
  214. function getheadersize:longint;virtual;abstract;
  215. function getheaderaddr:pentryheader;virtual;abstract;
  216. public
  217. entrytyp : byte;
  218. size : integer;
  219. change_endian : boolean; { Used in ppudump util }
  220. {$ifdef generic_cpu}
  221. has_more,
  222. {$endif not generic_cpu}
  223. error : boolean;
  224. constructor create(const fn:string);
  225. destructor destroy;override;
  226. function getversion:integer;
  227. procedure flush;
  228. procedure closefile;virtual;
  229. procedure newentry;
  230. property position:longint read getposition write setposition;
  231. { Warning: don't keep the stream open during a tempclose! }
  232. function substream(ofs,len:longint):TCStream;
  233. { Warning: don't use the put* or write* functions anymore when writing through this }
  234. property stream:TCStream read f;
  235. {read}
  236. function openfile:boolean;
  237. function openstream(strm:TCStream):boolean;
  238. procedure reloadbuf;
  239. procedure readdata(out b;len:integer);
  240. procedure skipdata(len:integer);
  241. function readentry:byte;
  242. function EndOfEntry:boolean;
  243. function entrysize:longint;
  244. function entryleft:longint;
  245. procedure getdatabuf(out b;len:integer;out res:integer);
  246. procedure getdata(out b;len:integer);
  247. function getbyte:byte;
  248. function getword:word;
  249. function getdword:dword;
  250. function getlongint:longint;
  251. function getint64:int64;
  252. function getqword:qword;
  253. function getaint:{$ifdef generic_cpu}int64{$else}aint{$endif};
  254. function getasizeint:{$ifdef generic_cpu}int64{$else}asizeint{$endif};
  255. function getpuint:{$ifdef generic_cpu}qword{$else}puint{$endif};
  256. function getaword:{$ifdef generic_cpu}qword{$else}aword{$endif};
  257. function getreal:entryreal;
  258. function getrealsize(sizeofreal : longint):entryreal;
  259. function getstring:string;
  260. function getpshortstring:pshortstring;
  261. function getansistring:ansistring;
  262. procedure getnormalset(out b);
  263. procedure getsmallset(out b);
  264. function skipuntilentry(untilb:byte):boolean;
  265. {write}
  266. function createfile:boolean;virtual;
  267. function createstream(strm:TCStream):boolean;
  268. procedure writeheader;virtual;abstract;
  269. procedure writebuf;
  270. procedure writedata(const b;len:integer);
  271. procedure writeentry(ibnr:byte);
  272. procedure putdata(const b;len:integer);virtual;
  273. procedure putbyte(b:byte);
  274. procedure putword(w:word);
  275. procedure putdword(w:dword);
  276. procedure putlongint(l:longint);
  277. procedure putint64(i:int64);
  278. procedure putqword(q:qword);
  279. procedure putaint(i:aint);
  280. procedure putasizeint(i:asizeint);
  281. procedure putpuint(i:puint);
  282. procedure putaword(i:aword);
  283. procedure putreal(d:entryreal);
  284. procedure putstring(const s:string);
  285. procedure putansistring(const s:ansistring);
  286. procedure putnormalset(const b);
  287. procedure putsmallset(const b);
  288. procedure tempclose; // MG: not used, obsolete?
  289. function tempopen:boolean; // MG: not used, obsolete?
  290. end;
  291. implementation
  292. uses
  293. cutils;
  294. function swapendian_entryreal(d:entryreal):entryreal;
  295. type
  296. entryreal_bytes=array[0..sizeof(d)-1] of byte;
  297. var
  298. i:0..sizeof(d)-1;
  299. begin
  300. for i:=low(entryreal_bytes) to high(entryreal_bytes) do
  301. entryreal_bytes(result)[i]:=entryreal_bytes(d)[high(entryreal_bytes)-i];
  302. end;
  303. {*****************************************************************************
  304. tentryfile
  305. *****************************************************************************}
  306. function tentryfile.outputallowed: boolean;
  307. begin
  308. result:=true;
  309. end;
  310. constructor tentryfile.create(const fn:string);
  311. begin
  312. fname:=fn;
  313. fisfile:=false;
  314. change_endian:=false;
  315. mode:=0;
  316. newheader;
  317. error:=false;
  318. closed:=true;
  319. tempclosed:=false;
  320. getmem(buf,entryfilebufsize);
  321. end;
  322. destructor tentryfile.destroy;
  323. begin
  324. closefile;
  325. if assigned(buf) then
  326. freemem(buf,entryfilebufsize);
  327. end;
  328. function tentryfile.getversion:integer;
  329. var
  330. l : integer;
  331. code : integer;
  332. header : pentryheader;
  333. begin
  334. header:=getheaderaddr;
  335. Val(header^.ver[1]+header^.ver[2]+header^.ver[3],l,code);
  336. if code=0 then
  337. result:=l
  338. else
  339. result:=0;
  340. end;
  341. procedure tentryfile.flush;
  342. begin
  343. if mode=2 then
  344. writebuf;
  345. end;
  346. procedure tentryfile.closefile;
  347. begin
  348. if mode<>0 then
  349. begin
  350. flush;
  351. if fisfile then
  352. f.Free;
  353. mode:=0;
  354. closed:=true;
  355. end;
  356. end;
  357. procedure tentryfile.setposition(value:longint);
  358. begin
  359. if assigned(f) then
  360. f.Position:=value
  361. else
  362. if tempclosed then
  363. closepos:=value;
  364. end;
  365. function tentryfile.getposition:longint;
  366. begin
  367. if assigned(f) then
  368. result:=f.Position
  369. else
  370. if tempclosed then
  371. result:=closepos
  372. else
  373. result:=0;
  374. end;
  375. function tentryfile.substream(ofs,len:longint):TCStream;
  376. begin
  377. result:=nil;
  378. if assigned(f) then
  379. result:=TCRangeStream.Create(f,ofs,len);
  380. end;
  381. {*****************************************************************************
  382. tentryfile Reading
  383. *****************************************************************************}
  384. function tentryfile.openfile:boolean;
  385. var
  386. strm : TCStream;
  387. begin
  388. openfile:=false;
  389. try
  390. strm:=CFileStreamClass.Create(fname,fmOpenRead)
  391. except
  392. exit;
  393. end;
  394. openfile:=openstream(strm);
  395. fisfile:=result;
  396. end;
  397. function tentryfile.openstream(strm:TCStream):boolean;
  398. var
  399. i : longint;
  400. begin
  401. openstream:=false;
  402. f:=strm;
  403. closed:=false;
  404. {read ppuheader}
  405. fsize:=f.Size;
  406. i:=readheader;
  407. if i<0 then
  408. exit;
  409. {reset buffer}
  410. bufstart:=i;
  411. bufsize:=0;
  412. bufidx:=0;
  413. mode:=1;
  414. FillChar(entry,sizeof(tentry),0);
  415. entryidx:=0;
  416. entrystart:=0;
  417. entrybufstart:=0;
  418. error:=false;
  419. openstream:=true;
  420. end;
  421. procedure tentryfile.reloadbuf;
  422. begin
  423. inc(bufstart,bufsize);
  424. bufsize:=f.Read(buf^,entryfilebufsize);
  425. bufidx:=0;
  426. end;
  427. procedure tentryfile.readdata(out b;len:integer);
  428. var
  429. p,pbuf : pchar;
  430. left : integer;
  431. begin
  432. p:=pchar(@b);
  433. pbuf:=@buf[bufidx];
  434. repeat
  435. left:=bufsize-bufidx;
  436. if len<left then
  437. break;
  438. move(pbuf^,p^,left);
  439. dec(len,left);
  440. inc(p,left);
  441. reloadbuf;
  442. pbuf:=@buf[bufidx];
  443. if bufsize=0 then
  444. exit;
  445. until false;
  446. move(pbuf^,p^,len);
  447. inc(bufidx,len);
  448. end;
  449. procedure tentryfile.skipdata(len:integer);
  450. var
  451. left : integer;
  452. begin
  453. while len>0 do
  454. begin
  455. left:=bufsize-bufidx;
  456. if len>left then
  457. begin
  458. dec(len,left);
  459. reloadbuf;
  460. if bufsize=0 then
  461. exit;
  462. end
  463. else
  464. begin
  465. inc(bufidx,len);
  466. exit;
  467. end;
  468. end;
  469. end;
  470. function tentryfile.readentry:byte;
  471. begin
  472. if entryidx<entry.size then
  473. begin
  474. {$ifdef generic_cpu}
  475. has_more:=true;
  476. {$endif not generic_cpu}
  477. skipdata(entry.size-entryidx);
  478. end;
  479. readdata(entry,sizeof(tentry));
  480. if change_endian then
  481. entry.size:=swapendian(entry.size);
  482. entrystart:=bufstart+bufidx;
  483. entryidx:=0;
  484. {$ifdef generic_cpu}
  485. has_more:=false;
  486. {$endif not generic_cpu}
  487. if not(entry.id in [mainentryid,subentryid]) then
  488. begin
  489. readentry:=iberror;
  490. error:=true;
  491. exit;
  492. end;
  493. readentry:=entry.nr;
  494. end;
  495. function tentryfile.endofentry:boolean;
  496. begin
  497. {$ifdef generic_cpu}
  498. endofentry:=(entryidx=entry.size);
  499. {$else not generic_cpu}
  500. endofentry:=(entryidx>=entry.size);
  501. {$endif not generic_cpu}
  502. end;
  503. function tentryfile.entrysize:longint;
  504. begin
  505. entrysize:=entry.size;
  506. end;
  507. function tentryfile.entryleft:longint;
  508. begin
  509. entryleft:=entry.size-entryidx;
  510. end;
  511. procedure tentryfile.getdatabuf(out b;len:integer;out res:integer);
  512. begin
  513. if entryidx+len>entry.size then
  514. res:=entry.size-entryidx
  515. else
  516. res:=len;
  517. readdata(b,res);
  518. inc(entryidx,res);
  519. end;
  520. procedure tentryfile.getdata(out b;len:integer);
  521. begin
  522. if entryidx+len>entry.size then
  523. begin
  524. error:=true;
  525. exit;
  526. end;
  527. readdata(b,len);
  528. inc(entryidx,len);
  529. end;
  530. function tentryfile.getbyte:byte;
  531. begin
  532. if entryidx+1>entry.size then
  533. begin
  534. error:=true;
  535. result:=0;
  536. exit;
  537. end;
  538. if bufsize-bufidx>=1 then
  539. begin
  540. result:=pbyte(@buf[bufidx])^;
  541. inc(bufidx);
  542. end
  543. else
  544. readdata(result,1);
  545. inc(entryidx);
  546. end;
  547. function tentryfile.getword:word;
  548. begin
  549. if entryidx+2>entry.size then
  550. begin
  551. error:=true;
  552. result:=0;
  553. exit;
  554. end;
  555. if bufsize-bufidx>=sizeof(word) then
  556. begin
  557. result:=Unaligned(pword(@buf[bufidx])^);
  558. inc(bufidx,sizeof(word));
  559. end
  560. else
  561. readdata(result,sizeof(word));
  562. if change_endian then
  563. result:=swapendian(result);
  564. inc(entryidx,2);
  565. end;
  566. function tentryfile.getlongint:longint;
  567. begin
  568. if entryidx+4>entry.size then
  569. begin
  570. error:=true;
  571. result:=0;
  572. exit;
  573. end;
  574. if bufsize-bufidx>=sizeof(longint) then
  575. begin
  576. result:=Unaligned(plongint(@buf[bufidx])^);
  577. inc(bufidx,sizeof(longint));
  578. end
  579. else
  580. readdata(result,sizeof(longint));
  581. if change_endian then
  582. result:=swapendian(result);
  583. inc(entryidx,4);
  584. end;
  585. function tentryfile.getdword:dword;
  586. begin
  587. if entryidx+4>entry.size then
  588. begin
  589. error:=true;
  590. result:=0;
  591. exit;
  592. end;
  593. if bufsize-bufidx>=sizeof(dword) then
  594. begin
  595. result:=Unaligned(plongint(@buf[bufidx])^);
  596. inc(bufidx,sizeof(longint));
  597. end
  598. else
  599. readdata(result,sizeof(dword));
  600. if change_endian then
  601. result:=swapendian(result);
  602. inc(entryidx,4);
  603. end;
  604. function tentryfile.getint64:int64;
  605. begin
  606. if entryidx+8>entry.size then
  607. begin
  608. error:=true;
  609. result:=0;
  610. exit;
  611. end;
  612. if bufsize-bufidx>=sizeof(int64) then
  613. begin
  614. result:=Unaligned(pint64(@buf[bufidx])^);
  615. inc(bufidx,sizeof(int64));
  616. end
  617. else
  618. readdata(result,sizeof(int64));
  619. if change_endian then
  620. result:=swapendian(result);
  621. inc(entryidx,8);
  622. end;
  623. function tentryfile.getqword:qword;
  624. begin
  625. if entryidx+8>entry.size then
  626. begin
  627. error:=true;
  628. result:=0;
  629. exit;
  630. end;
  631. if bufsize-bufidx>=sizeof(qword) then
  632. begin
  633. result:=Unaligned(pqword(@buf[bufidx])^);
  634. inc(bufidx,sizeof(qword));
  635. end
  636. else
  637. readdata(result,sizeof(qword));
  638. if change_endian then
  639. result:=swapendian(result);
  640. inc(entryidx,8);
  641. end;
  642. function tentryfile.getaint:{$ifdef generic_cpu}int64{$else}aint{$endif};
  643. {$ifdef generic_cpu}
  644. var
  645. header : pentryheader;
  646. {$endif generic_cpu}
  647. begin
  648. {$ifdef generic_cpu}
  649. header:=getheaderaddr;
  650. if CpuAluBitSize[tsystemcpu(header^.cpu)]=64 then
  651. result:=getint64
  652. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=32 then
  653. result:=getlongint
  654. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=16 then
  655. result:=smallint(getword)
  656. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=8 then
  657. result:=shortint(getbyte)
  658. else
  659. begin
  660. error:=true;
  661. result:=0;
  662. end;
  663. {$else not generic_cpu}
  664. result:=4;
  665. case sizeof(aint) of
  666. 8: result:=getint64;
  667. 4: result:=getlongint;
  668. 2: result:=smallint(getword);
  669. 1: result:=shortint(getbyte);
  670. end;
  671. {$endif not generic_cpu}
  672. end;
  673. function tentryfile.getasizeint:{$ifdef generic_cpu}int64{$else}asizeint{$endif};
  674. {$ifdef generic_cpu}
  675. var
  676. header : pentryheader;
  677. {$endif generic_cpu}
  678. begin
  679. {$ifdef generic_cpu}
  680. header:=getheaderaddr;
  681. if CpuAddrBitSize[tsystemcpu(header^.cpu)]=64 then
  682. result:=getint64
  683. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=32 then
  684. result:=getlongint
  685. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=16 then
  686. result:=smallint(getword)
  687. else
  688. begin
  689. error:=true;
  690. result:=0;
  691. end;
  692. {$else not generic_cpu}
  693. case sizeof(asizeint) of
  694. 8: result:=asizeint(getint64);
  695. 4: result:=asizeint(getlongint);
  696. 2: result:=asizeint(getword);
  697. 1: result:=asizeint(getbyte);
  698. else
  699. result:=0;
  700. end;
  701. {$endif not generic_cpu}
  702. end;
  703. function tentryfile.getpuint:{$ifdef generic_cpu}qword{$else}puint{$endif};
  704. {$ifdef generic_cpu}
  705. var
  706. header : pentryheader;
  707. {$endif generic_cpu}
  708. begin
  709. {$ifdef generic_cpu}
  710. header:=getheaderaddr;
  711. if CpuAddrBitSize[tsystemcpu(header^.cpu)]=64 then
  712. result:=getqword
  713. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=32 then
  714. result:=getdword
  715. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=16 then
  716. result:=getbyte
  717. else
  718. begin
  719. error:=true;
  720. result:=0;
  721. end;
  722. {$else not generic_cpu}
  723. case sizeof(puint) of
  724. 8: result:=getqword;
  725. 4: result:=getdword;
  726. 2: result:=getword;
  727. 1: result:=getbyte;
  728. else
  729. result:=0;
  730. end;
  731. {$endif not generic_cpu}
  732. end;
  733. function tentryfile.getaword:{$ifdef generic_cpu}qword{$else}aword{$endif};
  734. {$ifdef generic_cpu}
  735. var
  736. header : pentryheader;
  737. {$endif generic_cpu}
  738. begin
  739. {$ifdef generic_cpu}
  740. header:=getheaderaddr;
  741. if CpuAluBitSize[tsystemcpu(header^.cpu)]=64 then
  742. result:=getqword
  743. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=32 then
  744. result:=getdword
  745. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=16 then
  746. result:=getword
  747. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=8 then
  748. result:=getbyte
  749. else
  750. begin
  751. error:=true;
  752. result:=0;
  753. end;
  754. {$else not generic_cpu}
  755. result:=4;
  756. case sizeof(aword) of
  757. 8: result:=getqword;
  758. 4: result:=getdword;
  759. 2: result:=getword;
  760. 1: result:=getbyte;
  761. end;
  762. {$endif not generic_cpu}
  763. end;
  764. function tentryfile.getrealsize(sizeofreal : longint):entryreal;
  765. var
  766. e : entryreal;
  767. d : double;
  768. s : single;
  769. begin
  770. if sizeofreal=sizeof(e) then
  771. begin
  772. if entryidx+sizeof(e)>entry.size then
  773. begin
  774. error:=true;
  775. result:=0;
  776. exit;
  777. end;
  778. readdata(e,sizeof(e));
  779. if change_endian then
  780. result:=swapendian_entryreal(e)
  781. else
  782. result:=e;
  783. inc(entryidx,sizeof(e));
  784. exit;
  785. end;
  786. if sizeofreal=sizeof(d) then
  787. begin
  788. if entryidx+sizeof(d)>entry.size then
  789. begin
  790. error:=true;
  791. result:=0;
  792. exit;
  793. end;
  794. readdata(d,sizeof(d));
  795. if change_endian then
  796. result:=swapendian(pqword(@d)^)
  797. else
  798. result:=d;
  799. inc(entryidx,sizeof(d));
  800. result:=d;
  801. exit;
  802. end;
  803. if sizeofreal=sizeof(s) then
  804. begin
  805. if entryidx+sizeof(s)>entry.size then
  806. begin
  807. error:=true;
  808. result:=0;
  809. exit;
  810. end;
  811. readdata(s,sizeof(s));
  812. if change_endian then
  813. result:=swapendian(pdword(@s)^)
  814. else
  815. result:=s;
  816. inc(entryidx,sizeof(s));
  817. result:=s;
  818. exit;
  819. end;
  820. error:=true;
  821. result:=0.0;
  822. end;
  823. function tentryfile.getreal:entryreal;
  824. var
  825. d : entryreal;
  826. hd : double;
  827. begin
  828. if target_info.system=system_x86_64_win64 then
  829. begin
  830. hd:=getrealsize(sizeof(hd));
  831. getreal:=hd;
  832. end
  833. else
  834. begin
  835. d:=getrealsize(sizeof(d));
  836. getreal:=d;
  837. end;
  838. end;
  839. function tentryfile.getstring:string;
  840. begin
  841. result[0]:=chr(getbyte);
  842. if entryidx+length(result)>entry.size then
  843. begin
  844. error:=true;
  845. exit;
  846. end;
  847. ReadData(result[1],length(result));
  848. inc(entryidx,length(result));
  849. end;
  850. function tentryfile.getpshortstring:pshortstring;
  851. var
  852. len: char;
  853. begin
  854. result:=nil;
  855. len:=chr(getbyte);
  856. if entryidx+ord(len)>entry.size then
  857. begin
  858. error:=true;
  859. exit;
  860. end;
  861. getmem(result,ord(len)+1);
  862. result^[0]:=len;
  863. ReadData(result^[1],ord(len));
  864. inc(entryidx,ord(len));
  865. end;
  866. function tentryfile.getansistring:ansistring;
  867. var
  868. len: longint;
  869. begin
  870. len:=getlongint;
  871. if entryidx+len>entry.size then
  872. begin
  873. error:=true;
  874. result:='';
  875. exit;
  876. end;
  877. setlength(result,len);
  878. if len>0 then
  879. getdata(result[1],len);
  880. end;
  881. procedure tentryfile.getsmallset(out b);
  882. var
  883. i : longint;
  884. begin
  885. getdata(b,4);
  886. if change_endian then
  887. for i:=0 to 3 do
  888. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  889. end;
  890. procedure tentryfile.getnormalset(out b);
  891. var
  892. i : longint;
  893. begin
  894. getdata(b,32);
  895. if change_endian then
  896. for i:=0 to 31 do
  897. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  898. end;
  899. function tentryfile.skipuntilentry(untilb:byte):boolean;
  900. var
  901. b : byte;
  902. begin
  903. repeat
  904. b:=readentry;
  905. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  906. skipuntilentry:=(b=untilb);
  907. end;
  908. {*****************************************************************************
  909. tentryfile Writing
  910. *****************************************************************************}
  911. function tentryfile.createfile:boolean;
  912. var
  913. ok: boolean;
  914. strm : TCStream;
  915. begin
  916. createfile:=false;
  917. strm:=nil;
  918. if outputallowed then
  919. begin
  920. {$ifdef MACOS}
  921. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  922. SetDefaultMacOSCreator('FPas');
  923. SetDefaultMacOSFiletype('FPPU');
  924. {$endif}
  925. ok:=false;
  926. try
  927. strm:=CFileStreamClass.Create(fname,fmCreate);
  928. ok:=true;
  929. except
  930. end;
  931. {$ifdef MACOS}
  932. SetDefaultMacOSCreator('MPS ');
  933. SetDefaultMacOSFiletype('TEXT');
  934. {$endif}
  935. if not ok then
  936. exit;
  937. end;
  938. createfile:=createstream(strm);
  939. fisfile:=result;
  940. end;
  941. function tentryfile.createstream(strm:TCStream):boolean;
  942. begin
  943. createstream:=false;
  944. if outputallowed then
  945. begin
  946. f:=strm;
  947. mode:=2;
  948. {write header for sure}
  949. f.Write(getheaderaddr^,getheadersize);
  950. end;
  951. bufsize:=entryfilebufsize;
  952. bufstart:=getheadersize;
  953. bufidx:=0;
  954. {reset}
  955. resetfile;
  956. error:=false;
  957. size:=0;
  958. entrytyp:=mainentryid;
  959. {start}
  960. newentry;
  961. createstream:=true;
  962. end;
  963. procedure tentryfile.writebuf;
  964. begin
  965. if outputallowed and
  966. (bufidx <> 0) then
  967. f.Write(buf^,bufidx);
  968. inc(bufstart,bufidx);
  969. bufidx:=0;
  970. end;
  971. procedure tentryfile.writedata(const b;len:integer);
  972. var
  973. p : pchar;
  974. left,
  975. idx : integer;
  976. begin
  977. if not outputallowed then
  978. exit;
  979. p:=pchar(@b);
  980. idx:=0;
  981. while len>0 do
  982. begin
  983. left:=bufsize-bufidx;
  984. if len>left then
  985. begin
  986. move(p[idx],buf[bufidx],left);
  987. dec(len,left);
  988. inc(idx,left);
  989. inc(bufidx,left);
  990. writebuf;
  991. end
  992. else
  993. begin
  994. move(p[idx],buf[bufidx],len);
  995. inc(bufidx,len);
  996. exit;
  997. end;
  998. end;
  999. end;
  1000. procedure tentryfile.newentry;
  1001. begin
  1002. with entry do
  1003. begin
  1004. id:=entrytyp;
  1005. nr:=ibend;
  1006. size:=0;
  1007. end;
  1008. {Reset Entry State}
  1009. entryidx:=0;
  1010. entrybufstart:=bufstart;
  1011. entrystart:=bufstart+bufidx;
  1012. {Alloc in buffer}
  1013. writedata(entry,sizeof(tentry));
  1014. end;
  1015. procedure tentryfile.writeentry(ibnr:byte);
  1016. var
  1017. opos : integer;
  1018. begin
  1019. {create entry}
  1020. entry.id:=entrytyp;
  1021. entry.nr:=ibnr;
  1022. entry.size:=entryidx;
  1023. {it's already been sent to disk ?}
  1024. if entrybufstart<>bufstart then
  1025. begin
  1026. if outputallowed then
  1027. begin
  1028. {flush to be sure}
  1029. WriteBuf;
  1030. {write entry}
  1031. opos:=f.Position;
  1032. f.Position:=entrystart;
  1033. f.write(entry,sizeof(tentry));
  1034. f.Position:=opos;
  1035. end;
  1036. entrybufstart:=bufstart;
  1037. end
  1038. else
  1039. move(entry,buf[entrystart-bufstart],sizeof(entry));
  1040. {Add New Entry, which is ibend by default}
  1041. entrystart:=bufstart+bufidx; {next entry position}
  1042. newentry;
  1043. end;
  1044. procedure tentryfile.putdata(const b;len:integer);
  1045. begin
  1046. if outputallowed then
  1047. writedata(b,len);
  1048. inc(entryidx,len);
  1049. end;
  1050. procedure tentryfile.putbyte(b:byte);
  1051. begin
  1052. putdata(b,1);
  1053. end;
  1054. procedure tentryfile.putword(w:word);
  1055. begin
  1056. putdata(w,2);
  1057. end;
  1058. procedure tentryfile.putdword(w:dword);
  1059. begin
  1060. putdata(w,4);
  1061. end;
  1062. procedure tentryfile.putlongint(l:longint);
  1063. begin
  1064. putdata(l,4);
  1065. end;
  1066. procedure tentryfile.putint64(i:int64);
  1067. begin
  1068. putdata(i,8);
  1069. end;
  1070. procedure tentryfile.putqword(q:qword);
  1071. begin
  1072. putdata(q,sizeof(qword));
  1073. end;
  1074. procedure tentryfile.putaint(i:aint);
  1075. begin
  1076. putdata(i,sizeof(aint));
  1077. end;
  1078. procedure tentryfile.putasizeint(i: asizeint);
  1079. begin
  1080. putdata(i,sizeof(asizeint));
  1081. end;
  1082. procedure tentryfile.putpuint(i : puint);
  1083. begin
  1084. putdata(i,sizeof(puint));
  1085. end;
  1086. procedure tentryfile.putaword(i:aword);
  1087. begin
  1088. putdata(i,sizeof(aword));
  1089. end;
  1090. procedure tentryfile.putreal(d:entryreal);
  1091. var
  1092. hd : double;
  1093. begin
  1094. if target_info.system=system_x86_64_win64 then
  1095. begin
  1096. hd:=d;
  1097. putdata(hd,sizeof(hd));
  1098. end
  1099. else
  1100. putdata(d,sizeof(entryreal));
  1101. end;
  1102. procedure tentryfile.putstring(const s:string);
  1103. begin
  1104. putdata(s,length(s)+1);
  1105. end;
  1106. procedure tentryfile.putansistring(const s:ansistring);
  1107. var
  1108. len: longint;
  1109. begin
  1110. len:=length(s);
  1111. putlongint(len);
  1112. if len>0 then
  1113. putdata(s[1],len);
  1114. end;
  1115. procedure tentryfile.putsmallset(const b);
  1116. var
  1117. l : longint;
  1118. begin
  1119. l:=longint(b);
  1120. putlongint(l);
  1121. end;
  1122. procedure tentryfile.putnormalset(const b);
  1123. begin
  1124. putdata(b,32);
  1125. end;
  1126. procedure tentryfile.tempclose;
  1127. begin
  1128. if not closed then
  1129. begin
  1130. closepos:=f.Position;
  1131. f.Free;
  1132. f:=nil;
  1133. closed:=true;
  1134. tempclosed:=true;
  1135. end;
  1136. end;
  1137. function tentryfile.tempopen:boolean;
  1138. begin
  1139. tempopen:=false;
  1140. if not closed or not tempclosed then
  1141. exit;
  1142. { MG: not sure, if this is correct
  1143. f.position:=0;
  1144. No, f was freed in tempclose above, we need to
  1145. recreate it. PM 2011/06/06 }
  1146. try
  1147. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1148. except
  1149. exit;
  1150. end;
  1151. closed:=false;
  1152. tempclosed:=false;
  1153. { restore state }
  1154. f.Position:=closepos;
  1155. tempopen:=true;
  1156. end;
  1157. end.