ppu.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines to read/write ppu files
  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 ppu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. systems,globtype,constexp,cstreams;
  22. { Also write the ppu if only crc if done, this can be used with ppudump to
  23. see the differences between the intf and implementation }
  24. { define INTFPPU}
  25. {$ifdef Test_Double_checksum}
  26. var
  27. CRCFile : text;
  28. const
  29. CRC_array_Size = 200000;
  30. type
  31. tcrc_array = array[0..crc_array_size] of dword;
  32. pcrc_array = ^tcrc_array;
  33. {$endif Test_Double_checksum}
  34. const
  35. CurrentPPUVersion = 162;
  36. { buffer sizes }
  37. maxentrysize = 1024;
  38. ppubufsize = 16384;
  39. {ppu entries}
  40. mainentryid = 1;
  41. subentryid = 2;
  42. {special}
  43. iberror = 0;
  44. ibstartdefs = 248;
  45. ibenddefs = 249;
  46. ibstartsyms = 250;
  47. ibendsyms = 251;
  48. ibendinterface = 252;
  49. ibendimplementation = 253;
  50. // ibendbrowser = 254;
  51. ibend = 255;
  52. {general}
  53. ibmodulename = 1;
  54. ibsourcefiles = 2;
  55. ibloadunit = 3;
  56. ibinitunit = 4;
  57. iblinkunitofiles = 5;
  58. iblinkunitstaticlibs = 6;
  59. iblinkunitsharedlibs = 7;
  60. iblinkotherofiles = 8;
  61. iblinkotherstaticlibs = 9;
  62. iblinkothersharedlibs = 10;
  63. ibImportSymbols = 11;
  64. ibsymref = 12;
  65. ibdefref = 13;
  66. // ibendsymtablebrowser = 14;
  67. // ibbeginsymtablebrowser = 15;
  68. {$IFDEF MACRO_DIFF_HINT}
  69. ibusedmacros = 16;
  70. {$ENDIF}
  71. ibderefdata = 17;
  72. ibexportedmacros = 18;
  73. ibderefmap = 19;
  74. {syms}
  75. ibtypesym = 20;
  76. ibprocsym = 21;
  77. ibstaticvarsym = 22;
  78. ibconstsym = 23;
  79. ibenumsym = 24;
  80. // ibtypedconstsym = 25;
  81. ibabsolutevarsym = 26;
  82. ibpropertysym = 27;
  83. ibfieldvarsym = 28;
  84. ibunitsym = 29;
  85. iblabelsym = 30;
  86. ibsyssym = 31;
  87. ibnamespacesym = 32;
  88. iblocalvarsym = 33;
  89. ibparavarsym = 34;
  90. ibmacrosym = 35;
  91. {definitions}
  92. iborddef = 40;
  93. ibpointerdef = 41;
  94. ibarraydef = 42;
  95. ibprocdef = 43;
  96. ibshortstringdef = 44;
  97. ibrecorddef = 45;
  98. ibfiledef = 46;
  99. ibformaldef = 47;
  100. ibobjectdef = 48;
  101. ibenumdef = 49;
  102. ibsetdef = 50;
  103. ibprocvardef = 51;
  104. ibfloatdef = 52;
  105. ibclassrefdef = 53;
  106. iblongstringdef = 54;
  107. ibansistringdef = 55;
  108. ibwidestringdef = 56;
  109. ibvariantdef = 57;
  110. ibundefineddef = 58;
  111. ibunicodestringdef = 59;
  112. {implementation/ObjData}
  113. ibnodetree = 80;
  114. ibasmsymbols = 81;
  115. ibresources = 82;
  116. ibcreatedobjtypes = 83;
  117. ibwpofile = 84;
  118. ibmoduleoptions = 85;
  119. ibmainname = 90;
  120. ibsymtableoptions = 91;
  121. ibrecsymtableoptions = 91;
  122. { target-specific things }
  123. iblinkotherframeworks = 100;
  124. ibjvmnamespace = 101;
  125. { unit flags }
  126. uf_init = $000001; { unit has initialization section }
  127. uf_finalize = $000002; { unit has finalization section }
  128. uf_big_endian = $000004;
  129. //uf_has_browser = $000010;
  130. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  131. uf_smart_linked = $000040; { the ppu can be smartlinked }
  132. uf_static_linked = $000080; { the ppu can be linked static }
  133. uf_shared_linked = $000100; { the ppu can be linked shared }
  134. //uf_local_browser = $000200;
  135. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  136. uf_has_resourcestrings = $000800; { unit has resource string section }
  137. uf_little_endian = $001000;
  138. uf_release = $002000; { unit was compiled with -Ur option }
  139. uf_threadvars = $004000; { unit has threadvars }
  140. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  141. uf_has_stabs_debuginfo = $010000; { this unit has stabs debuginfo generated }
  142. uf_local_symtable = $020000; { this unit has a local symtable stored }
  143. uf_uses_variants = $040000; { this unit uses variants }
  144. uf_has_resourcefiles = $080000; { this unit has external resources (using $R directive)}
  145. uf_has_exports = $100000; { this module or a used unit has exports }
  146. uf_has_dwarf_debuginfo = $200000; { this unit has dwarf debuginfo generated }
  147. uf_wideinits = $400000; { this unit has winlike widestring typed constants }
  148. uf_classinits = $800000; { this unit has class constructors/destructors }
  149. uf_resstrinits = $1000000; { this unit has string consts referencing resourcestrings }
  150. {$ifdef generic_cpu}
  151. { We need to use the correct size of aint and pint for
  152. the target CPU }
  153. const
  154. CpuAddrBitSize : 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 } 16 {'avr'},
  169. { 13 } 32 {'mipsel'},
  170. { 14 } 32 {'jvm'},
  171. { 15 } 16 {'i8086'}
  172. );
  173. CpuAluBitSize : array[tsystemcpu] of longint =
  174. (
  175. { 0 } 32 {'none'},
  176. { 1 } 32 {'i386'},
  177. { 2 } 32 {'m68k'},
  178. { 3 } 32 {'alpha'},
  179. { 4 } 32 {'powerpc'},
  180. { 5 } 32 {'sparc'},
  181. { 6 } 32 {'vis'},
  182. { 7 } 64 {'ia64'},
  183. { 8 } 64 {'x86_64'},
  184. { 9 } 32 {'mipseb'},
  185. { 10 } 32 {'arm'},
  186. { 11 } 64 {'powerpc64'},
  187. { 12 } 8 {'avr'},
  188. { 13 } 32 {'mipsel'},
  189. { 14 } 64 {'jvm'},
  190. { 15 } 16 {'i8086'}
  191. );
  192. {$endif generic_cpu}
  193. type
  194. { bestreal is defined based on the target architecture }
  195. ppureal=bestreal;
  196. tppuerror=(ppuentrytoobig,ppuentryerror);
  197. tppuheader=record
  198. id : array[1..3] of char; { = 'PPU' }
  199. ver : array[1..3] of char;
  200. compiler : word;
  201. cpu : word;
  202. target : word;
  203. flags : longint;
  204. size : longint; { size of the ppufile without header }
  205. checksum : cardinal; { checksum for this ppufile }
  206. interface_checksum : cardinal;
  207. deflistsize,
  208. symlistsize : longint;
  209. indirect_checksum: cardinal;
  210. end;
  211. tppuentry=packed record
  212. size : longint;
  213. id : byte;
  214. nr : byte;
  215. end;
  216. { tppufile }
  217. tppufile=class
  218. private
  219. f : TCCustomFileStream;
  220. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  221. fname : string;
  222. fsize : integer;
  223. {$ifdef Test_Double_checksum}
  224. public
  225. crcindex,
  226. crc_index,
  227. crcindex2,
  228. crc_index2 : cardinal;
  229. crc_test,
  230. crc_test2 : pcrc_array;
  231. private
  232. {$endif def Test_Double_checksum}
  233. buf : pchar;
  234. bufstart,
  235. bufsize,
  236. bufidx : integer;
  237. entrybufstart,
  238. entrystart,
  239. entryidx : integer;
  240. entry : tppuentry;
  241. closed,
  242. tempclosed : boolean;
  243. closepos : integer;
  244. public
  245. entrytyp : byte;
  246. header : tppuheader;
  247. size : integer;
  248. change_endian : boolean; { Used in ppudump util }
  249. { crc for the entire unit }
  250. crc,
  251. { crc for the interface definitions in this unit }
  252. interface_crc,
  253. { crc of all object/class definitions in the interface of this unit, xor'ed
  254. by the crc's of all object/class definitions in the interfaces of units
  255. used by this unit. Reason: see mantis #13840 }
  256. indirect_crc : cardinal;
  257. error,
  258. {$ifdef generic_cpu}
  259. has_more,
  260. {$endif not generic_cpu}
  261. do_crc,
  262. do_interface_crc,
  263. do_indirect_crc : boolean;
  264. crc_only : boolean; { used to calculate interface_crc before implementation }
  265. constructor Create(const fn:string);
  266. destructor Destroy;override;
  267. procedure flush;
  268. procedure closefile;
  269. function CheckPPUId:boolean;
  270. function GetPPUVersion:integer;
  271. procedure NewHeader;
  272. procedure NewEntry;
  273. {read}
  274. function openfile:boolean;
  275. procedure reloadbuf;
  276. procedure readdata(out b;len:integer);
  277. procedure skipdata(len:integer);
  278. function readentry:byte;
  279. function EndOfEntry:boolean;
  280. function entrysize:longint;
  281. function entryleft:longint;
  282. procedure getdatabuf(out b;len:integer;out res:integer);
  283. procedure getdata(out b;len:integer);
  284. function getbyte:byte;
  285. function getword:word;
  286. function getdword:dword;
  287. function getlongint:longint;
  288. function getint64:int64;
  289. function getqword:qword;
  290. function getaint:aint;
  291. function getasizeint:asizeint;
  292. function getaword:aword;
  293. function getreal:ppureal;
  294. function getrealsize(sizeofreal : longint):ppureal;
  295. function getstring:string;
  296. function getansistring:ansistring;
  297. procedure getnormalset(out b);
  298. procedure getsmallset(out b);
  299. function skipuntilentry(untilb:byte):boolean;
  300. {write}
  301. function createfile:boolean;
  302. procedure writeheader;
  303. procedure writebuf;
  304. procedure writedata(const b;len:integer);
  305. procedure writeentry(ibnr:byte);
  306. procedure putdata(const b;len:integer);
  307. procedure putbyte(b:byte);
  308. procedure putword(w:word);
  309. procedure putdword(w:dword);
  310. procedure putlongint(l:longint);
  311. procedure putint64(i:int64);
  312. procedure putqword(q:qword);
  313. procedure putaint(i:aint);
  314. procedure putasizeint(i:asizeint);
  315. procedure putaword(i:aword);
  316. procedure putreal(d:ppureal);
  317. procedure putstring(const s:string);
  318. procedure putansistring(const s:ansistring);
  319. procedure putnormalset(const b);
  320. procedure putsmallset(const b);
  321. procedure tempclose; // MG: not used, obsolete?
  322. function tempopen:boolean; // MG: not used, obsolete?
  323. end;
  324. implementation
  325. uses
  326. {$ifdef Test_Double_checksum}
  327. comphook,
  328. {$endif def Test_Double_checksum}
  329. fpccrc,
  330. cutils;
  331. function swapendian_ppureal(d:ppureal):ppureal;
  332. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  333. var i:0..sizeof(d)-1;
  334. begin
  335. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  336. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  337. end;
  338. {*****************************************************************************
  339. TPPUFile
  340. *****************************************************************************}
  341. constructor tppufile.Create(const fn:string);
  342. begin
  343. fname:=fn;
  344. change_endian:=false;
  345. crc_only:=false;
  346. Mode:=0;
  347. NewHeader;
  348. Error:=false;
  349. closed:=true;
  350. tempclosed:=false;
  351. getmem(buf,ppubufsize);
  352. end;
  353. destructor tppufile.destroy;
  354. begin
  355. closefile;
  356. if assigned(buf) then
  357. freemem(buf,ppubufsize);
  358. end;
  359. procedure tppufile.flush;
  360. begin
  361. if Mode=2 then
  362. writebuf;
  363. end;
  364. procedure tppufile.closefile;
  365. begin
  366. {$ifdef Test_Double_checksum}
  367. if mode=2 then
  368. begin
  369. if assigned(crc_test) then
  370. dispose(crc_test);
  371. if assigned(crc_test2) then
  372. dispose(crc_test2);
  373. end;
  374. {$endif Test_Double_checksum}
  375. if Mode<>0 then
  376. begin
  377. Flush;
  378. f.Free;
  379. Mode:=0;
  380. closed:=true;
  381. end;
  382. end;
  383. function tppufile.CheckPPUId:boolean;
  384. begin
  385. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  386. end;
  387. function tppufile.GetPPUVersion:integer;
  388. var
  389. l : integer;
  390. code : integer;
  391. begin
  392. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  393. if code=0 then
  394. GetPPUVersion:=l
  395. else
  396. GetPPUVersion:=0;
  397. end;
  398. procedure tppufile.NewHeader;
  399. var
  400. s : string;
  401. begin
  402. fillchar(header,sizeof(tppuheader),0);
  403. str(currentppuversion,s);
  404. while length(s)<3 do
  405. s:='0'+s;
  406. with header do
  407. begin
  408. Id[1]:='P';
  409. Id[2]:='P';
  410. Id[3]:='U';
  411. Ver[1]:=s[1];
  412. Ver[2]:=s[2];
  413. Ver[3]:=s[3];
  414. end;
  415. end;
  416. {*****************************************************************************
  417. TPPUFile Reading
  418. *****************************************************************************}
  419. function tppufile.openfile:boolean;
  420. var
  421. i : integer;
  422. begin
  423. openfile:=false;
  424. try
  425. f:=CFileStreamClass.Create(fname,fmOpenRead)
  426. except
  427. exit;
  428. end;
  429. closed:=false;
  430. {read ppuheader}
  431. fsize:=f.Size;
  432. if fsize<sizeof(tppuheader) then
  433. exit;
  434. i:=f.Read(header,sizeof(tppuheader));
  435. { The header is always stored in little endian order }
  436. { therefore swap if on a big endian machine }
  437. {$IFDEF ENDIAN_BIG}
  438. header.compiler := swapendian(header.compiler);
  439. header.cpu := swapendian(header.cpu);
  440. header.target := swapendian(header.target);
  441. header.flags := swapendian(header.flags);
  442. header.size := swapendian(header.size);
  443. header.checksum := swapendian(header.checksum);
  444. header.interface_checksum := swapendian(header.interface_checksum);
  445. header.indirect_checksum := swapendian(header.indirect_checksum);
  446. header.deflistsize:=swapendian(header.deflistsize);
  447. header.symlistsize:=swapendian(header.symlistsize);
  448. {$ENDIF}
  449. { the PPU DATA is stored in native order }
  450. if (header.flags and uf_big_endian) = uf_big_endian then
  451. Begin
  452. {$IFDEF ENDIAN_LITTLE}
  453. change_endian := TRUE;
  454. {$ELSE}
  455. change_endian := FALSE;
  456. {$ENDIF}
  457. End
  458. else if (header.flags and uf_little_endian) = uf_little_endian then
  459. Begin
  460. {$IFDEF ENDIAN_BIG}
  461. change_endian := TRUE;
  462. {$ELSE}
  463. change_endian := FALSE;
  464. {$ENDIF}
  465. End;
  466. {reset buffer}
  467. bufstart:=i;
  468. bufsize:=0;
  469. bufidx:=0;
  470. Mode:=1;
  471. FillChar(entry,sizeof(tppuentry),0);
  472. entryidx:=0;
  473. entrystart:=0;
  474. entrybufstart:=0;
  475. Error:=false;
  476. openfile:=true;
  477. end;
  478. procedure tppufile.reloadbuf;
  479. begin
  480. inc(bufstart,bufsize);
  481. bufsize:=f.Read(buf^,ppubufsize);
  482. bufidx:=0;
  483. end;
  484. procedure tppufile.readdata(out b;len:integer);
  485. var
  486. p,pbuf : pchar;
  487. left : integer;
  488. begin
  489. p:=pchar(@b);
  490. pbuf:=@buf[bufidx];
  491. repeat
  492. left:=bufsize-bufidx;
  493. if len<left then
  494. break;
  495. move(pbuf^,p^,left);
  496. dec(len,left);
  497. inc(p,left);
  498. reloadbuf;
  499. pbuf:=@buf[bufidx];
  500. if bufsize=0 then
  501. exit;
  502. until false;
  503. move(pbuf^,p^,len);
  504. inc(bufidx,len);
  505. end;
  506. procedure tppufile.skipdata(len:integer);
  507. var
  508. left : integer;
  509. begin
  510. while len>0 do
  511. begin
  512. left:=bufsize-bufidx;
  513. if len>left then
  514. begin
  515. dec(len,left);
  516. reloadbuf;
  517. if bufsize=0 then
  518. exit;
  519. end
  520. else
  521. begin
  522. inc(bufidx,len);
  523. exit;
  524. end;
  525. end;
  526. end;
  527. function tppufile.readentry:byte;
  528. begin
  529. if entryidx<entry.size then
  530. begin
  531. {$ifdef generic_cpu}
  532. has_more:=true;
  533. {$endif not generic_cpu}
  534. skipdata(entry.size-entryidx);
  535. end;
  536. readdata(entry,sizeof(tppuentry));
  537. if change_endian then
  538. entry.size:=swapendian(entry.size);
  539. entrystart:=bufstart+bufidx;
  540. entryidx:=0;
  541. {$ifdef generic_cpu}
  542. has_more:=false;
  543. {$endif not generic_cpu}
  544. if not(entry.id in [mainentryid,subentryid]) then
  545. begin
  546. readentry:=iberror;
  547. error:=true;
  548. exit;
  549. end;
  550. readentry:=entry.nr;
  551. end;
  552. function tppufile.endofentry:boolean;
  553. begin
  554. {$ifdef generic_cpu}
  555. endofentry:=(entryidx=entry.size);
  556. {$else not generic_cpu}
  557. endofentry:=(entryidx>=entry.size);
  558. {$endif not generic_cpu}
  559. end;
  560. function tppufile.entrysize:longint;
  561. begin
  562. entrysize:=entry.size;
  563. end;
  564. function tppufile.entryleft:longint;
  565. begin
  566. entryleft:=entry.size-entryidx;
  567. end;
  568. procedure tppufile.getdatabuf(out b;len:integer;out res:integer);
  569. begin
  570. if entryidx+len>entry.size then
  571. res:=entry.size-entryidx
  572. else
  573. res:=len;
  574. readdata(b,res);
  575. inc(entryidx,res);
  576. end;
  577. procedure tppufile.getdata(out b;len:integer);
  578. begin
  579. if entryidx+len>entry.size then
  580. begin
  581. error:=true;
  582. exit;
  583. end;
  584. readdata(b,len);
  585. inc(entryidx,len);
  586. end;
  587. function tppufile.getbyte:byte;
  588. begin
  589. if entryidx+1>entry.size then
  590. begin
  591. error:=true;
  592. result:=0;
  593. exit;
  594. end;
  595. if bufsize-bufidx>=1 then
  596. begin
  597. result:=pbyte(@buf[bufidx])^;
  598. inc(bufidx);
  599. end
  600. else
  601. readdata(result,1);
  602. inc(entryidx);
  603. end;
  604. function tppufile.getword:word;
  605. begin
  606. if entryidx+2>entry.size then
  607. begin
  608. error:=true;
  609. result:=0;
  610. exit;
  611. end;
  612. if bufsize-bufidx>=sizeof(word) then
  613. begin
  614. result:=Unaligned(pword(@buf[bufidx])^);
  615. inc(bufidx,sizeof(word));
  616. end
  617. else
  618. readdata(result,sizeof(word));
  619. if change_endian then
  620. result:=swapendian(result);
  621. inc(entryidx,2);
  622. end;
  623. function tppufile.getlongint:longint;
  624. begin
  625. if entryidx+4>entry.size then
  626. begin
  627. error:=true;
  628. result:=0;
  629. exit;
  630. end;
  631. if bufsize-bufidx>=sizeof(longint) then
  632. begin
  633. result:=Unaligned(plongint(@buf[bufidx])^);
  634. inc(bufidx,sizeof(longint));
  635. end
  636. else
  637. readdata(result,sizeof(longint));
  638. if change_endian then
  639. result:=swapendian(result);
  640. inc(entryidx,4);
  641. end;
  642. function tppufile.getdword:dword;
  643. begin
  644. if entryidx+4>entry.size then
  645. begin
  646. error:=true;
  647. result:=0;
  648. exit;
  649. end;
  650. if bufsize-bufidx>=sizeof(dword) then
  651. begin
  652. result:=Unaligned(plongint(@buf[bufidx])^);
  653. inc(bufidx,sizeof(longint));
  654. end
  655. else
  656. readdata(result,sizeof(dword));
  657. if change_endian then
  658. result:=swapendian(result);
  659. inc(entryidx,4);
  660. end;
  661. function tppufile.getint64:int64;
  662. begin
  663. if entryidx+8>entry.size then
  664. begin
  665. error:=true;
  666. result:=0;
  667. exit;
  668. end;
  669. if bufsize-bufidx>=sizeof(int64) then
  670. begin
  671. result:=Unaligned(pint64(@buf[bufidx])^);
  672. inc(bufidx,sizeof(int64));
  673. end
  674. else
  675. readdata(result,sizeof(int64));
  676. if change_endian then
  677. result:=swapendian(result);
  678. inc(entryidx,8);
  679. end;
  680. function tppufile.getqword:qword;
  681. begin
  682. if entryidx+8>entry.size then
  683. begin
  684. error:=true;
  685. result:=0;
  686. exit;
  687. end;
  688. if bufsize-bufidx>=sizeof(qword) then
  689. begin
  690. result:=Unaligned(pqword(@buf[bufidx])^);
  691. inc(bufidx,sizeof(qword));
  692. end
  693. else
  694. readdata(result,sizeof(qword));
  695. if change_endian then
  696. result:=swapendian(result);
  697. inc(entryidx,8);
  698. end;
  699. function tppufile.getaint:aint;
  700. begin
  701. {$ifdef generic_cpu}
  702. if CpuAluBitSize[tsystemcpu(header.cpu)]=64 then
  703. result:=getint64
  704. else if CpuAluBitSize[tsystemcpu(header.cpu)]=32 then
  705. result:=getlongint
  706. else if CpuAluBitSize[tsystemcpu(header.cpu)]=16 then
  707. result:=smallint(getword)
  708. else if CpuAluBitSize[tsystemcpu(header.cpu)]=8 then
  709. result:=shortint(getbyte)
  710. else
  711. begin
  712. error:=true;
  713. result:=0;
  714. end;
  715. {$else not generic_cpu}
  716. case sizeof(aint) of
  717. 8: result:=getint64;
  718. 4: result:=getlongint;
  719. 2: result:=smallint(getword);
  720. 1: result:=shortint(getbyte);
  721. end;
  722. {$endif not generic_cpu}
  723. end;
  724. function tppufile.getasizeint:asizeint;
  725. begin
  726. {$ifdef generic_cpu}
  727. if CpuAddrBitSize[tsystemcpu(header.cpu)]=64 then
  728. result:=getint64
  729. else if CpuAddrBitSize[tsystemcpu(header.cpu)]=32 then
  730. result:=getlongint
  731. else if CpuAddrBitSize[tsystemcpu(header.cpu)]=16 then
  732. result:=smallint(getword)
  733. else
  734. begin
  735. error:=true;
  736. result:=0;
  737. end;
  738. {$else not generic_cpu}
  739. {$if defined(cpu64bitaddr)}
  740. result:=getint64;
  741. {$elseif defined(cpu32bitaddr)}
  742. result:=getlongint;
  743. {$elseif defined(cpu16bitaddr)}
  744. result:=getword;
  745. {$endif}
  746. {$endif not generic_cpu}
  747. end;
  748. function tppufile.getaword:aword;
  749. begin
  750. {$ifdef generic_cpu}
  751. if CpuAluBitSize[tsystemcpu(header.cpu)]=64 then
  752. result:=getqword
  753. else if CpuAluBitSize[tsystemcpu(header.cpu)]=32 then
  754. result:=getdword
  755. else if CpuAluBitSize[tsystemcpu(header.cpu)]=16 then
  756. result:=getword
  757. else if CpuAluBitSize[tsystemcpu(header.cpu)]=8 then
  758. result:=getbyte
  759. else
  760. begin
  761. error:=true;
  762. result:=0;
  763. end;
  764. {$else not generic_cpu}
  765. {$ifdef cpu64bitalu}
  766. result:=getqword;
  767. {$else cpu64bitalu}
  768. result:=getdword;
  769. {$endif cpu64bitalu}
  770. {$endif not generic_cpu}
  771. end;
  772. function tppufile.getrealsize(sizeofreal : longint):ppureal;
  773. var
  774. e : ppureal;
  775. d : double;
  776. s : single;
  777. begin
  778. if sizeofreal=sizeof(e) then
  779. begin
  780. if entryidx+sizeof(e)>entry.size then
  781. begin
  782. error:=true;
  783. result:=0;
  784. exit;
  785. end;
  786. readdata(e,sizeof(e));
  787. if change_endian then
  788. result:=swapendian_ppureal(e)
  789. else
  790. result:=e;
  791. inc(entryidx,sizeof(e));
  792. exit;
  793. end;
  794. if sizeofreal=sizeof(d) then
  795. begin
  796. if entryidx+sizeof(d)>entry.size then
  797. begin
  798. error:=true;
  799. result:=0;
  800. exit;
  801. end;
  802. readdata(d,sizeof(d));
  803. if change_endian then
  804. result:=swapendian(pqword(@d)^)
  805. else
  806. result:=d;
  807. inc(entryidx,sizeof(d));
  808. result:=d;
  809. exit;
  810. end;
  811. if sizeofreal=sizeof(s) then
  812. begin
  813. if entryidx+sizeof(s)>entry.size then
  814. begin
  815. error:=true;
  816. result:=0;
  817. exit;
  818. end;
  819. readdata(s,sizeof(s));
  820. if change_endian then
  821. result:=swapendian(pdword(@s)^)
  822. else
  823. result:=s;
  824. inc(entryidx,sizeof(s));
  825. result:=s;
  826. exit;
  827. end;
  828. error:=true;
  829. result:=0.0;
  830. end;
  831. function tppufile.getreal:ppureal;
  832. var
  833. d : ppureal;
  834. hd : double;
  835. begin
  836. if target_info.system=system_x86_64_win64 then
  837. begin
  838. hd:=getrealsize(sizeof(hd));
  839. getreal:=hd;
  840. end
  841. else
  842. begin
  843. d:=getrealsize(sizeof(d));
  844. getreal:=d;
  845. end;
  846. end;
  847. function tppufile.getstring:string;
  848. begin
  849. result[0]:=chr(getbyte);
  850. if entryidx+length(result)>entry.size then
  851. begin
  852. error:=true;
  853. exit;
  854. end;
  855. ReadData(result[1],length(result));
  856. inc(entryidx,length(result));
  857. end;
  858. function tppufile.getansistring:ansistring;
  859. var
  860. len: longint;
  861. begin
  862. len:=getlongint;
  863. if entryidx+len>entry.size then
  864. begin
  865. error:=true;
  866. exit;
  867. end;
  868. setlength(result,len);
  869. if len>0 then
  870. getdata(result[1],len);
  871. end;
  872. procedure tppufile.getsmallset(out b);
  873. var
  874. i : longint;
  875. begin
  876. getdata(b,4);
  877. if change_endian then
  878. for i:=0 to 3 do
  879. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  880. end;
  881. procedure tppufile.getnormalset(out b);
  882. var
  883. i : longint;
  884. begin
  885. getdata(b,32);
  886. if change_endian then
  887. for i:=0 to 31 do
  888. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  889. end;
  890. function tppufile.skipuntilentry(untilb:byte):boolean;
  891. var
  892. b : byte;
  893. begin
  894. repeat
  895. b:=readentry;
  896. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  897. skipuntilentry:=(b=untilb);
  898. end;
  899. {*****************************************************************************
  900. TPPUFile Writing
  901. *****************************************************************************}
  902. function tppufile.createfile:boolean;
  903. var
  904. ok: boolean;
  905. begin
  906. createfile:=false;
  907. {$ifdef INTFPPU}
  908. if crc_only then
  909. begin
  910. fname:=fname+'.intf';
  911. crc_only:=false;
  912. end;
  913. {$endif}
  914. if not crc_only then
  915. begin
  916. {$ifdef MACOS}
  917. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  918. SetDefaultMacOSCreator('FPas');
  919. SetDefaultMacOSFiletype('FPPU');
  920. {$endif}
  921. ok:=false;
  922. try
  923. f:=CFileStreamClass.Create(fname,fmCreate);
  924. ok:=true;
  925. except
  926. end;
  927. {$ifdef MACOS}
  928. SetDefaultMacOSCreator('MPS ');
  929. SetDefaultMacOSFiletype('TEXT');
  930. {$endif}
  931. if not ok then
  932. exit;
  933. Mode:=2;
  934. {write header for sure}
  935. f.Write(header,sizeof(tppuheader));
  936. end;
  937. bufsize:=ppubufsize;
  938. bufstart:=sizeof(tppuheader);
  939. bufidx:=0;
  940. {reset}
  941. crc:=0;
  942. interface_crc:=0;
  943. indirect_crc:=0;
  944. do_interface_crc:=true;
  945. do_indirect_crc:=false;
  946. Error:=false;
  947. do_crc:=true;
  948. size:=0;
  949. entrytyp:=mainentryid;
  950. {start}
  951. NewEntry;
  952. createfile:=true;
  953. end;
  954. procedure tppufile.writeheader;
  955. var
  956. opos : integer;
  957. begin
  958. if crc_only then
  959. exit;
  960. { flush buffer }
  961. writebuf;
  962. { update size (w/o header!) in the header }
  963. header.size:=bufstart-sizeof(tppuheader);
  964. { set the endian flag }
  965. {$ifndef FPC_BIG_ENDIAN}
  966. header.flags := header.flags or uf_little_endian;
  967. {$else not FPC_BIG_ENDIAN}
  968. header.flags := header.flags or uf_big_endian;
  969. { Now swap the header in the correct endian (always little endian) }
  970. header.compiler := swapendian(header.compiler);
  971. header.cpu := swapendian(header.cpu);
  972. header.target := swapendian(header.target);
  973. header.flags := swapendian(header.flags);
  974. header.size := swapendian(header.size);
  975. header.checksum := swapendian(header.checksum);
  976. header.interface_checksum := swapendian(header.interface_checksum);
  977. header.indirect_checksum := swapendian(header.indirect_checksum);
  978. header.deflistsize:=swapendian(header.deflistsize);
  979. header.symlistsize:=swapendian(header.symlistsize);
  980. {$endif not FPC_BIG_ENDIAN}
  981. { write header and restore filepos after it }
  982. opos:=f.Position;
  983. f.Position:=0;
  984. f.Write(header,sizeof(tppuheader));
  985. f.Position:=opos;
  986. end;
  987. procedure tppufile.writebuf;
  988. begin
  989. if not crc_only and
  990. (bufidx <> 0) then
  991. f.Write(buf^,bufidx);
  992. inc(bufstart,bufidx);
  993. bufidx:=0;
  994. end;
  995. procedure tppufile.writedata(const b;len:integer);
  996. var
  997. p : pchar;
  998. left,
  999. idx : integer;
  1000. begin
  1001. if crc_only then
  1002. exit;
  1003. p:=pchar(@b);
  1004. idx:=0;
  1005. while len>0 do
  1006. begin
  1007. left:=bufsize-bufidx;
  1008. if len>left then
  1009. begin
  1010. move(p[idx],buf[bufidx],left);
  1011. dec(len,left);
  1012. inc(idx,left);
  1013. inc(bufidx,left);
  1014. writebuf;
  1015. end
  1016. else
  1017. begin
  1018. move(p[idx],buf[bufidx],len);
  1019. inc(bufidx,len);
  1020. exit;
  1021. end;
  1022. end;
  1023. end;
  1024. procedure tppufile.NewEntry;
  1025. begin
  1026. with entry do
  1027. begin
  1028. id:=entrytyp;
  1029. nr:=ibend;
  1030. size:=0;
  1031. end;
  1032. {Reset Entry State}
  1033. entryidx:=0;
  1034. entrybufstart:=bufstart;
  1035. entrystart:=bufstart+bufidx;
  1036. {Alloc in buffer}
  1037. writedata(entry,sizeof(tppuentry));
  1038. end;
  1039. procedure tppufile.writeentry(ibnr:byte);
  1040. var
  1041. opos : integer;
  1042. begin
  1043. {create entry}
  1044. entry.id:=entrytyp;
  1045. entry.nr:=ibnr;
  1046. entry.size:=entryidx;
  1047. {it's already been sent to disk ?}
  1048. if entrybufstart<>bufstart then
  1049. begin
  1050. if not crc_only then
  1051. begin
  1052. {flush to be sure}
  1053. WriteBuf;
  1054. {write entry}
  1055. opos:=f.Position;
  1056. f.Position:=entrystart;
  1057. f.write(entry,sizeof(tppuentry));
  1058. f.Position:=opos;
  1059. end;
  1060. entrybufstart:=bufstart;
  1061. end
  1062. else
  1063. move(entry,buf[entrystart-bufstart],sizeof(entry));
  1064. {Add New Entry, which is ibend by default}
  1065. entrystart:=bufstart+bufidx; {next entry position}
  1066. NewEntry;
  1067. end;
  1068. procedure tppufile.putdata(const b;len:integer);
  1069. begin
  1070. if do_crc then
  1071. begin
  1072. crc:=UpdateCrc32(crc,b,len);
  1073. {$ifdef Test_Double_checksum}
  1074. if crc_only then
  1075. begin
  1076. crc_test2^[crc_index2]:=crc;
  1077. {$ifdef Test_Double_checksum_write}
  1078. Writeln(CRCFile,crc);
  1079. {$endif Test_Double_checksum_write}
  1080. if crc_index2<crc_array_size then
  1081. inc(crc_index2);
  1082. end
  1083. else
  1084. begin
  1085. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  1086. (crc_test2^[crcindex2]<>crc) then
  1087. Do_comment(V_Note,'impl CRC changed');
  1088. {$ifdef Test_Double_checksum_write}
  1089. Writeln(CRCFile,crc);
  1090. {$endif Test_Double_checksum_write}
  1091. inc(crcindex2);
  1092. end;
  1093. {$endif def Test_Double_checksum}
  1094. if do_interface_crc then
  1095. begin
  1096. interface_crc:=UpdateCrc32(interface_crc,b,len);
  1097. {$ifdef Test_Double_checksum}
  1098. if crc_only then
  1099. begin
  1100. crc_test^[crc_index]:=interface_crc;
  1101. {$ifdef Test_Double_checksum_write}
  1102. Writeln(CRCFile,interface_crc);
  1103. {$endif Test_Double_checksum_write}
  1104. if crc_index<crc_array_size then
  1105. inc(crc_index);
  1106. end
  1107. else
  1108. begin
  1109. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  1110. (crc_test^[crcindex]<>interface_crc) then
  1111. Do_comment(V_Warning,'CRC changed');
  1112. {$ifdef Test_Double_checksum_write}
  1113. Writeln(CRCFile,interface_crc);
  1114. {$endif Test_Double_checksum_write}
  1115. inc(crcindex);
  1116. end;
  1117. {$endif def Test_Double_checksum}
  1118. { indirect crc must only be calculated for the interface; changes
  1119. to a class in the implementation cannot require another unit to
  1120. be recompiled }
  1121. if do_indirect_crc then
  1122. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  1123. end;
  1124. end;
  1125. if not crc_only then
  1126. writedata(b,len);
  1127. inc(entryidx,len);
  1128. end;
  1129. procedure tppufile.putbyte(b:byte);
  1130. begin
  1131. putdata(b,1);
  1132. end;
  1133. procedure tppufile.putword(w:word);
  1134. begin
  1135. putdata(w,2);
  1136. end;
  1137. procedure tppufile.putdword(w:dword);
  1138. begin
  1139. putdata(w,4);
  1140. end;
  1141. procedure tppufile.putlongint(l:longint);
  1142. begin
  1143. putdata(l,4);
  1144. end;
  1145. procedure tppufile.putint64(i:int64);
  1146. begin
  1147. putdata(i,8);
  1148. end;
  1149. procedure tppufile.putqword(q:qword);
  1150. begin
  1151. putdata(q,sizeof(qword));
  1152. end;
  1153. procedure tppufile.putaint(i:aint);
  1154. begin
  1155. putdata(i,sizeof(aint));
  1156. end;
  1157. procedure tppufile.putasizeint(i: asizeint);
  1158. begin
  1159. putdata(i,sizeof(asizeint));
  1160. end;
  1161. procedure tppufile.putaword(i:aword);
  1162. begin
  1163. putdata(i,sizeof(aword));
  1164. end;
  1165. procedure tppufile.putreal(d:ppureal);
  1166. var
  1167. hd : double;
  1168. begin
  1169. if target_info.system=system_x86_64_win64 then
  1170. begin
  1171. hd:=d;
  1172. putdata(hd,sizeof(hd));
  1173. end
  1174. else
  1175. putdata(d,sizeof(ppureal));
  1176. end;
  1177. procedure tppufile.putstring(const s:string);
  1178. begin
  1179. putdata(s,length(s)+1);
  1180. end;
  1181. procedure tppufile.putansistring(const s:ansistring);
  1182. var
  1183. len: longint;
  1184. begin
  1185. len:=length(s);
  1186. putlongint(len);
  1187. if len>0 then
  1188. putdata(s[1],len);
  1189. end;
  1190. procedure tppufile.putsmallset(const b);
  1191. var
  1192. l : longint;
  1193. begin
  1194. l:=longint(b);
  1195. putlongint(l);
  1196. end;
  1197. procedure tppufile.putnormalset(const b);
  1198. begin
  1199. putdata(b,32);
  1200. end;
  1201. procedure tppufile.tempclose;
  1202. begin
  1203. if not closed then
  1204. begin
  1205. closepos:=f.Position;
  1206. f.Free;
  1207. f:=nil;
  1208. closed:=true;
  1209. tempclosed:=true;
  1210. end;
  1211. end;
  1212. function tppufile.tempopen:boolean;
  1213. begin
  1214. tempopen:=false;
  1215. if not closed or not tempclosed then
  1216. exit;
  1217. { MG: not sure, if this is correct
  1218. f.position:=0;
  1219. No, f was freed in tempclose above, we need to
  1220. recreate it. PM 2011/06/06 }
  1221. try
  1222. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1223. except
  1224. exit;
  1225. end;
  1226. closed:=false;
  1227. tempclosed:=false;
  1228. { restore state }
  1229. f.Position:=closepos;
  1230. tempopen:=true;
  1231. end;
  1232. end.