ppu.pas 23 KB

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