dumppem.lpr 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. {
  2. This file is part of the Free Component Library.
  3. Copyright (c) 2023 by the Free Pascal team.
  4. Demo to dump the contents of a PEM file
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. }
  11. program dumppem;
  12. uses sysutils, types, custapp, classes, fpasn, basenenc;
  13. Type
  14. { TDumpApplication }
  15. TDumpApplication = class(TCustomApplication)
  16. private
  17. FRaw : Boolean;
  18. procedure DumpAsnList(aList: TStrings);
  19. procedure DumpASNList(Prefix: string; aList: TStrings; AStart, aCount: Integer);
  20. function GetBytes(FN: String) : TBytes;
  21. procedure ShowASN(FN: String);
  22. procedure Usage(aError : String);
  23. Protected
  24. procedure DoRun; override;
  25. end;
  26. Procedure TDumpApplication.DumpASNList(Prefix : string; aList : TStrings; AStart,aCount : Integer);
  27. var
  28. I : Integer;
  29. ASize,aType : Integer;
  30. begin
  31. I:=aStart;
  32. While I<=aCount do
  33. begin
  34. ASNParse_GetItem(aList,i,aType,aSize);
  35. writeln(Prefix,'ASNType=',ASNtypeToString(aType),' ASNSize=',aSize,' S="',aList[i],'"');
  36. Inc(I);
  37. end;
  38. end;
  39. Procedure TDumpApplication.DumpAsnList(aList : TStrings);
  40. begin
  41. DumpASNList('',aList,0,aList.Count-1);
  42. end;
  43. function TDumpApplication.GetBytes(FN : String) : TBytes;
  44. Var
  45. L : TStrings;
  46. S : String;
  47. I : Integer;
  48. begin
  49. if FRaw then
  50. Result:=GetFileContents(FN)
  51. else
  52. begin
  53. L:=TStringList.Create;
  54. try
  55. L.LoadFromFile(FN);
  56. S:='';
  57. For I:=1 to L.Count-2 do
  58. S:=S+Trim(L[i]);
  59. Result:=BaseNenc.Base64.Decode(S);
  60. finally
  61. L.Free;
  62. end;
  63. end;
  64. end;
  65. Procedure TDumpApplication.ShowASN(FN : String);
  66. var
  67. Bytes : TBytes;
  68. L : TStrings;
  69. begin
  70. Writeln('ASN.1 Contents of '+FN);
  71. Bytes:=GetBytes(FN);
  72. if HasOption('d','debug') then
  73. Writeln(ASNDebug(Bytes))
  74. else
  75. begin
  76. L:=TStringList.Create;
  77. try
  78. ASNParse(Bytes,L);
  79. DumpAsnList(L);
  80. finally
  81. L.Free;
  82. end;
  83. end;
  84. end;
  85. procedure TDumpApplication.Usage(aError: String);
  86. begin
  87. if (aError<>'') then
  88. Writeln(aError);
  89. Writeln('Usage : ',ExtractFileName(ParamStr(0)),' [options] FileName1 [FileName2..FileNameN]');
  90. Writeln('Where options is one of:');
  91. Writeln('-h --help This help');
  92. Writeln('-r --raw Treat filenames as raw byte dumps (default is to assume .PEM format)');
  93. Writeln('-d --debug Use debug routine to dump content.');
  94. ExitCode:=Ord(aError<>'');
  95. end;
  96. procedure TDumpApplication.DoRun ;
  97. const
  98. Short = 'hrd';
  99. Long : Array of string = ('help','raw','debug');
  100. var
  101. S,FN : String;
  102. NonOpt : TStringDynArray;
  103. begin
  104. Terminate;
  105. S:=CheckOptions(Short,Long);
  106. if S='' then
  107. begin
  108. NonOpt:=GetNonOptions(Short,Long);
  109. if 0=Length(NonOpt) then
  110. S:='One or more filenames must be specified';
  111. end;
  112. if (S<>'') or HasOption('h','help') then
  113. Usage(S)
  114. else
  115. begin
  116. for FN in nonOpt do
  117. ShowAsn(FN);
  118. end;
  119. end;
  120. begin
  121. With TDumpApplication.Create(Nil) do
  122. try
  123. Initialize;
  124. Run;
  125. finally
  126. Free;
  127. end;
  128. end.