ppu.pas 23 KB

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