ppu.pas 23 KB

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