entfile.pas 27 KB

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