entfile.pas 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. );
  145. CpuAluBitSize : array[tsystemcpu] of longint =
  146. (
  147. { 0 } 32 {'none'},
  148. { 1 } 32 {'i386'},
  149. { 2 } 32 {'m68k'},
  150. { 3 } 32 {'alpha'},
  151. { 4 } 32 {'powerpc'},
  152. { 5 } 32 {'sparc'},
  153. { 6 } 32 {'vis'},
  154. { 7 } 64 {'ia64'},
  155. { 8 } 64 {'x86_64'},
  156. { 9 } 32 {'mipseb'},
  157. { 10 } 32 {'arm'},
  158. { 11 } 64 {'powerpc64'},
  159. { 12 } 8 {'avr'},
  160. { 13 } 32 {'mipsel'},
  161. { 14 } 64 {'jvm'},
  162. { 15 } 16 {'i8086'},
  163. { 16 } 64 {'aarch64'},
  164. { 17 } 64 {'wasm'}
  165. );
  166. {$endif generic_cpu}
  167. type
  168. { bestreal is defined based on the target architecture }
  169. entryreal=bestreal;
  170. { common part of the header for all kinds of entry files }
  171. tentryheader=record
  172. id : array[1..3] of char;
  173. ver : array[1..3] of char;
  174. compiler : word;
  175. cpu : word;
  176. target : word;
  177. flags : longint;
  178. size : longint; { size of the ppufile without header }
  179. end;
  180. pentryheader=^tentryheader;
  181. tentry=packed record
  182. size : longint;
  183. id : byte;
  184. nr : byte;
  185. end;
  186. tentryfile=class
  187. private
  188. function getposition:longint;
  189. procedure setposition(value:longint);
  190. protected
  191. buf : pchar;
  192. bufstart,
  193. bufsize,
  194. bufidx : integer;
  195. entrybufstart,
  196. entrystart,
  197. entryidx : integer;
  198. entry : tentry;
  199. closed,
  200. tempclosed : boolean;
  201. closepos : integer;
  202. protected
  203. f : TCStream;
  204. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  205. fisfile : boolean;
  206. fname : string;
  207. fsize : integer;
  208. procedure newheader;virtual;abstract;
  209. function readheader:longint;virtual;abstract;
  210. function outputallowed:boolean;virtual;
  211. procedure resetfile;virtual;abstract;
  212. function getheadersize:longint;virtual;abstract;
  213. function getheaderaddr:pentryheader;virtual;abstract;
  214. public
  215. entrytyp : byte;
  216. size : integer;
  217. change_endian : boolean; { Used in ppudump util }
  218. {$ifdef generic_cpu}
  219. has_more,
  220. {$endif not generic_cpu}
  221. error : boolean;
  222. constructor create(const fn:string);
  223. destructor destroy;override;
  224. function getversion:integer;
  225. procedure flush;
  226. procedure closefile;virtual;
  227. procedure newentry;
  228. property position:longint read getposition write setposition;
  229. { Warning: don't keep the stream open during a tempclose! }
  230. function substream(ofs,len:longint):TCStream;
  231. { Warning: don't use the put* or write* functions anymore when writing through this }
  232. property stream:TCStream read f;
  233. {read}
  234. function openfile:boolean;
  235. function openstream(strm:TCStream):boolean;
  236. procedure reloadbuf;
  237. procedure readdata(out b;len:integer);
  238. procedure skipdata(len:integer);
  239. function readentry:byte;
  240. function EndOfEntry:boolean;
  241. function entrysize:longint;
  242. function entryleft:longint;
  243. procedure getdatabuf(out b;len:integer;out res:integer);
  244. procedure getdata(out b;len:integer);
  245. function getbyte:byte;
  246. function getword:word;
  247. function getdword:dword;
  248. function getlongint:longint;
  249. function getint64:int64;
  250. function getqword:qword;
  251. function getaint:{$ifdef generic_cpu}int64{$else}aint{$endif};
  252. function getasizeint:{$ifdef generic_cpu}int64{$else}asizeint{$endif};
  253. function getaword:{$ifdef generic_cpu}qword{$else}aword{$endif};
  254. function getreal:entryreal;
  255. function getrealsize(sizeofreal : longint):entryreal;
  256. function getstring:string;
  257. function getpshortstring:pshortstring;
  258. function getansistring:ansistring;
  259. procedure getnormalset(out b);
  260. procedure getsmallset(out b);
  261. function skipuntilentry(untilb:byte):boolean;
  262. {write}
  263. function createfile:boolean;virtual;
  264. function createstream(strm:TCStream):boolean;
  265. procedure writeheader;virtual;abstract;
  266. procedure writebuf;
  267. procedure writedata(const b;len:integer);
  268. procedure writeentry(ibnr:byte);
  269. procedure putdata(const b;len:integer);virtual;
  270. procedure putbyte(b:byte);
  271. procedure putword(w:word);
  272. procedure putdword(w:dword);
  273. procedure putlongint(l:longint);
  274. procedure putint64(i:int64);
  275. procedure putqword(q:qword);
  276. procedure putaint(i:aint);
  277. procedure putasizeint(i:asizeint);
  278. procedure putaword(i:aword);
  279. procedure putreal(d:entryreal);
  280. procedure putstring(const s:string);
  281. procedure putansistring(const s:ansistring);
  282. procedure putnormalset(const b);
  283. procedure putsmallset(const b);
  284. procedure tempclose; // MG: not used, obsolete?
  285. function tempopen:boolean; // MG: not used, obsolete?
  286. end;
  287. implementation
  288. uses
  289. cutils;
  290. function swapendian_entryreal(d:entryreal):entryreal;
  291. type
  292. entryreal_bytes=array[0..sizeof(d)-1] of byte;
  293. var
  294. i:0..sizeof(d)-1;
  295. begin
  296. for i:=low(entryreal_bytes) to high(entryreal_bytes) do
  297. entryreal_bytes(result)[i]:=entryreal_bytes(d)[high(entryreal_bytes)-i];
  298. end;
  299. {*****************************************************************************
  300. tentryfile
  301. *****************************************************************************}
  302. function tentryfile.outputallowed: boolean;
  303. begin
  304. result:=true;
  305. end;
  306. constructor tentryfile.create(const fn:string);
  307. begin
  308. fname:=fn;
  309. fisfile:=false;
  310. change_endian:=false;
  311. mode:=0;
  312. newheader;
  313. error:=false;
  314. closed:=true;
  315. tempclosed:=false;
  316. getmem(buf,entryfilebufsize);
  317. end;
  318. destructor tentryfile.destroy;
  319. begin
  320. closefile;
  321. if assigned(buf) then
  322. freemem(buf,entryfilebufsize);
  323. end;
  324. function tentryfile.getversion:integer;
  325. var
  326. l : integer;
  327. code : integer;
  328. header : pentryheader;
  329. begin
  330. header:=getheaderaddr;
  331. Val(header^.ver[1]+header^.ver[2]+header^.ver[3],l,code);
  332. if code=0 then
  333. result:=l
  334. else
  335. result:=0;
  336. end;
  337. procedure tentryfile.flush;
  338. begin
  339. if mode=2 then
  340. writebuf;
  341. end;
  342. procedure tentryfile.closefile;
  343. begin
  344. if mode<>0 then
  345. begin
  346. flush;
  347. if fisfile then
  348. f.Free;
  349. mode:=0;
  350. closed:=true;
  351. end;
  352. end;
  353. procedure tentryfile.setposition(value:longint);
  354. begin
  355. if assigned(f) then
  356. f.Position:=value
  357. else
  358. if tempclosed then
  359. closepos:=value;
  360. end;
  361. function tentryfile.getposition:longint;
  362. begin
  363. if assigned(f) then
  364. result:=f.Position
  365. else
  366. if tempclosed then
  367. result:=closepos
  368. else
  369. result:=0;
  370. end;
  371. function tentryfile.substream(ofs,len:longint):TCStream;
  372. begin
  373. result:=nil;
  374. if assigned(f) then
  375. result:=TCRangeStream.Create(f,ofs,len);
  376. end;
  377. {*****************************************************************************
  378. tentryfile Reading
  379. *****************************************************************************}
  380. function tentryfile.openfile:boolean;
  381. var
  382. strm : TCStream;
  383. begin
  384. openfile:=false;
  385. try
  386. strm:=CFileStreamClass.Create(fname,fmOpenRead)
  387. except
  388. exit;
  389. end;
  390. openfile:=openstream(strm);
  391. fisfile:=result;
  392. end;
  393. function tentryfile.openstream(strm:TCStream):boolean;
  394. var
  395. i : longint;
  396. begin
  397. openstream:=false;
  398. f:=strm;
  399. closed:=false;
  400. {read ppuheader}
  401. fsize:=f.Size;
  402. i:=readheader;
  403. if i<0 then
  404. exit;
  405. {reset buffer}
  406. bufstart:=i;
  407. bufsize:=0;
  408. bufidx:=0;
  409. mode:=1;
  410. FillChar(entry,sizeof(tentry),0);
  411. entryidx:=0;
  412. entrystart:=0;
  413. entrybufstart:=0;
  414. error:=false;
  415. openstream:=true;
  416. end;
  417. procedure tentryfile.reloadbuf;
  418. begin
  419. inc(bufstart,bufsize);
  420. bufsize:=f.Read(buf^,entryfilebufsize);
  421. bufidx:=0;
  422. end;
  423. procedure tentryfile.readdata(out b;len:integer);
  424. var
  425. p,pbuf : pchar;
  426. left : integer;
  427. begin
  428. p:=pchar(@b);
  429. pbuf:=@buf[bufidx];
  430. repeat
  431. left:=bufsize-bufidx;
  432. if len<left then
  433. break;
  434. move(pbuf^,p^,left);
  435. dec(len,left);
  436. inc(p,left);
  437. reloadbuf;
  438. pbuf:=@buf[bufidx];
  439. if bufsize=0 then
  440. exit;
  441. until false;
  442. move(pbuf^,p^,len);
  443. inc(bufidx,len);
  444. end;
  445. procedure tentryfile.skipdata(len:integer);
  446. var
  447. left : integer;
  448. begin
  449. while len>0 do
  450. begin
  451. left:=bufsize-bufidx;
  452. if len>left then
  453. begin
  454. dec(len,left);
  455. reloadbuf;
  456. if bufsize=0 then
  457. exit;
  458. end
  459. else
  460. begin
  461. inc(bufidx,len);
  462. exit;
  463. end;
  464. end;
  465. end;
  466. function tentryfile.readentry:byte;
  467. begin
  468. if entryidx<entry.size then
  469. begin
  470. {$ifdef generic_cpu}
  471. has_more:=true;
  472. {$endif not generic_cpu}
  473. skipdata(entry.size-entryidx);
  474. end;
  475. readdata(entry,sizeof(tentry));
  476. if change_endian then
  477. entry.size:=swapendian(entry.size);
  478. entrystart:=bufstart+bufidx;
  479. entryidx:=0;
  480. {$ifdef generic_cpu}
  481. has_more:=false;
  482. {$endif not generic_cpu}
  483. if not(entry.id in [mainentryid,subentryid]) then
  484. begin
  485. readentry:=iberror;
  486. error:=true;
  487. exit;
  488. end;
  489. readentry:=entry.nr;
  490. end;
  491. function tentryfile.endofentry:boolean;
  492. begin
  493. {$ifdef generic_cpu}
  494. endofentry:=(entryidx=entry.size);
  495. {$else not generic_cpu}
  496. endofentry:=(entryidx>=entry.size);
  497. {$endif not generic_cpu}
  498. end;
  499. function tentryfile.entrysize:longint;
  500. begin
  501. entrysize:=entry.size;
  502. end;
  503. function tentryfile.entryleft:longint;
  504. begin
  505. entryleft:=entry.size-entryidx;
  506. end;
  507. procedure tentryfile.getdatabuf(out b;len:integer;out res:integer);
  508. begin
  509. if entryidx+len>entry.size then
  510. res:=entry.size-entryidx
  511. else
  512. res:=len;
  513. readdata(b,res);
  514. inc(entryidx,res);
  515. end;
  516. procedure tentryfile.getdata(out b;len:integer);
  517. begin
  518. if entryidx+len>entry.size then
  519. begin
  520. error:=true;
  521. exit;
  522. end;
  523. readdata(b,len);
  524. inc(entryidx,len);
  525. end;
  526. function tentryfile.getbyte:byte;
  527. begin
  528. if entryidx+1>entry.size then
  529. begin
  530. error:=true;
  531. result:=0;
  532. exit;
  533. end;
  534. if bufsize-bufidx>=1 then
  535. begin
  536. result:=pbyte(@buf[bufidx])^;
  537. inc(bufidx);
  538. end
  539. else
  540. readdata(result,1);
  541. inc(entryidx);
  542. end;
  543. function tentryfile.getword:word;
  544. begin
  545. if entryidx+2>entry.size then
  546. begin
  547. error:=true;
  548. result:=0;
  549. exit;
  550. end;
  551. if bufsize-bufidx>=sizeof(word) then
  552. begin
  553. result:=Unaligned(pword(@buf[bufidx])^);
  554. inc(bufidx,sizeof(word));
  555. end
  556. else
  557. readdata(result,sizeof(word));
  558. if change_endian then
  559. result:=swapendian(result);
  560. inc(entryidx,2);
  561. end;
  562. function tentryfile.getlongint:longint;
  563. begin
  564. if entryidx+4>entry.size then
  565. begin
  566. error:=true;
  567. result:=0;
  568. exit;
  569. end;
  570. if bufsize-bufidx>=sizeof(longint) then
  571. begin
  572. result:=Unaligned(plongint(@buf[bufidx])^);
  573. inc(bufidx,sizeof(longint));
  574. end
  575. else
  576. readdata(result,sizeof(longint));
  577. if change_endian then
  578. result:=swapendian(result);
  579. inc(entryidx,4);
  580. end;
  581. function tentryfile.getdword:dword;
  582. begin
  583. if entryidx+4>entry.size then
  584. begin
  585. error:=true;
  586. result:=0;
  587. exit;
  588. end;
  589. if bufsize-bufidx>=sizeof(dword) then
  590. begin
  591. result:=Unaligned(plongint(@buf[bufidx])^);
  592. inc(bufidx,sizeof(longint));
  593. end
  594. else
  595. readdata(result,sizeof(dword));
  596. if change_endian then
  597. result:=swapendian(result);
  598. inc(entryidx,4);
  599. end;
  600. function tentryfile.getint64:int64;
  601. begin
  602. if entryidx+8>entry.size then
  603. begin
  604. error:=true;
  605. result:=0;
  606. exit;
  607. end;
  608. if bufsize-bufidx>=sizeof(int64) then
  609. begin
  610. result:=Unaligned(pint64(@buf[bufidx])^);
  611. inc(bufidx,sizeof(int64));
  612. end
  613. else
  614. readdata(result,sizeof(int64));
  615. if change_endian then
  616. result:=swapendian(result);
  617. inc(entryidx,8);
  618. end;
  619. function tentryfile.getqword:qword;
  620. begin
  621. if entryidx+8>entry.size then
  622. begin
  623. error:=true;
  624. result:=0;
  625. exit;
  626. end;
  627. if bufsize-bufidx>=sizeof(qword) then
  628. begin
  629. result:=Unaligned(pqword(@buf[bufidx])^);
  630. inc(bufidx,sizeof(qword));
  631. end
  632. else
  633. readdata(result,sizeof(qword));
  634. if change_endian then
  635. result:=swapendian(result);
  636. inc(entryidx,8);
  637. end;
  638. function tentryfile.getaint:{$ifdef generic_cpu}int64{$else}aint{$endif};
  639. {$ifdef generic_cpu}
  640. var
  641. header : pentryheader;
  642. {$endif generic_cpu}
  643. begin
  644. {$ifdef generic_cpu}
  645. header:=getheaderaddr;
  646. if CpuAluBitSize[tsystemcpu(header^.cpu)]=64 then
  647. result:=getint64
  648. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=32 then
  649. result:=getlongint
  650. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=16 then
  651. result:=smallint(getword)
  652. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=8 then
  653. result:=shortint(getbyte)
  654. else
  655. begin
  656. error:=true;
  657. result:=0;
  658. end;
  659. {$else not generic_cpu}
  660. result:=4;
  661. case sizeof(aint) of
  662. 8: result:=getint64;
  663. 4: result:=getlongint;
  664. 2: result:=smallint(getword);
  665. 1: result:=shortint(getbyte);
  666. end;
  667. {$endif not generic_cpu}
  668. end;
  669. function tentryfile.getasizeint:{$ifdef generic_cpu}int64{$else}asizeint{$endif};
  670. {$ifdef generic_cpu}
  671. var
  672. header : pentryheader;
  673. {$endif generic_cpu}
  674. begin
  675. {$ifdef generic_cpu}
  676. header:=getheaderaddr;
  677. if CpuAddrBitSize[tsystemcpu(header^.cpu)]=64 then
  678. result:=getint64
  679. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=32 then
  680. result:=getlongint
  681. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=16 then
  682. result:=smallint(getword)
  683. else
  684. begin
  685. error:=true;
  686. result:=0;
  687. end;
  688. {$else not generic_cpu}
  689. case sizeof(asizeint) of
  690. 8: result:=asizeint(getint64);
  691. 4: result:=asizeint(getlongint);
  692. 2: result:=asizeint(getword);
  693. 1: result:=asizeint(getbyte);
  694. else
  695. result:=0;
  696. end;
  697. {$endif not generic_cpu}
  698. end;
  699. function tentryfile.getaword:{$ifdef generic_cpu}qword{$else}aword{$endif};
  700. {$ifdef generic_cpu}
  701. var
  702. header : pentryheader;
  703. {$endif generic_cpu}
  704. begin
  705. {$ifdef generic_cpu}
  706. header:=getheaderaddr;
  707. if CpuAluBitSize[tsystemcpu(header^.cpu)]=64 then
  708. result:=getqword
  709. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=32 then
  710. result:=getdword
  711. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=16 then
  712. result:=getword
  713. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=8 then
  714. result:=getbyte
  715. else
  716. begin
  717. error:=true;
  718. result:=0;
  719. end;
  720. {$else not generic_cpu}
  721. result:=4;
  722. case sizeof(aword) of
  723. 8: result:=getqword;
  724. 4: result:=getdword;
  725. 2: result:=getword;
  726. 1: result:=getbyte;
  727. end;
  728. {$endif not generic_cpu}
  729. end;
  730. function tentryfile.getrealsize(sizeofreal : longint):entryreal;
  731. var
  732. e : entryreal;
  733. d : double;
  734. s : single;
  735. begin
  736. if sizeofreal=sizeof(e) then
  737. begin
  738. if entryidx+sizeof(e)>entry.size then
  739. begin
  740. error:=true;
  741. result:=0;
  742. exit;
  743. end;
  744. readdata(e,sizeof(e));
  745. if change_endian then
  746. result:=swapendian_entryreal(e)
  747. else
  748. result:=e;
  749. inc(entryidx,sizeof(e));
  750. exit;
  751. end;
  752. if sizeofreal=sizeof(d) then
  753. begin
  754. if entryidx+sizeof(d)>entry.size then
  755. begin
  756. error:=true;
  757. result:=0;
  758. exit;
  759. end;
  760. readdata(d,sizeof(d));
  761. if change_endian then
  762. result:=swapendian(pqword(@d)^)
  763. else
  764. result:=d;
  765. inc(entryidx,sizeof(d));
  766. result:=d;
  767. exit;
  768. end;
  769. if sizeofreal=sizeof(s) then
  770. begin
  771. if entryidx+sizeof(s)>entry.size then
  772. begin
  773. error:=true;
  774. result:=0;
  775. exit;
  776. end;
  777. readdata(s,sizeof(s));
  778. if change_endian then
  779. result:=swapendian(pdword(@s)^)
  780. else
  781. result:=s;
  782. inc(entryidx,sizeof(s));
  783. result:=s;
  784. exit;
  785. end;
  786. error:=true;
  787. result:=0.0;
  788. end;
  789. function tentryfile.getreal:entryreal;
  790. var
  791. d : entryreal;
  792. hd : double;
  793. begin
  794. if target_info.system=system_x86_64_win64 then
  795. begin
  796. hd:=getrealsize(sizeof(hd));
  797. getreal:=hd;
  798. end
  799. else
  800. begin
  801. d:=getrealsize(sizeof(d));
  802. getreal:=d;
  803. end;
  804. end;
  805. function tentryfile.getstring:string;
  806. begin
  807. result[0]:=chr(getbyte);
  808. if entryidx+length(result)>entry.size then
  809. begin
  810. error:=true;
  811. exit;
  812. end;
  813. ReadData(result[1],length(result));
  814. inc(entryidx,length(result));
  815. end;
  816. function tentryfile.getpshortstring:pshortstring;
  817. var
  818. len: char;
  819. begin
  820. result:=nil;
  821. len:=chr(getbyte);
  822. if entryidx+ord(len)>entry.size then
  823. begin
  824. error:=true;
  825. exit;
  826. end;
  827. getmem(result,ord(len)+1);
  828. result^[0]:=len;
  829. ReadData(result^[1],ord(len));
  830. inc(entryidx,ord(len));
  831. end;
  832. function tentryfile.getansistring:ansistring;
  833. var
  834. len: longint;
  835. begin
  836. len:=getlongint;
  837. if entryidx+len>entry.size then
  838. begin
  839. error:=true;
  840. result:='';
  841. exit;
  842. end;
  843. setlength(result,len);
  844. if len>0 then
  845. getdata(result[1],len);
  846. end;
  847. procedure tentryfile.getsmallset(out b);
  848. var
  849. i : longint;
  850. begin
  851. getdata(b,4);
  852. if change_endian then
  853. for i:=0 to 3 do
  854. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  855. end;
  856. procedure tentryfile.getnormalset(out b);
  857. var
  858. i : longint;
  859. begin
  860. getdata(b,32);
  861. if change_endian then
  862. for i:=0 to 31 do
  863. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  864. end;
  865. function tentryfile.skipuntilentry(untilb:byte):boolean;
  866. var
  867. b : byte;
  868. begin
  869. repeat
  870. b:=readentry;
  871. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  872. skipuntilentry:=(b=untilb);
  873. end;
  874. {*****************************************************************************
  875. tentryfile Writing
  876. *****************************************************************************}
  877. function tentryfile.createfile:boolean;
  878. var
  879. ok: boolean;
  880. strm : TCStream;
  881. begin
  882. createfile:=false;
  883. strm:=nil;
  884. if outputallowed then
  885. begin
  886. {$ifdef MACOS}
  887. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  888. SetDefaultMacOSCreator('FPas');
  889. SetDefaultMacOSFiletype('FPPU');
  890. {$endif}
  891. ok:=false;
  892. try
  893. strm:=CFileStreamClass.Create(fname,fmCreate);
  894. ok:=true;
  895. except
  896. end;
  897. {$ifdef MACOS}
  898. SetDefaultMacOSCreator('MPS ');
  899. SetDefaultMacOSFiletype('TEXT');
  900. {$endif}
  901. if not ok then
  902. exit;
  903. end;
  904. createfile:=createstream(strm);
  905. fisfile:=result;
  906. end;
  907. function tentryfile.createstream(strm:TCStream):boolean;
  908. begin
  909. createstream:=false;
  910. if outputallowed then
  911. begin
  912. f:=strm;
  913. mode:=2;
  914. {write header for sure}
  915. f.Write(getheaderaddr^,getheadersize);
  916. end;
  917. bufsize:=entryfilebufsize;
  918. bufstart:=getheadersize;
  919. bufidx:=0;
  920. {reset}
  921. resetfile;
  922. error:=false;
  923. size:=0;
  924. entrytyp:=mainentryid;
  925. {start}
  926. newentry;
  927. createstream:=true;
  928. end;
  929. procedure tentryfile.writebuf;
  930. begin
  931. if outputallowed and
  932. (bufidx <> 0) then
  933. f.Write(buf^,bufidx);
  934. inc(bufstart,bufidx);
  935. bufidx:=0;
  936. end;
  937. procedure tentryfile.writedata(const b;len:integer);
  938. var
  939. p : pchar;
  940. left,
  941. idx : integer;
  942. begin
  943. if not outputallowed then
  944. exit;
  945. p:=pchar(@b);
  946. idx:=0;
  947. while len>0 do
  948. begin
  949. left:=bufsize-bufidx;
  950. if len>left then
  951. begin
  952. move(p[idx],buf[bufidx],left);
  953. dec(len,left);
  954. inc(idx,left);
  955. inc(bufidx,left);
  956. writebuf;
  957. end
  958. else
  959. begin
  960. move(p[idx],buf[bufidx],len);
  961. inc(bufidx,len);
  962. exit;
  963. end;
  964. end;
  965. end;
  966. procedure tentryfile.newentry;
  967. begin
  968. with entry do
  969. begin
  970. id:=entrytyp;
  971. nr:=ibend;
  972. size:=0;
  973. end;
  974. {Reset Entry State}
  975. entryidx:=0;
  976. entrybufstart:=bufstart;
  977. entrystart:=bufstart+bufidx;
  978. {Alloc in buffer}
  979. writedata(entry,sizeof(tentry));
  980. end;
  981. procedure tentryfile.writeentry(ibnr:byte);
  982. var
  983. opos : integer;
  984. begin
  985. {create entry}
  986. entry.id:=entrytyp;
  987. entry.nr:=ibnr;
  988. entry.size:=entryidx;
  989. {it's already been sent to disk ?}
  990. if entrybufstart<>bufstart then
  991. begin
  992. if outputallowed then
  993. begin
  994. {flush to be sure}
  995. WriteBuf;
  996. {write entry}
  997. opos:=f.Position;
  998. f.Position:=entrystart;
  999. f.write(entry,sizeof(tentry));
  1000. f.Position:=opos;
  1001. end;
  1002. entrybufstart:=bufstart;
  1003. end
  1004. else
  1005. move(entry,buf[entrystart-bufstart],sizeof(entry));
  1006. {Add New Entry, which is ibend by default}
  1007. entrystart:=bufstart+bufidx; {next entry position}
  1008. newentry;
  1009. end;
  1010. procedure tentryfile.putdata(const b;len:integer);
  1011. begin
  1012. if outputallowed then
  1013. writedata(b,len);
  1014. inc(entryidx,len);
  1015. end;
  1016. procedure tentryfile.putbyte(b:byte);
  1017. begin
  1018. putdata(b,1);
  1019. end;
  1020. procedure tentryfile.putword(w:word);
  1021. begin
  1022. putdata(w,2);
  1023. end;
  1024. procedure tentryfile.putdword(w:dword);
  1025. begin
  1026. putdata(w,4);
  1027. end;
  1028. procedure tentryfile.putlongint(l:longint);
  1029. begin
  1030. putdata(l,4);
  1031. end;
  1032. procedure tentryfile.putint64(i:int64);
  1033. begin
  1034. putdata(i,8);
  1035. end;
  1036. procedure tentryfile.putqword(q:qword);
  1037. begin
  1038. putdata(q,sizeof(qword));
  1039. end;
  1040. procedure tentryfile.putaint(i:aint);
  1041. begin
  1042. putdata(i,sizeof(aint));
  1043. end;
  1044. procedure tentryfile.putasizeint(i: asizeint);
  1045. begin
  1046. putdata(i,sizeof(asizeint));
  1047. end;
  1048. procedure tentryfile.putaword(i:aword);
  1049. begin
  1050. putdata(i,sizeof(aword));
  1051. end;
  1052. procedure tentryfile.putreal(d:entryreal);
  1053. var
  1054. hd : double;
  1055. begin
  1056. if target_info.system=system_x86_64_win64 then
  1057. begin
  1058. hd:=d;
  1059. putdata(hd,sizeof(hd));
  1060. end
  1061. else
  1062. putdata(d,sizeof(entryreal));
  1063. end;
  1064. procedure tentryfile.putstring(const s:string);
  1065. begin
  1066. putdata(s,length(s)+1);
  1067. end;
  1068. procedure tentryfile.putansistring(const s:ansistring);
  1069. var
  1070. len: longint;
  1071. begin
  1072. len:=length(s);
  1073. putlongint(len);
  1074. if len>0 then
  1075. putdata(s[1],len);
  1076. end;
  1077. procedure tentryfile.putsmallset(const b);
  1078. var
  1079. l : longint;
  1080. begin
  1081. l:=longint(b);
  1082. putlongint(l);
  1083. end;
  1084. procedure tentryfile.putnormalset(const b);
  1085. begin
  1086. putdata(b,32);
  1087. end;
  1088. procedure tentryfile.tempclose;
  1089. begin
  1090. if not closed then
  1091. begin
  1092. closepos:=f.Position;
  1093. f.Free;
  1094. f:=nil;
  1095. closed:=true;
  1096. tempclosed:=true;
  1097. end;
  1098. end;
  1099. function tentryfile.tempopen:boolean;
  1100. begin
  1101. tempopen:=false;
  1102. if not closed or not tempclosed then
  1103. exit;
  1104. { MG: not sure, if this is correct
  1105. f.position:=0;
  1106. No, f was freed in tempclose above, we need to
  1107. recreate it. PM 2011/06/06 }
  1108. try
  1109. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1110. except
  1111. exit;
  1112. end;
  1113. closed:=false;
  1114. tempclosed:=false;
  1115. { restore state }
  1116. f.Position:=closepos;
  1117. tempopen:=true;
  1118. end;
  1119. end.