2
0

entfile.pas 28 KB

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