bin2obj.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. program bin2obj;
  2. {
  3. $Id$
  4. This file is part of the Free Pascal run time library.
  5. Copyright (c) 1999-2000 by Michael Van Canneyt, member of the
  6. Free Pascal development team
  7. Binary file to include file converter.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. {$mode objfpc}
  15. uses classes,getopts, iostream,zstream,idea,sysutils,dos;
  16. var
  17. ConstName,
  18. OutFileName,
  19. UnitName : String;
  20. WriteAsciiData,
  21. CompressData,
  22. EnCodeData,
  23. CompileUnit : Boolean;
  24. Cryptkey : IDEAcryptKey;
  25. InStream,
  26. MemStream,
  27. CryptStream,
  28. CompStream : TStream;
  29. Procedure Usage;
  30. begin
  31. Writeln ('Usage: bin2obj [options] -c constname [infile] ');
  32. Writeln ('Where options is a combination of : ');
  33. Writeln (' -a write asciii data instead of bytes');
  34. Writeln (' -z compress data.');
  35. Writeln (' -e key encrypt data with key (must have 8 characters)');
  36. Writeln (' -o output filename');
  37. Writeln (' -u [name] make a unit instead of an include file (unit name is outfile)');
  38. Writeln (' -U [name] same as -u, and compile the unit. (requires outfile)');
  39. Halt(1);
  40. end;
  41. Procedure ProcessCommandLine;
  42. Var C : Char;
  43. I : longint;
  44. NeedUnitName : Boolean;
  45. begin
  46. OptErr:=False;
  47. ConstName:='';
  48. CompressData:=False;
  49. EncodeData:=False;
  50. CompileUnit:=False;
  51. UnitName:='';
  52. NeedUnitName:=False;
  53. WriteAsciiData:=False;
  54. Repeat
  55. c:=GetOpt('ac:e:o:zhu::U::');
  56. Case C of
  57. 'a' : WriteAsciiData:=True;
  58. 'c' : ConstName:=OptArg;
  59. 'h','?' : usage;
  60. 'z' : CompressData := True;
  61. 'e' : begin
  62. EncodeData:=True;
  63. If Length(OptArg)<8 then
  64. Usage;
  65. For I:=0 to 7 do
  66. CryptKey[i]:=Ord(OptArg[I+1]);
  67. end;
  68. 'o' : OutFileName:=optArg;
  69. 'u','U':
  70. begin
  71. UnitName:=OptArg;
  72. If Length(UnitName)=0 then
  73. NeedUnitName:=True;
  74. If C='U' then
  75. CompileUnit:=True;
  76. end;
  77. end;
  78. until C=EndOfOptions;
  79. if ConstName='' then
  80. usage;
  81. If NeedUnitName then
  82. If Length (OutFileName)=0 then
  83. begin
  84. Writeln ('Error : cannot determine unitname from filename');
  85. Usage;
  86. end
  87. else
  88. UnitName:=ExtractFileName(OutFileName);
  89. if CompileUnit and (Length(OutFileName)=0) then
  90. usage;
  91. end;
  92. Function SetupInput : TStream;
  93. begin
  94. if OptInd=ParamCount then
  95. InStream:=TFileStream.Create(Paramstr(Optind),fmOpenRead)
  96. else
  97. InStream:=TIOStream(iosInput);
  98. Result:=InStream;
  99. end;
  100. Function SetupOutput : TStream;
  101. Var Key : ideaKey;
  102. begin
  103. MemStream:=TMemoryStream.Create;
  104. Result:=MemStream;
  105. If ComPressData then
  106. begin
  107. CompStream:=TCompressionStream.Create(cldefault,Result);
  108. Result:=CompStream;
  109. end;
  110. if EncodeData Then
  111. begin
  112. EnKeyIdea(CryptKey,Key);
  113. CryptStream:=TIDEAEncryptStream.Create(Key,Result);
  114. Result:=CryptStream;
  115. end;
  116. end;
  117. Procedure CopyStreams (Ins,Outs : TStream);
  118. Const BufSize = 1024;
  119. Var Buffer : Array[1..BufSize] of byte;
  120. Count : longint;
  121. begin
  122. repeat
  123. Count:=Ins.Read(Buffer,SizeOf(Buffer));
  124. If Count>0 then
  125. Outs.Write(Buffer,Count);
  126. until Count<SizeOf(Buffer);
  127. {
  128. freeing these streams will flush their buffers.
  129. Order is important !!!
  130. }
  131. CryptStream.Free;
  132. CompStream.Free;
  133. // Now Out stream has all data.
  134. end;
  135. Procedure WriteMemStream;
  136. Var OutStream : TStream;
  137. Procedure WriteStr(Const St : String);
  138. begin
  139. OutStream.Write(St[1],Length(St));
  140. end;
  141. Procedure WriteStrLn(Const St : String);
  142. Const
  143. {$ifdef unix}
  144. Eoln : String = #10;
  145. {$else}
  146. Eoln : String = #13#10;
  147. {$endif}
  148. begin
  149. OutStream.Write(St[1],Length(St));
  150. OutStream.Write(Eoln[1],Length(Eoln));
  151. end;
  152. Const Prefix = ' ';
  153. MaxLineLength = 72;
  154. Var I,Count : longint;
  155. b : byte;
  156. Line,ToAdd : String;
  157. begin
  158. If Length(OutFileName)=0 Then
  159. OutStream:=TIOStream.Create(iosOutput)
  160. else
  161. OutStream:=TFileStream.Create(OutFileName,fmCreate);
  162. If UnitName<>'' then
  163. begin
  164. WriteStrLn(Format('Unit %s;',[UnitName]));
  165. WriteStrLn('');
  166. WriteStrLn('Interface');
  167. WriteStrLn('');
  168. end;
  169. WriteStrLn('');
  170. WriteStrLn('Const');
  171. MemStream.Seek(0,soFromBeginning);
  172. Count:=MemStream.Size;
  173. If WriteAsciidata then
  174. WriteStrLn(Format(' %s : Array[0..%d] of char = (',[ConstName,Count-1]))
  175. else
  176. WriteStrLn(Format(' %s : Array[0..%d] of byte = (',[ConstName,Count-1]));
  177. Line:=Prefix;
  178. For I:=1 to Count do
  179. begin
  180. MemStream.Read(B,1);
  181. If Not WriteAsciiData then
  182. ToAdd:=Format('%3d',[b])
  183. else
  184. If (B in [32..127]) and not (B in [10,13,39]) then
  185. ToAdd:=''''+Chr(b)+''''
  186. else
  187. // ToAdd:=Format('''%s''',[Chr(b)]);
  188. ToAdd:=Format('#%d',[B]);
  189. If I<Count then
  190. ToAdd:=ToAdd+',';
  191. Line:=Line+ToAdd;
  192. If Length(Line)>=MaxLineLength Then
  193. begin
  194. WriteStrLn(Line);
  195. Line:=PreFix;
  196. end;
  197. end;
  198. WriteStrln(Line+');');
  199. If Length(UnitName)<>0 then
  200. begin
  201. WriteStrLn('');
  202. WriteStrLn('Implementation');
  203. WriteStrln('');
  204. WriteStrLn('end.')
  205. end;
  206. MemStream.Free;
  207. end;
  208. Procedure CompileTheUNit;
  209. begin
  210. Exec('ppc386',' -Un '+UnitName);
  211. end;
  212. begin
  213. ProcessCommandline;
  214. CopyStreams(SetupInput,SetupOutPut);
  215. WriteMemStream;
  216. If CompileUNit then
  217. CompileTheUnit;
  218. end.
  219. {
  220. $Log$
  221. Revision 1.5 2002-09-07 15:40:30 peter
  222. * old logs removed and tabs fixed
  223. }