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