ppu.pas 23 KB

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