demobasenenc.lpr 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2021 by Michael Van Canneyt,
  4. member of the Free Pascal development team
  5. Demo program for Base16,Base32,Base32-hex,Base32-crockford, Base64,Base64url encoding/decoding
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program demobasenenc;
  13. uses sysutils, basenenc, typinfo, custapp, Classes;
  14. Type
  15. { TDemoApp }
  16. TDemoApp = Class(TCustomApplication)
  17. private
  18. FEncoder : TStandardEncoder;
  19. FPadding,
  20. FDoDecode : Boolean;
  21. procedure ParseOptions;
  22. procedure Usage(aError: String);
  23. Protected
  24. Procedure DoRun; override;
  25. end;
  26. { TDemoApp }
  27. procedure TDemoApp.Usage(aError : String);
  28. Var
  29. Enc : TStandardEncoder;
  30. begin
  31. if (aError<>'') then
  32. Writeln('Error : ',aError);
  33. Writeln('Usage ', ExtractFileName(Self.ExeName),' [options]');
  34. Writeln('Where options is one or more of');
  35. Writeln('-h --help This message');
  36. Writeln('-e --encode=ENC Encode input to output using algorithm ENC, one of');
  37. For Enc in TStandardEncoder do
  38. Writeln(' ',Copy(GetEnumName(TypeInfo(TStandardEncoder),Ord(Enc)),3,MaxInt));
  39. Writeln('-d --decode=ENC Encode input to output using algorithm ENC, one of the above');
  40. Writeln('-i --input=FileName Set input filename. Required.');
  41. Writeln('-o --output=FileName Set input filename. Required.');
  42. Writeln('-p --pad Use Padding when encoding.');
  43. ExitCode:=Ord(aError<>'');
  44. end;
  45. procedure TDemoApp.ParseOptions;
  46. Var
  47. S : String;
  48. I : Integer;
  49. begin
  50. FDoDecode:=False;
  51. S:=CheckOptions('hi:o:e:p',['help','input:','output:','encode:','decode:','pad']);
  52. if Not (HasOption('i','input') and HasOption('o','output')) then
  53. S:='Input and output filename are required';
  54. if (S<>'') or HasOption('h','help') then
  55. begin
  56. Usage(S);
  57. Exit;
  58. end;
  59. FPadding:=HasOption('p','pad');
  60. S:=GetOptionValue('e','encode');
  61. if S='' then
  62. begin
  63. S:=GetOptionValue('d','decode');
  64. if S<>'' then
  65. FDoDecode:=True;
  66. end;
  67. if (S='') then
  68. S:='base64';
  69. i:=GetEnumValue(TypeInfo(TStandardEncoder),S);
  70. if I=-1 then
  71. i:=GetEnumValue(TypeInfo(TStandardEncoder),'se'+S);
  72. if I=-1 then
  73. begin
  74. Usage('Not a valid algorithm: '+s);
  75. Exit;
  76. end;
  77. FEncoder:=TStandardEncoder(I);
  78. end;
  79. procedure TDemoApp.DoRun;
  80. Var
  81. B,Res : TBytes;
  82. F : TFileStream;
  83. Coder : TAlphabetEncoder;
  84. begin
  85. B:=[];
  86. Terminate;
  87. Parseoptions;
  88. if ExitCode<>0 then
  89. exit;
  90. F:=TFileStream.Create(GetOptionValue('i','input'),fmOpenRead or fmShareDenyWrite);
  91. try
  92. SetLength(B,F.Size);
  93. F.ReadBuffer(B,F.Size);
  94. finally
  95. F.Free;
  96. end;
  97. Coder:=GetStandardEncoder(FEncoder);
  98. if FDoDecode then
  99. Res:=Coder.Decode(PByte(B),Length(B))
  100. else
  101. Res:=TEncoding.UTF8.GetAnsiBytes(Coder.Encode(PByte(B),Length(B),FPadding));
  102. F:=TFileStream.Create(GetOptionValue('o','output'),fmCreate);
  103. try
  104. F.WriteBuffer(Res,Length(Res))
  105. finally
  106. F.Free;
  107. end;
  108. end;
  109. begin
  110. CustomApplication:=TDemoApp.Create(Nil);
  111. CustomApplication.Initialize;
  112. CustomApplication.Run;
  113. CustomApplication.Free;
  114. end.