2
0

entfile.pas 42 KB

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