entfile.pas 42 KB

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