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=56;
  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/ObjData}
  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_resourcestrings = $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,pmax,pbuf : pchar;
  425. left : integer;
  426. begin
  427. p:=pchar(@b);
  428. pbuf:=@buf[bufidx];
  429. repeat
  430. left:=bufsize-bufidx;
  431. if len<left then
  432. break;
  433. move(pbuf^,p^,left);
  434. dec(len,left);
  435. inc(p,left);
  436. reloadbuf;
  437. pbuf:=@buf[bufidx];
  438. if bufsize=0 then
  439. exit;
  440. until false;
  441. { For small values copy directly }
  442. if len<=sizeof(ptrint) then
  443. begin
  444. pmax:=p+len;
  445. while (p<pmax) do
  446. begin
  447. p^:=pbuf^;
  448. inc(pbuf);
  449. inc(p);
  450. end;
  451. end
  452. else
  453. move(pbuf^,p^,len);
  454. inc(bufidx,len);
  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.