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