Skip to content

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

Closed

Description

@abenmrad

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.

Activity

  1. jedisct1 commented on May 29, 2016

    @jedisct1
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions