ppu.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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=49;
  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. ibdbxcount = 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. {$ifdef ansistring_bits}
  108. ibansistring16def = 58;
  109. ibansistring32def = 55;
  110. ibansistring64def = 59;
  111. {$else}
  112. ibansistringdef = 55;
  113. {$endif}
  114. ibwidestringdef = 56;
  115. ibvariantdef = 57;
  116. {implementation/objectdata}
  117. ibnodetree = 80;
  118. ibasmsymbols = 81;
  119. { unit flags }
  120. uf_init = $1;
  121. uf_finalize = $2;
  122. uf_big_endian = $4;
  123. uf_has_dbx = $8;
  124. uf_has_browser = $10;
  125. uf_in_library = $20; { is the file in another file than <ppufile>.* ? }
  126. uf_smart_linked = $40; { the ppu can be smartlinked }
  127. uf_static_linked = $80; { the ppu can be linked static }
  128. uf_shared_linked = $100; { the ppu can be linked shared }
  129. uf_local_browser = $200;
  130. uf_no_link = $400; { unit has no .o generated, but can still have
  131. external linking! }
  132. uf_has_resources = $800; { unit has resource section }
  133. uf_little_endian = $1000;
  134. uf_release = $2000; { unit was compiled with -Ur option }
  135. uf_threadvars = $4000; { unit has threadvars }
  136. uf_fpu_emulation = $8000; { this unit was compiled with fpu emulation on }
  137. uf_has_debuginfo = $10000; { this unit has debuginfo generated }
  138. uf_local_symtable = $20000; { this unit has a local symtable stored }
  139. uf_uses_variants = $40000; { this unit uses variants }
  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. crc,
  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.