ppu.pas 26 KB

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