ppu.pas 31 KB

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