entfile.pas 24 KB

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