ppu.pas 30 KB

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