entfile.pas 25 KB

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