entfile.pas 25 KB

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