Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug fix] Support for to_string calls with big Uint8Arrays #57

Closed
abenmrad opened this issue May 28, 2016 · 1 comment
Closed

[Bug fix] Support for to_string calls with big Uint8Arrays #57

abenmrad opened this issue May 28, 2016 · 1 comment

Comments

@abenmrad
Copy link
Contributor

abenmrad commented May 28, 2016

It seems that, despite not being defined the ECMAScript spec, JavaScript runtimes tend to impose a limit on how many arguments a function can receive. And since the current to_string implementation in the libsodium wrapper uses a Function.call paradigm, you tend to hit that wall at some point.

Here is a fix (among other possible ones) :

/*
The main idea is to split the buffer into a smaller buffers,
call to_string on each one of them, and concatenate the results
*/
var toStringChunkSize = 32767;

function to_string(bytes) {
        if (typeof TextDecoder === "function") {
            return new TextDecoder("utf-8", {fatal: true}).decode(bytes);
        }

    var numChunks = Math.ceil(bytes.length / toStringChunkSize);
    if (numChunks > 1){
        var totalString = '';
        for (var i = 0; i < numChunks; i++){
            totalString += to_string(Array.prototype.slice.call(bytes, i * toStringChunkSize, (i + 1) * toStringChunkSize));
        }
        return totalString;
    }

    try {
        return decodeURIComponent(escape(String.fromCharCode.apply(null, bytes)));
    }
    catch (_) {
        throw new TypeError("The encoded data was not valid.");
    }
}

PS: Sorry for not submitting this as a PR.

@jedisct1
Copy link
Owner

Unfortunately, the bytes cannot be split at arbitrary positions, because the split could then happen in the middle of a UTF-8 sequence :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants