bin2obj.pp 6.0 KB

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