entfile.pas 42 KB

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