Delphi 自带了 Base64 编解码的单元
Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,
这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因。
这个单元提供两套四个公开函数: 对流的编解码: procedure EncodeStream(Input, Output: TStream);
// 编码 procedure DecodeStream(Input, Output: TStream); // 解码
// 对字符串的编解码:
function EncodeString(const Input: string): string;
// 编码
function DecodeString(const Input: string): string;
// 解码 这几个函数在帮助中没有。应该不算是标准库中的函数。
{********************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 2000, 2001 Borland Software Corporation }
{ }
{********************************************************}
unit EncdDecd;
{ Have string use stream encoding since that logic wraps properly }
interface uses Classes;
procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);
function EncodeString(const Input: string): string;
function DecodeString(const Input: string): string; implementation
const
EncodeTable: array[..] of Char =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/';
DecodeTable: array[#..#] of Integer = (
Byte('='), , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , );
type PPacket = ^TPacket;
TPacket = packed record
case Integer of : (b0, b1, b2, b3: Byte);
: (i: Integer);
: (a: array[..] of Byte);
: (c: array[..] of Char);
end;
procedure EncodePacket(const Packet: TPacket; NumChars: Integer; OutBuf: PChar);
begin
OutBuf[] := EnCodeTable[Packet.a[] shr ];
OutBuf[] := EnCodeTable[((Packet.a[] shl ) or (Packet.a[] shr )) and $0000003f];
if NumChars < then
OutBuf[] := '='
else
OutBuf[] := EnCodeTable[((Packet.a[] shl ) or (Packet.a[] shr )) and $0000003f];
if NumChars < then
OutBuf[] := '='
else
OutBuf[] := EnCodeTable[Packet.a[] and $0000003f];
end;
function DecodePacket(InBuf: PChar; var nChars: Integer): TPacket;
begin
Result.a[] := (DecodeTable[InBuf[]] shl ) or (DecodeTable[InBuf[]] shr );
NChars := ;
if InBuf[] <> '=' then
begin
Inc(NChars);
Result.a[] := Byte((DecodeTable[InBuf[]] shl ) or (DecodeTable[InBuf[]] shr ));
end;
if InBuf[] <> '=' then
begin
Inc(NChars);
Result.a[] := Byte((DecodeTable[InBuf[]] shl ) or DecodeTable[InBuf[]]);
end;
end;
procedure EncodeStream(Input, Output: TStream);
type PInteger = ^Integer;
var InBuf: array[..] of Byte;
OutBuf: array[..] of Char;
BufPtr: PChar; I, J, K, BytesRead: Integer;
Packet: TPacket;
begin
K := ;
repeat BytesRead := Input.Read(InBuf, SizeOf(InBuf)); I := ;
BufPtr := OutBuf;
while I < BytesRead do
begin
if BytesRead - I < then
J := BytesRead - I
else
J := ;
Packet.i := ;
Packet.b0 := InBuf[I];
if J > then
Packet.b1 := InBuf[I + ];
if J > then
Packet.b2 := InBuf[I + ];
EncodePacket(Packet, J, BufPtr);
Inc(I, );
Inc(BufPtr, );
Inc(K, );
if K > then
begin
BufPtr[] := #$0D;
BufPtr[] := #$0A;
Inc(BufPtr, );
K := ;
end;
end;
Output.Write(Outbuf, BufPtr - PChar(@OutBuf));
until BytesRead = ; end;
procedure DecodeStream(Input, Output: TStream);
var
InBuf: array[..] of Char;
OutBuf: array[..] of Byte;
InBufPtr, OutBufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket;
procedure SkipWhite;
var
C: Char;
NumRead: Integer;
begin
while True do
begin NumRead := Input.Read(C, );
if NumRead = then
begin
if C in [''..'','A'..'Z','a'..'z','+','/','='] then
begin
Input.Position := Input.Position - ;
Break;
end;
end
else
Break;
end;
end;
function ReadInput: Integer;
var WhiteFound, EndReached : Boolean;
CntRead, Idx, IdxEnd: Integer;
begin
IdxEnd:= ; repeat WhiteFound := False; CntRead := Input.Read(InBuf[IdxEnd], (SizeOf(InBuf)-IdxEnd));
EndReached := CntRead < (SizeOf(InBuf)-IdxEnd);
Idx := IdxEnd; IdxEnd := CntRead + IdxEnd;
while (Idx < IdxEnd) do begin if not (InBuf[Idx] in [''..'','A'..'Z','a'..'z','+','/','=']) then
begin
Dec(IdxEnd);
if Idx < IdxEnd then
Move(InBuf[Idx+], InBuf[Idx], IdxEnd-Idx);
WhiteFound := True;
end
else
Inc(Idx);
end;
until (not WhiteFound) or (EndReached);
Result := IdxEnd;
end;
begin
repeat SkipWhite;
{ BytesRead := Input.Read(InBuf, SizeOf(InBuf)); }
BytesRead := ReadInput;
InBufPtr := InBuf;
OutBufPtr := @OutBuf;
I := ;
while I < BytesRead do
begin
Packet := DecodePacket(InBufPtr, J);
K := ;
while J > do
begin
OutBufPtr^ := Char(Packet.a[K]); Inc(OutBufPtr);
Dec(J);
Inc(K);
end;
Inc(InBufPtr, );
Inc(I, );
end;
Output.Write(OutBuf, OutBufPtr - PChar(@OutBuf));
until BytesRead = ;
end;
function EncodeString(const Input: string): string;
var InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('');
try
EncodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;
function DecodeString(const Input: string): string;
var InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('');
try
DecodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;
end.
Delphi 自带了 Base64 编解码的单元的更多相关文章
- Delphi 自带的 Base64 编解码函数
今天帮别人解决一个关于 Base64 编解码的问题,竟然发现 Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因. 这个 ...
- Delphi Base64 编解码函数
Delphi 自带 Base64 编解码的单元, EncdDecd这个单元提供两套四个公开函数: 对流的编解码:procedure EncodeStream(Input, Output: TStrea ...
- Notepad++插件Base64编解码
我们平常进行Base64编码需要自己写代码转换, 或者使用其他人编写的小工具程序, 也可以使用在线base64编码工具, 现在我们还可以使用Notepad++自带的插件, 进行Base64编码和解码, ...
- ios Base64编解码工具类及使用
为了避免明码传递http内容,可以用base64编码后传输,收到方再解码,也方便了2进制数据的字符串式传输. 对于ios来说,google给提供了一个很好的工具类,方便进行base64编解码,当然也可 ...
- Java实现BASE64编解码
Java实现BASE64编解码 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...
- openssl命令行Base64编解码
openssl对base64编解码的规范支持较差,用它编解码的结果别的语言如php处理很不方便,注意的几点整理如下 1,如果php加密结果做base64编码长度小于64,则需要添加一个换行符opens ...
- python rsa 加密解密 (编解码,base64编解码)
最近有需求,需要研究一下RSA加密解密安全:在网上百度了一下例子文章,很少有文章介绍怎么保存.传输.打印加密后的文本信息,都是千篇一律的.直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密.仔 ...
- python base64 编解码,转换成Opencv,PIL.Image图片格式
二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...
- EasyDarwin开源流媒体云平台中boost Base64编解码后与源长度不匹配的bug
本文转自EasyDarwin团队Alex的博客:http://blog.csdn.net/cai6811376 EasyDarwin云平台中部分协议使用了Base64编码昨晚报文通信的载体.比如在对摄 ...
随机推荐
- day37—javascript对表格table的操作应用(二)
转行学开发,代码100天——2018-04-22 昨天学习了JavaScript对table的基本操作,包括表格的创建,表格元素的获取,隔行换色及鼠标动作等.今天主要学习table的搜索查询及排序操作 ...
- python自定义异常实例详解
python自定义异常实例详解 本文通过两种方法对Python 自定义异常进行讲解,第一种:创建一个新的exception类来拥有自己的异常,第二种:raise 唯一的一个参数指定了要被抛出的异常 1 ...
- 从零开始创建 symfony-cmf
前提: 官方 https://symfony.com/doc/master/cmf/quick_tour/the_big_picture.html#setting-up-the-database 由于 ...
- 06 案例篇:系统的 CPU 使用率很高,但为啥却找不到高 CPU 的应用?
上一节我讲了 CPU 使用率是什么,并通过一个案例教你使用 top.vmstat.pidstat 等工具,排查高 CPU 使用率的进程,然后再使用 perf top 工具,定位应用内部函数的问题.不过 ...
- 动态规划-递推-HDU2048
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2048 全错=全不匹配 设当前全错的个数是dp[n] 那么前(n-1)个全错的话,第n个数就可以从前(n- ...
- 自动发现项目中的url
def check_url_exclude(url): """ 判断url是否需要自动被发现,如果不是则移除 :param url: 自动发现的url :return: ...
- 最长公共上升子序列 (LIS+LCS+记录)
[题目描述] 给出两个序列,求出最长公共上升子序列的长度,并输出其中一个解. [题目链接] http://noi.openjudge.cn/ch0206/2000/ [算法] 经典问题,结合了LIS和 ...
- Codeforces - 1194B - Yet Another Crosses Problem - 水题
https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...
- python学习第三十二天函数的闭包
python函数中嵌套另外一个函数,另外一个函数形成一个封闭的环境,里面的那个函数叫做函数的闭包,函数的闭包好处可以保护函数里面的变量,下面讲述函数闭包的实例和用法 1,函数闭包的实例 a='cat' ...
- THUPC/CTS/APIO2019划水记
THUPC:划水的咸鱼 CTS:打铁 APIO:压线cu 终于又回归了文化课. 落下10天的课程,OI又得停一停了 这次划水,又见识了许多的神仙,再一次被吊打 5.11~5.20,有太多的事情需要回忆 ...