bin2obj.pp 5.9 KB

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