entfile.pas 46 KB

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