2
0

ppu.pas 31 KB

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