entfile.pas 28 KB

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