A little change inside the function also must be made to make it work:

The line with htoi2 need a little change, the complete code is this:

function UrlDecode(const S : AnsiString; SrcCodePage: Cardinal = CP_ACP;
  DetectUtf8: Boolean = TRUE) : String;
var
    I, J, L : Integer;
    U8Str   : AnsiString;
    Ch      : AnsiChar;
begin
    L := Length(S);
    SetLength(U8Str, L);
    I := 1;
    J := 0;
    while (I <= L) and (S[I] <> '&') do begin
        Ch := AnsiChar(S[I]);
        if Ch = '%' then begin
            Ch := AnsiChar(htoi2(PAnsiChar(@S[I + 1])));
            Inc(I, 2);
        end
        else if Ch = '+' then
            Ch := ' ';
        Inc(J);
        U8Str[J] := Ch;
        Inc(I);
    end;
    SetLength(U8Str, J);
    if (SrcCodePage = CP_UTF8) or (DetectUtf8 and IsUtf8Valid(U8Str)) then
{$IFDEF COMPILER12_UP}
        Result := Utf8ToStringW(U8Str)
    else
        Result := AnsiToUnicode(U8Str, SrcCodePage);
{$ELSE}
        Result := Utf8ToStringA(U8Str)
    else
        Result := U8Str;
{$ENDIF}
end;

Regards
Bjørnar

-----Original Message-----
From: twsocket-boun...@elists.org [mailto:twsocket-boun...@elists.org] On 
Behalf Of Bjørnar Nielsen
Sent: 5. august 2010 12:52
To: ICS support mailing (twsocket@elists.org)
Subject: [twsocket] Found a bug and made a fix in function UrlDecode

Proposal to a fix on bug in UrlDecode in OverbyteIcsUrl.pas and 
OverbyteIcsHttpSrv.pas.

When calling the function like this:
Memo2->Text = UrlDecode("Ã...ge",CP_ACP,false); // Ã...ge is 
Memo2->UTF8encoding of Åge

The resulting text in Memo2 is "Ã&ge" and is impossible to UTF8-dekode back to 
the original text.

The fix is to change this:
function UrlDecode(const S : String; SrcCodePage: Cardinal = CP_ACP;
  DetectUtf8: Boolean = TRUE) : String;

To this:
function UrlDecode(const S : AnsiString; SrcCodePage: Cardinal = CP_ACP;
  DetectUtf8: Boolean = TRUE) : String;

Anyone have any comment on this fix?

Regards Bjørnar

--
To unsubscribe or change your settings for TWSocket mailing list please goto 
http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to