sys.base64.pas
unit sys.base64; //cxg 2025-1-9 fit(delphi+fpc) interface uses {$ifndef fpc} System.NetEncoding, {$else} base64, {$endif} SysUtils, Classes; function base64Encode(const val: string): string; overload; function base64Encode(const val: TBytes): TBytes; overload; procedure base64Encode(const input, output: TStream); overload; function base64Decode(const val: string): string; overload; function base64Decode(const val: TBytes): TBytes; overload; procedure base64Decode(input, output: TStream); overload; implementation procedure base64Decode(input, output: TStream); {$ifdef fpc} var b64Stream: TBase64DecodingStream; outStream: TStringStream; {$endif} begin {$ifndef fpc} TNetEncoding.Base64.Decode(input, output); {$else} outStream := TStringStream.Create(''); b64Stream := TBase64DecodingStream.Create(outStream); try b64Stream.CopyFrom(input, input.Size); finally b64Stream.Free; end; {$endif} end; function base64Decode(const val: TBytes): TBytes; {$ifdef fpc} var b64Stream: TBase64DecodingStream; outStream: TStringStream; {$endif} begin {$ifndef fpc} Result := TNetEncoding.Base64.Decode(val); {$else} outStream := TStringStream.Create(''); b64Stream := TBase64DecodingStream.Create(outStream); try b64Stream.Write(PByte(val)^, length(val)); setlength(result, outStream.Size); outStream.Read(PByte(result)^, outStream.Size); finally outStream.Free; b64Stream.Free; end; {$endif} end; function base64Decode(const val: string): string; begin {$ifndef fpc} Result := TNetEncoding.Base64.Decode(val); {$else} result := DecodeStringBase64(val); {$endif} end; function base64Encode(const val: TBytes): TBytes; {$ifdef fpc} var b64Stream: TBase64EncodingStream; outStream: TStringStream; {$endif} begin {$ifndef fpc} Result := TNetEncoding.Base64.Encode(val); {$else} outStream := TStringStream.Create(''); b64Stream := TBase64EncodingStream.Create(outStream); try b64Stream.Write(PByte(val)^, length(val)); setlength(result, outStream.Size); outStream.Read(PByte(result)^, outStream.Size); finally outStream.Free; b64Stream.Free; end; {$endif} end; procedure base64Encode(const input, output: TStream); {$ifdef fpc} var b64Stream: TBase64EncodingStream; outStream: TStringStream; {$endif} begin {$ifndef fpc} TNetEncoding.Base64.Encode(input, output); {$else} outStream := TStringStream.Create(''); b64Stream := TBase64EncodingStream.Create(outStream); try b64Stream.CopyFrom(input, input.Size); finally b64Stream.Free; end; {$endif} end; function base64Encode(const val: string): string; begin {$ifndef fpc} Result := TNetEncoding.Base64.Encode(val); {$else} result := EncodeStringBase64(val); {$endif} end; end.
标签:pas,end,fpc,val,base64,outStream,sys,b64Stream,endif From: https://www.cnblogs.com/hnxxcxg/p/18662830