Commit 7baf1959 authored by Jérémy Lal's avatar Jérémy Lal

New upstream version 2.3.2

parent fe608b2f
......@@ -91,7 +91,7 @@ Encode from one character encoding to another:
var iconv = new Iconv('UTF-8', 'ISO-8859-1');
var buffer = iconv.convert('Hello, world!');
var buffer2 = iconv.convert(new Buffer('Hello, world!'));
var buffer2 = iconv.convert(Buffer.from('Hello, world!'));
assert.equals(buffer.inspect(), buffer2.inspect());
// do something useful with the buffers
......
......@@ -20,6 +20,7 @@ exports.Iconv = Iconv;
var stream = require('stream');
var util = require('util');
var Buffer = require('safer-buffer').Buffer;
var bindings;
try {
......@@ -54,22 +55,21 @@ function Iconv(fromEncoding, toEncoding)
toEncoding + ' is not supported.');
}
var convert_ = convert.bind({ conv_: conv });
var context_ = { trailer: null };
this.convert = function(input, encoding) {
if (typeof(input) === 'string') {
input = new Buffer(input, encoding || 'utf8');
input = Buffer.from(input, encoding || 'utf8');
}
return convert_(input, null);
return convert(conv, input, null);
};
this.write = function(input, encoding) {
if (typeof(input) === 'string') {
input = new Buffer(input, encoding || 'utf8');
input = Buffer.from(input, encoding || 'utf8');
}
try {
var buf = convert_(input, context_);
var buf = convert(conv, input, context_);
}
catch (e) {
this.emit('error', e);
......@@ -98,8 +98,8 @@ function fixEncoding(encoding)
return /^utf[^-]/i.test(encoding) ? 'utf-' + encoding.substr(3) : encoding;
}
function convert(input, context) {
if (!(input instanceof Buffer) && input !== FLUSH) {
function convert(conv, input, context) {
if (!Buffer.isBuffer(input) && input !== FLUSH) {
throw new Error('Bad argument.'); // Not a buffer or a string.
}
if (context !== null && context.trailer !== null && input === FLUSH) {
......@@ -107,20 +107,20 @@ function convert(input, context) {
}
if (context !== null && context.trailer !== null) {
// Prepend input buffer with trailer from last chunk.
var newbuf = new Buffer(context.trailer.length + input.length);
var newbuf = Buffer.alloc(context.trailer.length + input.length);
context.trailer.copy(newbuf, 0, 0, context.trailer.length);
input.copy(newbuf, context.trailer.length, 0, input.length);
context.trailer = null;
input = newbuf;
}
var output = new Buffer(input.length * 2); // To a first approximation.
var output = Buffer.alloc(input.length * 2); // To a first approximation.
var input_start = 0;
var output_start = 0;
var input_size = input.length;
var output_size = output.length;
var out = [0,0];
for (;;) {
var errno = bindings.convert(this.conv_,
var errno = bindings.convert(conv,
input,
input_start,
input_size,
......@@ -138,7 +138,7 @@ function convert(input, context) {
if (errno) {
if (errno === E2BIG) {
output_size += output.length;
var newbuf = new Buffer(output.length * 2);
var newbuf = Buffer.alloc(output.length * 2);
output.copy(newbuf, 0, 0, output_start);
output = newbuf;
continue;
......
{
"name": "iconv",
"main": "./lib/iconv",
"version": "2.3.0",
"version": "2.3.2",
"description": "Text recoding in JavaScript for fun and profit!",
"homepage": "https://github.com/bnoordhuis/node-iconv",
"repository": {
......@@ -18,7 +18,8 @@
"license": "ISC",
"readmeFilename": "README.md",
"dependencies": {
"nan": "^2.3.5"
"nan": "^2.11.1",
"safer-buffer": "^2.1.2"
},
"scripts": {
"test": "node test/run-tests.js"
......
......@@ -32,7 +32,6 @@ namespace
using v8::Array;
using v8::Boolean;
using v8::FunctionTemplate;
using v8::Handle;
using v8::Integer;
using v8::Local;
using v8::Null;
......@@ -62,7 +61,7 @@ struct Iconv
delete data.GetParameter();
}
static void Initialize(Handle<Object> obj)
static void Initialize(Local<Object> obj)
{
Local<ObjectTemplate> t = Nan::New<ObjectTemplate>();
t->SetInternalFieldCount(1);
......@@ -81,8 +80,8 @@ struct Iconv
static NAN_METHOD(Make)
{
String::Utf8Value from_encoding(info[0]);
String::Utf8Value to_encoding(info[1]);
Nan::Utf8String from_encoding(info[0]);
Nan::Utf8String to_encoding(info[1]);
iconv_t conv = iconv_open(*to_encoding, *from_encoding);
if (conv == reinterpret_cast<iconv_t>(-1)) {
return info.GetReturnValue().SetNull();
......@@ -100,14 +99,14 @@ struct Iconv
{
Iconv* iv = static_cast<Iconv*>(
Nan::GetInternalFieldPointer(info[0].As<Object>(), 0));
const bool is_flush = info[8]->BooleanValue();
const bool is_flush = Nan::To<bool>(info[8]).FromJust();
ICONV_CONST char* input_buf =
is_flush ? NULL : node::Buffer::Data(info[1].As<Object>());
size_t input_start = info[2]->Uint32Value();
size_t input_size = info[3]->Uint32Value();
size_t input_start = Nan::To<uint32_t>(info[2]).FromJust();
size_t input_size = Nan::To<uint32_t>(info[3]).FromJust();
char* output_buf = node::Buffer::Data(info[4].As<Object>());
size_t output_start = info[5]->Uint32Value();
size_t output_size = info[6]->Uint32Value();
size_t output_start = Nan::To<uint32_t>(info[5]).FromJust();
size_t output_size = Nan::To<uint32_t>(info[6]).FromJust();
Local<Array> rc = info[7].As<Array>();
if (input_buf != NULL) input_buf += input_start;
output_buf += output_start;
......
......@@ -18,6 +18,7 @@
var assert = require('assert');
var Iconv = require('../lib/iconv').Iconv;
var Buffer = require('safer-buffer').Buffer;
// unknown source/target encoding
assert.throws(function() { new Iconv('utf-8', 'xxx'); });
......@@ -38,13 +39,13 @@ assert.throws(function() { iconv.convert() });
assert.throws(function() { iconv.convert(1) });
assert.throws(function() { iconv.convert({}) });
assert(iconv.convert(new Buffer('xxx')) instanceof Buffer);
assert(iconv.convert('xxx') instanceof Buffer);
assert(Buffer.isBuffer(iconv.convert(Buffer.from('xxx'))));
assert(Buffer.isBuffer(iconv.convert('xxx')));
assert.deepEqual(iconv.convert('xxx'), new Buffer('xxx'));
assert.deepEqual(iconv.convert(new Buffer('xxx')), new Buffer('xxx'));
assert.deepEqual(iconv.convert('xxx'), Buffer.from('xxx'));
assert.deepEqual(iconv.convert(Buffer.from('xxx')), Buffer.from('xxx'));
var buffer = new Buffer(1); buffer[0] = 235; // ë
var buffer = Buffer.alloc(1); buffer[0] = 235; // ë
assert.deepEqual(iconv.convert('ë'), buffer);
// test conversion error messages
......@@ -68,7 +69,7 @@ try {
}
// partial character sequence should throw EINVAL
buffer = new Buffer(1); buffer[0] = 195;
buffer = Buffer.alloc(1); buffer[0] = 195;
try {
iconv.convert(buffer);
} catch (e) {
......@@ -76,20 +77,20 @@ try {
}
// belongs to partial character sequence test - new input should be recoded without issues
buffer = new Buffer(1); buffer[0] = 235; // ë
buffer = Buffer.alloc(1); buffer[0] = 235; // ë
assert.deepEqual(iconv.convert('ë'), buffer);
// stateful encodings should do the Right Thing
iconv = new Iconv('iso-2022-jp', 'utf-8');
buffer = new Buffer(5);
buffer = Buffer.alloc(5);
buffer[0] = 0x1b; // start escape sequence
buffer[1] = 0x24;
buffer[2] = 0x40;
buffer[3] = 0x24; // start character sequence
buffer[4] = 0x2c;
assert.deepEqual(iconv.convert(buffer), new Buffer(''));
assert.deepEqual(iconv.convert(buffer), Buffer.from(''));
buffer = new Buffer(4);
buffer = Buffer.alloc(4);
buffer[0] = 0x1b; // start escape sequence
buffer[1] = 0x24;
buffer[2] = 0x40;
......
......@@ -18,10 +18,11 @@
var Iconv = require('../lib/iconv').Iconv;
var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;
var iconv = new Iconv('UTF-8', 'UTF-16LE');
var utf8 = new Buffer(20000000);
var utf8 = Buffer.alloc(20000000);
for (var i = 0; i < utf8.length; i++) {
utf8[i] = 97 + i % 26; // cycle from 'a' to 'z'.
}
......
......@@ -21,6 +21,7 @@ var assert = require('assert');
var stream = require('stream');
var net = require('net');
var fs = require('fs');
var Buffer = require('safer-buffer').Buffer;
var PORT = 12345;
assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
......@@ -124,8 +125,8 @@ assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
assert.equal(buf[0], 0xA9);
ok = true;
});
stream.write(Buffer([0xC2]));
stream.write(Buffer([0xA9]));
stream.write(Buffer.from([0xC2]));
stream.write(Buffer.from([0xA9]));
assert(ok);
})();
......@@ -143,8 +144,8 @@ assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
assert.equal(buf[0], 0xA9);
ok = true;
}
stream.write(Buffer([0xC2,0xAE,0xC2]));
stream.write(Buffer([0xA9]));
stream.write(Buffer.from([0xC2,0xAE,0xC2]));
stream.write(Buffer.from([0xA9]));
assert(ok);
})();
......@@ -181,7 +182,7 @@ assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
var octets = [
0x00, 0xf1, 0x52, 0x00, 0x00, 0x78, 0x51, 0xd9, 0xf7, 0x78, 0x51, 0xd9
];
stream.end(new Buffer(octets));
stream.end(Buffer.from(octets));
assert(ok);
})();
......@@ -193,6 +194,6 @@ assert(new Iconv('ascii', 'ascii') instanceof stream.Stream);
assert.equal(e.code, 'EINVAL');
ok = true;
});
stream.end(new Buffer([0xc3]));
stream.end(Buffer.from([0xc3]));
assert(ok);
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment