熟悉base64编码的来看看(20)

  • 主题发起人 主题发起人 dragoon1974
  • 开始时间 开始时间
D

dragoon1974

Unregistered / Unconfirmed
GUEST, unregistred user!
需要将流编码为字符串,采用的是delphi中带的EncdDecd单元进行base64编码。现在发现其EncodeStream方法会每隔76字符就添加回车换行符#$D#$A,不知道是何道理,我能删除这部分代码吗。procedure EncodeStream(Input, Output: TStream);type PInteger = ^Integer;var InBuf: array[0..509] of Byte; OutBuf: array[0..1023] of Char; BufPtr: PChar; I, J, K, BytesRead: Integer; Packet: TPacket;begin K := 0; repeat BytesRead := Input.Read(InBuf, SizeOf(InBuf)); I := 0; BufPtr := OutBuf; while I < BytesRead do begin if BytesRead - I < 3 then J := BytesRead - I else J := 3; Packet.i := 0; Packet.b0 := InBuf; if J > 1 then Packet.b1 := InBuf[I + 1]; if J > 2 then Packet.b2 := InBuf[I + 2]; EncodePacket(Packet, J, BufPtr); Inc(I, 3); Inc(BufPtr, 4); Inc(K, 4); [red]if K > 75 then begin BufPtr[0] := #$0D; BufPtr[1] := #$0A; Inc(BufPtr, 2); K := 0; end;[/red] end; Output.Write(Outbuf, BufPtr - PChar(@OutBuf)); until BytesRead = 0;end;
 
这是我以前用到的两个转换函数,希望对你有用:(*function TFileUpload.FileDownload(StrFileName: string): TByteDynArray;var MS: TMemoryStream; SS: TStringStream; Count: Int64; StrFile: string;begin MS := nil; SS := nil; try StrFile := DownloadPath + StrFileName; if not FileExists(StrFile) then begin Result := 'FileName Error'; Exit; end; MS := TMemoryStream.Create(); MS.LoadFromFile(StrFile); Count := MS.Size; SS := TStringStream.Create(''); SS.CopyFrom(MS, Count); FreeAndNil(MS); Result := EncodeString(SS.DataString); FreeAndNil(SS); except Result := 'Error'; end; if MS <> nil then FreeAndNil(MS); if SS <> nil then FreeAndNil(SS);end;function TFileUpload.FileUploadUnZip(StrFileName: string; cData: TByteDynArray): string; stdcall;var MS: TMemoryStream; SS: TStringStream; sData: string; Count: Int64; StrFile: string;begin MS := nil; SS := nil; try StrFile := UploadPath + StrFileName; sData := DecodeString(cData); SS := TStringStream.Create(sData); Count := SS.Size; SS.Position := 0; MS := TMemoryStream.Create(); MS.CopyFrom(SS, Count); MS.SaveToFile(StrFile); FreeAndNil(MS); FreeAndNil(SS); Result := 'Success'; except Result := 'Error'; end; if MS <> nil then FreeAndNil(MS); if SS <> nil then FreeAndNil(SS);end;*)
 
to 草原骏马EncodeString和DecodeString最后还是调用的EncodeStream和DecodeStream,这没有用
 
