ppu.pas 23 KB

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