ppu.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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=52;
  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. iblinkdlls = 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/objectdata}
  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_resources = $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. {$ifdef Test_Double_checksum}
  244. comphook,
  245. {$endif def Test_Double_checksum}
  246. fpccrc,
  247. cutils;
  248. {*****************************************************************************
  249. Endian Handling
  250. *****************************************************************************}
  251. Function SwapLong(x : longint): longint;
  252. var
  253. y : word;
  254. z : word;
  255. Begin
  256. y := x shr 16;
  257. y := word(longint(y) shl 8) or (y shr 8);
  258. z := x and $FFFF;
  259. z := word(longint(z) shl 8) or (z shr 8);
  260. SwapLong := (longint(z) shl 16) or longint(y);
  261. End;
  262. Function SwapWord(x : word): word;
  263. var
  264. z : byte;
  265. Begin
  266. z := x shr 8;
  267. x := x and $ff;
  268. x := word(x shl 8);
  269. SwapWord := x or z;
  270. End;
  271. {*****************************************************************************
  272. TPPUFile
  273. *****************************************************************************}
  274. constructor tppufile.Create(const fn:string);
  275. begin
  276. fname:=fn;
  277. change_endian:=false;
  278. crc_only:=false;
  279. Mode:=0;
  280. NewHeader;
  281. Error:=false;
  282. closed:=true;
  283. tempclosed:=false;
  284. getmem(buf,ppubufsize);
  285. end;
  286. destructor tppufile.destroy;
  287. begin
  288. closefile;
  289. if assigned(buf) then
  290. freemem(buf,ppubufsize);
  291. end;
  292. procedure tppufile.flush;
  293. begin
  294. if Mode=2 then
  295. writebuf;
  296. end;
  297. procedure tppufile.closefile;
  298. begin
  299. {$ifdef Test_Double_checksum}
  300. if mode=2 then
  301. begin
  302. if assigned(crc_test) then
  303. dispose(crc_test);
  304. if assigned(crc_test2) then
  305. dispose(crc_test2);
  306. end;
  307. {$endif Test_Double_checksum}
  308. if Mode<>0 then
  309. begin
  310. Flush;
  311. {$I-}
  312. system.close(f);
  313. {$I+}
  314. if ioresult<>0 then;
  315. Mode:=0;
  316. closed:=true;
  317. end;
  318. end;
  319. function tppufile.CheckPPUId:boolean;
  320. begin
  321. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  322. end;
  323. function tppufile.GetPPUVersion:integer;
  324. var
  325. l : integer;
  326. code : integer;
  327. begin
  328. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  329. if code=0 then
  330. GetPPUVersion:=l
  331. else
  332. GetPPUVersion:=0;
  333. end;
  334. procedure tppufile.NewHeader;
  335. var
  336. s : string;
  337. begin
  338. fillchar(header,sizeof(tppuheader),0);
  339. str(currentppuversion,s);
  340. while length(s)<3 do
  341. s:='0'+s;
  342. with header do
  343. begin
  344. Id[1]:='P';
  345. Id[2]:='P';
  346. Id[3]:='U';
  347. Ver[1]:=s[1];
  348. Ver[2]:=s[2];
  349. Ver[3]:=s[3];
  350. end;
  351. end;
  352. {*****************************************************************************
  353. TPPUFile Reading
  354. *****************************************************************************}
  355. function tppufile.openfile:boolean;
  356. var
  357. ofmode : byte;
  358. i : integer;
  359. begin
  360. openfile:=false;
  361. assign(f,fname);
  362. ofmode:=filemode;
  363. filemode:=$0;
  364. {$I-}
  365. reset(f,1);
  366. {$I+}
  367. filemode:=ofmode;
  368. if ioresult<>0 then
  369. exit;
  370. closed:=false;
  371. {read ppuheader}
  372. fsize:=filesize(f);
  373. if fsize<sizeof(tppuheader) then
  374. exit;
  375. blockread(f,header,sizeof(tppuheader),i);
  376. { The header is always stored in little endian order }
  377. { therefore swap if on a big endian machine }
  378. {$IFDEF ENDIAN_BIG}
  379. header.compiler := SwapWord(header.compiler);
  380. header.cpu := SwapWord(header.cpu);
  381. header.target := SwapWord(header.target);
  382. header.flags := SwapLong(header.flags);
  383. header.size := SwapLong(header.size);
  384. header.checksum := cardinal(SwapLong(longint(header.checksum)));
  385. header.interface_checksum := cardinal(SwapLong(longint(header.interface_checksum)));
  386. {$ENDIF}
  387. { the PPU DATA is stored in native order }
  388. if (header.flags and uf_big_endian) = uf_big_endian then
  389. Begin
  390. {$IFDEF ENDIAN_LITTLE}
  391. change_endian := TRUE;
  392. {$ELSE}
  393. change_endian := FALSE;
  394. {$ENDIF}
  395. End
  396. else if (header.flags and uf_little_endian) = uf_little_endian then
  397. Begin
  398. {$IFDEF ENDIAN_BIG}
  399. change_endian := TRUE;
  400. {$ELSE}
  401. change_endian := FALSE;
  402. {$ENDIF}
  403. End;
  404. {reset buffer}
  405. bufstart:=i;
  406. bufsize:=0;
  407. bufidx:=0;
  408. Mode:=1;
  409. FillChar(entry,sizeof(tppuentry),0);
  410. entryidx:=0;
  411. entrystart:=0;
  412. entrybufstart:=0;
  413. Error:=false;
  414. openfile:=true;
  415. end;
  416. procedure tppufile.reloadbuf;
  417. begin
  418. inc(bufstart,bufsize);
  419. blockread(f,buf^,ppubufsize,bufsize);
  420. bufidx:=0;
  421. end;
  422. procedure tppufile.readdata(var b;len:integer);
  423. var
  424. p : pchar;
  425. left,
  426. idx : integer;
  427. begin
  428. p:=pchar(@b);
  429. idx:=0;
  430. while len>0 do
  431. begin
  432. left:=bufsize-bufidx;
  433. if len>left then
  434. begin
  435. move(buf[bufidx],p[idx],left);
  436. dec(len,left);
  437. inc(idx,left);
  438. reloadbuf;
  439. if bufsize=0 then
  440. exit;
  441. end
  442. else
  443. begin
  444. move(buf[bufidx],p[idx],len);
  445. inc(bufidx,len);
  446. exit;
  447. end;
  448. end;
  449. end;
  450. procedure tppufile.skipdata(len:integer);
  451. var
  452. left : integer;
  453. begin
  454. while len>0 do
  455. begin
  456. left:=bufsize-bufidx;
  457. if len>left then
  458. begin
  459. dec(len,left);
  460. reloadbuf;
  461. if bufsize=0 then
  462. exit;
  463. end
  464. else
  465. begin
  466. inc(bufidx,len);
  467. exit;
  468. end;
  469. end;
  470. end;
  471. function tppufile.readentry:byte;
  472. begin
  473. if entryidx<entry.size then
  474. skipdata(entry.size-entryidx);
  475. readdata(entry,sizeof(tppuentry));
  476. if change_endian then
  477. entry.size:=swaplong(entry.size);
  478. entrystart:=bufstart+bufidx;
  479. entryidx:=0;
  480. if not(entry.id in [mainentryid,subentryid]) then
  481. begin
  482. readentry:=iberror;
  483. error:=true;
  484. exit;
  485. end;
  486. readentry:=entry.nr;
  487. end;
  488. function tppufile.endofentry:boolean;
  489. begin
  490. endofentry:=(entryidx>=entry.size);
  491. end;
  492. function tppufile.entrysize:longint;
  493. begin
  494. entrysize:=entry.size;
  495. end;
  496. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  497. begin
  498. if entryidx+len>entry.size then
  499. res:=entry.size-entryidx
  500. else
  501. res:=len;
  502. readdata(b,res);
  503. inc(entryidx,res);
  504. end;
  505. procedure tppufile.getdata(var b;len:integer);
  506. begin
  507. if entryidx+len>entry.size then
  508. begin
  509. error:=true;
  510. exit;
  511. end;
  512. readdata(b,len);
  513. inc(entryidx,len);
  514. end;
  515. function tppufile.getbyte:byte;
  516. var
  517. b : byte;
  518. begin
  519. if entryidx+1>entry.size then
  520. begin
  521. error:=true;
  522. getbyte:=0;
  523. exit;
  524. end;
  525. readdata(b,1);
  526. getbyte:=b;
  527. inc(entryidx);
  528. end;
  529. function tppufile.getword:word;
  530. var
  531. w : word;
  532. begin
  533. if entryidx+2>entry.size then
  534. begin
  535. error:=true;
  536. getword:=0;
  537. exit;
  538. end;
  539. readdata(w,2);
  540. if change_endian then
  541. getword:=swapword(w)
  542. else
  543. getword:=w;
  544. inc(entryidx,2);
  545. end;
  546. function tppufile.getlongint:longint;
  547. var
  548. l : longint;
  549. begin
  550. if entryidx+4>entry.size then
  551. begin
  552. error:=true;
  553. getlongint:=0;
  554. exit;
  555. end;
  556. readdata(l,4);
  557. if change_endian then
  558. getlongint:=swaplong(l)
  559. else
  560. getlongint:=l;
  561. inc(entryidx,4);
  562. end;
  563. function tppufile.getint64:int64;
  564. var
  565. i : int64;
  566. begin
  567. if entryidx+8>entry.size then
  568. begin
  569. error:=true;
  570. result:=0;
  571. exit;
  572. end;
  573. readdata(i,8);
  574. if change_endian then
  575. result:=swapint64(i)
  576. else
  577. result:=i;
  578. inc(entryidx,8);
  579. end;
  580. function tppufile.getaint:aint;
  581. begin
  582. {$ifdef cpu64bit}
  583. result:=getint64;
  584. {$else cpu64bit}
  585. result:=getlongint;
  586. {$endif cpu64bit}
  587. end;
  588. function tppufile.getreal:ppureal;
  589. var
  590. d : ppureal;
  591. begin
  592. if entryidx+sizeof(ppureal)>entry.size then
  593. begin
  594. error:=true;
  595. getreal:=0;
  596. exit;
  597. end;
  598. readdata(d,sizeof(ppureal));
  599. getreal:=d;
  600. inc(entryidx,sizeof(ppureal));
  601. end;
  602. function tppufile.getstring:string;
  603. var
  604. s : string;
  605. begin
  606. s[0]:=chr(getbyte);
  607. if entryidx+length(s)>entry.size then
  608. begin
  609. error:=true;
  610. exit;
  611. end;
  612. ReadData(s[1],length(s));
  613. getstring:=s;
  614. inc(entryidx,length(s));
  615. end;
  616. procedure tppufile.getsmallset(var b);
  617. var
  618. l : longint;
  619. begin
  620. l:=getlongint;
  621. longint(b):=l;
  622. end;
  623. procedure tppufile.getnormalset(var b);
  624. type
  625. SetLongintArray = Array [0..7] of longint;
  626. var
  627. i : longint;
  628. begin
  629. if change_endian then
  630. begin
  631. for i:=0 to 7 do
  632. SetLongintArray(b)[i]:=getlongint;
  633. end
  634. else
  635. getdata(b,32);
  636. end;
  637. function tppufile.skipuntilentry(untilb:byte):boolean;
  638. var
  639. b : byte;
  640. begin
  641. repeat
  642. b:=readentry;
  643. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  644. skipuntilentry:=(b=untilb);
  645. end;
  646. {*****************************************************************************
  647. TPPUFile Writing
  648. *****************************************************************************}
  649. function tppufile.createfile:boolean;
  650. begin
  651. createfile:=false;
  652. {$ifdef INTFPPU}
  653. if crc_only then
  654. begin
  655. fname:=fname+'.intf';
  656. crc_only:=false;
  657. end;
  658. {$endif}
  659. if not crc_only then
  660. begin
  661. assign(f,fname);
  662. {$ifdef MACOS}
  663. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  664. SetDefaultMacOSCreator('FPas');
  665. SetDefaultMacOSFiletype('FPPU');
  666. {$endif}
  667. {$I-}
  668. rewrite(f,1);
  669. {$I+}
  670. {$ifdef MACOS}
  671. SetDefaultMacOSCreator('MPS ');
  672. SetDefaultMacOSFiletype('TEXT');
  673. {$endif}
  674. if ioresult<>0 then
  675. exit;
  676. Mode:=2;
  677. {write header for sure}
  678. blockwrite(f,header,sizeof(tppuheader));
  679. end;
  680. bufsize:=ppubufsize;
  681. bufstart:=sizeof(tppuheader);
  682. bufidx:=0;
  683. {reset}
  684. crc:=cardinal($ffffffff);
  685. interface_crc:=cardinal($ffffffff);
  686. do_interface_crc:=true;
  687. Error:=false;
  688. do_crc:=true;
  689. size:=0;
  690. entrytyp:=mainentryid;
  691. {start}
  692. NewEntry;
  693. createfile:=true;
  694. end;
  695. procedure tppufile.writeheader;
  696. var
  697. opos : integer;
  698. begin
  699. if crc_only then
  700. exit;
  701. { flush buffer }
  702. writebuf;
  703. { update size (w/o header!) in the header }
  704. header.size:=bufstart-sizeof(tppuheader);
  705. { set the endian flag }
  706. {$ifndef FPC_BIG_ENDIAN}
  707. header.flags := header.flags or uf_little_endian;
  708. {$else not FPC_BIG_ENDIAN}
  709. header.flags := header.flags or uf_big_endian;
  710. { Now swap the header in the correct endian (always little endian) }
  711. header.compiler := SwapWord(header.compiler);
  712. header.cpu := SwapWord(header.cpu);
  713. header.target := SwapWord(header.target);
  714. header.flags := SwapLong(header.flags);
  715. header.size := SwapLong(header.size);
  716. header.checksum := cardinal(SwapLong(longint(header.checksum)));
  717. header.interface_checksum := cardinal(SwapLong(longint(header.interface_checksum)));
  718. {$endif not FPC_BIG_ENDIAN}
  719. { write header and restore filepos after it }
  720. opos:=filepos(f);
  721. seek(f,0);
  722. blockwrite(f,header,sizeof(tppuheader));
  723. seek(f,opos);
  724. end;
  725. procedure tppufile.writebuf;
  726. begin
  727. if not crc_only then
  728. blockwrite(f,buf^,bufidx);
  729. inc(bufstart,bufidx);
  730. bufidx:=0;
  731. end;
  732. procedure tppufile.writedata(const b;len:integer);
  733. var
  734. p : pchar;
  735. left,
  736. idx : integer;
  737. begin
  738. if crc_only then
  739. exit;
  740. p:=pchar(@b);
  741. idx:=0;
  742. while len>0 do
  743. begin
  744. left:=bufsize-bufidx;
  745. if len>left then
  746. begin
  747. move(p[idx],buf[bufidx],left);
  748. dec(len,left);
  749. inc(idx,left);
  750. inc(bufidx,left);
  751. writebuf;
  752. end
  753. else
  754. begin
  755. move(p[idx],buf[bufidx],len);
  756. inc(bufidx,len);
  757. exit;
  758. end;
  759. end;
  760. end;
  761. procedure tppufile.NewEntry;
  762. begin
  763. with entry do
  764. begin
  765. id:=entrytyp;
  766. nr:=ibend;
  767. size:=0;
  768. end;
  769. {Reset Entry State}
  770. entryidx:=0;
  771. entrybufstart:=bufstart;
  772. entrystart:=bufstart+bufidx;
  773. {Alloc in buffer}
  774. writedata(entry,sizeof(tppuentry));
  775. end;
  776. procedure tppufile.writeentry(ibnr:byte);
  777. var
  778. opos : integer;
  779. begin
  780. {create entry}
  781. entry.id:=entrytyp;
  782. entry.nr:=ibnr;
  783. entry.size:=entryidx;
  784. {it's already been sent to disk ?}
  785. if entrybufstart<>bufstart then
  786. begin
  787. if not crc_only then
  788. begin
  789. {flush to be sure}
  790. WriteBuf;
  791. {write entry}
  792. opos:=filepos(f);
  793. seek(f,entrystart);
  794. blockwrite(f,entry,sizeof(tppuentry));
  795. seek(f,opos);
  796. end;
  797. entrybufstart:=bufstart;
  798. end
  799. else
  800. move(entry,buf[entrystart-bufstart],sizeof(entry));
  801. {Add New Entry, which is ibend by default}
  802. entrystart:=bufstart+bufidx; {next entry position}
  803. NewEntry;
  804. end;
  805. procedure tppufile.putdata(const b;len:integer);
  806. begin
  807. if do_crc then
  808. begin
  809. crc:=UpdateCrc32(crc,b,len);
  810. {$ifdef Test_Double_checksum}
  811. if crc_only then
  812. begin
  813. crc_test2^[crc_index2]:=crc;
  814. {$ifdef Test_Double_checksum_write}
  815. Writeln(CRCFile,crc);
  816. {$endif Test_Double_checksum_write}
  817. if crc_index2<crc_array_size then
  818. inc(crc_index2);
  819. end
  820. else
  821. begin
  822. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  823. (crc_test2^[crcindex2]<>crc) then
  824. Do_comment(V_Note,'impl CRC changed');
  825. {$ifdef Test_Double_checksum_write}
  826. Writeln(CRCFile,crc);
  827. {$endif Test_Double_checksum_write}
  828. inc(crcindex2);
  829. end;
  830. {$endif def Test_Double_checksum}
  831. if do_interface_crc then
  832. begin
  833. interface_crc:=UpdateCrc32(interface_crc,b,len);
  834. {$ifdef Test_Double_checksum}
  835. if crc_only then
  836. begin
  837. crc_test^[crc_index]:=interface_crc;
  838. {$ifdef Test_Double_checksum_write}
  839. Writeln(CRCFile,interface_crc);
  840. {$endif Test_Double_checksum_write}
  841. if crc_index<crc_array_size then
  842. inc(crc_index);
  843. end
  844. else
  845. begin
  846. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  847. (crc_test^[crcindex]<>interface_crc) then
  848. Do_comment(V_Warning,'CRC changed');
  849. {$ifdef Test_Double_checksum_write}
  850. Writeln(CRCFile,interface_crc);
  851. {$endif Test_Double_checksum_write}
  852. inc(crcindex);
  853. end;
  854. {$endif def Test_Double_checksum}
  855. end;
  856. end;
  857. if not crc_only then
  858. writedata(b,len);
  859. inc(entryidx,len);
  860. end;
  861. procedure tppufile.putbyte(b:byte);
  862. begin
  863. putdata(b,1);
  864. end;
  865. procedure tppufile.putword(w:word);
  866. begin
  867. putdata(w,2);
  868. end;
  869. procedure tppufile.putlongint(l:longint);
  870. begin
  871. putdata(l,4);
  872. end;
  873. procedure tppufile.putint64(i:int64);
  874. begin
  875. putdata(i,8);
  876. end;
  877. procedure tppufile.putaint(i:aint);
  878. begin
  879. putdata(i,sizeof(aint));
  880. end;
  881. procedure tppufile.putreal(d:ppureal);
  882. begin
  883. putdata(d,sizeof(ppureal));
  884. end;
  885. procedure tppufile.putstring(s:string);
  886. begin
  887. putdata(s,length(s)+1);
  888. end;
  889. procedure tppufile.putsmallset(const b);
  890. var
  891. l : longint;
  892. begin
  893. l:=longint(b);
  894. putlongint(l);
  895. end;
  896. procedure tppufile.putnormalset(const b);
  897. type
  898. SetLongintArray = Array [0..7] of longint;
  899. var
  900. i : longint;
  901. tempb : setlongintarray;
  902. begin
  903. if change_endian then
  904. begin
  905. for i:=0 to 7 do
  906. tempb[i]:=SwapLong(SetLongintArray(b)[i]);
  907. putdata(tempb,32);
  908. end
  909. else
  910. putdata(b,32);
  911. end;
  912. procedure tppufile.tempclose;
  913. begin
  914. if not closed then
  915. begin
  916. closepos:=filepos(f);
  917. {$I-}
  918. system.close(f);
  919. {$I+}
  920. if ioresult<>0 then;
  921. closed:=true;
  922. tempclosed:=true;
  923. end;
  924. end;
  925. function tppufile.tempopen:boolean;
  926. var
  927. ofm : byte;
  928. begin
  929. tempopen:=false;
  930. if not closed or not tempclosed then
  931. exit;
  932. ofm:=filemode;
  933. filemode:=0;
  934. {$I-}
  935. reset(f,1);
  936. {$I+}
  937. filemode:=ofm;
  938. if ioresult<>0 then
  939. exit;
  940. closed:=false;
  941. tempclosed:=false;
  942. { restore state }
  943. seek(f,closepos);
  944. tempopen:=true;
  945. end;
  946. end.