ppu.pas 23 KB

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