entfile.pas 28 KB

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