Debian Codegraph @

src/compatibility/delphi-only/base64.pas

raw
1 {
2 Copyright 2021-2021 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" 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 ----------------------------------------------------------------------------
14 }
15
16 { Base64 decoding, with API compatible to FPC Base64 unit. }
17 unit Base64;
18
19 interface
20
21 uses SysUtils, Classes, System.NetEncoding;
22
23 type
24 TBase64DecodingMode = (bdmStrict, bdmMIME);
25
26 { Decode base64 stream.
27
28 Note: Underneath, in Delphi, the whole decoding (so, reading Source)
29 is done immediately at constructor.
30
31 Note: in Delphi, AMode is ignored.
32
33 The base64 string of characters (input) is assumed to be String,
34 i.e. Delphi UTF-16 string.
35
36 The output is just binary data, what it is depends on what was
37 encoded. It may be UTF-8 characters in AnsiString. }
38 TBase64DecodingStream = class(TMemoryStream)
39 strict private
40 FSource: TStream;
41 FSourceOwner: Boolean;
42 public
43 constructor Create(const ASource: TStream); overload;
44 constructor Create(const ASource: TStream; const AMode: TBase64DecodingMode); overload;
45 destructor Destroy; override;
46 property SourceOwner: Boolean read FSourceOwner write FSourceOwner default false;
47 end;
48
49 { Encode base64 stream.
50
51 Note: Underneath, in Delphi, the whole encoding (so, writing to Destination)
52 is done at destructor.
53
54 The base64 string of characters (output) is assumed to be String,
55 i.e. Delphi UTF-16 string.
56
57 The input is just binary data, whatever you want to encode.
58 encoded. It may be UTF-8 characters in AnsiString, e.g. you can write
59 them using CastleClassUtils.WriteStr. }
60 TBase64EncodingStream = class(TMemoryStream)
61 strict private
62 FDestination: TStream;
63 FDestinationOwner: Boolean;
64 public
65 constructor Create(const ADestination: TStream);
66 destructor Destroy; override;
67 property DestinationOwner: Boolean read FDestinationOwner write FDestinationOwner default false;
68 end;
69
70 implementation
71
72 { TBase64DecodingStream ------------------------------------------------------ }
73
74 constructor TBase64DecodingStream.Create(const ASource: TStream);
75 begin
76 inherited Create;
77 FSource := ASource;
78 while TNetEncoding.Base64.Decode(FSource, Self) <> 0 do ;
79 Position := 0;
80 end;
81
82 constructor TBase64DecodingStream.Create(const ASource: TStream;
83 const AMode: TBase64DecodingMode);
84 begin
85 Create(ASource);
86 end;
87
88 destructor TBase64DecodingStream.Destroy;
89 begin
90 if SourceOwner then
91 FreeAndNil(FSource);
92 inherited;
93 end;
94
95 { TBase64EncodingStream ------------------------------------------------------ }
96
97 constructor TBase64EncodingStream.Create(const ADestination: TStream);
98 begin
99 inherited Create;
100 FDestination := ADestination;
101 end;
102
103 destructor TBase64EncodingStream.Destroy;
104 begin
105 if FDestination <> nil then
106 begin
107 Position := 0;
108 while TNetEncoding.Base64.Encode(Self, FDestination) <> 0 do ;
109 end;
110
111 if DestinationOwner then
112 FreeAndNil(FDestination);
113 inherited;
114 end;
115
116 end.