ppu.pas 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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 = 93;
  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. ibunicodestringdef = 59;
  112. {implementation/ObjData}
  113. ibnodetree = 80;
  114. ibasmsymbols = 81;
  115. ibresources = 82;
  116. ibcreatedobjtypes = 83;
  117. ibmainname = 90;
  118. { target-specific things }
  119. iblinkotherframeworks = 100;
  120. { unit flags }
  121. uf_init = $1;
  122. uf_finalize = $2;
  123. uf_big_endian = $4;
  124. // uf_has_browser = $10;
  125. uf_in_library = $20; { is the file in another file than <ppufile>.* ? }
  126. uf_smart_linked = $40; { the ppu can be smartlinked }
  127. uf_static_linked = $80; { the ppu can be linked static }
  128. uf_shared_linked = $100; { the ppu can be linked shared }
  129. // uf_local_browser = $200;
  130. uf_no_link = $400; { unit has no .o generated, but can still have
  131. external linking! }
  132. uf_has_resourcestrings = $800; { unit has resource string section }
  133. uf_little_endian = $1000;
  134. uf_release = $2000; { unit was compiled with -Ur option }
  135. uf_threadvars = $4000; { unit has threadvars }
  136. uf_fpu_emulation = $8000; { this unit was compiled with fpu emulation on }
  137. uf_has_debuginfo = $10000; { this unit has debuginfo generated }
  138. uf_local_symtable = $20000; { this unit has a local symtable stored }
  139. uf_uses_variants = $40000; { this unit uses variants }
  140. uf_has_resourcefiles = $80000; { this unit has external resources (using $R directive)}
  141. uf_has_exports = $100000; { this module or a used unit has exports }
  142. type
  143. { bestreal is defined based on the target architecture }
  144. ppureal=bestreal;
  145. tppuerror=(ppuentrytoobig,ppuentryerror);
  146. tppuheader=record
  147. id : array[1..3] of char; { = 'PPU' }
  148. ver : array[1..3] of char;
  149. compiler : word;
  150. cpu : word;
  151. target : word;
  152. flags : longint;
  153. size : longint; { size of the ppufile without header }
  154. checksum : cardinal; { checksum for this ppufile }
  155. interface_checksum : cardinal;
  156. deflistsize,
  157. symlistsize : longint;
  158. future : array[0..0] of longint;
  159. end;
  160. tppuentry=packed record
  161. size : longint;
  162. id : byte;
  163. nr : byte;
  164. end;
  165. tppufile=class
  166. private
  167. f : file;
  168. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  169. fname : string;
  170. fsize : integer;
  171. {$ifdef Test_Double_checksum}
  172. public
  173. crcindex,
  174. crc_index,
  175. crcindex2,
  176. crc_index2 : cardinal;
  177. crc_test,
  178. crc_test2 : pcrc_array;
  179. private
  180. {$endif def Test_Double_checksum}
  181. change_endian : boolean;
  182. buf : pchar;
  183. bufstart,
  184. bufsize,
  185. bufidx : integer;
  186. entrybufstart,
  187. entrystart,
  188. entryidx : integer;
  189. entry : tppuentry;
  190. closed,
  191. tempclosed : boolean;
  192. closepos : integer;
  193. public
  194. entrytyp : byte;
  195. header : tppuheader;
  196. size : integer;
  197. crc,
  198. interface_crc : cardinal;
  199. error,
  200. do_crc,
  201. do_interface_crc : boolean;
  202. crc_only : boolean; { used to calculate interface_crc before implementation }
  203. constructor Create(const fn:string);
  204. destructor Destroy;override;
  205. procedure flush;
  206. procedure closefile;
  207. function CheckPPUId:boolean;
  208. function GetPPUVersion:integer;
  209. procedure NewHeader;
  210. procedure NewEntry;
  211. {read}
  212. function openfile:boolean;
  213. procedure reloadbuf;
  214. procedure readdata(var b;len:integer);
  215. procedure skipdata(len:integer);
  216. function readentry:byte;
  217. function EndOfEntry:boolean;
  218. function entrysize:longint;
  219. procedure getdatabuf(var b;len:integer;var res:integer);
  220. procedure getdata(var b;len:integer);
  221. function getbyte:byte;
  222. function getword:word;
  223. function getlongint:longint;
  224. function getint64:int64;
  225. function getaint:aint;
  226. function getreal:ppureal;
  227. function getstring:string;
  228. procedure getnormalset(var b);
  229. procedure getsmallset(var b);
  230. function skipuntilentry(untilb:byte):boolean;
  231. {write}
  232. function createfile:boolean;
  233. procedure writeheader;
  234. procedure writebuf;
  235. procedure writedata(const b;len:integer);
  236. procedure writeentry(ibnr:byte);
  237. procedure putdata(const b;len:integer);
  238. procedure putbyte(b:byte);
  239. procedure putword(w:word);
  240. procedure putlongint(l:longint);
  241. procedure putint64(i:int64);
  242. procedure putaint(i:aint);
  243. procedure putreal(d:ppureal);
  244. procedure putstring(const s:string);
  245. procedure putnormalset(const b);
  246. procedure putsmallset(const b);
  247. procedure tempclose;
  248. function tempopen:boolean;
  249. end;
  250. implementation
  251. uses
  252. systems,
  253. {$ifdef Test_Double_checksum}
  254. comphook,
  255. {$endif def Test_Double_checksum}
  256. fpccrc,
  257. cutils;
  258. function swapendian_ppureal(d:ppureal):ppureal;
  259. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  260. var i:0..sizeof(d)-1;
  261. begin
  262. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  263. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  264. end;
  265. {*****************************************************************************
  266. TPPUFile
  267. *****************************************************************************}
  268. constructor tppufile.Create(const fn:string);
  269. begin
  270. fname:=fn;
  271. change_endian:=false;
  272. crc_only:=false;
  273. Mode:=0;
  274. NewHeader;
  275. Error:=false;
  276. closed:=true;
  277. tempclosed:=false;
  278. getmem(buf,ppubufsize);
  279. end;
  280. destructor tppufile.destroy;
  281. begin
  282. closefile;
  283. if assigned(buf) then
  284. freemem(buf,ppubufsize);
  285. end;
  286. procedure tppufile.flush;
  287. begin
  288. if Mode=2 then
  289. writebuf;
  290. end;
  291. procedure tppufile.closefile;
  292. begin
  293. {$ifdef Test_Double_checksum}
  294. if mode=2 then
  295. begin
  296. if assigned(crc_test) then
  297. dispose(crc_test);
  298. if assigned(crc_test2) then
  299. dispose(crc_test2);
  300. end;
  301. {$endif Test_Double_checksum}
  302. if Mode<>0 then
  303. begin
  304. Flush;
  305. {$I-}
  306. system.close(f);
  307. {$I+}
  308. if ioresult<>0 then;
  309. Mode:=0;
  310. closed:=true;
  311. end;
  312. end;
  313. function tppufile.CheckPPUId:boolean;
  314. begin
  315. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  316. end;
  317. function tppufile.GetPPUVersion:integer;
  318. var
  319. l : integer;
  320. code : integer;
  321. begin
  322. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  323. if code=0 then
  324. GetPPUVersion:=l
  325. else
  326. GetPPUVersion:=0;
  327. end;
  328. procedure tppufile.NewHeader;
  329. var
  330. s : string;
  331. begin
  332. fillchar(header,sizeof(tppuheader),0);
  333. str(currentppuversion,s);
  334. while length(s)<3 do
  335. s:='0'+s;
  336. with header do
  337. begin
  338. Id[1]:='P';
  339. Id[2]:='P';
  340. Id[3]:='U';
  341. Ver[1]:=s[1];
  342. Ver[2]:=s[2];
  343. Ver[3]:=s[3];
  344. end;
  345. end;
  346. {*****************************************************************************
  347. TPPUFile Reading
  348. *****************************************************************************}
  349. function tppufile.openfile:boolean;
  350. var
  351. ofmode : byte;
  352. i : integer;
  353. begin
  354. openfile:=false;
  355. assign(f,fname);
  356. ofmode:=filemode;
  357. filemode:=$0;
  358. {$I-}
  359. reset(f,1);
  360. {$I+}
  361. filemode:=ofmode;
  362. if ioresult<>0 then
  363. exit;
  364. closed:=false;
  365. {read ppuheader}
  366. fsize:=filesize(f);
  367. if fsize<sizeof(tppuheader) then
  368. exit;
  369. blockread(f,header,sizeof(tppuheader),i);
  370. { The header is always stored in little endian order }
  371. { therefore swap if on a big endian machine }
  372. {$IFDEF ENDIAN_BIG}
  373. header.compiler := swapendian(header.compiler);
  374. header.cpu := swapendian(header.cpu);
  375. header.target := swapendian(header.target);
  376. header.flags := swapendian(header.flags);
  377. header.size := swapendian(header.size);
  378. header.checksum := swapendian(header.checksum);
  379. header.interface_checksum := swapendian(header.interface_checksum);
  380. header.deflistsize:=swapendian(header.deflistsize);
  381. header.symlistsize:=swapendian(header.symlistsize);
  382. {$ENDIF}
  383. { the PPU DATA is stored in native order }
  384. if (header.flags and uf_big_endian) = uf_big_endian then
  385. Begin
  386. {$IFDEF ENDIAN_LITTLE}
  387. change_endian := TRUE;
  388. {$ELSE}
  389. change_endian := FALSE;
  390. {$ENDIF}
  391. End
  392. else if (header.flags and uf_little_endian) = uf_little_endian then
  393. Begin
  394. {$IFDEF ENDIAN_BIG}
  395. change_endian := TRUE;
  396. {$ELSE}
  397. change_endian := FALSE;
  398. {$ENDIF}
  399. End;
  400. {reset buffer}
  401. bufstart:=i;
  402. bufsize:=0;
  403. bufidx:=0;
  404. Mode:=1;
  405. FillChar(entry,sizeof(tppuentry),0);
  406. entryidx:=0;
  407. entrystart:=0;
  408. entrybufstart:=0;
  409. Error:=false;
  410. openfile:=true;
  411. end;
  412. procedure tppufile.reloadbuf;
  413. begin
  414. inc(bufstart,bufsize);
  415. blockread(f,buf^,ppubufsize,bufsize);
  416. bufidx:=0;
  417. end;
  418. procedure tppufile.readdata(var b;len:integer);
  419. var
  420. p,pbuf : pchar;
  421. left : integer;
  422. begin
  423. p:=pchar(@b);
  424. pbuf:=@buf[bufidx];
  425. repeat
  426. left:=bufsize-bufidx;
  427. if len<left then
  428. break;
  429. move(pbuf^,p^,left);
  430. dec(len,left);
  431. inc(p,left);
  432. reloadbuf;
  433. pbuf:=@buf[bufidx];
  434. if bufsize=0 then
  435. exit;
  436. until false;
  437. move(pbuf^,p^,len);
  438. inc(bufidx,len);
  439. end;
  440. procedure tppufile.skipdata(len:integer);
  441. var
  442. left : integer;
  443. begin
  444. while len>0 do
  445. begin
  446. left:=bufsize-bufidx;
  447. if len>left then
  448. begin
  449. dec(len,left);
  450. reloadbuf;
  451. if bufsize=0 then
  452. exit;
  453. end
  454. else
  455. begin
  456. inc(bufidx,len);
  457. exit;
  458. end;
  459. end;
  460. end;
  461. function tppufile.readentry:byte;
  462. begin
  463. if entryidx<entry.size then
  464. skipdata(entry.size-entryidx);
  465. readdata(entry,sizeof(tppuentry));
  466. if change_endian then
  467. entry.size:=swapendian(entry.size);
  468. entrystart:=bufstart+bufidx;
  469. entryidx:=0;
  470. if not(entry.id in [mainentryid,subentryid]) then
  471. begin
  472. readentry:=iberror;
  473. error:=true;
  474. exit;
  475. end;
  476. readentry:=entry.nr;
  477. end;
  478. function tppufile.endofentry:boolean;
  479. begin
  480. endofentry:=(entryidx>=entry.size);
  481. end;
  482. function tppufile.entrysize:longint;
  483. begin
  484. entrysize:=entry.size;
  485. end;
  486. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  487. begin
  488. if entryidx+len>entry.size then
  489. res:=entry.size-entryidx
  490. else
  491. res:=len;
  492. readdata(b,res);
  493. inc(entryidx,res);
  494. end;
  495. procedure tppufile.getdata(var b;len:integer);
  496. begin
  497. if entryidx+len>entry.size then
  498. begin
  499. error:=true;
  500. exit;
  501. end;
  502. readdata(b,len);
  503. inc(entryidx,len);
  504. end;
  505. function tppufile.getbyte:byte;
  506. begin
  507. if entryidx+1>entry.size then
  508. begin
  509. error:=true;
  510. result:=0;
  511. exit;
  512. end;
  513. if bufsize-bufidx>=1 then
  514. begin
  515. result:=pbyte(@buf[bufidx])^;
  516. inc(bufidx);
  517. end
  518. else
  519. readdata(result,1);
  520. inc(entryidx);
  521. end;
  522. function tppufile.getword:word;
  523. begin
  524. if entryidx+2>entry.size then
  525. begin
  526. error:=true;
  527. result:=0;
  528. exit;
  529. end;
  530. {$ifdef FPC_UNALIGNED_FIXED}
  531. if bufsize-bufidx>=sizeof(word) then
  532. begin
  533. result:=Unaligned(pword(@buf[bufidx])^);
  534. inc(bufidx,sizeof(word));
  535. end
  536. else
  537. {$endif FPC_UNALIGNED_FIXED}
  538. readdata(result,sizeof(word));
  539. if change_endian then
  540. result:=swapendian(result);
  541. inc(entryidx,2);
  542. end;
  543. function tppufile.getlongint:longint;
  544. begin
  545. if entryidx+4>entry.size then
  546. begin
  547. error:=true;
  548. getlongint:=0;
  549. exit;
  550. end;
  551. {$ifdef FPC_UNALIGNED_FIXED}
  552. if bufsize-bufidx>=sizeof(longint) then
  553. begin
  554. result:=Unaligned(plongint(@buf[bufidx])^);
  555. inc(bufidx,sizeof(longint));
  556. end
  557. else
  558. {$endif FPC_UNALIGNED_FIXED}
  559. readdata(result,sizeof(longint));
  560. if change_endian then
  561. result:=swapendian(result);
  562. inc(entryidx,4);
  563. end;
  564. function tppufile.getint64:int64;
  565. begin
  566. if entryidx+8>entry.size then
  567. begin
  568. error:=true;
  569. result:=0;
  570. exit;
  571. end;
  572. {$ifdef FPC_UNALIGNED_FIXED}
  573. if bufsize-bufidx>=sizeof(int64) then
  574. begin
  575. result:=Unaligned(pint64(@buf[bufidx])^);
  576. inc(bufidx,sizeof(int64));
  577. end
  578. else
  579. {$endif FPC_UNALIGNED_FIXED}
  580. readdata(result,sizeof(int64));
  581. if change_endian then
  582. result:=swapendian(result);
  583. inc(entryidx,8);
  584. end;
  585. function tppufile.getaint:aint;
  586. begin
  587. {$ifdef cpu64bitalu}
  588. result:=getint64;
  589. {$else cpu64bitalu}
  590. result:=getlongint;
  591. {$endif cpu64bitalu}
  592. end;
  593. function tppufile.getreal:ppureal;
  594. var
  595. d : ppureal;
  596. hd : double;
  597. begin
  598. if target_info.system=system_x86_64_win64 then
  599. begin
  600. if entryidx+sizeof(hd)>entry.size then
  601. begin
  602. error:=true;
  603. getreal:=0;
  604. exit;
  605. end;
  606. readdata(hd,sizeof(hd));
  607. if change_endian then
  608. getreal:=swapendian(qword(hd))
  609. else
  610. getreal:=hd;
  611. inc(entryidx,sizeof(hd));
  612. end
  613. else
  614. begin
  615. if entryidx+sizeof(ppureal)>entry.size then
  616. begin
  617. error:=true;
  618. getreal:=0;
  619. exit;
  620. end;
  621. readdata(d,sizeof(ppureal));
  622. if change_endian then
  623. getreal:=swapendian_ppureal(d)
  624. else
  625. getreal:=d;
  626. inc(entryidx,sizeof(ppureal));
  627. end;
  628. end;
  629. function tppufile.getstring:string;
  630. var
  631. s : string;
  632. begin
  633. s[0]:=chr(getbyte);
  634. if entryidx+length(s)>entry.size then
  635. begin
  636. error:=true;
  637. exit;
  638. end;
  639. ReadData(s[1],length(s));
  640. getstring:=s;
  641. inc(entryidx,length(s));
  642. end;
  643. procedure tppufile.getsmallset(var b);
  644. var
  645. i : longint;
  646. begin
  647. getdata(b,4);
  648. if change_endian then
  649. for i:=0 to 3 do
  650. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  651. end;
  652. procedure tppufile.getnormalset(var b);
  653. var
  654. i : longint;
  655. begin
  656. getdata(b,32);
  657. if change_endian then
  658. for i:=0 to 31 do
  659. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  660. end;
  661. function tppufile.skipuntilentry(untilb:byte):boolean;
  662. var
  663. b : byte;
  664. begin
  665. repeat
  666. b:=readentry;
  667. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  668. skipuntilentry:=(b=untilb);
  669. end;
  670. {*****************************************************************************
  671. TPPUFile Writing
  672. *****************************************************************************}
  673. function tppufile.createfile:boolean;
  674. begin
  675. createfile:=false;
  676. {$ifdef INTFPPU}
  677. if crc_only then
  678. begin
  679. fname:=fname+'.intf';
  680. crc_only:=false;
  681. end;
  682. {$endif}
  683. if not crc_only then
  684. begin
  685. assign(f,fname);
  686. {$ifdef MACOS}
  687. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  688. SetDefaultMacOSCreator('FPas');
  689. SetDefaultMacOSFiletype('FPPU');
  690. {$endif}
  691. {$I-}
  692. rewrite(f,1);
  693. {$I+}
  694. {$ifdef MACOS}
  695. SetDefaultMacOSCreator('MPS ');
  696. SetDefaultMacOSFiletype('TEXT');
  697. {$endif}
  698. if ioresult<>0 then
  699. exit;
  700. Mode:=2;
  701. {write header for sure}
  702. blockwrite(f,header,sizeof(tppuheader));
  703. end;
  704. bufsize:=ppubufsize;
  705. bufstart:=sizeof(tppuheader);
  706. bufidx:=0;
  707. {reset}
  708. crc:=0;
  709. interface_crc:=0;
  710. do_interface_crc:=true;
  711. Error:=false;
  712. do_crc:=true;
  713. size:=0;
  714. entrytyp:=mainentryid;
  715. {start}
  716. NewEntry;
  717. createfile:=true;
  718. end;
  719. procedure tppufile.writeheader;
  720. var
  721. opos : integer;
  722. begin
  723. if crc_only then
  724. exit;
  725. { flush buffer }
  726. writebuf;
  727. { update size (w/o header!) in the header }
  728. header.size:=bufstart-sizeof(tppuheader);
  729. { set the endian flag }
  730. {$ifndef FPC_BIG_ENDIAN}
  731. header.flags := header.flags or uf_little_endian;
  732. {$else not FPC_BIG_ENDIAN}
  733. header.flags := header.flags or uf_big_endian;
  734. { Now swap the header in the correct endian (always little endian) }
  735. header.compiler := swapendian(header.compiler);
  736. header.cpu := swapendian(header.cpu);
  737. header.target := swapendian(header.target);
  738. header.flags := swapendian(header.flags);
  739. header.size := swapendian(header.size);
  740. header.checksum := swapendian(header.checksum);
  741. header.interface_checksum := swapendian(header.interface_checksum);
  742. header.deflistsize:=swapendian(header.deflistsize);
  743. header.symlistsize:=swapendian(header.symlistsize);
  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(const 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. begin
  935. putdata(b,32);
  936. end;
  937. procedure tppufile.tempclose;
  938. begin
  939. if not closed then
  940. begin
  941. closepos:=filepos(f);
  942. {$I-}
  943. system.close(f);
  944. {$I+}
  945. if ioresult<>0 then;
  946. closed:=true;
  947. tempclosed:=true;
  948. end;
  949. end;
  950. function tppufile.tempopen:boolean;
  951. var
  952. ofm : byte;
  953. begin
  954. tempopen:=false;
  955. if not closed or not tempclosed then
  956. exit;
  957. ofm:=filemode;
  958. filemode:=0;
  959. {$I-}
  960. reset(f,1);
  961. {$I+}
  962. filemode:=ofm;
  963. if ioresult<>0 then
  964. exit;
  965. closed:=false;
  966. tempclosed:=false;
  967. { restore state }
  968. seek(f,closepos);
  969. tempopen:=true;
  970. end;
  971. end.