entfile.pas 26 KB

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