bin2obj.pp 5.6 KB

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