entfile.pas 27 KB

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