ppu.pas 30 KB

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