ppu.pas 31 KB

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