entfile.pas 26 KB

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