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