entfile.pas 25 KB

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