ppu.pas 23 KB

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