entfile.pas 28 KB

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