ppu.pas 23 KB

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