![]() |
unicodefähiges Base32Decode
Ich bin auf der Suche nach einer Funktion um Base32 strings zu decodieren.
Es existieren zwar einige Funktionen, nur scheinbar kommt da durch die Umstellung auf Unicode (Delphi 2009) nur Murks bei rum. Hat jemand eventuell einen Tip für mich? |
Re: unicodefähiges Base32Decode
Hast du mal soeine nichtfunktionierende Implementation, welche die ansonsten gefällt, zur Hand?
Und Base32 ist nicht gleich Base32 ... gibt da ja unterschiedliche Zeichensätze z.B.: A..Z 2..7 = oder 0..9 A..H K..N P R..Z = PS: schau mal ins DEC ... vielleicht ist dort ja was dafür drin |
Re: unicodefähiges Base32Decode
Das mit den beiden Versionen hab ich gesehen,
ich benötige die Standardvariante mit A..Z 2..7. DEC - *sich am Hinterkopf kratzt* - da war doch was. Ich schau mal nach, danke für den Tip schon einmal. |
Re: unicodefähiges Base32Decode
Hatte auf die Schnelle keinen "kleinen" Decoder gefunden,
aber wenn du eine kleine Version hast, dann könnte man notfalls mal schauen, was nicht geht. Auch wenn es nicht so aufwändig ist, wäre es dennoch einfacher was Fertiges zu reparieren. |
Re: unicodefähiges Base32Decode
|
Re: unicodefähiges Base32Decode
Dir ist schon bewusst, dass das hier wahrscheinlich kaum einer lesen kann ohne Zugang zu experts-exchange.com?
|
Re: unicodefähiges Base32Decode
Öhm, muss man da nicht einfach nur immer seitenweise nach unten scrollen?
Dann kann man die Lösung einsehen. Ich poste aber hier noch mal den dort gelisteten Code aus dem Jahre 2004:
Delphi-Quellcode:
function Base32Encode(source: String): String;
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; var i: Integer; nr: Int64; begin result := ''; while length(source) >= 5 do begin nr := 0; for i := 1 to 5 do nr := nr * 256 + ord(source[i]); for i := 1 to 8 do begin result := result + alphabet[(nr mod 32) + 1]; nr := nr div 32; end; delete(source, 1, 5); end; nr := 0; if length(source) > 0 then begin for i := 1 to length(source) do nr := nr * 256 + ord(source[i]); for i := 1 to 8 do begin if nr > 0 then begin result := result + alphabet[(nr mod 32) + 1]; nr := nr div 32; end else result := result + '='; end; end; end; function Base32Decode(source: String): String; const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; var i: Integer; nr: Int64; p: Integer; begin result := ''; while length(source) >= 8 do begin nr := 0; for i := 8 downto 1 do begin p := pos(source[i], alphabet); if p > 0 then nr := nr * 32 + p-1 else nr := nr * 32; end; for i := 1 to 5 do begin result := result + chr(nr mod 256); nr := nr div 256; end; delete(source, 1, 8); end; end; |
Re: unicodefähiges Base32Decode
Ich kann in dem Code erstmal nichts Unicode-kritisches erkennen, weshalb die Funktionen unter D2009+ nicht mehr gehen sollten.Ich habe den Code gerade unter D2010 mal ausprobiert und zusätzlich eine Variante mit AnsiStrings ausprobiert (also D2007-kompatibel) und im Ergebnis keine Unterschiede entdecken können.
Seltsamerweise erzeugt der Decode einen teilweise reversierten String des Inputs aus dem Encode, das aber in beiden Versionen. Ich vermute also mal, das die Funktionen an sich einen Bug haben... |
Re: unicodefähiges Base32Decode
Hier Code von Embarcadero, der einwandfrei auch unter D2010 funktioniert:
Delphi-Quellcode:
Ein Vergleich mit den beiden Funktionen von dir lässt auf einen Fehler im Encoder schließen, da der Decoder das gleiche Ergebnis erzeugt.
const
ValidChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; function FromBase32String(const inString : string) : string; var outString : string; aByte : byte; bit_buffer, inIndex, bits_in_buffer, outSize, pos1, pos2, i : integer; begin outSize := (length(inString) * 5 div 8) + 1; outString := StringOfChar(' ',outSize); pos1 := Pos(inString[1],ValidChars) - 1; pos2 := Pos(inString[2],ValidChars) - 1; bit_buffer := pos1 or (pos2 shl 5); if length(inString) < 3 then begin aByte := bit_buffer; outString[1] := Chr(aByte); result := outString; exit; end; bits_in_buffer := 10; inIndex := 3; for i := 1 to outSize + 1 do begin aByte := bit_buffer; outString[i] := Chr(aByte); bit_buffer := bit_buffer shr 8; bits_in_buffer := bits_in_buffer - 8; while (bits_in_buffer < 8) and (inIndex <= length(inString)) do begin pos1 := (Pos(inString[inIndex],ValidChars) - 1); Inc(inIndex); bit_buffer := bit_buffer or (pos1 shl bits_in_buffer); bits_in_buffer := bits_in_buffer + 5; end; end; result := outString; end; function ToBase32String(const inString : string) : string; var currentChar, outString : string; inIndex, validIndex, high : integer; aByte, currentByte : byte; begin high := 5; inIndex := 1; while inIndex <= Length(inString) do begin currentChar := inString[inIndex]; currentByte := Ord(currentChar[1]); if (high > 8) then begin // get the last piece from the current byte, shift it to the right // and increment the byte counter validIndex := (currentByte shr (high - 5)) mod 256; Inc(inIndex); currentChar := inString[inIndex]; currentByte := Ord(currentChar[1]); if (inIndex <> Length(inString) + 1) then begin // if we are not at the end, get the first piece from // the next byte, clear it and shift it to the left aByte := (currentByte shl (16 - high)) mod 256; validIndex := ((aByte shr 3 ) or validIndex) mod 256; end; high := high - 3; end else if(high = 8) then begin Inc(inIndex); validIndex := currentByte shr 3; high := high - 3; end else begin // simply get the stuff from the current byte aByte := currentByte shl (8 - high) mod 256; validIndex := aByte shr 3; high := high + 5; end; currentChar := ValidChars[validIndex + 1]; outString := outString + currentChar; end; result := outString; end; |
Re: unicodefähiges Base32Decode
Delphi-Quellcode:
Ein Bug ist erstmal hier, somit kann der Decoder nut mit String umgehen, welche auch Uncodiert nur vielefache von 5 Zeichen ergeben. (also encodiert 8 Zeichen)
function Base32Decode(source: String): String;
... begin result := ''; while length(source) >= 8 do begin |
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:46 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz