ppu.pas 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. systems,globtype,constexp,cstreams;
  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 dword;
  32. pcrc_array = ^tcrc_array;
  33. {$endif Test_Double_checksum}
  34. const
  35. CurrentPPUVersion = 153;
  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. ibnamespacesym = 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. ibmoduleoptions = 85;
  119. ibmainname = 90;
  120. ibsymtableoptions = 91;
  121. ibrecsymtableoptions = 91;
  122. { target-specific things }
  123. iblinkotherframeworks = 100;
  124. ibjvmnamespace = 101;
  125. { unit flags }
  126. uf_init = $000001; { unit has initialization section }
  127. uf_finalize = $000002; { unit has finalization section }
  128. uf_big_endian = $000004;
  129. //uf_has_browser = $000010;
  130. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  131. uf_smart_linked = $000040; { the ppu can be smartlinked }
  132. uf_static_linked = $000080; { the ppu can be linked static }
  133. uf_shared_linked = $000100; { the ppu can be linked shared }
  134. //uf_local_browser = $000200;
  135. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  136. uf_has_resourcestrings = $000800; { unit has resource string section }
  137. uf_little_endian = $001000;
  138. uf_release = $002000; { unit was compiled with -Ur option }
  139. uf_threadvars = $004000; { unit has threadvars }
  140. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  141. uf_has_stabs_debuginfo = $010000; { this unit has stabs debuginfo generated }
  142. uf_local_symtable = $020000; { this unit has a local symtable stored }
  143. uf_uses_variants = $040000; { this unit uses variants }
  144. uf_has_resourcefiles = $080000; { this unit has external resources (using $R directive)}
  145. uf_has_exports = $100000; { this module or a used unit has exports }
  146. uf_has_dwarf_debuginfo = $200000; { this unit has dwarf debuginfo generated }
  147. uf_wideinits = $400000; { this unit has winlike widestring typed constants }
  148. uf_classinits = $800000; { this unit has class constructors/destructors }
  149. uf_resstrinits = $1000000; { this unit has string consts referencing resourcestrings }
  150. {$ifdef generic_cpu}
  151. { We need to use the correct size of aint and pint for
  152. the target CPU }
  153. const
  154. CpuAddrBitSize : array[tsystemcpu] of longint =
  155. (
  156. { 0 } 32 {'none'},
  157. { 1 } 32 {'i386'},
  158. { 2 } 32 {'m68k'},
  159. { 3 } 32 {'alpha'},
  160. { 4 } 32 {'powerpc'},
  161. { 5 } 32 {'sparc'},
  162. { 6 } 32 {'vis'},
  163. { 7 } 64 {'ia64'},
  164. { 8 } 64 {'x86_64'},
  165. { 9 } 32 {'mipseb'},
  166. { 10 } 32 {'arm'},
  167. { 11 } 64 {'powerpc64'},
  168. { 12 } 16 {'avr'},
  169. { 13 } 32 {'mipsel'},
  170. { 14 } 32 {'jvm'}
  171. );
  172. CpuAluBitSize : array[tsystemcpu] of longint =
  173. (
  174. { 0 } 32 {'none'},
  175. { 1 } 32 {'i386'},
  176. { 2 } 32 {'m68k'},
  177. { 3 } 32 {'alpha'},
  178. { 4 } 32 {'powerpc'},
  179. { 5 } 32 {'sparc'},
  180. { 6 } 32 {'vis'},
  181. { 7 } 64 {'ia64'},
  182. { 8 } 64 {'x86_64'},
  183. { 9 } 32 {'mipseb'},
  184. { 10 } 32 {'arm'},
  185. { 11 } 64 {'powerpc64'},
  186. { 12 } 8 {'avr'},
  187. { 13 } 32 {'mipsel'},
  188. { 14 } 64 {'jvm'}
  189. );
  190. {$endif generic_cpu}
  191. type
  192. { bestreal is defined based on the target architecture }
  193. ppureal=bestreal;
  194. tppuerror=(ppuentrytoobig,ppuentryerror);
  195. tppuheader=record
  196. id : array[1..3] of char; { = 'PPU' }
  197. ver : array[1..3] of char;
  198. compiler : word;
  199. cpu : word;
  200. target : word;
  201. flags : longint;
  202. size : longint; { size of the ppufile without header }
  203. checksum : cardinal; { checksum for this ppufile }
  204. interface_checksum : cardinal;
  205. deflistsize,
  206. symlistsize : longint;
  207. indirect_checksum: cardinal;
  208. end;
  209. tppuentry=packed record
  210. size : longint;
  211. id : byte;
  212. nr : byte;
  213. end;
  214. { tppufile }
  215. tppufile=class
  216. private
  217. f : TCCustomFileStream;
  218. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  219. fname : string;
  220. fsize : integer;
  221. {$ifdef Test_Double_checksum}
  222. public
  223. crcindex,
  224. crc_index,
  225. crcindex2,
  226. crc_index2 : cardinal;
  227. crc_test,
  228. crc_test2 : pcrc_array;
  229. private
  230. {$endif def Test_Double_checksum}
  231. buf : pchar;
  232. bufstart,
  233. bufsize,
  234. bufidx : integer;
  235. entrybufstart,
  236. entrystart,
  237. entryidx : integer;
  238. entry : tppuentry;
  239. closed,
  240. tempclosed : boolean;
  241. closepos : integer;
  242. public
  243. entrytyp : byte;
  244. header : tppuheader;
  245. size : integer;
  246. change_endian : boolean; { Used in ppudump util }
  247. { crc for the entire unit }
  248. crc,
  249. { crc for the interface definitions in this unit }
  250. interface_crc,
  251. { crc of all object/class definitions in the interface of this unit, xor'ed
  252. by the crc's of all object/class definitions in the interfaces of units
  253. used by this unit. Reason: see mantis #13840 }
  254. indirect_crc : cardinal;
  255. error,
  256. {$ifdef generic_cpu}
  257. has_more,
  258. {$endif not generic_cpu}
  259. do_crc,
  260. do_interface_crc,
  261. do_indirect_crc : boolean;
  262. crc_only : boolean; { used to calculate interface_crc before implementation }
  263. constructor Create(const fn:string);
  264. destructor Destroy;override;
  265. procedure flush;
  266. procedure closefile;
  267. function CheckPPUId:boolean;
  268. function GetPPUVersion:integer;
  269. procedure NewHeader;
  270. procedure NewEntry;
  271. {read}
  272. function openfile:boolean;
  273. procedure reloadbuf;
  274. procedure readdata(out b;len:integer);
  275. procedure skipdata(len:integer);
  276. function readentry:byte;
  277. function EndOfEntry:boolean;
  278. function entrysize:longint;
  279. function entryleft:longint;
  280. procedure getdatabuf(out b;len:integer;out res:integer);
  281. procedure getdata(out b;len:integer);
  282. function getbyte:byte;
  283. function getword:word;
  284. function getdword:dword;
  285. function getlongint:longint;
  286. function getint64:int64;
  287. function getqword:qword;
  288. function getaint:aint;
  289. function getasizeint:asizeint;
  290. function getaword:aword;
  291. function getreal:ppureal;
  292. function getrealsize(sizeofreal : longint):ppureal;
  293. function getstring:string;
  294. function getansistring:ansistring;
  295. procedure getnormalset(out b);
  296. procedure getsmallset(out b);
  297. function skipuntilentry(untilb:byte):boolean;
  298. {write}
  299. function createfile:boolean;
  300. procedure writeheader;
  301. procedure writebuf;
  302. procedure writedata(const b;len:integer);
  303. procedure writeentry(ibnr:byte);
  304. procedure putdata(const b;len:integer);
  305. procedure putbyte(b:byte);
  306. procedure putword(w:word);
  307. procedure putdword(w:dword);
  308. procedure putlongint(l:longint);
  309. procedure putint64(i:int64);
  310. procedure putqword(q:qword);
  311. procedure putaint(i:aint);
  312. procedure putasizeint(i:asizeint);
  313. procedure putaword(i:aword);
  314. procedure putreal(d:ppureal);
  315. procedure putstring(const s:string);
  316. procedure putansistring(const s:ansistring);
  317. procedure putnormalset(const b);
  318. procedure putsmallset(const b);
  319. procedure tempclose; // MG: not used, obsolete?
  320. function tempopen:boolean; // MG: not used, obsolete?
  321. end;
  322. implementation
  323. uses
  324. {$ifdef Test_Double_checksum}
  325. comphook,
  326. {$endif def Test_Double_checksum}
  327. fpccrc,
  328. cutils;
  329. function swapendian_ppureal(d:ppureal):ppureal;
  330. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  331. var i:0..sizeof(d)-1;
  332. begin
  333. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  334. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  335. end;
  336. {*****************************************************************************
  337. TPPUFile
  338. *****************************************************************************}
  339. constructor tppufile.Create(const fn:string);
  340. begin
  341. fname:=fn;
  342. change_endian:=false;
  343. crc_only:=false;
  344. Mode:=0;
  345. NewHeader;
  346. Error:=false;
  347. closed:=true;
  348. tempclosed:=false;
  349. getmem(buf,ppubufsize);
  350. end;
  351. destructor tppufile.destroy;
  352. begin
  353. closefile;
  354. if assigned(buf) then
  355. freemem(buf,ppubufsize);
  356. end;
  357. procedure tppufile.flush;
  358. begin
  359. if Mode=2 then
  360. writebuf;
  361. end;
  362. procedure tppufile.closefile;
  363. begin
  364. {$ifdef Test_Double_checksum}
  365. if mode=2 then
  366. begin
  367. if assigned(crc_test) then
  368. dispose(crc_test);
  369. if assigned(crc_test2) then
  370. dispose(crc_test2);
  371. end;
  372. {$endif Test_Double_checksum}
  373. if Mode<>0 then
  374. begin
  375. Flush;
  376. f.Free;
  377. Mode:=0;
  378. closed:=true;
  379. end;
  380. end;
  381. function tppufile.CheckPPUId:boolean;
  382. begin
  383. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  384. end;
  385. function tppufile.GetPPUVersion:integer;
  386. var
  387. l : integer;
  388. code : integer;
  389. begin
  390. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  391. if code=0 then
  392. GetPPUVersion:=l
  393. else
  394. GetPPUVersion:=0;
  395. end;
  396. procedure tppufile.NewHeader;
  397. var
  398. s : string;
  399. begin
  400. fillchar(header,sizeof(tppuheader),0);
  401. str(currentppuversion,s);
  402. while length(s)<3 do
  403. s:='0'+s;
  404. with header do
  405. begin
  406. Id[1]:='P';
  407. Id[2]:='P';
  408. Id[3]:='U';
  409. Ver[1]:=s[1];
  410. Ver[2]:=s[2];
  411. Ver[3]:=s[3];
  412. end;
  413. end;
  414. {*****************************************************************************
  415. TPPUFile Reading
  416. *****************************************************************************}
  417. function tppufile.openfile:boolean;
  418. var
  419. i : integer;
  420. begin
  421. openfile:=false;
  422. try
  423. f:=CFileStreamClass.Create(fname,fmOpenRead)
  424. except
  425. exit;
  426. end;
  427. closed:=false;
  428. {read ppuheader}
  429. fsize:=f.Size;
  430. if fsize<sizeof(tppuheader) then
  431. exit;
  432. i:=f.Read(header,sizeof(tppuheader));
  433. { The header is always stored in little endian order }
  434. { therefore swap if on a big endian machine }
  435. {$IFDEF ENDIAN_BIG}
  436. header.compiler := swapendian(header.compiler);
  437. header.cpu := swapendian(header.cpu);
  438. header.target := swapendian(header.target);
  439. header.flags := swapendian(header.flags);
  440. header.size := swapendian(header.size);
  441. header.checksum := swapendian(header.checksum);
  442. header.interface_checksum := swapendian(header.interface_checksum);
  443. header.indirect_checksum := swapendian(header.indirect_checksum);
  444. header.deflistsize:=swapendian(header.deflistsize);
  445. header.symlistsize:=swapendian(header.symlistsize);
  446. {$ENDIF}
  447. { the PPU DATA is stored in native order }
  448. if (header.flags and uf_big_endian) = uf_big_endian then
  449. Begin
  450. {$IFDEF ENDIAN_LITTLE}
  451. change_endian := TRUE;
  452. {$ELSE}
  453. change_endian := FALSE;
  454. {$ENDIF}
  455. End
  456. else if (header.flags and uf_little_endian) = uf_little_endian then
  457. Begin
  458. {$IFDEF ENDIAN_BIG}
  459. change_endian := TRUE;
  460. {$ELSE}
  461. change_endian := FALSE;
  462. {$ENDIF}
  463. End;
  464. {reset buffer}
  465. bufstart:=i;
  466. bufsize:=0;
  467. bufidx:=0;
  468. Mode:=1;
  469. FillChar(entry,sizeof(tppuentry),0);
  470. entryidx:=0;
  471. entrystart:=0;
  472. entrybufstart:=0;
  473. Error:=false;
  474. openfile:=true;
  475. end;
  476. procedure tppufile.reloadbuf;
  477. begin
  478. inc(bufstart,bufsize);
  479. bufsize:=f.Read(buf^,ppubufsize);
  480. bufidx:=0;
  481. end;
  482. procedure tppufile.readdata(out b;len:integer);
  483. var
  484. p,pbuf : pchar;
  485. left : integer;
  486. begin
  487. p:=pchar(@b);
  488. pbuf:=@buf[bufidx];
  489. repeat
  490. left:=bufsize-bufidx;
  491. if len<left then
  492. break;
  493. move(pbuf^,p^,left);
  494. dec(len,left);
  495. inc(p,left);
  496. reloadbuf;
  497. pbuf:=@buf[bufidx];
  498. if bufsize=0 then
  499. exit;
  500. until false;
  501. move(pbuf^,p^,len);
  502. inc(bufidx,len);
  503. end;
  504. procedure tppufile.skipdata(len:integer);
  505. var
  506. left : integer;
  507. begin
  508. while len>0 do
  509. begin
  510. left:=bufsize-bufidx;
  511. if len>left then
  512. begin
  513. dec(len,left);
  514. reloadbuf;
  515. if bufsize=0 then
  516. exit;
  517. end
  518. else
  519. begin
  520. inc(bufidx,len);
  521. exit;
  522. end;
  523. end;
  524. end;
  525. function tppufile.readentry:byte;
  526. begin
  527. if entryidx<entry.size then
  528. begin
  529. {$ifdef generic_cpu}
  530. has_more:=true;
  531. {$endif not generic_cpu}
  532. skipdata(entry.size-entryidx);
  533. end;
  534. readdata(entry,sizeof(tppuentry));
  535. if change_endian then
  536. entry.size:=swapendian(entry.size);
  537. entrystart:=bufstart+bufidx;
  538. entryidx:=0;
  539. {$ifdef generic_cpu}
  540. has_more:=false;
  541. {$endif not generic_cpu}
  542. if not(entry.id in [mainentryid,subentryid]) then
  543. begin
  544. readentry:=iberror;
  545. error:=true;
  546. exit;
  547. end;
  548. readentry:=entry.nr;
  549. end;
  550. function tppufile.endofentry:boolean;
  551. begin
  552. {$ifdef generic_cpu}
  553. endofentry:=(entryidx=entry.size);
  554. {$else not generic_cpu}
  555. endofentry:=(entryidx>=entry.size);
  556. {$endif not generic_cpu}
  557. end;
  558. function tppufile.entrysize:longint;
  559. begin
  560. entrysize:=entry.size;
  561. end;
  562. function tppufile.entryleft:longint;
  563. begin
  564. entryleft:=entry.size-entryidx;
  565. end;
  566. procedure tppufile.getdatabuf(out b;len:integer;out res:integer);
  567. begin
  568. if entryidx+len>entry.size then
  569. res:=entry.size-entryidx
  570. else
  571. res:=len;
  572. readdata(b,res);
  573. inc(entryidx,res);
  574. end;
  575. procedure tppufile.getdata(out b;len:integer);
  576. begin
  577. if entryidx+len>entry.size then
  578. begin
  579. error:=true;
  580. exit;
  581. end;
  582. readdata(b,len);
  583. inc(entryidx,len);
  584. end;
  585. function tppufile.getbyte:byte;
  586. begin
  587. if entryidx+1>entry.size then
  588. begin
  589. error:=true;
  590. result:=0;
  591. exit;
  592. end;
  593. if bufsize-bufidx>=1 then
  594. begin
  595. result:=pbyte(@buf[bufidx])^;
  596. inc(bufidx);
  597. end
  598. else
  599. readdata(result,1);
  600. inc(entryidx);
  601. end;
  602. function tppufile.getword:word;
  603. begin
  604. if entryidx+2>entry.size then
  605. begin
  606. error:=true;
  607. result:=0;
  608. exit;
  609. end;
  610. if bufsize-bufidx>=sizeof(word) then
  611. begin
  612. result:=Unaligned(pword(@buf[bufidx])^);
  613. inc(bufidx,sizeof(word));
  614. end
  615. else
  616. readdata(result,sizeof(word));
  617. if change_endian then
  618. result:=swapendian(result);
  619. inc(entryidx,2);
  620. end;
  621. function tppufile.getlongint:longint;
  622. begin
  623. if entryidx+4>entry.size then
  624. begin
  625. error:=true;
  626. result:=0;
  627. exit;
  628. end;
  629. if bufsize-bufidx>=sizeof(longint) then
  630. begin
  631. result:=Unaligned(plongint(@buf[bufidx])^);
  632. inc(bufidx,sizeof(longint));
  633. end
  634. else
  635. readdata(result,sizeof(longint));
  636. if change_endian then
  637. result:=swapendian(result);
  638. inc(entryidx,4);
  639. end;
  640. function tppufile.getdword:dword;
  641. begin
  642. if entryidx+4>entry.size then
  643. begin
  644. error:=true;
  645. result:=0;
  646. exit;
  647. end;
  648. if bufsize-bufidx>=sizeof(dword) then
  649. begin
  650. result:=Unaligned(plongint(@buf[bufidx])^);
  651. inc(bufidx,sizeof(longint));
  652. end
  653. else
  654. readdata(result,sizeof(dword));
  655. if change_endian then
  656. result:=swapendian(result);
  657. inc(entryidx,4);
  658. end;
  659. function tppufile.getint64:int64;
  660. begin
  661. if entryidx+8>entry.size then
  662. begin
  663. error:=true;
  664. result:=0;
  665. exit;
  666. end;
  667. if bufsize-bufidx>=sizeof(int64) then
  668. begin
  669. result:=Unaligned(pint64(@buf[bufidx])^);
  670. inc(bufidx,sizeof(int64));
  671. end
  672. else
  673. readdata(result,sizeof(int64));
  674. if change_endian then
  675. result:=swapendian(result);
  676. inc(entryidx,8);
  677. end;
  678. function tppufile.getqword:qword;
  679. begin
  680. if entryidx+8>entry.size then
  681. begin
  682. error:=true;
  683. result:=0;
  684. exit;
  685. end;
  686. if bufsize-bufidx>=sizeof(qword) then
  687. begin
  688. result:=Unaligned(pqword(@buf[bufidx])^);
  689. inc(bufidx,sizeof(qword));
  690. end
  691. else
  692. readdata(result,sizeof(qword));
  693. if change_endian then
  694. result:=swapendian(result);
  695. inc(entryidx,8);
  696. end;
  697. function tppufile.getaint:aint;
  698. begin
  699. {$ifdef generic_cpu}
  700. if CpuAluBitSize[tsystemcpu(header.cpu)]=64 then
  701. result:=getint64
  702. else if CpuAluBitSize[tsystemcpu(header.cpu)]=32 then
  703. result:=getlongint
  704. else if CpuAluBitSize[tsystemcpu(header.cpu)]=16 then
  705. result:=smallint(getword)
  706. else if CpuAluBitSize[tsystemcpu(header.cpu)]=8 then
  707. result:=shortint(getbyte)
  708. else
  709. begin
  710. error:=true;
  711. result:=0;
  712. end;
  713. {$else not generic_cpu}
  714. {$ifdef cpu64bitalu}
  715. result:=getint64
  716. {$else cpu64bitalu}
  717. result:=getlongint;
  718. {$endif cpu64bitalu}
  719. {$endif not generic_cpu}
  720. end;
  721. function tppufile.getasizeint:asizeint;
  722. begin
  723. {$ifdef generic_cpu}
  724. if CpuAddrBitSize[tsystemcpu(header.cpu)]=64 then
  725. result:=getint64
  726. else if CpuAddrBitSize[tsystemcpu(header.cpu)]=32 then
  727. result:=getlongint
  728. else if CpuAddrBitSize[tsystemcpu(header.cpu)]=16 then
  729. result:=smallint(getword)
  730. else
  731. begin
  732. error:=true;
  733. result:=0;
  734. end;
  735. {$else not generic_cpu}
  736. {$ifdef cpu64bitaddr}
  737. result:=getint64;
  738. {$else cpu64bitaddr}
  739. result:=getlongint;
  740. {$endif cpu32bitaddr}
  741. {$endif not generic_cpu}
  742. end;
  743. function tppufile.getaword:aword;
  744. begin
  745. {$ifdef generic_cpu}
  746. if CpuAluBitSize[tsystemcpu(header.cpu)]=64 then
  747. result:=getqword
  748. else if CpuAluBitSize[tsystemcpu(header.cpu)]=32 then
  749. result:=getdword
  750. else if CpuAluBitSize[tsystemcpu(header.cpu)]=16 then
  751. result:=getword
  752. else if CpuAluBitSize[tsystemcpu(header.cpu)]=8 then
  753. result:=getbyte
  754. else
  755. begin
  756. error:=true;
  757. result:=0;
  758. end;
  759. {$else not generic_cpu}
  760. {$ifdef cpu64bitalu}
  761. result:=getqword;
  762. {$else cpu64bitalu}
  763. result:=getdword;
  764. {$endif cpu64bitalu}
  765. {$endif not generic_cpu}
  766. end;
  767. function tppufile.getrealsize(sizeofreal : longint):ppureal;
  768. var
  769. e : ppureal;
  770. d : double;
  771. s : single;
  772. begin
  773. if sizeofreal=sizeof(e) then
  774. begin
  775. if entryidx+sizeof(e)>entry.size then
  776. begin
  777. error:=true;
  778. result:=0;
  779. exit;
  780. end;
  781. readdata(e,sizeof(e));
  782. if change_endian then
  783. result:=swapendian_ppureal(e)
  784. else
  785. result:=e;
  786. inc(entryidx,sizeof(e));
  787. exit;
  788. end;
  789. if sizeofreal=sizeof(d) then
  790. begin
  791. if entryidx+sizeof(d)>entry.size then
  792. begin
  793. error:=true;
  794. result:=0;
  795. exit;
  796. end;
  797. readdata(d,sizeof(d));
  798. if change_endian then
  799. result:=swapendian(pqword(@d)^)
  800. else
  801. result:=d;
  802. inc(entryidx,sizeof(d));
  803. result:=d;
  804. exit;
  805. end;
  806. if sizeofreal=sizeof(s) then
  807. begin
  808. if entryidx+sizeof(s)>entry.size then
  809. begin
  810. error:=true;
  811. result:=0;
  812. exit;
  813. end;
  814. readdata(s,sizeof(s));
  815. if change_endian then
  816. result:=swapendian(pdword(@s)^)
  817. else
  818. result:=s;
  819. inc(entryidx,sizeof(s));
  820. result:=s;
  821. exit;
  822. end;
  823. error:=true;
  824. result:=0.0;
  825. end;
  826. function tppufile.getreal:ppureal;
  827. var
  828. d : ppureal;
  829. hd : double;
  830. begin
  831. if target_info.system=system_x86_64_win64 then
  832. begin
  833. hd:=getrealsize(sizeof(hd));
  834. getreal:=hd;
  835. end
  836. else
  837. begin
  838. d:=getrealsize(sizeof(d));
  839. getreal:=d;
  840. end;
  841. end;
  842. function tppufile.getstring:string;
  843. begin
  844. result[0]:=chr(getbyte);
  845. if entryidx+length(result)>entry.size then
  846. begin
  847. error:=true;
  848. exit;
  849. end;
  850. ReadData(result[1],length(result));
  851. inc(entryidx,length(result));
  852. end;
  853. function tppufile.getansistring:ansistring;
  854. var
  855. len: longint;
  856. begin
  857. len:=getlongint;
  858. if entryidx+len>entry.size then
  859. begin
  860. error:=true;
  861. exit;
  862. end;
  863. setlength(result,len);
  864. if len>0 then
  865. getdata(result[1],len);
  866. end;
  867. procedure tppufile.getsmallset(out b);
  868. var
  869. i : longint;
  870. begin
  871. getdata(b,4);
  872. if change_endian then
  873. for i:=0 to 3 do
  874. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  875. end;
  876. procedure tppufile.getnormalset(out b);
  877. var
  878. i : longint;
  879. begin
  880. getdata(b,32);
  881. if change_endian then
  882. for i:=0 to 31 do
  883. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  884. end;
  885. function tppufile.skipuntilentry(untilb:byte):boolean;
  886. var
  887. b : byte;
  888. begin
  889. repeat
  890. b:=readentry;
  891. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  892. skipuntilentry:=(b=untilb);
  893. end;
  894. {*****************************************************************************
  895. TPPUFile Writing
  896. *****************************************************************************}
  897. function tppufile.createfile:boolean;
  898. var
  899. ok: boolean;
  900. begin
  901. createfile:=false;
  902. {$ifdef INTFPPU}
  903. if crc_only then
  904. begin
  905. fname:=fname+'.intf';
  906. crc_only:=false;
  907. end;
  908. {$endif}
  909. if not crc_only then
  910. begin
  911. {$ifdef MACOS}
  912. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  913. SetDefaultMacOSCreator('FPas');
  914. SetDefaultMacOSFiletype('FPPU');
  915. {$endif}
  916. ok:=false;
  917. try
  918. f:=CFileStreamClass.Create(fname,fmCreate);
  919. ok:=true;
  920. except
  921. end;
  922. {$ifdef MACOS}
  923. SetDefaultMacOSCreator('MPS ');
  924. SetDefaultMacOSFiletype('TEXT');
  925. {$endif}
  926. if not ok then
  927. exit;
  928. Mode:=2;
  929. {write header for sure}
  930. f.Write(header,sizeof(tppuheader));
  931. end;
  932. bufsize:=ppubufsize;
  933. bufstart:=sizeof(tppuheader);
  934. bufidx:=0;
  935. {reset}
  936. crc:=0;
  937. interface_crc:=0;
  938. indirect_crc:=0;
  939. do_interface_crc:=true;
  940. do_indirect_crc:=false;
  941. Error:=false;
  942. do_crc:=true;
  943. size:=0;
  944. entrytyp:=mainentryid;
  945. {start}
  946. NewEntry;
  947. createfile:=true;
  948. end;
  949. procedure tppufile.writeheader;
  950. var
  951. opos : integer;
  952. begin
  953. if crc_only then
  954. exit;
  955. { flush buffer }
  956. writebuf;
  957. { update size (w/o header!) in the header }
  958. header.size:=bufstart-sizeof(tppuheader);
  959. { set the endian flag }
  960. {$ifndef FPC_BIG_ENDIAN}
  961. header.flags := header.flags or uf_little_endian;
  962. {$else not FPC_BIG_ENDIAN}
  963. header.flags := header.flags or uf_big_endian;
  964. { Now swap the header in the correct endian (always little endian) }
  965. header.compiler := swapendian(header.compiler);
  966. header.cpu := swapendian(header.cpu);
  967. header.target := swapendian(header.target);
  968. header.flags := swapendian(header.flags);
  969. header.size := swapendian(header.size);
  970. header.checksum := swapendian(header.checksum);
  971. header.interface_checksum := swapendian(header.interface_checksum);
  972. header.indirect_checksum := swapendian(header.indirect_checksum);
  973. header.deflistsize:=swapendian(header.deflistsize);
  974. header.symlistsize:=swapendian(header.symlistsize);
  975. {$endif not FPC_BIG_ENDIAN}
  976. { write header and restore filepos after it }
  977. opos:=f.Position;
  978. f.Position:=0;
  979. f.Write(header,sizeof(tppuheader));
  980. f.Position:=opos;
  981. end;
  982. procedure tppufile.writebuf;
  983. begin
  984. if not crc_only and
  985. (bufidx <> 0) then
  986. f.Write(buf^,bufidx);
  987. inc(bufstart,bufidx);
  988. bufidx:=0;
  989. end;
  990. procedure tppufile.writedata(const b;len:integer);
  991. var
  992. p : pchar;
  993. left,
  994. idx : integer;
  995. begin
  996. if crc_only then
  997. exit;
  998. p:=pchar(@b);
  999. idx:=0;
  1000. while len>0 do
  1001. begin
  1002. left:=bufsize-bufidx;
  1003. if len>left then
  1004. begin
  1005. move(p[idx],buf[bufidx],left);
  1006. dec(len,left);
  1007. inc(idx,left);
  1008. inc(bufidx,left);
  1009. writebuf;
  1010. end
  1011. else
  1012. begin
  1013. move(p[idx],buf[bufidx],len);
  1014. inc(bufidx,len);
  1015. exit;
  1016. end;
  1017. end;
  1018. end;
  1019. procedure tppufile.NewEntry;
  1020. begin
  1021. with entry do
  1022. begin
  1023. id:=entrytyp;
  1024. nr:=ibend;
  1025. size:=0;
  1026. end;
  1027. {Reset Entry State}
  1028. entryidx:=0;
  1029. entrybufstart:=bufstart;
  1030. entrystart:=bufstart+bufidx;
  1031. {Alloc in buffer}
  1032. writedata(entry,sizeof(tppuentry));
  1033. end;
  1034. procedure tppufile.writeentry(ibnr:byte);
  1035. var
  1036. opos : integer;
  1037. begin
  1038. {create entry}
  1039. entry.id:=entrytyp;
  1040. entry.nr:=ibnr;
  1041. entry.size:=entryidx;
  1042. {it's already been sent to disk ?}
  1043. if entrybufstart<>bufstart then
  1044. begin
  1045. if not crc_only then
  1046. begin
  1047. {flush to be sure}
  1048. WriteBuf;
  1049. {write entry}
  1050. opos:=f.Position;
  1051. f.Position:=entrystart;
  1052. f.write(entry,sizeof(tppuentry));
  1053. f.Position:=opos;
  1054. end;
  1055. entrybufstart:=bufstart;
  1056. end
  1057. else
  1058. move(entry,buf[entrystart-bufstart],sizeof(entry));
  1059. {Add New Entry, which is ibend by default}
  1060. entrystart:=bufstart+bufidx; {next entry position}
  1061. NewEntry;
  1062. end;
  1063. procedure tppufile.putdata(const b;len:integer);
  1064. begin
  1065. if do_crc then
  1066. begin
  1067. crc:=UpdateCrc32(crc,b,len);
  1068. {$ifdef Test_Double_checksum}
  1069. if crc_only then
  1070. begin
  1071. crc_test2^[crc_index2]:=crc;
  1072. {$ifdef Test_Double_checksum_write}
  1073. Writeln(CRCFile,crc);
  1074. {$endif Test_Double_checksum_write}
  1075. if crc_index2<crc_array_size then
  1076. inc(crc_index2);
  1077. end
  1078. else
  1079. begin
  1080. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  1081. (crc_test2^[crcindex2]<>crc) then
  1082. Do_comment(V_Note,'impl CRC changed');
  1083. {$ifdef Test_Double_checksum_write}
  1084. Writeln(CRCFile,crc);
  1085. {$endif Test_Double_checksum_write}
  1086. inc(crcindex2);
  1087. end;
  1088. {$endif def Test_Double_checksum}
  1089. if do_interface_crc then
  1090. begin
  1091. interface_crc:=UpdateCrc32(interface_crc,b,len);
  1092. {$ifdef Test_Double_checksum}
  1093. if crc_only then
  1094. begin
  1095. crc_test^[crc_index]:=interface_crc;
  1096. {$ifdef Test_Double_checksum_write}
  1097. Writeln(CRCFile,interface_crc);
  1098. {$endif Test_Double_checksum_write}
  1099. if crc_index<crc_array_size then
  1100. inc(crc_index);
  1101. end
  1102. else
  1103. begin
  1104. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  1105. (crc_test^[crcindex]<>interface_crc) then
  1106. Do_comment(V_Warning,'CRC changed');
  1107. {$ifdef Test_Double_checksum_write}
  1108. Writeln(CRCFile,interface_crc);
  1109. {$endif Test_Double_checksum_write}
  1110. inc(crcindex);
  1111. end;
  1112. {$endif def Test_Double_checksum}
  1113. { indirect crc must only be calculated for the interface; changes
  1114. to a class in the implementation cannot require another unit to
  1115. be recompiled }
  1116. if do_indirect_crc then
  1117. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  1118. end;
  1119. end;
  1120. if not crc_only then
  1121. writedata(b,len);
  1122. inc(entryidx,len);
  1123. end;
  1124. procedure tppufile.putbyte(b:byte);
  1125. begin
  1126. putdata(b,1);
  1127. end;
  1128. procedure tppufile.putword(w:word);
  1129. begin
  1130. putdata(w,2);
  1131. end;
  1132. procedure tppufile.putdword(w:dword);
  1133. begin
  1134. putdata(w,4);
  1135. end;
  1136. procedure tppufile.putlongint(l:longint);
  1137. begin
  1138. putdata(l,4);
  1139. end;
  1140. procedure tppufile.putint64(i:int64);
  1141. begin
  1142. putdata(i,8);
  1143. end;
  1144. procedure tppufile.putqword(q:qword);
  1145. begin
  1146. putdata(q,sizeof(qword));
  1147. end;
  1148. procedure tppufile.putaint(i:aint);
  1149. begin
  1150. putdata(i,sizeof(aint));
  1151. end;
  1152. procedure tppufile.putasizeint(i: asizeint);
  1153. begin
  1154. putdata(i,sizeof(asizeint));
  1155. end;
  1156. procedure tppufile.putaword(i:aword);
  1157. begin
  1158. putdata(i,sizeof(aword));
  1159. end;
  1160. procedure tppufile.putreal(d:ppureal);
  1161. var
  1162. hd : double;
  1163. begin
  1164. if target_info.system=system_x86_64_win64 then
  1165. begin
  1166. hd:=d;
  1167. putdata(hd,sizeof(hd));
  1168. end
  1169. else
  1170. putdata(d,sizeof(ppureal));
  1171. end;
  1172. procedure tppufile.putstring(const s:string);
  1173. begin
  1174. putdata(s,length(s)+1);
  1175. end;
  1176. procedure tppufile.putansistring(const s:ansistring);
  1177. var
  1178. len: longint;
  1179. begin
  1180. len:=length(s);
  1181. putlongint(len);
  1182. if len>0 then
  1183. putdata(s[1],len);
  1184. end;
  1185. procedure tppufile.putsmallset(const b);
  1186. var
  1187. l : longint;
  1188. begin
  1189. l:=longint(b);
  1190. putlongint(l);
  1191. end;
  1192. procedure tppufile.putnormalset(const b);
  1193. begin
  1194. putdata(b,32);
  1195. end;
  1196. procedure tppufile.tempclose;
  1197. begin
  1198. if not closed then
  1199. begin
  1200. closepos:=f.Position;
  1201. f.Free;
  1202. f:=nil;
  1203. closed:=true;
  1204. tempclosed:=true;
  1205. end;
  1206. end;
  1207. function tppufile.tempopen:boolean;
  1208. begin
  1209. tempopen:=false;
  1210. if not closed or not tempclosed then
  1211. exit;
  1212. { MG: not sure, if this is correct
  1213. f.position:=0;
  1214. No, f was freed in tempclose above, we need to
  1215. recreate it. PM 2011/06/06 }
  1216. try
  1217. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1218. except
  1219. exit;
  1220. end;
  1221. closed:=false;
  1222. tempclosed:=false;
  1223. { restore state }
  1224. f.Position:=closepos;
  1225. tempopen:=true;
  1226. end;
  1227. end.