用这两个函数试试, 来自网上的 faststring 单元。//Encode to Base64function Base64Encode(const Source: AnsiString): AnsiString;var NewLength: Integer;begin NewLength := ((2 + Length(Source)) div 3) * 4; SetLength( Result, NewLength); asm Push ESI Push EDI Push EBX Lea EBX, Base64_Table Inc EBX // Move past String Size (ShortString) Mov EDI, Result Mov EDI, [EDI] Mov ESI, Source Mov EDX, [ESI-4] //Length of Input String@WriteFirst2: CMP EDX, 0 JLE @Done MOV AL, [ESI] SHR AL, 2{$IFDEF VER140} // Changes to BASM in D6 XLATB{$ELSE} XLAT{$ENDIF} MOV [EDI], AL INC EDI MOV AL, [ESI + 1] MOV AH, [ESI] SHR AX, 4 AND AL, 63{$IFDEF VER140} // Changes to BASM in D6 XLATB{$ELSE} XLAT{$ENDIF} MOV [EDI], AL INC EDI CMP EDX, 1 JNE @Write3 MOV AL, 61 // Add == MOV [EDI], AL INC EDI MOV [EDI], AL INC EDI JMP @Done@Write3: MOV AL, [ESI + 2] MOV AH, [ESI + 1] SHR AX, 6 AND AL, 63{$IFDEF VER140} // Changes to BASM in D6 XLATB{$ELSE} XLAT{$ENDIF} MOV [EDI], AL INC EDI CMP EDX, 2 JNE @Write4 MOV AL, 61 // Add = MOV [EDI], AL INC EDI JMP @Done@Write4: MOV AL, [ESI + 2] AND AL, 63{$IFDEF VER140} // Changes to BASM in D6 XLATB{$ELSE} XLAT{$ENDIF} MOV [EDI], AL INC EDI ADD ESI, 3 SUB EDX, 3 JMP @WriteFirst2@done: Pop EBX Pop EDI Pop ESI end;end;//Decode Base64function Base64Decode(const Source: string): string;var NewLength: Integer;begin{ NB: On invalid input this routine will simply skip the bad data, abetter solution would probably report the error ESI -> Source String EDI -> Result String ECX -> length of Source (number of DWords) EAX -> 32 Bits from Source EDX -> 24 Bits Decoded BL -> Current number of bytes decoded} SetLength( Result, (Length(Source) div 4) * 3); NewLength := 0; asm Push ESI Push EDI Push EBX Mov ESI, Source Mov EDI, Result //Result address Mov EDI, [EDI] Or ESI,ESI // Nil Strings Jz @Done Mov ECX, [ESI-4] Shr ECX,2 // DWord Count JeCxZ @Error // Empty String Cld jmp @Read4 @Next: Dec ECX Jz @Done @Read4: lodsd Xor BL, BL Xor EDX, EDX Call @DecodeTo6Bits Shl EDX, 6 Shr EAX,8 Call @DecodeTo6Bits Shl EDX, 6 Shr EAX,8 Call @DecodeTo6Bits Shl EDX, 6 Shr EAX,8 Call @DecodeTo6Bits // Write Word Or BL, BL JZ @Next // No Data Dec BL Or BL, BL JZ @Next // Minimum of 2 decode values to translate to 1 byte Mov EAX, EDX Cmp BL, 2 JL @WriteByte Rol EAX, 8 BSWAP EAX StoSW Add NewLength, 2 @WriteByte: Cmp BL, 2 JE @Next SHR EAX, 16 StoSB Inc NewLength jmp @Next @Error: jmp @Done @DecodeTo6Bits: @TestLower: Cmp AL, 'a' Jl @TestCaps Cmp AL, 'z' Jg @Skip Sub AL, 71 Jmp @Finish @TestCaps: Cmp AL, 'A' Jl @TestEqual Cmp AL, 'Z' Jg @Skip Sub AL, 65 Jmp @Finish @TestEqual: Cmp AL, '=' Jne @TestNum // Skip byte ret @TestNum: Cmp AL, '9' Jg @Skip Cmp AL, '0' JL @TestSlash Add AL, 4 Jmp @Finish @TestSlash: Cmp AL, '/' Jne @TestPlus Mov AL, 63 Jmp @Finish @TestPlus: Cmp AL, '+' Jne @Skip Mov AL, 62 @Finish: Or DL, AL Inc BL @Skip: Ret @Done: Pop EBX Pop EDI Pop ESI end; SetLength( Result, NewLength); // Trim off the excessend;
 
可以删除换行的代码,不过解码函数也得做修改
 
多人接受答案了。
 
后退
顶部