ppu.pas 23 KB

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