Last active
March 21, 2017 03:28
-
-
Save freeonterminate/f05236b776f46309654a35b837a2da9b to your computer and use it in GitHub Desktop.
Base64 Encoder / Decoder By Asm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
* 概要 Base64 エンコード | |
* 引数 iSrc エンコードしたいストリーム | |
* iDest エンコード結果格納先 | |
* iSepCount セパレータを入れる場所(32 なら 32 個エンコードした所で入る) | |
* iSep セパレータ | |
*) | |
procedure EncodeBase64( | |
const iSrc, iDest: TStream; | |
iSepCount: Integer = 0; | |
const iSep: String = sLineBreak); | |
var | |
Buff: Cardinal; | |
Buff2: Cardinal; | |
Count: Integer; | |
SepCount: Integer; | |
begin | |
asm | |
//push ebx | |
// 改行位置 | |
mov eax, iSepCount | |
shr eax, 2 | |
mov iSepCount, eax | |
mov SepCount, eax | |
@@Loop: | |
xor eax, eax | |
mov Buff, eax | |
mov Buff2, eax | |
// データ読み込み | |
mov eax, iSrc | |
mov ebx, [eax] | |
lea edx, Buff | |
mov ecx, 3 | |
call [ebx + VMTOFFSET TStream.Read] | |
inc eax | |
mov Count, eax | |
cmp eax, 2 | |
jc @@4 | |
// | |
xor ebx, ebx | |
mov edx, [Buff] | |
bswap edx | |
mov ecx, 4 | |
@@1: | |
xor eax, eax | |
shld eax, edx, 6 | |
shl edx, 6 | |
mov al, [eax + GBaseEncodeTable] | |
sub Count, 1 | |
jnc @@2 | |
mov eax, CBasePadding | |
mov Count, 0 | |
@@2: | |
shrd ebx, eax, 8 | |
dec ecx | |
jnz @@1 | |
// データ書き込み | |
lea edx, Buff2 | |
mov [edx], ebx | |
mov ecx, 4 | |
mov eax, iDest | |
mov ebx, [eax] | |
call [ebx + VMTOFFSET TStream.Write] | |
mov eax, iSepCount | |
test eax, eax | |
jz @@3 | |
dec SepCount | |
jnz @@3 | |
// 改行書き込み | |
mov SepCount, eax | |
mov eax, iDest | |
lea edx, iSep | |
mov edx, [edx] | |
mov ecx, [edx - 4] | |
mov ebx, [eax] | |
call [ebx + VMTOFFSET TStream.Write] | |
@@3: | |
jmp @@Loop | |
@@4: | |
mov eax, iSepCount | |
test eax, eax | |
jz @@5 | |
cmp eax, SepCount | |
jz @@5 | |
// 改行書き込み | |
mov eax, iDest | |
lea edx, iSep | |
mov edx, [edx] | |
mov ecx, [edx - 4] | |
mov ebx, [eax] | |
call [ebx + VMTOFFSET TStream.Write] | |
@@5: | |
//pop ebx | |
end; | |
end; | |
(* | |
* 概要 Base64 デコード | |
* 引数 iSrc デコードしたいストリーム | |
* iDest デコード結果格納先 | |
*) | |
procedure DecodeBase64(const iSrc, iDest: TStream); | |
var | |
Buff: Cardinal; | |
Buff2: Cardinal; | |
Count: Cardinal; | |
DecodedCount: Cardinal; | |
begin | |
asm | |
//push ebx | |
mov DecodedCount, 4 | |
@@Loop: | |
// データ読み込み | |
mov eax, iSrc | |
mov ebx, [eax] | |
lea edx, Buff | |
mov ecx, 4 | |
call [ebx + VMTOFFSET TStream.Read] | |
mov Count, eax | |
test eax, eax | |
jz @@4 | |
mov ebx, Buff | |
mov edx, Buff2 | |
mov ecx, 4 | |
@@1: | |
xor eax, eax | |
mov al, bl | |
shr ebx, 8 | |
cmp eax, CBaseEncodeMinValue | |
jc @@3 | |
cmp eax, CBasePadding | |
jnz @@2 | |
xor eax, eax | |
dec Count | |
@@2: | |
mov al, [eax + GBaseDecodeTable] | |
shl eax, 26 | |
shld edx, eax, 6 | |
dec DecodedCount | |
jnz @@3 | |
push ebx | |
push ecx | |
bswap edx | |
shr edx, 8 | |
mov Buff2, edx | |
lea edx, Buff2 | |
mov ecx, Count | |
dec ecx | |
mov eax, iDest | |
mov ebx, [eax] | |
call dword ptr [ebx + VMTOFFSET TStream.Write] | |
mov DecodedCount, 4 | |
pop ecx | |
pop ebx | |
@@3: | |
dec ecx | |
jnz @@1 | |
mov Buff2, edx | |
jmp @@Loop | |
@@4: | |
//pop ebx | |
end; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment