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编码昨晚报文通信的载体.比如在对摄 ...
随机推荐
- 第 2 章 前端基础之CSS
一.CSS语法 CSS规则由两个主要的部分构成:选择器,以及一条或多条声明. ''' selector { property: value; property: value; ... property ...
- jQuery遍历集合
jQuery 遍历List集合 $(function(){ var tbody = ""; var obj =[{"name ":"xxxx&quo ...
- Python入门习题7.分别统计输入各类字符个数
例7.用户从键盘输入一行字符,编写一个程序,统计并输出其中的英文字符(包括中文字符).数字.空格和其他字符个数. #字符数统计.py Str = input('请输入一行字符:') alpha = 0 ...
- seaborn教程1——风格选择
原文链接:https://segmentfault.com/a/1190000014915873 Seaborn学习大纲 seaborn的学习内容主要包含以下几个部分: 风格管理 绘图风格设置 颜色风 ...
- C# Winform 窗体界面”假死”后台线程阻塞 解决办法–BeginInvoke
原文:C# Winform 窗体界面"假死"后台线程阻塞 解决办法–BeginInvoke 这个方法可以用在任何后台任务耗时较长,造成界面“假死”界面控件不更新的情况. 比如要要执 ...
- MySQL的数据类型:文本、数字、日期/时间
在MySQL中,有三种主要的类型:文本.数字和日期/时间类型. 文本类型(text):数据类型 描述 CHAR(size) 保存固定长度 ...
- css标签学习-vertical-align标签
今天在学习查阅代码的时候,发现了一个不认识的CSS代码,于是进行学习. <html> <head> <style type="text/css"> ...
- 关于在IE下JavaScript的 Stack overflow at line 错误可能的原因
该错误只在IE中出现,出现该提示的原因主要有两种: 1. 重定义了系统的触发事件名称作为自定义函数名如: onclick / onsubmit … 都是系统保留的事件名称,不允许作为重定义函数名称 ...
- 【知识强化】第五章 中央处理器 5.1 CPU的功能和基本结构
那么在前四章我们已经把数据的表示和运算,存储系统和指令系统都已经给大家讲完了.那么从这一章开始,我们将要讲解中央处理器的内容.那么这一部分内容我们就进入到我们计算机组成原理的一个深水区,它是我们计算机 ...
- python常用函数 P
popleft(iterable) 对应pop,左侧弹出,队列适用. 例子: permutations(iterable, int) itertools的permutations方法可以产生集合的所有 ...