ppu.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 = 87;
  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. move(pbuf^,p^,len);
  434. inc(bufidx,len);
  435. end;
  436. procedure tppufile.skipdata(len:integer);
  437. var
  438. left : integer;
  439. begin
  440. while len>0 do
  441. begin
  442. left:=bufsize-bufidx;
  443. if len>left then
  444. begin
  445. dec(len,left);
  446. reloadbuf;
  447. if bufsize=0 then
  448. exit;
  449. end
  450. else
  451. begin
  452. inc(bufidx,len);
  453. exit;
  454. end;
  455. end;
  456. end;
  457. function tppufile.readentry:byte;
  458. begin
  459. if entryidx<entry.size then
  460. skipdata(entry.size-entryidx);
  461. readdata(entry,sizeof(tppuentry));
  462. if change_endian then
  463. entry.size:=swapendian(entry.size);
  464. entrystart:=bufstart+bufidx;
  465. entryidx:=0;
  466. if not(entry.id in [mainentryid,subentryid]) then
  467. begin
  468. readentry:=iberror;
  469. error:=true;
  470. exit;
  471. end;
  472. readentry:=entry.nr;
  473. end;
  474. function tppufile.endofentry:boolean;
  475. begin
  476. endofentry:=(entryidx>=entry.size);
  477. end;
  478. function tppufile.entrysize:longint;
  479. begin
  480. entrysize:=entry.size;
  481. end;
  482. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  483. begin
  484. if entryidx+len>entry.size then
  485. res:=entry.size-entryidx
  486. else
  487. res:=len;
  488. readdata(b,res);
  489. inc(entryidx,res);
  490. end;
  491. procedure tppufile.getdata(var b;len:integer);
  492. begin
  493. if entryidx+len>entry.size then
  494. begin
  495. error:=true;
  496. exit;
  497. end;
  498. readdata(b,len);
  499. inc(entryidx,len);
  500. end;
  501. function tppufile.getbyte:byte;
  502. begin
  503. if entryidx+1>entry.size then
  504. begin
  505. error:=true;
  506. result:=0;
  507. exit;
  508. end;
  509. if bufsize-bufidx>=1 then
  510. begin
  511. result:=pbyte(@buf[bufidx])^;
  512. inc(bufidx);
  513. end
  514. else
  515. readdata(result,1);
  516. inc(entryidx);
  517. end;
  518. function tppufile.getword:word;
  519. begin
  520. if entryidx+2>entry.size then
  521. begin
  522. error:=true;
  523. result:=0;
  524. exit;
  525. end;
  526. {$ifdef FPC_UNALIGNED_FIXED}
  527. if bufsize-bufidx>=sizeof(word) then
  528. begin
  529. result:=Unaligned(pword(@buf[bufidx]))^;
  530. inc(bufidx,sizeof(word));
  531. end
  532. else
  533. {$endif FPC_UNALIGNED_FIXED}
  534. readdata(result,sizeof(word));
  535. if change_endian then
  536. result:=swapendian(result);
  537. inc(entryidx,2);
  538. end;
  539. function tppufile.getlongint:longint;
  540. begin
  541. if entryidx+4>entry.size then
  542. begin
  543. error:=true;
  544. getlongint:=0;
  545. exit;
  546. end;
  547. {$ifdef FPC_UNALIGNED_FIXED}
  548. if bufsize-bufidx>=sizeof(longint) then
  549. begin
  550. result:=Unaligned(plongint(@buf[bufidx]))^;
  551. inc(bufidx,sizeof(longint));
  552. end
  553. else
  554. {$endif FPC_UNALIGNED_FIXED}
  555. readdata(result,sizeof(longint));
  556. if change_endian then
  557. result:=swapendian(result);
  558. inc(entryidx,4);
  559. end;
  560. function tppufile.getint64:int64;
  561. begin
  562. if entryidx+8>entry.size then
  563. begin
  564. error:=true;
  565. result:=0;
  566. exit;
  567. end;
  568. {$ifdef FPC_UNALIGNED_FIXED}
  569. if bufsize-bufidx>=sizeof(int64) then
  570. begin
  571. result:=Unaligned(pint64(@buf[bufidx])^);
  572. inc(bufidx,sizeof(int64));
  573. end
  574. else
  575. {$endif FPC_UNALIGNED_FIXED}
  576. readdata(result,sizeof(int64));
  577. if change_endian then
  578. result:=swapendian(result);
  579. inc(entryidx,8);
  580. end;
  581. function tppufile.getaint:aint;
  582. begin
  583. {$ifdef cpu64bit}
  584. result:=getint64;
  585. {$else cpu64bit}
  586. result:=getlongint;
  587. {$endif cpu64bit}
  588. end;
  589. function tppufile.getreal:ppureal;
  590. var
  591. d : ppureal;
  592. hd : double;
  593. begin
  594. if target_info.system=system_x86_64_win64 then
  595. begin
  596. if entryidx+sizeof(hd)>entry.size then
  597. begin
  598. error:=true;
  599. getreal:=0;
  600. exit;
  601. end;
  602. readdata(hd,sizeof(hd));
  603. if change_endian then
  604. getreal:=swapendian(qword(hd))
  605. else
  606. getreal:=hd;
  607. inc(entryidx,sizeof(hd));
  608. end
  609. else
  610. begin
  611. if entryidx+sizeof(ppureal)>entry.size then
  612. begin
  613. error:=true;
  614. getreal:=0;
  615. exit;
  616. end;
  617. readdata(d,sizeof(ppureal));
  618. if change_endian then
  619. getreal:=swapendian_ppureal(d)
  620. else
  621. getreal:=d;
  622. inc(entryidx,sizeof(ppureal));
  623. end;
  624. end;
  625. function tppufile.getstring:string;
  626. var
  627. s : string;
  628. begin
  629. s[0]:=chr(getbyte);
  630. if entryidx+length(s)>entry.size then
  631. begin
  632. error:=true;
  633. exit;
  634. end;
  635. ReadData(s[1],length(s));
  636. getstring:=s;
  637. inc(entryidx,length(s));
  638. end;
  639. procedure tppufile.getsmallset(var b);
  640. var
  641. i : longint;
  642. begin
  643. getdata(b,4);
  644. if change_endian then
  645. for i:=0 to 3 do
  646. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  647. end;
  648. procedure tppufile.getnormalset(var b);
  649. var
  650. i : longint;
  651. begin
  652. getdata(b,32);
  653. if change_endian then
  654. for i:=0 to 31 do
  655. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  656. end;
  657. function tppufile.skipuntilentry(untilb:byte):boolean;
  658. var
  659. b : byte;
  660. begin
  661. repeat
  662. b:=readentry;
  663. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  664. skipuntilentry:=(b=untilb);
  665. end;
  666. {*****************************************************************************
  667. TPPUFile Writing
  668. *****************************************************************************}
  669. function tppufile.createfile:boolean;
  670. begin
  671. createfile:=false;
  672. {$ifdef INTFPPU}
  673. if crc_only then
  674. begin
  675. fname:=fname+'.intf';
  676. crc_only:=false;
  677. end;
  678. {$endif}
  679. if not crc_only then
  680. begin
  681. assign(f,fname);
  682. {$ifdef MACOS}
  683. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  684. SetDefaultMacOSCreator('FPas');
  685. SetDefaultMacOSFiletype('FPPU');
  686. {$endif}
  687. {$I-}
  688. rewrite(f,1);
  689. {$I+}
  690. {$ifdef MACOS}
  691. SetDefaultMacOSCreator('MPS ');
  692. SetDefaultMacOSFiletype('TEXT');
  693. {$endif}
  694. if ioresult<>0 then
  695. exit;
  696. Mode:=2;
  697. {write header for sure}
  698. blockwrite(f,header,sizeof(tppuheader));
  699. end;
  700. bufsize:=ppubufsize;
  701. bufstart:=sizeof(tppuheader);
  702. bufidx:=0;
  703. {reset}
  704. crc:=cardinal($ffffffff);
  705. interface_crc:=cardinal($ffffffff);
  706. do_interface_crc:=true;
  707. Error:=false;
  708. do_crc:=true;
  709. size:=0;
  710. entrytyp:=mainentryid;
  711. {start}
  712. NewEntry;
  713. createfile:=true;
  714. end;
  715. procedure tppufile.writeheader;
  716. var
  717. opos : integer;
  718. begin
  719. if crc_only then
  720. exit;
  721. { flush buffer }
  722. writebuf;
  723. { update size (w/o header!) in the header }
  724. header.size:=bufstart-sizeof(tppuheader);
  725. { set the endian flag }
  726. {$ifndef FPC_BIG_ENDIAN}
  727. header.flags := header.flags or uf_little_endian;
  728. {$else not FPC_BIG_ENDIAN}
  729. header.flags := header.flags or uf_big_endian;
  730. { Now swap the header in the correct endian (always little endian) }
  731. header.compiler := swapendian(header.compiler);
  732. header.cpu := swapendian(header.cpu);
  733. header.target := swapendian(header.target);
  734. header.flags := swapendian(header.flags);
  735. header.size := swapendian(header.size);
  736. header.checksum := swapendian(header.checksum);
  737. header.interface_checksum := swapendian(header.interface_checksum);
  738. header.deflistsize:=swapendian(header.deflistsize);
  739. header.symlistsize:=swapendian(header.symlistsize);
  740. {$endif not FPC_BIG_ENDIAN}
  741. { write header and restore filepos after it }
  742. opos:=filepos(f);
  743. seek(f,0);
  744. blockwrite(f,header,sizeof(tppuheader));
  745. seek(f,opos);
  746. end;
  747. procedure tppufile.writebuf;
  748. begin
  749. if not crc_only and
  750. (bufidx <> 0) then
  751. blockwrite(f,buf^,bufidx);
  752. inc(bufstart,bufidx);
  753. bufidx:=0;
  754. end;
  755. procedure tppufile.writedata(const b;len:integer);
  756. var
  757. p : pchar;
  758. left,
  759. idx : integer;
  760. begin
  761. if crc_only then
  762. exit;
  763. p:=pchar(@b);
  764. idx:=0;
  765. while len>0 do
  766. begin
  767. left:=bufsize-bufidx;
  768. if len>left then
  769. begin
  770. move(p[idx],buf[bufidx],left);
  771. dec(len,left);
  772. inc(idx,left);
  773. inc(bufidx,left);
  774. writebuf;
  775. end
  776. else
  777. begin
  778. move(p[idx],buf[bufidx],len);
  779. inc(bufidx,len);
  780. exit;
  781. end;
  782. end;
  783. end;
  784. procedure tppufile.NewEntry;
  785. begin
  786. with entry do
  787. begin
  788. id:=entrytyp;
  789. nr:=ibend;
  790. size:=0;
  791. end;
  792. {Reset Entry State}
  793. entryidx:=0;
  794. entrybufstart:=bufstart;
  795. entrystart:=bufstart+bufidx;
  796. {Alloc in buffer}
  797. writedata(entry,sizeof(tppuentry));
  798. end;
  799. procedure tppufile.writeentry(ibnr:byte);
  800. var
  801. opos : integer;
  802. begin
  803. {create entry}
  804. entry.id:=entrytyp;
  805. entry.nr:=ibnr;
  806. entry.size:=entryidx;
  807. {it's already been sent to disk ?}
  808. if entrybufstart<>bufstart then
  809. begin
  810. if not crc_only then
  811. begin
  812. {flush to be sure}
  813. WriteBuf;
  814. {write entry}
  815. opos:=filepos(f);
  816. seek(f,entrystart);
  817. blockwrite(f,entry,sizeof(tppuentry));
  818. seek(f,opos);
  819. end;
  820. entrybufstart:=bufstart;
  821. end
  822. else
  823. move(entry,buf[entrystart-bufstart],sizeof(entry));
  824. {Add New Entry, which is ibend by default}
  825. entrystart:=bufstart+bufidx; {next entry position}
  826. NewEntry;
  827. end;
  828. procedure tppufile.putdata(const b;len:integer);
  829. begin
  830. if do_crc then
  831. begin
  832. crc:=UpdateCrc32(crc,b,len);
  833. {$ifdef Test_Double_checksum}
  834. if crc_only then
  835. begin
  836. crc_test2^[crc_index2]:=crc;
  837. {$ifdef Test_Double_checksum_write}
  838. Writeln(CRCFile,crc);
  839. {$endif Test_Double_checksum_write}
  840. if crc_index2<crc_array_size then
  841. inc(crc_index2);
  842. end
  843. else
  844. begin
  845. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  846. (crc_test2^[crcindex2]<>crc) then
  847. Do_comment(V_Note,'impl CRC changed');
  848. {$ifdef Test_Double_checksum_write}
  849. Writeln(CRCFile,crc);
  850. {$endif Test_Double_checksum_write}
  851. inc(crcindex2);
  852. end;
  853. {$endif def Test_Double_checksum}
  854. if do_interface_crc then
  855. begin
  856. interface_crc:=UpdateCrc32(interface_crc,b,len);
  857. {$ifdef Test_Double_checksum}
  858. if crc_only then
  859. begin
  860. crc_test^[crc_index]:=interface_crc;
  861. {$ifdef Test_Double_checksum_write}
  862. Writeln(CRCFile,interface_crc);
  863. {$endif Test_Double_checksum_write}
  864. if crc_index<crc_array_size then
  865. inc(crc_index);
  866. end
  867. else
  868. begin
  869. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  870. (crc_test^[crcindex]<>interface_crc) then
  871. Do_comment(V_Warning,'CRC changed');
  872. {$ifdef Test_Double_checksum_write}
  873. Writeln(CRCFile,interface_crc);
  874. {$endif Test_Double_checksum_write}
  875. inc(crcindex);
  876. end;
  877. {$endif def Test_Double_checksum}
  878. end;
  879. end;
  880. if not crc_only then
  881. writedata(b,len);
  882. inc(entryidx,len);
  883. end;
  884. procedure tppufile.putbyte(b:byte);
  885. begin
  886. putdata(b,1);
  887. end;
  888. procedure tppufile.putword(w:word);
  889. begin
  890. putdata(w,2);
  891. end;
  892. procedure tppufile.putlongint(l:longint);
  893. begin
  894. putdata(l,4);
  895. end;
  896. procedure tppufile.putint64(i:int64);
  897. begin
  898. putdata(i,8);
  899. end;
  900. procedure tppufile.putaint(i:aint);
  901. begin
  902. putdata(i,sizeof(aint));
  903. end;
  904. procedure tppufile.putreal(d:ppureal);
  905. var
  906. hd : double;
  907. begin
  908. if target_info.system=system_x86_64_win64 then
  909. begin
  910. hd:=d;
  911. putdata(hd,sizeof(hd));
  912. end
  913. else
  914. putdata(d,sizeof(ppureal));
  915. end;
  916. procedure tppufile.putstring(s:string);
  917. begin
  918. putdata(s,length(s)+1);
  919. end;
  920. procedure tppufile.putsmallset(const b);
  921. var
  922. l : longint;
  923. begin
  924. l:=longint(b);
  925. putlongint(l);
  926. end;
  927. procedure tppufile.putnormalset(const b);
  928. type
  929. SetLongintArray = Array [0..7] of longint;
  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.