bin2obj.pp 6.3 KB

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