entfile.pas 28 KB

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