ppu.pas 22 KB

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