mb_convert_encoding gives other result than iconv for UTF7 String

InputString:

$value = "ACM=1,1+eval(1+name+(+ACM-1),ACM)";

Method with mb_convert_encoding

mb_convert_encoding($value, 'UTF-8', 'UTF-7');

Method with iconv

iconv("utf-7", "utf-8//IGNORE", $value);

Result differs, for mb_convert_encoding

ACM=1,1競(1鶩(#1),ACM)

for iconv

ACM=1,1競뗺皦(#1),ACM)

My Problem is, that i want to run a PHP-IDS Centrifuge implemented in JS, where i don't have mb functions. This string is a testcase by PHP-IDS and i'm not able to get the same results for this.

Does someone know, how i get the same results as mb with js? Or is it a bug in mb?

Answers


After reading the RFC 2152 it seems to me mb_convert_encoding is very strict in handling malformed input and iconv is trying to hard sanitizing. But i think i got the wanted behaviour covered with this:

function convertUTF7toUTF8(string) {
    var b64Token = /\+([a-z\d\/+]*\-?)/gi,
        hex, len, replace, i;

    return string.replace(b64Token, function(match, grp) {
        hex = Buffer(grp, 'base64');
        len = hex.length >> 1 << 1;
        replace = '';
        i = 1;

        for(i; i < len; i = i + 2) {
            replace += String.fromCharCode(hex.readUInt16BE(i - 1));
        }

        return replace;
    });
}

Minimized:

function convertUTF7toUTF81(s){return s.replace(/\+([a-z\d\/+]*\-?)/gi,function(m,a){var i=0,c='',h=Buffer(a,'base64'),l=h.length>>1<<1-1;while(i<l)c+=String.fromCharCode(h.readUInt16BE(i++*2));return c})};

Need Your Help

getting place name from Json using google API

java javascript ajax html5

When I run this code I get a blank screen and nothing gets displayed.What changes I have to make in order to get this right and where am I going wrong?

Why does method require a return value after statement which is always return true?

java if-statement

Why does this method (test) need a return value (it is always true)?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.