ppu.pas 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. do_crc,
  213. do_interface_crc,
  214. do_indirect_crc : boolean;
  215. crc_only : boolean; { used to calculate interface_crc before implementation }
  216. constructor Create(const fn:string);
  217. destructor Destroy;override;
  218. procedure flush;
  219. procedure closefile;
  220. function CheckPPUId:boolean;
  221. function GetPPUVersion:integer;
  222. procedure NewHeader;
  223. procedure NewEntry;
  224. {read}
  225. function openfile:boolean;
  226. procedure reloadbuf;
  227. procedure readdata(var b;len:integer);
  228. procedure skipdata(len:integer);
  229. function readentry:byte;
  230. function EndOfEntry:boolean;
  231. function entrysize:longint;
  232. procedure getdatabuf(var b;len:integer;var res:integer);
  233. procedure getdata(var b;len:integer);
  234. function getbyte:byte;
  235. function getword:word;
  236. function getdword:dword;
  237. function getlongint:longint;
  238. function getint64:int64;
  239. function getqword:qword;
  240. function getaint:aint;
  241. function getasizeint:asizeint;
  242. function getaword:aword;
  243. function getreal:ppureal;
  244. function getstring:string;
  245. function getansistring:ansistring;
  246. procedure getnormalset(var b);
  247. procedure getsmallset(var b);
  248. function skipuntilentry(untilb:byte):boolean;
  249. {write}
  250. function createfile:boolean;
  251. procedure writeheader;
  252. procedure writebuf;
  253. procedure writedata(const b;len:integer);
  254. procedure writeentry(ibnr:byte);
  255. procedure putdata(const b;len:integer);
  256. procedure putbyte(b:byte);
  257. procedure putword(w:word);
  258. procedure putdword(w:dword);
  259. procedure putlongint(l:longint);
  260. procedure putint64(i:int64);
  261. procedure putqword(q:qword);
  262. procedure putaint(i:aint);
  263. procedure putaword(i:aword);
  264. procedure putreal(d:ppureal);
  265. procedure putstring(const s:string);
  266. procedure putansistring(const s:ansistring);
  267. procedure putnormalset(const b);
  268. procedure putsmallset(const b);
  269. procedure tempclose; // MG: not used, obsolete?
  270. function tempopen:boolean; // MG: not used, obsolete?
  271. end;
  272. implementation
  273. uses
  274. systems,
  275. {$ifdef Test_Double_checksum}
  276. comphook,
  277. {$endif def Test_Double_checksum}
  278. fpccrc,
  279. cutils;
  280. function swapendian_ppureal(d:ppureal):ppureal;
  281. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  282. var i:0..sizeof(d)-1;
  283. begin
  284. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  285. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  286. end;
  287. {*****************************************************************************
  288. TPPUFile
  289. *****************************************************************************}
  290. constructor tppufile.Create(const fn:string);
  291. begin
  292. fname:=fn;
  293. change_endian:=false;
  294. crc_only:=false;
  295. Mode:=0;
  296. NewHeader;
  297. Error:=false;
  298. closed:=true;
  299. tempclosed:=false;
  300. getmem(buf,ppubufsize);
  301. end;
  302. destructor tppufile.destroy;
  303. begin
  304. closefile;
  305. if assigned(buf) then
  306. freemem(buf,ppubufsize);
  307. end;
  308. procedure tppufile.flush;
  309. begin
  310. if Mode=2 then
  311. writebuf;
  312. end;
  313. procedure tppufile.closefile;
  314. begin
  315. {$ifdef Test_Double_checksum}
  316. if mode=2 then
  317. begin
  318. if assigned(crc_test) then
  319. dispose(crc_test);
  320. if assigned(crc_test2) then
  321. dispose(crc_test2);
  322. end;
  323. {$endif Test_Double_checksum}
  324. if Mode<>0 then
  325. begin
  326. Flush;
  327. f.Free;
  328. Mode:=0;
  329. closed:=true;
  330. end;
  331. end;
  332. function tppufile.CheckPPUId:boolean;
  333. begin
  334. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  335. end;
  336. function tppufile.GetPPUVersion:integer;
  337. var
  338. l : integer;
  339. code : integer;
  340. begin
  341. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  342. if code=0 then
  343. GetPPUVersion:=l
  344. else
  345. GetPPUVersion:=0;
  346. end;
  347. procedure tppufile.NewHeader;
  348. var
  349. s : string;
  350. begin
  351. fillchar(header,sizeof(tppuheader),0);
  352. str(currentppuversion,s);
  353. while length(s)<3 do
  354. s:='0'+s;
  355. with header do
  356. begin
  357. Id[1]:='P';
  358. Id[2]:='P';
  359. Id[3]:='U';
  360. Ver[1]:=s[1];
  361. Ver[2]:=s[2];
  362. Ver[3]:=s[3];
  363. end;
  364. end;
  365. {*****************************************************************************
  366. TPPUFile Reading
  367. *****************************************************************************}
  368. function tppufile.openfile:boolean;
  369. var
  370. i : integer;
  371. begin
  372. openfile:=false;
  373. try
  374. f:=CFileStreamClass.Create(fname,fmOpenRead)
  375. except
  376. exit;
  377. end;
  378. closed:=false;
  379. {read ppuheader}
  380. fsize:=f.Size;
  381. if fsize<sizeof(tppuheader) then
  382. exit;
  383. i:=f.Read(header,sizeof(tppuheader));
  384. { The header is always stored in little endian order }
  385. { therefore swap if on a big endian machine }
  386. {$IFDEF ENDIAN_BIG}
  387. header.compiler := swapendian(header.compiler);
  388. header.cpu := swapendian(header.cpu);
  389. header.target := swapendian(header.target);
  390. header.flags := swapendian(header.flags);
  391. header.size := swapendian(header.size);
  392. header.checksum := swapendian(header.checksum);
  393. header.interface_checksum := swapendian(header.interface_checksum);
  394. header.indirect_checksum := swapendian(header.indirect_checksum);
  395. header.deflistsize:=swapendian(header.deflistsize);
  396. header.symlistsize:=swapendian(header.symlistsize);
  397. {$ENDIF}
  398. { the PPU DATA is stored in native order }
  399. if (header.flags and uf_big_endian) = uf_big_endian then
  400. Begin
  401. {$IFDEF ENDIAN_LITTLE}
  402. change_endian := TRUE;
  403. {$ELSE}
  404. change_endian := FALSE;
  405. {$ENDIF}
  406. End
  407. else if (header.flags and uf_little_endian) = uf_little_endian then
  408. Begin
  409. {$IFDEF ENDIAN_BIG}
  410. change_endian := TRUE;
  411. {$ELSE}
  412. change_endian := FALSE;
  413. {$ENDIF}
  414. End;
  415. {reset buffer}
  416. bufstart:=i;
  417. bufsize:=0;
  418. bufidx:=0;
  419. Mode:=1;
  420. FillChar(entry,sizeof(tppuentry),0);
  421. entryidx:=0;
  422. entrystart:=0;
  423. entrybufstart:=0;
  424. Error:=false;
  425. openfile:=true;
  426. end;
  427. procedure tppufile.reloadbuf;
  428. begin
  429. inc(bufstart,bufsize);
  430. bufsize:=f.Read(buf^,ppubufsize);
  431. bufidx:=0;
  432. end;
  433. procedure tppufile.readdata(var b;len:integer);
  434. var
  435. p,pbuf : pchar;
  436. left : integer;
  437. begin
  438. p:=pchar(@b);
  439. pbuf:=@buf[bufidx];
  440. repeat
  441. left:=bufsize-bufidx;
  442. if len<left then
  443. break;
  444. move(pbuf^,p^,left);
  445. dec(len,left);
  446. inc(p,left);
  447. reloadbuf;
  448. pbuf:=@buf[bufidx];
  449. if bufsize=0 then
  450. exit;
  451. until false;
  452. move(pbuf^,p^,len);
  453. inc(bufidx,len);
  454. end;
  455. procedure tppufile.skipdata(len:integer);
  456. var
  457. left : integer;
  458. begin
  459. while len>0 do
  460. begin
  461. left:=bufsize-bufidx;
  462. if len>left then
  463. begin
  464. dec(len,left);
  465. reloadbuf;
  466. if bufsize=0 then
  467. exit;
  468. end
  469. else
  470. begin
  471. inc(bufidx,len);
  472. exit;
  473. end;
  474. end;
  475. end;
  476. function tppufile.readentry:byte;
  477. begin
  478. if entryidx<entry.size then
  479. skipdata(entry.size-entryidx);
  480. readdata(entry,sizeof(tppuentry));
  481. if change_endian then
  482. entry.size:=swapendian(entry.size);
  483. entrystart:=bufstart+bufidx;
  484. entryidx:=0;
  485. if not(entry.id in [mainentryid,subentryid]) then
  486. begin
  487. readentry:=iberror;
  488. error:=true;
  489. exit;
  490. end;
  491. readentry:=entry.nr;
  492. end;
  493. function tppufile.endofentry:boolean;
  494. begin
  495. endofentry:=(entryidx>=entry.size);
  496. end;
  497. function tppufile.entrysize:longint;
  498. begin
  499. entrysize:=entry.size;
  500. end;
  501. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  502. begin
  503. if entryidx+len>entry.size then
  504. res:=entry.size-entryidx
  505. else
  506. res:=len;
  507. readdata(b,res);
  508. inc(entryidx,res);
  509. end;
  510. procedure tppufile.getdata(var b;len:integer);
  511. begin
  512. if entryidx+len>entry.size then
  513. begin
  514. error:=true;
  515. exit;
  516. end;
  517. readdata(b,len);
  518. inc(entryidx,len);
  519. end;
  520. function tppufile.getbyte:byte;
  521. begin
  522. if entryidx+1>entry.size then
  523. begin
  524. error:=true;
  525. result:=0;
  526. exit;
  527. end;
  528. if bufsize-bufidx>=1 then
  529. begin
  530. result:=pbyte(@buf[bufidx])^;
  531. inc(bufidx);
  532. end
  533. else
  534. readdata(result,1);
  535. inc(entryidx);
  536. end;
  537. function tppufile.getword:word;
  538. begin
  539. if entryidx+2>entry.size then
  540. begin
  541. error:=true;
  542. result:=0;
  543. exit;
  544. end;
  545. if bufsize-bufidx>=sizeof(word) then
  546. begin
  547. result:=Unaligned(pword(@buf[bufidx])^);
  548. inc(bufidx,sizeof(word));
  549. end
  550. else
  551. readdata(result,sizeof(word));
  552. if change_endian then
  553. result:=swapendian(result);
  554. inc(entryidx,2);
  555. end;
  556. function tppufile.getlongint:longint;
  557. begin
  558. if entryidx+4>entry.size then
  559. begin
  560. error:=true;
  561. result:=0;
  562. exit;
  563. end;
  564. if bufsize-bufidx>=sizeof(longint) then
  565. begin
  566. result:=Unaligned(plongint(@buf[bufidx])^);
  567. inc(bufidx,sizeof(longint));
  568. end
  569. else
  570. readdata(result,sizeof(longint));
  571. if change_endian then
  572. result:=swapendian(result);
  573. inc(entryidx,4);
  574. end;
  575. function tppufile.getdword:dword;
  576. begin
  577. if entryidx+4>entry.size then
  578. begin
  579. error:=true;
  580. result:=0;
  581. exit;
  582. end;
  583. if bufsize-bufidx>=sizeof(dword) then
  584. begin
  585. result:=Unaligned(plongint(@buf[bufidx])^);
  586. inc(bufidx,sizeof(longint));
  587. end
  588. else
  589. readdata(result,sizeof(dword));
  590. if change_endian then
  591. result:=swapendian(result);
  592. inc(entryidx,4);
  593. end;
  594. function tppufile.getint64:int64;
  595. begin
  596. if entryidx+8>entry.size then
  597. begin
  598. error:=true;
  599. result:=0;
  600. exit;
  601. end;
  602. if bufsize-bufidx>=sizeof(int64) then
  603. begin
  604. result:=Unaligned(pint64(@buf[bufidx])^);
  605. inc(bufidx,sizeof(int64));
  606. end
  607. else
  608. readdata(result,sizeof(int64));
  609. if change_endian then
  610. result:=swapendian(result);
  611. inc(entryidx,8);
  612. end;
  613. function tppufile.getqword:qword;
  614. begin
  615. if entryidx+8>entry.size then
  616. begin
  617. error:=true;
  618. result:=0;
  619. exit;
  620. end;
  621. if bufsize-bufidx>=sizeof(qword) then
  622. begin
  623. result:=Unaligned(pqword(@buf[bufidx])^);
  624. inc(bufidx,sizeof(qword));
  625. end
  626. else
  627. readdata(result,sizeof(qword));
  628. if change_endian then
  629. result:=swapendian(result);
  630. inc(entryidx,8);
  631. end;
  632. function tppufile.getaint:aint;
  633. begin
  634. {$ifdef cpu64bitalu}
  635. result:=getint64;
  636. {$else cpu64bitalu}
  637. result:=getlongint;
  638. {$endif cpu64bitalu}
  639. end;
  640. function tppufile.getasizeint:asizeint;
  641. begin
  642. {$ifdef cpu64bitaddr}
  643. result:=getint64;
  644. {$else cpu64bitaddr}
  645. result:=getlongint;
  646. {$endif cpu32bitaddr}
  647. end;
  648. function tppufile.getaword:aword;
  649. begin
  650. {$ifdef cpu64bitalu}
  651. result:=getqword;
  652. {$else cpu64bitalu}
  653. result:=getdword;
  654. {$endif cpu64bitalu}
  655. end;
  656. function tppufile.getreal:ppureal;
  657. var
  658. d : ppureal;
  659. hd : double;
  660. begin
  661. if target_info.system=system_x86_64_win64 then
  662. begin
  663. if entryidx+sizeof(hd)>entry.size then
  664. begin
  665. error:=true;
  666. getreal:=0;
  667. exit;
  668. end;
  669. readdata(hd,sizeof(hd));
  670. if change_endian then
  671. getreal:=swapendian(qword(hd))
  672. else
  673. getreal:=hd;
  674. inc(entryidx,sizeof(hd));
  675. end
  676. else
  677. begin
  678. if entryidx+sizeof(ppureal)>entry.size then
  679. begin
  680. error:=true;
  681. getreal:=0;
  682. exit;
  683. end;
  684. readdata(d,sizeof(ppureal));
  685. if change_endian then
  686. getreal:=swapendian_ppureal(d)
  687. else
  688. getreal:=d;
  689. inc(entryidx,sizeof(ppureal));
  690. end;
  691. end;
  692. function tppufile.getstring:string;
  693. begin
  694. result[0]:=chr(getbyte);
  695. if entryidx+length(result)>entry.size then
  696. begin
  697. error:=true;
  698. exit;
  699. end;
  700. ReadData(result[1],length(result));
  701. inc(entryidx,length(result));
  702. end;
  703. function tppufile.getansistring: ansistring;
  704. var
  705. l : longint;
  706. begin
  707. l:=getlongint;
  708. if entryidx+l>entry.size then
  709. begin
  710. error:=true;
  711. exit;
  712. end;
  713. SetLength(Result,l);
  714. ReadData(result[1],l);
  715. inc(entryidx,l);
  716. end;
  717. procedure tppufile.getsmallset(var b);
  718. var
  719. i : longint;
  720. begin
  721. getdata(b,4);
  722. if change_endian then
  723. for i:=0 to 3 do
  724. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  725. end;
  726. procedure tppufile.getnormalset(var b);
  727. var
  728. i : longint;
  729. begin
  730. getdata(b,32);
  731. if change_endian then
  732. for i:=0 to 31 do
  733. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  734. end;
  735. function tppufile.skipuntilentry(untilb:byte):boolean;
  736. var
  737. b : byte;
  738. begin
  739. repeat
  740. b:=readentry;
  741. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  742. skipuntilentry:=(b=untilb);
  743. end;
  744. {*****************************************************************************
  745. TPPUFile Writing
  746. *****************************************************************************}
  747. function tppufile.createfile:boolean;
  748. var
  749. ok: boolean;
  750. begin
  751. createfile:=false;
  752. {$ifdef INTFPPU}
  753. if crc_only then
  754. begin
  755. fname:=fname+'.intf';
  756. crc_only:=false;
  757. end;
  758. {$endif}
  759. if not crc_only then
  760. begin
  761. {$ifdef MACOS}
  762. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  763. SetDefaultMacOSCreator('FPas');
  764. SetDefaultMacOSFiletype('FPPU');
  765. {$endif}
  766. ok:=false;
  767. try
  768. f:=CFileStreamClass.Create(fname,fmCreate);
  769. ok:=true;
  770. except
  771. end;
  772. {$ifdef MACOS}
  773. SetDefaultMacOSCreator('MPS ');
  774. SetDefaultMacOSFiletype('TEXT');
  775. {$endif}
  776. if not ok then
  777. exit;
  778. Mode:=2;
  779. {write header for sure}
  780. f.Write(header,sizeof(tppuheader));
  781. end;
  782. bufsize:=ppubufsize;
  783. bufstart:=sizeof(tppuheader);
  784. bufidx:=0;
  785. {reset}
  786. crc:=0;
  787. interface_crc:=0;
  788. indirect_crc:=0;
  789. do_interface_crc:=true;
  790. do_indirect_crc:=false;
  791. Error:=false;
  792. do_crc:=true;
  793. size:=0;
  794. entrytyp:=mainentryid;
  795. {start}
  796. NewEntry;
  797. createfile:=true;
  798. end;
  799. procedure tppufile.writeheader;
  800. var
  801. opos : integer;
  802. begin
  803. if crc_only then
  804. exit;
  805. { flush buffer }
  806. writebuf;
  807. { update size (w/o header!) in the header }
  808. header.size:=bufstart-sizeof(tppuheader);
  809. { set the endian flag }
  810. {$ifndef FPC_BIG_ENDIAN}
  811. header.flags := header.flags or uf_little_endian;
  812. {$else not FPC_BIG_ENDIAN}
  813. header.flags := header.flags or uf_big_endian;
  814. { Now swap the header in the correct endian (always little endian) }
  815. header.compiler := swapendian(header.compiler);
  816. header.cpu := swapendian(header.cpu);
  817. header.target := swapendian(header.target);
  818. header.flags := swapendian(header.flags);
  819. header.size := swapendian(header.size);
  820. header.checksum := swapendian(header.checksum);
  821. header.interface_checksum := swapendian(header.interface_checksum);
  822. header.indirect_checksum := swapendian(header.indirect_checksum);
  823. header.deflistsize:=swapendian(header.deflistsize);
  824. header.symlistsize:=swapendian(header.symlistsize);
  825. {$endif not FPC_BIG_ENDIAN}
  826. { write header and restore filepos after it }
  827. opos:=f.Position;
  828. f.Position:=0;
  829. f.Write(header,sizeof(tppuheader));
  830. f.Position:=opos;
  831. end;
  832. procedure tppufile.writebuf;
  833. begin
  834. if not crc_only and
  835. (bufidx <> 0) then
  836. f.Write(buf^,bufidx);
  837. inc(bufstart,bufidx);
  838. bufidx:=0;
  839. end;
  840. procedure tppufile.writedata(const b;len:integer);
  841. var
  842. p : pchar;
  843. left,
  844. idx : integer;
  845. begin
  846. if crc_only then
  847. exit;
  848. p:=pchar(@b);
  849. idx:=0;
  850. while len>0 do
  851. begin
  852. left:=bufsize-bufidx;
  853. if len>left then
  854. begin
  855. move(p[idx],buf[bufidx],left);
  856. dec(len,left);
  857. inc(idx,left);
  858. inc(bufidx,left);
  859. writebuf;
  860. end
  861. else
  862. begin
  863. move(p[idx],buf[bufidx],len);
  864. inc(bufidx,len);
  865. exit;
  866. end;
  867. end;
  868. end;
  869. procedure tppufile.NewEntry;
  870. begin
  871. with entry do
  872. begin
  873. id:=entrytyp;
  874. nr:=ibend;
  875. size:=0;
  876. end;
  877. {Reset Entry State}
  878. entryidx:=0;
  879. entrybufstart:=bufstart;
  880. entrystart:=bufstart+bufidx;
  881. {Alloc in buffer}
  882. writedata(entry,sizeof(tppuentry));
  883. end;
  884. procedure tppufile.writeentry(ibnr:byte);
  885. var
  886. opos : integer;
  887. begin
  888. {create entry}
  889. entry.id:=entrytyp;
  890. entry.nr:=ibnr;
  891. entry.size:=entryidx;
  892. {it's already been sent to disk ?}
  893. if entrybufstart<>bufstart then
  894. begin
  895. if not crc_only then
  896. begin
  897. {flush to be sure}
  898. WriteBuf;
  899. {write entry}
  900. opos:=f.Position;
  901. f.Position:=entrystart;
  902. f.write(entry,sizeof(tppuentry));
  903. f.Position:=opos;
  904. end;
  905. entrybufstart:=bufstart;
  906. end
  907. else
  908. move(entry,buf[entrystart-bufstart],sizeof(entry));
  909. {Add New Entry, which is ibend by default}
  910. entrystart:=bufstart+bufidx; {next entry position}
  911. NewEntry;
  912. end;
  913. procedure tppufile.putdata(const b;len:integer);
  914. begin
  915. if do_crc then
  916. begin
  917. crc:=UpdateCrc32(crc,b,len);
  918. {$ifdef Test_Double_checksum}
  919. if crc_only then
  920. begin
  921. crc_test2^[crc_index2]:=crc;
  922. {$ifdef Test_Double_checksum_write}
  923. Writeln(CRCFile,crc);
  924. {$endif Test_Double_checksum_write}
  925. if crc_index2<crc_array_size then
  926. inc(crc_index2);
  927. end
  928. else
  929. begin
  930. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  931. (crc_test2^[crcindex2]<>crc) then
  932. Do_comment(V_Note,'impl CRC changed');
  933. {$ifdef Test_Double_checksum_write}
  934. Writeln(CRCFile,crc);
  935. {$endif Test_Double_checksum_write}
  936. inc(crcindex2);
  937. end;
  938. {$endif def Test_Double_checksum}
  939. if do_interface_crc then
  940. begin
  941. interface_crc:=UpdateCrc32(interface_crc,b,len);
  942. {$ifdef Test_Double_checksum}
  943. if crc_only then
  944. begin
  945. crc_test^[crc_index]:=interface_crc;
  946. {$ifdef Test_Double_checksum_write}
  947. Writeln(CRCFile,interface_crc);
  948. {$endif Test_Double_checksum_write}
  949. if crc_index<crc_array_size then
  950. inc(crc_index);
  951. end
  952. else
  953. begin
  954. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  955. (crc_test^[crcindex]<>interface_crc) then
  956. Do_comment(V_Warning,'CRC changed');
  957. {$ifdef Test_Double_checksum_write}
  958. Writeln(CRCFile,interface_crc);
  959. {$endif Test_Double_checksum_write}
  960. inc(crcindex);
  961. end;
  962. {$endif def Test_Double_checksum}
  963. { indirect crc must only be calculated for the interface; changes
  964. to a class in the implementation cannot require another unit to
  965. be recompiled }
  966. if do_indirect_crc then
  967. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  968. end;
  969. end;
  970. if not crc_only then
  971. writedata(b,len);
  972. inc(entryidx,len);
  973. end;
  974. procedure tppufile.putbyte(b:byte);
  975. begin
  976. putdata(b,1);
  977. end;
  978. procedure tppufile.putword(w:word);
  979. begin
  980. putdata(w,2);
  981. end;
  982. procedure tppufile.putdword(w:dword);
  983. begin
  984. putdata(w,4);
  985. end;
  986. procedure tppufile.putlongint(l:longint);
  987. begin
  988. putdata(l,4);
  989. end;
  990. procedure tppufile.putint64(i:int64);
  991. begin
  992. putdata(i,8);
  993. end;
  994. procedure tppufile.putqword(q:qword);
  995. begin
  996. putdata(q,sizeof(qword));
  997. end;
  998. procedure tppufile.putaint(i:aint);
  999. begin
  1000. putdata(i,sizeof(aint));
  1001. end;
  1002. procedure tppufile.putaword(i:aword);
  1003. begin
  1004. putdata(i,sizeof(aword));
  1005. end;
  1006. procedure tppufile.putreal(d:ppureal);
  1007. var
  1008. hd : double;
  1009. begin
  1010. if target_info.system=system_x86_64_win64 then
  1011. begin
  1012. hd:=d;
  1013. putdata(hd,sizeof(hd));
  1014. end
  1015. else
  1016. putdata(d,sizeof(ppureal));
  1017. end;
  1018. procedure tppufile.putstring(const s:string);
  1019. begin
  1020. putdata(s,length(s)+1);
  1021. end;
  1022. procedure tppufile.putansistring(const s: ansistring);
  1023. var
  1024. l : longint;
  1025. begin
  1026. l:=length(s);
  1027. putdata(l,4);
  1028. putdata(s[1],l);
  1029. end;
  1030. procedure tppufile.putsmallset(const b);
  1031. var
  1032. l : longint;
  1033. begin
  1034. l:=longint(b);
  1035. putlongint(l);
  1036. end;
  1037. procedure tppufile.putnormalset(const b);
  1038. type
  1039. SetLongintArray = Array [0..7] of longint;
  1040. begin
  1041. putdata(b,32);
  1042. end;
  1043. procedure tppufile.tempclose;
  1044. begin
  1045. if not closed then
  1046. begin
  1047. closepos:=f.Position;
  1048. f.Free;
  1049. f:=nil;
  1050. closed:=true;
  1051. tempclosed:=true;
  1052. end;
  1053. end;
  1054. function tppufile.tempopen:boolean;
  1055. begin
  1056. tempopen:=false;
  1057. if not closed or not tempclosed then
  1058. exit;
  1059. { MG: not sure, if this is correct
  1060. f.position:=0;
  1061. No, f was freed in tempclose above, we need to
  1062. recreate it. PM 2011/06/06 }
  1063. try
  1064. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1065. except
  1066. exit;
  1067. end;
  1068. closed:=false;
  1069. tempclosed:=false;
  1070. { restore state }
  1071. f.Position:=closepos;
  1072. tempopen:=true;
  1073. end;
  1074. end.