![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Base64 кодирование файлов [D7]
FatBase © (2004-04-22 06:50:00)Может кто нибудь подбросит фу-ю Base64 кодирования файлов, стандартный компонент Delphi использовать неохота. Заранее благодарен.
FatBase (2004-04-25 05:48:00) [1]Всем спасибо проблему решил
SergP (2004-04-24 13:30:00) [2]
> Это насколько я понял для кодирование декодирования текста,
> а как закодировать файлы, кто нить подскажет?
Ну так засунь файл в строку, закодируй, а потом закодированое высунь обратно в файл...
Piter (2004-04-24 13:05:00) [3]это для кодирования
Piter (2004-04-24 13:04:00) [4]
function EncodeBase64(const inStr: string): string;
function Encode_Byte(b: Byte): char;
const
Base64Code: string[64] =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
Result := Base64Code[(b and $3F)+1];
end;
var
i: Integer;
begin
i := 1;
Result := '';
while i <=Length(InStr) do
begin
Result := Result + Encode_Byte(Byte(inStr[i]) shr 2);
Result := Result + Encode_Byte((Byte(inStr[i]) shl 4) or (Byte(inStr[i+1]) shr 4));
if i+1 <=Length(inStr) then
Result := Result + Encode_Byte((Byte(inStr[i+1]) shl 2) or (Byte(inStr[i+2]) shr 6))
else
Result := Result + '=';
if i+2 <=Length(inStr) then
Result := Result + Encode_Byte(Byte(inStr[i+2]))
else
Result := Result + '=';
Inc(i, 3);
end;
end;
FatBase (2004-04-24 12:07:00) [5]xShadow, что это за тип данных TPAByte, дай плиз описание. Это насколько я понял для кодирование декодирования текста, а как закодировать файлы, кто нить подскажет?
FatBase (2004-04-23 05:30:00) [6]xShadow, ограомное тебе спасибо!, выручил!
xShadow (2004-04-22 10:01:00) [7]
//BASE64------------------------------------------------------------------------
Function TArcMan.EncodeBase64( Data: PChar; Len: Integer ): String;
const B64 : array [0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/';
var Ic : Integer;
Pi, Po: TPAByte;
C1 : DWord;
Begin
if Len > 0 then
Begin
SetLength( Result, ( ( Len + 2 ) div 3 ) * 4 );
Pi := Pointer( Data );
Po := Pointer( Result );
for Ic := 1 to Len div 3 do
Begin
C1 := Pi^[0] shl 16 + Pi^[1] shl 8 + Pi^[2];
Po^[0] := Byte( B64[( C1 shr 18 ) and $3f] );
Po^[1] := Byte( B64[( C1 shr 12 ) and $3f] );
Po^[2] := Byte( B64[( C1 shr 6 ) and $3f] );
Po^[3] := Byte( b64[( c1 ) and $3f] );
Inc( DWord( Po ), 4 );
Inc( DWord( Pi ), 3 );
end;
Case Len mod 3 of
1: Begin
C1 := Pi^[0] shl 16;
Po^[0] := Byte( B64[( C1 shr 18 ) and $3f] );
Po^[1] := Byte( B64[( C1 shr 12 ) and $3f] );
Po^[2] := Byte( '=' );
Po^[3] := Byte('=');
end;
2: Begin
C1 := Pi^[0] shl 16 + Pi^[1] shl 8;
Po^[0] := Byte( B64[( C1 shr 18 ) and $3f] );
Po^[1] := Byte( B64[( C1 shr 12 ) and $3f] );
Po^[2] := Byte( B64[( C1 shr 6 ) and $3f] );
Po^[3] := Byte('=');
end;
end;
end else Result := '';
end;
Function TArcMan.EncodeBase64( Data: String ): String;
Begin
Result := EncodeBase64( PChar( Data ), Length( Data ) );
end;
Function TArcMan.DecodeBase64( Data: PChar; Len: Integer ): String;
var I1, I2: Integer;
Pi, Po: TPAByte;
ch1 : Char;
c1 : DWord;
Begin
if ( Len > 0 ) and ( Len mod 4 = 0 ) then
Begin
Len := Len shr 2;
SetLength( Result, Len * 3 );
Pi := Pointer( Data );
Po := Pointer( Result );
for I1 := 1 to Len do
Begin
C1 := 0;
I2 := 0;
While True do
Begin
Ch1 := Char( Pi^[I2] );
Case Ch1 of
'A'..'Z': C1 := C1 or ( DWord( Ch1 ) - Byte( 'A' ) );
'a'..'z': C1 := C1 or ( DWord( Ch1 ) - Byte( 'a' ) + 26 );
'0'..'9': C1 := C1 or ( DWord( Ch1 ) - Byte( '0' ) + 52 );
'+' : C1 := C1 or 62;
'/' : C1 := C1 or 63;
else
Begin
if I2 = 3 then
Begin
Po^[0] := C1 shr 16;
Po^[1] := Byte( C1 shr 8 );
SetLength( Result, Length( Result ) - 1 );
end else
Begin
Po^[0] := C1 shr 10;
SetLength( Result, Length( Result ) - 2 );
end;
Exit;
end;
end;
if I2 = 3 then break;
Inc( I2 );
C1 := C1 shl 6;
end;
Po^[0] := C1 shr 16;
Po^[1] := Byte( C1 shr 8 );
Po^[2] := Byte( C1 );
Inc( DWord( Pi ), 4 );
Inc( DWord( Po ), 3 );
end;
end else Result := '';
end;
Function TArcMan.DecodeBase64( Data: String ): String;
Begin
Result := DecodeBase64( PChar( Data ), Length( Data ) );
end;
//END BASE----------------------------------------------------------------------