entfile.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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. // Unused, and wrong as there are entries that are larger then 1024 bytes
  26. entryfilebufsize = 16384;
  27. {ppu entries}
  28. mainentryid = 1;
  29. subentryid = 2;
  30. {special}
  31. iberror = 0;
  32. ibextraheader = 242;
  33. ibpputable = 243;
  34. ibstartrequireds = 244;
  35. ibendrequireds = 245;
  36. ibstartcontained = 246;
  37. ibendcontained = 247;
  38. ibstartdefs = 248;
  39. ibenddefs = 249;
  40. ibstartsyms = 250;
  41. ibendsyms = 251;
  42. ibendinterface = 252;
  43. ibendimplementation = 253;
  44. // ibendbrowser = 254;
  45. ibend = 255;
  46. {general}
  47. ibmodulename = 1;
  48. ibsourcefiles = 2;
  49. ibloadunit = 3;
  50. ibinitunit = 4;
  51. iblinkunitofiles = 5;
  52. iblinkunitstaticlibs = 6;
  53. iblinkunitsharedlibs = 7;
  54. iblinkotherofiles = 8;
  55. iblinkotherstaticlibs = 9;
  56. iblinkothersharedlibs = 10;
  57. ibImportSymbols = 11;
  58. ibsymref = 12;
  59. ibdefref = 13;
  60. ibfeatures = 14;
  61. {$IFDEF MACRO_DIFF_HINT}
  62. ibusedmacros = 16;
  63. {$ENDIF}
  64. ibderefdata = 17;
  65. ibexportedmacros = 18;
  66. ibderefmap = 19;
  67. {syms}
  68. ibtypesym = 20;
  69. ibprocsym = 21;
  70. ibstaticvarsym = 22;
  71. ibconstsym = 23;
  72. ibenumsym = 24;
  73. // ibtypedconstsym = 25;
  74. ibabsolutevarsym = 26;
  75. ibpropertysym = 27;
  76. ibfieldvarsym = 28;
  77. ibunitsym = 29;
  78. iblabelsym = 30;
  79. ibsyssym = 31;
  80. ibnamespacesym = 32;
  81. iblocalvarsym = 33;
  82. ibparavarsym = 34;
  83. ibmacrosym = 35;
  84. {definitions}
  85. iborddef = 40;
  86. ibpointerdef = 41;
  87. ibarraydef = 42;
  88. ibprocdef = 43;
  89. ibshortstringdef = 44;
  90. ibrecorddef = 45;
  91. ibfiledef = 46;
  92. ibformaldef = 47;
  93. ibobjectdef = 48;
  94. ibenumdef = 49;
  95. ibsetdef = 50;
  96. ibprocvardef = 51;
  97. ibfloatdef = 52;
  98. ibclassrefdef = 53;
  99. iblongstringdef = 54;
  100. ibansistringdef = 55;
  101. ibwidestringdef = 56;
  102. ibvariantdef = 57;
  103. ibundefineddef = 58;
  104. ibunicodestringdef = 59;
  105. {implementation/ObjData}
  106. ibnodetree = 80;
  107. ibasmsymbols = 81;
  108. ibresources = 82;
  109. ibcreatedobjtypes = 83;
  110. ibwpofile = 84;
  111. ibmoduleoptions = 85;
  112. ibunitimportsyms = 86;
  113. iborderedsymbols = 87;
  114. ibmainname = 90;
  115. ibsymtableoptions = 91;
  116. ibpackagefiles = 92;
  117. ibpackagename = 93;
  118. ibrecsymtableoptions = 94;
  119. { target-specific things }
  120. iblinkotherframeworks = 100;
  121. ibjvmnamespace = 101;
  122. {$ifdef generic_cpu}
  123. { We need to use the correct size of aint and pint for
  124. the target CPU }
  125. const
  126. CpuAddrBitSize : array[tsystemcpu] of longint =
  127. (
  128. { 0 } 32 {'none'},
  129. { 1 } 32 {'i386'},
  130. { 2 } 32 {'m68k'},
  131. { 3 } 32 {'alpha'},
  132. { 4 } 32 {'powerpc'},
  133. { 5 } 32 {'sparc'},
  134. { 6 } 32 {'vis'},
  135. { 7 } 64 {'ia64'},
  136. { 8 } 64 {'x86_64'},
  137. { 9 } 32 {'mipseb'},
  138. { 10 } 32 {'arm'},
  139. { 11 } 64 {'powerpc64'},
  140. { 12 } 16 {'avr'},
  141. { 13 } 32 {'mipsel'},
  142. { 14 } 32 {'jvm'},
  143. { 15 } 16 {'i8086'},
  144. { 16 } 64 {'aarch64'},
  145. { 17 } 32 {'wasm32'},
  146. { 18 } 64 {'sparc64'},
  147. { 19 } 32 {'riscv32'},
  148. { 20 } 64 {'riscv64'},
  149. { 21 } 32 {'xtensa'},
  150. { 22 } 16 {'z80'}
  151. );
  152. CpuAluBitSize : array[tsystemcpu] of longint =
  153. (
  154. { 0 } 32 {'none'},
  155. { 1 } 32 {'i386'},
  156. { 2 } 32 {'m68k'},
  157. { 3 } 32 {'alpha'},
  158. { 4 } 32 {'powerpc'},
  159. { 5 } 32 {'sparc'},
  160. { 6 } 32 {'vis'},
  161. { 7 } 64 {'ia64'},
  162. { 8 } 64 {'x86_64'},
  163. { 9 } 32 {'mipseb'},
  164. { 10 } 32 {'arm'},
  165. { 11 } 64 {'powerpc64'},
  166. { 12 } 8 {'avr'},
  167. { 13 } 32 {'mipsel'},
  168. { 14 } 64 {'jvm'},
  169. { 15 } 16 {'i8086'},
  170. { 16 } 64 {'aarch64'},
  171. { 17 } 64 {'wasm32'},
  172. { 18 } 64 {'sparc64'},
  173. { 19 } 32 {'riscv32'},
  174. { 20 } 64 {'riscv64'},
  175. { 21 } 32 {'xtensa'},
  176. { 22 } 8 {'z80'}
  177. );
  178. {$endif generic_cpu}
  179. type
  180. { bestreal is defined based on the target architecture }
  181. entryreal=bestreal;
  182. { common part of the header for all kinds of entry files }
  183. tentryheader=record
  184. id : array[1..3] of char;
  185. ver : array[1..3] of char;
  186. compiler : word;
  187. cpu : word;
  188. target : word;
  189. flags : dword;
  190. size : dword; { size of the ppufile without header }
  191. end;
  192. pentryheader=^tentryheader;
  193. tentry=packed record
  194. size : longint;
  195. id : byte;
  196. nr : byte;
  197. end;
  198. tentryfile=class
  199. private
  200. function getposition:longint;
  201. procedure setposition(value:longint);
  202. protected
  203. buf : pchar;
  204. bufstart,
  205. bufsize,
  206. bufidx : integer;
  207. entrybufstart,
  208. entrystart,
  209. entryidx : integer;
  210. entry : tentry;
  211. closed,
  212. tempclosed : boolean;
  213. closepos : integer;
  214. protected
  215. f : TCStream;
  216. {$ifdef DEBUG_PPU}
  217. flog : text;
  218. flog_open : boolean;
  219. ppu_log_level : longint;
  220. ppu_log_idx : integer;
  221. {$endif}
  222. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  223. fisfile : boolean;
  224. fname : string;
  225. fsize : integer;
  226. procedure newheader;virtual;abstract;
  227. function readheader:longint;virtual;abstract;
  228. function outputallowed:boolean;virtual;
  229. procedure resetfile;virtual;abstract;
  230. function getheadersize:longint;virtual;abstract;
  231. function getheaderaddr:pentryheader;virtual;abstract;
  232. procedure RaiseAssertion(Code: Longint); virtual;
  233. public
  234. entrytyp : byte;
  235. size : integer;
  236. change_endian : boolean; { Used in ppudump util }
  237. {$ifdef generic_cpu}
  238. has_more,
  239. {$endif not generic_cpu}
  240. error : boolean;
  241. constructor create(const fn:string);
  242. destructor destroy;override;
  243. function getversion:integer;
  244. procedure flush; {$ifdef USEINLINE}inline;{$endif}
  245. procedure closefile;virtual;
  246. procedure newentry;
  247. property position:longint read getposition write setposition;
  248. { Warning: don't keep the stream open during a tempclose! }
  249. function substream(ofs,len:longint):TCStream;
  250. { Warning: don't use the put* or write* functions anymore when writing through this }
  251. property stream:TCStream read f;
  252. {$ifdef DEBUG_PPU}
  253. procedure ppu_log(st :string);virtual;
  254. procedure ppu_log_val(st :string);virtual;
  255. procedure inc_log_level;
  256. procedure dec_log_level;
  257. {$endif}
  258. {read}
  259. function openfile:boolean;
  260. function openstream(strm:TCStream):boolean;
  261. procedure reloadbuf;
  262. procedure readdata(out b;len:integer);
  263. procedure skipdata(len:integer);
  264. function readentry:byte;
  265. function EndOfEntry:boolean; {$ifdef USEINLINE}inline;{$endif}
  266. function entrysize:longint; {$ifdef USEINLINE}inline;{$endif}
  267. function entryleft:longint; {$ifdef USEINLINE}inline;{$endif}
  268. procedure getdatabuf(out b;len:integer;out res:integer);
  269. procedure getdata(out b;len:integer);
  270. function getbyte:byte;
  271. function getword:word;
  272. function getdword:dword;
  273. function getlongint:longint;
  274. function getint64:int64;
  275. function getqword:qword;
  276. function getaint:{$ifdef generic_cpu}int64{$else}aint{$ifdef USEINLINE}; inline{$endif}{$endif};
  277. function getasizeint:{$ifdef generic_cpu}int64{$else}asizeint{$ifdef USEINLINE}; inline{$endif}{$endif};
  278. function getpuint:{$ifdef generic_cpu}qword{$else}puint{$ifdef USEINLINE}; inline{$endif}{$endif};
  279. function getptruint:{$ifdef generic_cpu}qword{$else}TConstPtrUInt{$ifdef USEINLINE}; inline{$endif}{$endif};
  280. function getaword:{$ifdef generic_cpu}qword{$else}aword{$ifdef USEINLINE}; inline{$endif}{$endif};
  281. function getreal:entryreal;
  282. function getrealsize(sizeofreal : longint):entryreal;
  283. function getboolean:boolean; {$ifdef USEINLINE}inline;{$endif}
  284. function getstring:string;
  285. function getpshortstring:pshortstring;
  286. function getansistring:ansistring;
  287. procedure getset(out arr: array of byte);
  288. function skipuntilentry(untilb:byte):boolean;
  289. {write}
  290. function createfile:boolean;virtual;
  291. function createstream(strm:TCStream):boolean;
  292. procedure writeheader;virtual;abstract;
  293. procedure writebuf;
  294. procedure writedata(const b;len:integer);
  295. procedure writeentry(ibnr:byte);
  296. procedure putdata(const b;len:integer);virtual;
  297. procedure putbyte(b:byte); {$ifdef USEINLINE}inline;{$endif}
  298. procedure putword(w:word); {$ifdef USEINLINE}inline;{$endif}
  299. procedure putdword(w:dword); {$ifdef USEINLINE}inline;{$endif}
  300. procedure putlongint(l:longint); {$ifdef USEINLINE}inline;{$endif}
  301. procedure putint64(i:int64); {$ifdef USEINLINE}inline;{$endif}
  302. procedure putqword(q:qword); {$ifdef USEINLINE}inline;{$endif}
  303. procedure putaint(i:aint); {$ifdef USEINLINE}inline;{$endif}
  304. procedure putasizeint(i:asizeint); {$ifdef USEINLINE}inline;{$endif}
  305. procedure putpuint(i:puint); {$ifdef USEINLINE}inline;{$endif}
  306. procedure putptruint(v:TConstPtrUInt); {$ifdef USEINLINE}inline;{$endif}
  307. procedure putaword(i:aword); {$ifdef USEINLINE}inline;{$endif}
  308. procedure putreal(d:entryreal);
  309. procedure putboolean(b:boolean); {$ifdef USEINLINE}inline;{$endif}
  310. procedure putstring(const s:string); {$ifdef USEINLINE}inline;{$endif}
  311. procedure putansistring(const s:ansistring);
  312. procedure putset(const arr: array of byte);
  313. procedure tempclose; // MG: not used, obsolete?
  314. function tempopen:boolean; // MG: not used, obsolete?
  315. end;
  316. implementation
  317. uses
  318. cutils;
  319. function swapendian_entryreal(d:entryreal):entryreal;
  320. type
  321. entryreal_bytes=array[0..sizeof(d)-1] of byte;
  322. var
  323. i:0..sizeof(d)-1;
  324. begin
  325. for i:=low(entryreal_bytes) to high(entryreal_bytes) do
  326. entryreal_bytes(result)[i]:=entryreal_bytes(d)[high(entryreal_bytes)-i];
  327. end;
  328. {*****************************************************************************
  329. tentryfile
  330. *****************************************************************************}
  331. function tentryfile.outputallowed: boolean;
  332. begin
  333. result:=true;
  334. end;
  335. constructor tentryfile.create(const fn:string);
  336. begin
  337. fname:=fn;
  338. fisfile:=false;
  339. change_endian:=false;
  340. mode:=0;
  341. newheader;
  342. error:=false;
  343. closed:=true;
  344. tempclosed:=false;
  345. getmem(buf,entryfilebufsize);
  346. {$ifdef DEBUG_PPU}
  347. assign(flog,fn+'.debug-log');
  348. flog_open:=false;
  349. {$endif DEBUG_PPU}
  350. end;
  351. destructor tentryfile.destroy;
  352. begin
  353. closefile;
  354. {$ifdef DEBUG_PPU}
  355. if flog_open then
  356. close(flog);
  357. flog_open:=false;
  358. {$endif DEBUG_PPU}
  359. if assigned(buf) then
  360. freemem(buf,entryfilebufsize);
  361. end;
  362. {$ifdef DEBUG_PPU}
  363. function entryid_name(nr : byte) : string;
  364. begin
  365. case nr of
  366. {ppu entries}
  367. mainentryid: entryid_name:='main_entry_id';
  368. subentryid: entryid_name:='sub_entry_id';
  369. else
  370. entryid_name:='unknown entryid '+tostr(nr);
  371. end;
  372. end;
  373. function entry_name(nr : byte) : string;
  374. begin
  375. case nr of
  376. {special}
  377. iberror: entry_name:='iberror';
  378. ibextraheader: entry_name:='ibextraheader';
  379. ibpputable: entry_name:='ibpputable';
  380. ibstartrequireds: entry_name:='ibstartrequireds';
  381. ibendrequireds: entry_name:='ibendrequireds';
  382. ibstartcontained: entry_name:='ibstartcontained';
  383. ibendcontained: entry_name:='ibendcontained';
  384. ibstartdefs: entry_name:='ibstartdefs';
  385. ibenddefs: entry_name:='ibenddefs';
  386. ibstartsyms: entry_name:='ibstartsyms';
  387. ibendsyms: entry_name:='ibendsyms';
  388. ibendinterface: entry_name:='ibendinterface';
  389. ibendimplementation: entry_name:='ibendimplementation';
  390. // ibendbrowser: entry_name:='ibendbrowser';
  391. ibend: entry_name:='ibend';
  392. {general}
  393. ibmodulename: entry_name:='ibmodulename';
  394. ibsourcefiles: entry_name:='ibsourcefiles';
  395. ibloadunit: entry_name:='ibloadunit';
  396. ibinitunit: entry_name:='ibinitunit';
  397. iblinkunitofiles: entry_name:='iblinkunitofiles';
  398. iblinkunitstaticlibs: entry_name:='iblinkunitstaticlibs';
  399. iblinkunitsharedlibs: entry_name:='iblinkunitsharedlibs';
  400. iblinkotherofiles: entry_name:='iblinkotherofiles';
  401. iblinkotherstaticlibs: entry_name:='iblinkotherstaticlibs';
  402. iblinkothersharedlibs: entry_name:='iblinkothersharedlibs';
  403. ibImportSymbols: entry_name:='ibImportSymbols';
  404. ibsymref: entry_name:='ibsymref';
  405. ibdefref: entry_name:='ibdefref';
  406. ibfeatures: entry_name:='ibfeatures';
  407. {$IFDEF MACRO_DIFF_HINT}
  408. ibusedmacros: entry_name:='ibusedmacros';
  409. {$ENDIF}
  410. ibderefdata: entry_name:='ibderefdata';
  411. ibexportedmacros: entry_name:='ibexportedmacros';
  412. ibderefmap: entry_name:='ibderefmap';
  413. {syms}
  414. ibtypesym: entry_name:='ibtypesym';
  415. ibprocsym: entry_name:='ibprocsym';
  416. ibstaticvarsym: entry_name:='ibstaticvarsym';
  417. ibconstsym: entry_name:='ibconstsym';
  418. ibenumsym: entry_name:='ibenumsym';
  419. // ibtypedconstsym: entry_name:='ibtypedconstsym';
  420. ibabsolutevarsym: entry_name:='ibabsolutevarsym';
  421. ibpropertysym: entry_name:='ibpropertysym';
  422. ibfieldvarsym: entry_name:='ibfieldvarsym';
  423. ibunitsym: entry_name:='ibunitsym';
  424. iblabelsym: entry_name:='iblabelsym';
  425. ibsyssym: entry_name:='ibsyssym';
  426. ibnamespacesym: entry_name:='ibnamespacesym';
  427. iblocalvarsym: entry_name:='iblocalvarsym';
  428. ibparavarsym: entry_name:='ibparavarsym';
  429. ibmacrosym: entry_name:='ibmacrosym';
  430. {definitions}
  431. iborddef: entry_name:='iborddef';
  432. ibpointerdef: entry_name:='ibpointerdef';
  433. ibarraydef: entry_name:='ibarraydef';
  434. ibprocdef: entry_name:='ibprocdef';
  435. ibshortstringdef: entry_name:='ibshortstringdef';
  436. ibrecorddef: entry_name:='ibrecorddef';
  437. ibfiledef: entry_name:='ibfiledef';
  438. ibformaldef: entry_name:='ibformaldef';
  439. ibobjectdef: entry_name:='ibobjectdef';
  440. ibenumdef: entry_name:='ibenumdef';
  441. ibsetdef: entry_name:='ibsetdef';
  442. ibprocvardef: entry_name:='ibprocvardef';
  443. ibfloatdef: entry_name:='ibfloatdef';
  444. ibclassrefdef: entry_name:='ibclassrefdef';
  445. iblongstringdef: entry_name:='iblongstringdef';
  446. ibansistringdef: entry_name:='ibansistringdef';
  447. ibwidestringdef: entry_name:='ibwidestringdef';
  448. ibvariantdef: entry_name:='ibvariantdef';
  449. ibundefineddef: entry_name:='ibundefineddef';
  450. ibunicodestringdef: entry_name:='ibunicodestringdef';
  451. {implementation/ObjData}
  452. ibnodetree: entry_name:='ibnodetree';
  453. ibasmsymbols: entry_name:='ibasmsymbols';
  454. ibresources: entry_name:='ibresources';
  455. ibcreatedobjtypes: entry_name:='ibcreatedobjtypes';
  456. ibwpofile: entry_name:='ibwpofile';
  457. ibmoduleoptions: entry_name:='ibmoduleoptions';
  458. ibunitimportsyms: entry_name:='ibunitimportsyms';
  459. iborderedsymbols: entry_name:='iborderedsymbols';
  460. ibmainname: entry_name:='ibmainname';
  461. ibsymtableoptions: entry_name:='ibsymtableoptions';
  462. // ibrecsymtableoptions: entry_name:='ibrecsymtableoptions';
  463. ibpackagefiles: entry_name:='ibpackagefiles';
  464. ibpackagename: entry_name:='ibpackagename';
  465. { target-specific things }
  466. iblinkotherframeworks: entry_name:='iblinkotherframeworks';
  467. ibjvmnamespace: entry_name:='ibjvmnamespace';
  468. else
  469. entry_name:='unknown entry '+tostr(nr);
  470. end;
  471. end;
  472. procedure tentryfile.ppu_log(st :string);
  473. begin
  474. if flog_open then
  475. begin
  476. writeln(flog,bufstart+bufidx,': ',st);
  477. end;
  478. {$ifdef IN_PPUDUMP}
  479. writeln(bufstart+bufidx,': ',st);
  480. {$endif}
  481. end;
  482. procedure tentryfile.inc_log_level;
  483. begin
  484. inc(ppu_log_level);
  485. end;
  486. procedure tentryfile.ppu_log_val(st :string);
  487. begin
  488. if flog_open then
  489. begin
  490. writeln(flog,'(',ppu_log_level,') value: ',st);
  491. end;
  492. {$ifdef IN_PPUDUMP}
  493. writeln('(',ppu_log_level,') value: ',st);
  494. {$endif}
  495. end;
  496. procedure tentryfile.dec_log_level;
  497. begin
  498. dec(ppu_log_level);
  499. end;
  500. {$endif}
  501. function tentryfile.getversion:integer;
  502. var
  503. l : integer;
  504. code : integer;
  505. header : pentryheader;
  506. begin
  507. header:=getheaderaddr;
  508. Val(header^.ver[1]+header^.ver[2]+header^.ver[3],l,code);
  509. if code=0 then
  510. result:=l
  511. else
  512. result:=0;
  513. end;
  514. procedure tentryfile.flush;
  515. begin
  516. if mode=2 then
  517. writebuf;
  518. end;
  519. procedure tentryfile.RaiseAssertion(Code: Longint);
  520. begin
  521. { It's down to descendent classes to raise an internal error as desired. [Kit] }
  522. error := true;
  523. end;
  524. procedure tentryfile.closefile;
  525. begin
  526. if mode<>0 then
  527. begin
  528. flush;
  529. {$ifdef DEBUG_PPU}
  530. if (entry.nr<>0) and (mode=1) then
  531. ppu_log('writeentry, id='+entryid_name(entry.id)+' nr='+entry_name(entry.nr)+' size='+tostr(entry.size));
  532. {$endif}
  533. if fisfile then
  534. f.Free;
  535. mode:=0;
  536. closed:=true;
  537. end;
  538. end;
  539. procedure tentryfile.setposition(value:longint);
  540. begin
  541. if assigned(f) then
  542. f.Position:=value
  543. else
  544. if tempclosed then
  545. closepos:=value;
  546. end;
  547. function tentryfile.getposition:longint;
  548. begin
  549. if assigned(f) then
  550. result:=f.Position
  551. else
  552. if tempclosed then
  553. result:=closepos
  554. else
  555. result:=0;
  556. end;
  557. function tentryfile.substream(ofs,len:longint):TCStream;
  558. begin
  559. result:=nil;
  560. if assigned(f) then
  561. result:=TCRangeStream.Create(f,ofs,len);
  562. end;
  563. {*****************************************************************************
  564. tentryfile Reading
  565. *****************************************************************************}
  566. function tentryfile.openfile:boolean;
  567. var
  568. strm : TCStream;
  569. begin
  570. openfile:=false;
  571. try
  572. strm:=CFileStreamClass.Create(fname,fmOpenRead)
  573. except
  574. exit;
  575. end;
  576. openfile:=openstream(strm);
  577. fisfile:=result;
  578. end;
  579. function tentryfile.openstream(strm:TCStream):boolean;
  580. var
  581. i : longint;
  582. begin
  583. openstream:=false;
  584. f:=strm;
  585. closed:=false;
  586. {$ifdef DEBUG_PPU}
  587. {$push}
  588. {$I-}
  589. assign(flog,fname+'.debug-read-log');
  590. rewrite(flog);
  591. if InOutRes=0 then
  592. flog_open:=true;
  593. {$pop}
  594. {$endif DEBUG_PPU}
  595. {read ppuheader}
  596. fsize:=f.Size;
  597. i:=readheader;
  598. if i<0 then
  599. exit;
  600. {reset buffer}
  601. bufstart:=i;
  602. bufsize:=0;
  603. bufidx:=0;
  604. mode:=1;
  605. FillChar(entry,sizeof(tentry),0);
  606. entryidx:=0;
  607. entrystart:=0;
  608. entrybufstart:=0;
  609. error:=false;
  610. openstream:=true;
  611. end;
  612. procedure tentryfile.reloadbuf;
  613. begin
  614. inc(bufstart,bufsize);
  615. bufsize:=f.Read(buf^,entryfilebufsize);
  616. bufidx:=0;
  617. end;
  618. procedure tentryfile.readdata(out b;len:integer);
  619. var
  620. p,pbuf : pchar;
  621. left : integer;
  622. {$ifdef DEBUG_PPU}
  623. i : integer;
  624. {$endif DEBUG_PPU}
  625. begin
  626. p:=pchar(@b);
  627. pbuf:=@buf[bufidx];
  628. repeat
  629. left:=bufsize-bufidx;
  630. if len<left then
  631. break;
  632. move(pbuf^,p^,left);
  633. dec(len,left);
  634. inc(p,left);
  635. reloadbuf;
  636. pbuf:=@buf[bufidx];
  637. if bufsize=0 then
  638. exit;
  639. until false;
  640. move(pbuf^,p^,len);
  641. {$ifdef DEBUG_PPU}
  642. if ppu_log_level <= 0 then
  643. begin
  644. ppu_log('writedata, length='+tostr(len)+' level='+tostr(ppu_log_level));
  645. for i:=0 to len-1 do
  646. ppu_log_val('p['+tostr(i)+']=$'+hexstr(byte(p[i]),2));
  647. end;
  648. {$endif DEBUG_PPU}
  649. inc(bufidx,len);
  650. end;
  651. procedure tentryfile.skipdata(len:integer);
  652. var
  653. left : integer;
  654. begin
  655. {$ifdef DEBUG_PPU}
  656. if len>0 then
  657. ppu_log('explicit skipdata '+tostr(len));
  658. {$endif}
  659. while len>0 do
  660. begin
  661. left:=bufsize-bufidx;
  662. if len>left then
  663. begin
  664. dec(len,left);
  665. reloadbuf;
  666. if bufsize=0 then
  667. exit;
  668. end
  669. else
  670. begin
  671. inc(bufidx,len);
  672. exit;
  673. end;
  674. end;
  675. end;
  676. function tentryfile.readentry:byte;
  677. begin
  678. if entryidx<entry.size then
  679. begin
  680. {$ifdef generic_cpu}
  681. has_more:=true;
  682. {$endif not generic_cpu}
  683. {$ifdef DEBUG_PPU}
  684. if entry.size-entryidx>0 then
  685. ppu_log('skipdata '+tostr(entry.size-entryidx));
  686. {$endif}
  687. skipdata(entry.size-entryidx);
  688. end;
  689. {$ifdef DEBUG_PPU}
  690. if entry.nr<>0 then
  691. ppu_log('writeentry, id='+entryid_name(entry.id)+' nr='+entry_name(entry.nr)+' size='+tostr(entry.size));
  692. ppu_log('entrystart');
  693. {$endif}
  694. readdata(entry,sizeof(tentry));
  695. if change_endian then
  696. entry.size:=swapendian(entry.size);
  697. entrystart:=bufstart+bufidx;
  698. entryidx:=0;
  699. {$ifdef generic_cpu}
  700. has_more:=false;
  701. {$endif not generic_cpu}
  702. if not(entry.id in [mainentryid,subentryid]) then
  703. begin
  704. readentry:=iberror;
  705. error:=true;
  706. exit;
  707. end;
  708. readentry:=entry.nr;
  709. end;
  710. function tentryfile.endofentry:boolean;
  711. begin
  712. {$ifdef generic_cpu}
  713. endofentry:=(entryidx=entry.size);
  714. {$else not generic_cpu}
  715. endofentry:=(entryidx>=entry.size);
  716. {$endif not generic_cpu}
  717. end;
  718. function tentryfile.entrysize:longint;
  719. begin
  720. entrysize:=entry.size;
  721. end;
  722. function tentryfile.entryleft:longint;
  723. begin
  724. entryleft:=entry.size-entryidx;
  725. end;
  726. procedure tentryfile.getdatabuf(out b;len:integer;out res:integer);
  727. begin
  728. if entryidx+len>entry.size then
  729. res:=entry.size-entryidx
  730. else
  731. res:=len;
  732. readdata(b,res);
  733. inc(entryidx,res);
  734. end;
  735. procedure tentryfile.getdata(out b;len:integer);
  736. begin
  737. if entryidx+len>entry.size then
  738. begin
  739. error:=true;
  740. exit;
  741. end;
  742. readdata(b,len);
  743. inc(entryidx,len);
  744. end;
  745. function tentryfile.getbyte:byte;
  746. begin
  747. if entryidx>=entry.size then
  748. begin
  749. error:=true;
  750. result:=0;
  751. exit;
  752. end;
  753. {$ifdef DEBUG_PPU}
  754. ppu_log('putbyte');
  755. inc_log_level;
  756. {$endif}
  757. if bufidx<bufsize then
  758. begin
  759. result:=pbyte(@buf[bufidx])^;
  760. inc(bufidx);
  761. end
  762. else
  763. readdata(result,1);
  764. {$ifdef DEBUG_PPU}
  765. ppu_log_val(tostr(result));
  766. dec_log_level;
  767. {$endif}
  768. inc(entryidx);
  769. end;
  770. function tentryfile.getword:word;
  771. begin
  772. if entryidx+2>entry.size then
  773. begin
  774. error:=true;
  775. result:=0;
  776. exit;
  777. end;
  778. {$ifdef DEBUG_PPU}
  779. ppu_log('putword');
  780. inc_log_level;
  781. {$endif}
  782. if bufsize-bufidx>=sizeof(word) then
  783. begin
  784. result:=Unaligned(pword(@buf[bufidx])^);
  785. inc(bufidx,sizeof(word));
  786. end
  787. else
  788. readdata(result,sizeof(word));
  789. if change_endian then
  790. result:=swapendian(result);
  791. {$ifdef DEBUG_PPU}
  792. ppu_log_val(tostr(result));
  793. dec_log_level;
  794. {$endif}
  795. inc(entryidx,2);
  796. end;
  797. function tentryfile.getlongint:longint;
  798. begin
  799. if entryidx+4>entry.size then
  800. begin
  801. error:=true;
  802. result:=0;
  803. exit;
  804. end;
  805. {$ifdef DEBUG_PPU}
  806. ppu_log('putlongint');
  807. inc_log_level;
  808. {$endif}
  809. if bufsize-bufidx>=sizeof(longint) then
  810. begin
  811. result:=Unaligned(plongint(@buf[bufidx])^);
  812. inc(bufidx,sizeof(longint));
  813. end
  814. else
  815. readdata(result,sizeof(longint));
  816. if change_endian then
  817. result:=swapendian(result);
  818. {$ifdef DEBUG_PPU}
  819. ppu_log_val(tostr(result));
  820. dec_log_level;
  821. {$endif}
  822. inc(entryidx,4);
  823. end;
  824. function tentryfile.getdword:dword;
  825. begin
  826. if entryidx+4>entry.size then
  827. begin
  828. error:=true;
  829. result:=0;
  830. exit;
  831. end;
  832. {$ifdef DEBUG_PPU}
  833. ppu_log('putdword');
  834. inc_log_level;
  835. {$endif}
  836. if bufsize-bufidx>=sizeof(dword) then
  837. begin
  838. result:=Unaligned(pdword(@buf[bufidx])^);
  839. inc(bufidx,sizeof(longint));
  840. end
  841. else
  842. readdata(result,sizeof(dword));
  843. if change_endian then
  844. result:=swapendian(result);
  845. {$ifdef DEBUG_PPU}
  846. ppu_log_val(tostr(result));
  847. dec_log_level;
  848. {$endif}
  849. inc(entryidx,4);
  850. end;
  851. function tentryfile.getint64:int64;
  852. begin
  853. if entryidx+8>entry.size then
  854. begin
  855. error:=true;
  856. result:=0;
  857. exit;
  858. end;
  859. {$ifdef DEBUG_PPU}
  860. ppu_log('putint64');
  861. inc_log_level;
  862. {$endif}
  863. if bufsize-bufidx>=sizeof(int64) then
  864. begin
  865. result:=Unaligned(pint64(@buf[bufidx])^);
  866. inc(bufidx,sizeof(int64));
  867. end
  868. else
  869. readdata(result,sizeof(int64));
  870. if change_endian then
  871. result:=swapendian(result);
  872. {$ifdef DEBUG_PPU}
  873. ppu_log_val(tostr(result));
  874. dec_log_level;
  875. {$endif}
  876. inc(entryidx,8);
  877. end;
  878. function tentryfile.getqword:qword;
  879. begin
  880. if entryidx+8>entry.size then
  881. begin
  882. error:=true;
  883. result:=0;
  884. exit;
  885. end;
  886. {$ifdef DEBUG_PPU}
  887. ppu_log('putqword');
  888. inc_log_level;
  889. {$endif}
  890. if bufsize-bufidx>=sizeof(qword) then
  891. begin
  892. result:=Unaligned(pqword(@buf[bufidx])^);
  893. inc(bufidx,sizeof(qword));
  894. end
  895. else
  896. readdata(result,sizeof(qword));
  897. if change_endian then
  898. result:=swapendian(result);
  899. {$ifdef DEBUG_PPU}
  900. ppu_log_val(tostr(result));
  901. dec_log_level;
  902. {$endif}
  903. inc(entryidx,8);
  904. end;
  905. function tentryfile.getaint:{$ifdef generic_cpu}int64{$else}aint{$endif};
  906. {$ifdef generic_cpu}
  907. var
  908. header : pentryheader;
  909. {$endif generic_cpu}
  910. begin
  911. {$ifdef DEBUG_PPU}
  912. ppu_log('putaint');
  913. inc_log_level;
  914. {$endif}
  915. {$ifdef generic_cpu}
  916. header:=getheaderaddr;
  917. if CpuAluBitSize[tsystemcpu(header^.cpu)]=64 then
  918. result:=getint64
  919. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=32 then
  920. result:=getlongint
  921. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=16 then
  922. result:=smallint(getword)
  923. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=8 then
  924. result:=shortint(getbyte)
  925. else
  926. begin
  927. error:=true;
  928. result:=0;
  929. end;
  930. {$else not generic_cpu}
  931. case sizeof(aint) of
  932. 8: result:=getint64;
  933. 4: result:=getlongint;
  934. 2: result:=smallint(getword);
  935. 1: result:=shortint(getbyte);
  936. else
  937. begin
  938. RaiseAssertion(2019041801);
  939. result:=0;
  940. end;
  941. end;
  942. {$endif not generic_cpu}
  943. {$ifdef DEBUG_PPU}
  944. ppu_log_val(tostr(result));
  945. dec_log_level;
  946. {$endif}
  947. end;
  948. function tentryfile.getasizeint:{$ifdef generic_cpu}int64{$else}asizeint{$endif};
  949. {$ifdef generic_cpu}
  950. var
  951. header : pentryheader;
  952. {$endif generic_cpu}
  953. begin
  954. {$ifdef DEBUG_PPU}
  955. ppu_log('putasizeint');
  956. inc_log_level;
  957. {$endif}
  958. {$ifdef generic_cpu}
  959. header:=getheaderaddr;
  960. if CpuAddrBitSize[tsystemcpu(header^.cpu)]=64 then
  961. result:=getint64
  962. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=32 then
  963. result:=getlongint
  964. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=16 then
  965. begin
  966. { result:=smallint(getword);
  967. would have been logical, but it contradicts
  968. definition of asizeint in globtype unit,
  969. which uses 32-bit lngint type even for 16-bit
  970. address size, to be able to cope with
  971. I8086 seg:ofs huge addresses }
  972. result:=getlongint;
  973. end
  974. else
  975. begin
  976. error:=true;
  977. result:=0;
  978. end;
  979. {$else not generic_cpu}
  980. case sizeof(asizeint) of
  981. 8: result:=asizeint(getint64);
  982. 4: result:=asizeint(getlongint);
  983. 2: result:=asizeint(getword);
  984. 1: result:=asizeint(getbyte);
  985. else
  986. begin
  987. RaiseAssertion(2019041802);
  988. result:=0;
  989. end;
  990. end;
  991. {$endif not generic_cpu}
  992. {$ifdef DEBUG_PPU}
  993. ppu_log_val(tostr(result));
  994. dec_log_level;
  995. {$endif}
  996. end;
  997. function tentryfile.getpuint:{$ifdef generic_cpu}qword{$else}puint{$endif};
  998. {$ifdef generic_cpu}
  999. var
  1000. header : pentryheader;
  1001. {$endif generic_cpu}
  1002. begin
  1003. {$ifdef DEBUG_PPU}
  1004. ppu_log('putpuint');
  1005. inc_log_level;
  1006. {$endif}
  1007. {$ifdef generic_cpu}
  1008. header:=getheaderaddr;
  1009. if CpuAddrBitSize[tsystemcpu(header^.cpu)]=64 then
  1010. result:=getqword
  1011. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=32 then
  1012. result:=getdword
  1013. else if CpuAddrBitSize[tsystemcpu(header^.cpu)]=16 then
  1014. result:=getword
  1015. else
  1016. begin
  1017. error:=true;
  1018. result:=0;
  1019. end;
  1020. {$else not generic_cpu}
  1021. case sizeof(puint) of
  1022. 8: result:=getqword;
  1023. 4: result:=getdword;
  1024. 2: result:=getword;
  1025. 1: result:=getbyte;
  1026. else
  1027. begin
  1028. RaiseAssertion(2019041803);
  1029. result:=0;
  1030. end;
  1031. end;
  1032. {$endif not generic_cpu}
  1033. {$ifdef DEBUG_PPU}
  1034. ppu_log_val(tostr(result));
  1035. dec_log_level;
  1036. {$endif}
  1037. end;
  1038. function tentryfile.getptruint:{$ifdef generic_cpu}qword{$else}TConstPtrUInt{$endif};
  1039. {$ifdef generic_cpu}
  1040. var
  1041. header : pentryheader;
  1042. {$endif generic_cpu}
  1043. begin
  1044. {$ifdef DEBUG_PPU}
  1045. ppu_log('putptruint');
  1046. inc_log_level;
  1047. {$endif}
  1048. {$ifdef generic_cpu}
  1049. header:=getheaderaddr;
  1050. if CpuAddrBitSize[tsystemcpu(header^.cpu)]=64 then
  1051. result:=getqword
  1052. else result:=getdword;
  1053. {$else not generic_cpu}
  1054. {$if sizeof(TConstPtrUInt)=8}
  1055. result:=tconstptruint(getint64);
  1056. {$else}
  1057. result:=TConstPtrUInt(getlongint);
  1058. {$endif}
  1059. {$endif not generic_cpu}
  1060. {$ifdef DEBUG_PPU}
  1061. ppu_log_val(tostr(result));
  1062. dec_log_level;
  1063. {$endif}
  1064. end;
  1065. function tentryfile.getaword:{$ifdef generic_cpu}qword{$else}aword{$endif};
  1066. {$ifdef generic_cpu}
  1067. var
  1068. header : pentryheader;
  1069. {$endif generic_cpu}
  1070. begin
  1071. {$ifdef DEBUG_PPU}
  1072. ppu_log('putaword');
  1073. inc_log_level;
  1074. {$endif}
  1075. {$ifdef generic_cpu}
  1076. header:=getheaderaddr;
  1077. if CpuAluBitSize[tsystemcpu(header^.cpu)]=64 then
  1078. result:=getqword
  1079. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=32 then
  1080. result:=getdword
  1081. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=16 then
  1082. result:=getword
  1083. else if CpuAluBitSize[tsystemcpu(header^.cpu)]=8 then
  1084. result:=getbyte
  1085. else
  1086. begin
  1087. error:=true;
  1088. result:=0;
  1089. end;
  1090. {$else not generic_cpu}
  1091. case sizeof(aword) of
  1092. 8: result:=getqword;
  1093. 4: result:=getdword;
  1094. 2: result:=getword;
  1095. 1: result:=getbyte;
  1096. else
  1097. begin
  1098. RaiseAssertion(2019041804);
  1099. result:=0;
  1100. end;
  1101. end;
  1102. {$endif not generic_cpu}
  1103. {$ifdef DEBUG_PPU}
  1104. ppu_log_val(tostr(result));
  1105. dec_log_level;
  1106. {$endif}
  1107. end;
  1108. function tentryfile.getrealsize(sizeofreal : longint):entryreal;
  1109. var
  1110. e : entryreal;
  1111. d : double;
  1112. di : qword;{ integer of same size as double }
  1113. s : single;
  1114. si : dword; { integer of same size as single }
  1115. begin
  1116. if sizeofreal=sizeof(e) then
  1117. begin
  1118. {$ifdef DEBUG_PPU}
  1119. ppu_log('putreal,size='+tostr(sizeof(e)));
  1120. inc_log_level;
  1121. {$endif}
  1122. if entryidx+sizeof(e)>entry.size then
  1123. begin
  1124. error:=true;
  1125. result:=0;
  1126. exit;
  1127. end;
  1128. readdata(e,sizeof(e));
  1129. if change_endian then
  1130. result:=swapendian_entryreal(e)
  1131. else
  1132. result:=e;
  1133. inc(entryidx,sizeof(e));
  1134. {$ifdef DEBUG_PPU}
  1135. ppu_log_val(realtostr(result));
  1136. dec_log_level;
  1137. {$endif}
  1138. exit;
  1139. end;
  1140. if sizeofreal=sizeof(d) then
  1141. begin
  1142. {$ifdef DEBUG_PPU}
  1143. ppu_log('putreal,size='+tostr(sizeof(d)));
  1144. inc_log_level;
  1145. {$endif}
  1146. if entryidx+sizeof(d)>entry.size then
  1147. begin
  1148. error:=true;
  1149. result:=0;
  1150. exit;
  1151. end;
  1152. readdata(d,sizeof(d));
  1153. if change_endian then
  1154. begin
  1155. di:=swapendian(pqword(@d)^);
  1156. d:=pdouble(@di)^;
  1157. end;
  1158. result:=d;
  1159. inc(entryidx,sizeof(d));
  1160. result:=d;
  1161. {$ifdef DEBUG_PPU}
  1162. ppu_log_val(realtostr(result));
  1163. dec_log_level;
  1164. {$endif}
  1165. exit;
  1166. end;
  1167. if sizeofreal=sizeof(s) then
  1168. begin
  1169. {$ifdef DEBUG_PPU}
  1170. ppu_log('putreal,size='+tostr(sizeof(s)));
  1171. inc_log_level;
  1172. {$endif}
  1173. if entryidx+sizeof(s)>entry.size then
  1174. begin
  1175. error:=true;
  1176. result:=0;
  1177. exit;
  1178. end;
  1179. readdata(s,sizeof(s));
  1180. if change_endian then
  1181. begin
  1182. si:=swapendian(pdword(@s)^);
  1183. s:=psingle(@si)^;
  1184. end;
  1185. result:=s;
  1186. inc(entryidx,sizeof(s));
  1187. result:=s;
  1188. {$ifdef DEBUG_PPU}
  1189. ppu_log_val(realtostr(result));
  1190. dec_log_level;
  1191. {$endif}
  1192. exit;
  1193. end;
  1194. error:=true;
  1195. result:=0.0;
  1196. end;
  1197. function tentryfile.getreal:entryreal;
  1198. var
  1199. d : entryreal;
  1200. hd : double;
  1201. begin
  1202. if target_info.system=system_x86_64_win64 then
  1203. begin
  1204. hd:=getrealsize(sizeof(hd));
  1205. getreal:=hd;
  1206. end
  1207. else
  1208. begin
  1209. d:=getrealsize(sizeof(d));
  1210. getreal:=d;
  1211. end;
  1212. end;
  1213. function tentryfile.getboolean:boolean;
  1214. begin
  1215. {$ifdef DEBUG_PPU}
  1216. ppu_log('putboolean');
  1217. {$endif}
  1218. result:=boolean(getbyte);
  1219. end;
  1220. function tentryfile.getstring:string;
  1221. begin
  1222. result[0]:=chr(getbyte);
  1223. {$ifdef DEBUG_PPU}
  1224. ppu_log('putstring,size='+tostr(length(result)+1));
  1225. inc_log_level;
  1226. {$endif}
  1227. if entryidx+length(result)>entry.size then
  1228. begin
  1229. error:=true;
  1230. exit;
  1231. end;
  1232. ReadData(result[1],length(result));
  1233. {$ifdef DEBUG_PPU}
  1234. ppu_log_val(result);
  1235. dec_log_level;
  1236. {$endif}
  1237. inc(entryidx,length(result));
  1238. end;
  1239. function tentryfile.getpshortstring:pshortstring;
  1240. var
  1241. len: char;
  1242. begin
  1243. result:=nil;
  1244. len:=chr(getbyte);
  1245. {$ifdef DEBUG_PPU}
  1246. ppu_log('putstring,size='+tostr(ord(len)+1));
  1247. inc_log_level;
  1248. {$endif}
  1249. if entryidx+ord(len)>entry.size then
  1250. begin
  1251. error:=true;
  1252. exit;
  1253. end;
  1254. getmem(result,ord(len)+1);
  1255. result^[0]:=len;
  1256. ReadData(result^[1],ord(len));
  1257. inc(entryidx,ord(len));
  1258. {$ifdef DEBUG_PPU}
  1259. ppu_log_val(result^);
  1260. dec_log_level;
  1261. {$endif}
  1262. end;
  1263. function tentryfile.getansistring:ansistring;
  1264. var
  1265. len: longint;
  1266. begin
  1267. {$ifdef DEBUG_PPU}
  1268. ppu_log('putansistring');
  1269. inc_log_level;
  1270. {$endif}
  1271. len:=getlongint;
  1272. if entryidx+len>entry.size then
  1273. begin
  1274. error:=true;
  1275. result:='';
  1276. exit;
  1277. end;
  1278. setlength(result,len);
  1279. if len>0 then
  1280. getdata(result[1],len);
  1281. {$ifdef DEBUG_PPU}
  1282. ppu_log_val(result);
  1283. dec_log_level;
  1284. {$endif}
  1285. end;
  1286. procedure tentryfile.getset(out arr: array of byte);
  1287. var
  1288. i : longint;
  1289. begin
  1290. {$ifdef DEBUG_PPU}
  1291. ppu_log('putset');
  1292. inc_log_level;
  1293. {$endif}
  1294. getdata(arr,sizeof(arr));
  1295. if change_endian then
  1296. for i:=low(arr) to high(arr) do
  1297. arr[i]:=reverse_byte(arr[i]);
  1298. {$ifdef DEBUG_PPU}
  1299. for i:=low(arr) to high(arr) do
  1300. ppu_log_val('byte['+tostr(i)+']=$'+hexstr(arr[i],2));
  1301. dec_log_level;
  1302. {$endif}
  1303. end;
  1304. function tentryfile.skipuntilentry(untilb:byte):boolean;
  1305. var
  1306. b : byte;
  1307. begin
  1308. {$ifdef DEBUG_PPU}
  1309. ppu_log('skipuntilentry '+tostr(untilb));
  1310. {$endif}
  1311. repeat
  1312. b:=readentry;
  1313. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  1314. skipuntilentry:=(b=untilb);
  1315. end;
  1316. {*****************************************************************************
  1317. tentryfile Writing
  1318. *****************************************************************************}
  1319. function tentryfile.createfile:boolean;
  1320. var
  1321. ok: boolean;
  1322. strm : TCStream;
  1323. begin
  1324. createfile:=false;
  1325. strm:=nil;
  1326. if outputallowed then
  1327. begin
  1328. {$ifdef MACOS}
  1329. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  1330. SetDefaultMacOSCreator('FPas');
  1331. SetDefaultMacOSFiletype('FPPU');
  1332. {$endif}
  1333. ok:=false;
  1334. try
  1335. strm:=CFileStreamClass.Create(fname,fmCreate);
  1336. ok:=true;
  1337. except
  1338. end;
  1339. {$ifdef MACOS}
  1340. SetDefaultMacOSCreator('MPS ');
  1341. SetDefaultMacOSFiletype('TEXT');
  1342. {$endif}
  1343. if not ok then
  1344. exit;
  1345. end;
  1346. createfile:=createstream(strm);
  1347. fisfile:=result;
  1348. end;
  1349. function tentryfile.createstream(strm:TCStream):boolean;
  1350. begin
  1351. createstream:=false;
  1352. if outputallowed then
  1353. begin
  1354. f:=strm;
  1355. mode:=2;
  1356. {write header for sure}
  1357. f.Write(getheaderaddr^,getheadersize);
  1358. end;
  1359. bufsize:=entryfilebufsize;
  1360. bufstart:=getheadersize;
  1361. bufidx:=0;
  1362. {reset}
  1363. resetfile;
  1364. error:=false;
  1365. size:=0;
  1366. entrytyp:=mainentryid;
  1367. {$ifdef DEBUG_PPU}
  1368. {$push}
  1369. {$I-}
  1370. assign(flog,fname+'.debug-write-log');
  1371. rewrite(flog);
  1372. if InOutRes=0 then
  1373. flog_open:=true;
  1374. {$pop}
  1375. {$endif DEBUG_PPU}
  1376. {start}
  1377. newentry;
  1378. createstream:=true;
  1379. end;
  1380. procedure tentryfile.writebuf;
  1381. begin
  1382. if outputallowed and
  1383. (bufidx <> 0) then
  1384. f.Write(buf^,bufidx);
  1385. inc(bufstart,bufidx);
  1386. bufidx:=0;
  1387. end;
  1388. procedure tentryfile.writedata(const b;len:integer);
  1389. var
  1390. p : pchar;
  1391. left,
  1392. idx : integer;
  1393. {$ifdef DEBUG_PPU}
  1394. start_len : integer;
  1395. {$endif}
  1396. begin
  1397. if not outputallowed then
  1398. exit;
  1399. {$ifdef DEBUG_PPU}
  1400. start_len:=len;
  1401. {$endif}
  1402. p:=pchar(@b);
  1403. idx:=0;
  1404. while len>0 do
  1405. begin
  1406. left:=bufsize-bufidx;
  1407. if len>left then
  1408. begin
  1409. move(p[idx],buf[bufidx],left);
  1410. dec(len,left);
  1411. inc(idx,left);
  1412. inc(bufidx,left);
  1413. writebuf;
  1414. end
  1415. else
  1416. begin
  1417. move(p[idx],buf[bufidx],len);
  1418. inc(bufidx,len);
  1419. {$ifdef DEBUG_PPU}
  1420. len:=0;
  1421. {$else}
  1422. exit;
  1423. {$endif}
  1424. end;
  1425. end;
  1426. {$ifdef DEBUG_PPU}
  1427. if (start_len > 0) and (ppu_log_level <= 0) then
  1428. begin
  1429. ppu_log('writedata, length='+tostr(start_len)+' level='+tostr(ppu_log_level));
  1430. for idx:=0 to start_len-1 do
  1431. ppu_log_val('p['+tostr(idx)+']=$'+hexstr(byte(p[idx]),2));
  1432. end;
  1433. {$endif DEBUG_PPU}
  1434. end;
  1435. procedure tentryfile.newentry;
  1436. begin
  1437. with entry do
  1438. begin
  1439. id:=entrytyp;
  1440. nr:=ibend;
  1441. size:=0;
  1442. end;
  1443. {Reset Entry State}
  1444. entryidx:=0;
  1445. entrybufstart:=bufstart;
  1446. entrystart:=bufstart+bufidx;
  1447. {$ifdef DEBUG_PPU}
  1448. ppu_log('entrystart');
  1449. {$endif}
  1450. {Alloc in buffer}
  1451. writedata(entry,sizeof(tentry));
  1452. end;
  1453. procedure tentryfile.writeentry(ibnr:byte);
  1454. var
  1455. opos : integer;
  1456. begin
  1457. {create entry}
  1458. entry.id:=entrytyp;
  1459. entry.nr:=ibnr;
  1460. entry.size:=entryidx;
  1461. {it's already been sent to disk ?}
  1462. if entrybufstart<>bufstart then
  1463. begin
  1464. if outputallowed then
  1465. begin
  1466. {flush to be sure}
  1467. WriteBuf;
  1468. {write entry}
  1469. opos:=f.Position;
  1470. f.Position:=entrystart;
  1471. f.write(entry,sizeof(tentry));
  1472. f.Position:=opos;
  1473. end;
  1474. entrybufstart:=bufstart;
  1475. end
  1476. else
  1477. move(entry,buf[entrystart-bufstart],sizeof(entry));
  1478. {$ifdef DEBUG_PPU}
  1479. ppu_log('writeentry, id='+entryid_name(entry.id)+' nr='+entry_name(entry.nr)+' size='+tostr(entry.size));
  1480. {$endif}
  1481. {Add New Entry, which is ibend by default}
  1482. entrystart:=bufstart+bufidx; {next entry position}
  1483. newentry;
  1484. end;
  1485. procedure tentryfile.putdata(const b;len:integer);
  1486. begin
  1487. if outputallowed then
  1488. writedata(b,len);
  1489. inc(entryidx,len);
  1490. end;
  1491. procedure tentryfile.putbyte(b:byte);
  1492. begin
  1493. {$ifdef DEBUG_PPU}
  1494. ppu_log('putbyte');
  1495. inc_log_level;
  1496. ppu_log_val(tostr(b));
  1497. {$endif}
  1498. putdata(b,1);
  1499. {$ifdef DEBUG_PPU}
  1500. dec_log_level;
  1501. {$endif}
  1502. end;
  1503. procedure tentryfile.putword(w:word);
  1504. begin
  1505. {$ifdef DEBUG_PPU}
  1506. ppu_log('putword');
  1507. inc_log_level;
  1508. ppu_log_val(tostr(w));
  1509. {$endif}
  1510. putdata(w,2);
  1511. {$ifdef DEBUG_PPU}
  1512. dec_log_level;
  1513. {$endif}
  1514. end;
  1515. procedure tentryfile.putdword(w:dword);
  1516. begin
  1517. {$ifdef DEBUG_PPU}
  1518. ppu_log('putdword');
  1519. inc_log_level;
  1520. ppu_log_val(tostr(w));
  1521. {$endif}
  1522. putdata(w,4);
  1523. {$ifdef DEBUG_PPU}
  1524. dec_log_level;
  1525. {$endif}
  1526. end;
  1527. procedure tentryfile.putlongint(l:longint);
  1528. begin
  1529. {$ifdef DEBUG_PPU}
  1530. ppu_log('putlongint');
  1531. inc_log_level;
  1532. ppu_log_val(tostr(l));
  1533. {$endif}
  1534. putdata(l,4);
  1535. {$ifdef DEBUG_PPU}
  1536. dec_log_level;
  1537. {$endif}
  1538. end;
  1539. procedure tentryfile.putint64(i:int64);
  1540. begin
  1541. {$ifdef DEBUG_PPU}
  1542. ppu_log('putint64');
  1543. inc_log_level;
  1544. ppu_log_val(tostr(i));
  1545. {$endif}
  1546. putdata(i,8);
  1547. {$ifdef DEBUG_PPU}
  1548. dec_log_level;
  1549. {$endif}
  1550. end;
  1551. procedure tentryfile.putqword(q:qword);
  1552. begin
  1553. {$ifdef DEBUG_PPU}
  1554. ppu_log('putqword');
  1555. inc_log_level;
  1556. ppu_log_val(tostr(q));
  1557. {$endif}
  1558. putdata(q,sizeof(qword));
  1559. {$ifdef DEBUG_PPU}
  1560. dec_log_level;
  1561. {$endif}
  1562. end;
  1563. procedure tentryfile.putaint(i:aint);
  1564. begin
  1565. {$ifdef DEBUG_PPU}
  1566. ppu_log('putaint');
  1567. inc_log_level;
  1568. case sizeof(aint) of
  1569. 8: ppu_log('putint64');
  1570. 4: ppu_log('putlongint');
  1571. 2: ppu_log('putword');
  1572. 1: ppu_log('putbyte');
  1573. end;
  1574. ppu_log_val(tostr(i));
  1575. {$endif}
  1576. putdata(i,sizeof(aint));
  1577. {$ifdef DEBUG_PPU}
  1578. dec_log_level;
  1579. {$endif}
  1580. end;
  1581. procedure tentryfile.putasizeint(i: asizeint);
  1582. begin
  1583. {$ifdef DEBUG_PPU}
  1584. ppu_log('putasizeint');
  1585. inc_log_level;
  1586. case sizeof(asizeint) of
  1587. 8: ppu_log('putint64');
  1588. 4: ppu_log('putlongint');
  1589. 2: ppu_log('putword');
  1590. 1: ppu_log('putbyte');
  1591. end;
  1592. ppu_log_val(tostr(i));
  1593. {$endif}
  1594. putdata(i,sizeof(asizeint));
  1595. {$ifdef DEBUG_PPU}
  1596. dec_log_level;
  1597. {$endif}
  1598. end;
  1599. procedure tentryfile.putpuint(i : puint);
  1600. begin
  1601. {$ifdef DEBUG_PPU}
  1602. ppu_log('putpuint');
  1603. inc_log_level;
  1604. ppu_log_val(tostr(i));
  1605. {$endif}
  1606. putdata(i,sizeof(puint));
  1607. {$ifdef DEBUG_PPU}
  1608. dec_log_level;
  1609. {$endif}
  1610. end;
  1611. procedure tentryfile.putptruint(v:TConstPtrUInt);
  1612. begin
  1613. {$ifdef DEBUG_PPU}
  1614. ppu_log('putptruint');
  1615. inc_log_level;
  1616. {$endif}
  1617. {$if sizeof(TConstPtrUInt)=8}
  1618. putint64(int64(v));
  1619. {$else}
  1620. putlongint(longint(v));
  1621. {$endif}
  1622. {$ifdef DEBUG_PPU}
  1623. dec_log_level;
  1624. {$endif}
  1625. end;
  1626. procedure tentryfile.putaword(i:aword);
  1627. begin
  1628. {$ifdef DEBUG_PPU}
  1629. ppu_log('putaword');
  1630. inc_log_level;
  1631. ppu_log_val(tostr(i));
  1632. {$endif}
  1633. putdata(i,sizeof(aword));
  1634. {$ifdef DEBUG_PPU}
  1635. dec_log_level;
  1636. {$endif}
  1637. end;
  1638. procedure tentryfile.putreal(d:entryreal);
  1639. var
  1640. hd : double;
  1641. begin
  1642. if target_info.system=system_x86_64_win64 then
  1643. begin
  1644. {$ifdef DEBUG_PPU}
  1645. ppu_log('putreal,size='+tostr(sizeof(hd)));
  1646. inc_log_level;
  1647. ppu_log_val(realtostr(d));
  1648. {$endif}
  1649. hd:=d;
  1650. putdata(hd,sizeof(hd));
  1651. end
  1652. else
  1653. begin
  1654. {$ifdef DEBUG_PPU}
  1655. ppu_log('putreal,size='+tostr(sizeof(d)));
  1656. inc_log_level;
  1657. ppu_log_val(realtostr(d));
  1658. {$endif}
  1659. putdata(d,sizeof(entryreal));
  1660. end;
  1661. {$ifdef DEBUG_PPU}
  1662. dec_log_level;
  1663. {$endif}
  1664. end;
  1665. procedure tentryfile.putboolean(b:boolean);
  1666. begin
  1667. {$ifdef DEBUG_PPU}
  1668. ppu_log('putboolean');
  1669. inc_log_level;
  1670. {$endif}
  1671. putbyte(byte(b));
  1672. {$ifdef DEBUG_PPU}
  1673. dec_log_level;
  1674. {$endif}
  1675. end;
  1676. procedure tentryfile.putstring(const s:string);
  1677. begin
  1678. {$ifdef DEBUG_PPU}
  1679. { The reading method uses getbyte, so fake it here }
  1680. ppu_log('putbyte');
  1681. inc_log_level;
  1682. inc(bufidx);
  1683. ppu_log('putstring,size='+tostr(length(s)+1));
  1684. dec(bufidx);
  1685. ppu_log_val(s);
  1686. {$endif}
  1687. putdata(s,length(s)+1);
  1688. {$ifdef DEBUG_PPU}
  1689. dec_log_level;
  1690. {$endif}
  1691. end;
  1692. procedure tentryfile.putansistring(const s:ansistring);
  1693. var
  1694. len: longint;
  1695. begin
  1696. len:=length(s);
  1697. {$ifdef DEBUG_PPU}
  1698. ppu_log('putansistring');
  1699. inc_log_level;
  1700. ppu_log_val(s);
  1701. {$endif}
  1702. putlongint(len);
  1703. if len>0 then
  1704. putdata(s[1],len);
  1705. {$ifdef DEBUG_PPU}
  1706. dec_log_level;
  1707. {$endif}
  1708. end;
  1709. procedure tentryfile.putset(const arr: array of byte);
  1710. {$ifdef DEBUG_PPU}
  1711. var
  1712. i : byte;
  1713. {$endif}
  1714. begin
  1715. {$ifdef DEBUG_PPU}
  1716. ppu_log('putset');
  1717. inc_log_level;
  1718. {$endif}
  1719. putdata(arr,sizeof(arr));
  1720. {$ifdef DEBUG_PPU}
  1721. for i:=0 to sizeof(arr)-1 do
  1722. ppu_log_val('byte['+tostr(i)+']=$'+hexstr(arr[i],2));
  1723. dec_log_level;
  1724. {$endif}
  1725. end;
  1726. procedure tentryfile.tempclose;
  1727. begin
  1728. if not closed then
  1729. begin
  1730. closepos:=f.Position;
  1731. f.Free;
  1732. f:=nil;
  1733. closed:=true;
  1734. tempclosed:=true;
  1735. end;
  1736. end;
  1737. function tentryfile.tempopen:boolean;
  1738. begin
  1739. tempopen:=false;
  1740. if not closed or not tempclosed then
  1741. exit;
  1742. { MG: not sure, if this is correct
  1743. f.position:=0;
  1744. No, f was freed in tempclose above, we need to
  1745. recreate it. PM 2011/06/06 }
  1746. try
  1747. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1748. except
  1749. exit;
  1750. end;
  1751. closed:=false;
  1752. tempclosed:=false;
  1753. { restore state }
  1754. f.Position:=closepos;
  1755. tempopen:=true;
  1756. end;
  1757. end.