ppu.pas 23 KB

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