entfile.pas 25 KB

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