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