demobasenenc.lpr 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. {$mode objfpc}
  14. {$h+}
  15. uses sysutils, basenenc, typinfo, custapp, Classes;
  16. Type
  17. { TDemoApp }
  18. TDemoApp = Class(TCustomApplication)
  19. private
  20. FEncoder : TStandardEncoder;
  21. FPadding,
  22. FDoDecode : Boolean;
  23. procedure ParseOptions;
  24. procedure Usage(aError: String);
  25. Protected
  26. Procedure DoRun; override;
  27. end;
  28. { TDemoApp }
  29. procedure TDemoApp.Usage(aError : String);
  30. Var
  31. Enc : TStandardEncoder;
  32. begin
  33. if (aError<>'') then
  34. Writeln('Error : ',aError);
  35. Writeln('Usage ', ExtractFileName(Self.ExeName),' [options]');
  36. Writeln('Where options is one or more of');
  37. Writeln('-h --help This message');
  38. Writeln('-e --encode=ENC Encode input to output using algorithm ENC, one of');
  39. For Enc in TStandardEncoder do
  40. Writeln(' ',Copy(GetEnumName(TypeInfo(TStandardEncoder),Ord(Enc)),3,MaxInt));
  41. Writeln('-d --decode=ENC Encode input to output using algorithm ENC, one of the above');
  42. Writeln('-i --input=FileName Set input filename. Required.');
  43. Writeln('-o --output=FileName Set input filename. Required.');
  44. Writeln('-p --pad Use Padding when encoding.');
  45. ExitCode:=Ord(aError<>'');
  46. end;
  47. procedure TDemoApp.ParseOptions;
  48. Var
  49. S : String;
  50. I : Integer;
  51. begin
  52. FDoDecode:=False;
  53. S:=CheckOptions('hi:o:e:p',['help','input:','output:','encode:','decode:','pad']);
  54. if Not (HasOption('i','input') and HasOption('o','output')) then
  55. S:='Input and output filename are required';
  56. if (S<>'') or HasOption('h','help') then
  57. begin
  58. Usage(S);
  59. Exit;
  60. end;
  61. FPadding:=HasOption('p','pad');
  62. S:=GetOptionValue('e','encode');
  63. if S='' then
  64. begin
  65. S:=GetOptionValue('d','decode');
  66. if S<>'' then
  67. FDoDecode:=True;
  68. end;
  69. if (S='') then
  70. S:='base64';
  71. i:=GetEnumValue(TypeInfo(TStandardEncoder),S);
  72. if I=-1 then
  73. i:=GetEnumValue(TypeInfo(TStandardEncoder),'se'+S);
  74. if I=-1 then
  75. begin
  76. Usage('Not a valid algorithm: '+s);
  77. Exit;
  78. end;
  79. FEncoder:=TStandardEncoder(I);
  80. end;
  81. procedure TDemoApp.DoRun;
  82. Var
  83. B,Res : TBytes;
  84. F : TFileStream;
  85. Coder : TAlphabetEncoder;
  86. begin
  87. B:=[];
  88. Terminate;
  89. Parseoptions;
  90. if ExitCode<>0 then
  91. exit;
  92. F:=TFileStream.Create(GetOptionValue('i','input'),fmOpenRead or fmShareDenyWrite);
  93. try
  94. SetLength(B,F.Size);
  95. F.ReadBuffer(B,F.Size);
  96. finally
  97. F.Free;
  98. end;
  99. Coder:=GetStandardEncoder(FEncoder);
  100. if FDoDecode then
  101. Res:=Coder.Decode(PByte(B),Length(B))
  102. else
  103. Res:=TEncoding.UTF8.GetAnsiBytes(Coder.Encode(PByte(B),Length(B),FPadding));
  104. F:=TFileStream.Create(GetOptionValue('o','output'),fmCreate);
  105. try
  106. F.WriteBuffer(Res,Length(Res))
  107. finally
  108. F.Free;
  109. end;
  110. end;
  111. begin
  112. CustomApplication:=TDemoApp.Create(Nil);
  113. CustomApplication.Initialize;
  114. CustomApplication.Run;
  115. CustomApplication.Free;
  116. end.