Comparing version 2.0.6 to 2.0.7
147
lib/iconv.js
@@ -30,11 +30,9 @@ /* | ||
var E2BIG = bindings.E2BIG | 0; | ||
var EILSEQ = bindings.EILSEQ | 0; | ||
var EINVAL = bindings.EINVAL | 0; | ||
// Marker object. | ||
var FLUSH = {}; | ||
function fixEncoding(encoding) | ||
{ | ||
// Convert "utf8" to "utf-8". | ||
return /^utf[^-]/i.test(encoding) ? 'utf-' + encoding.substr(3) : encoding; | ||
} | ||
function Iconv(fromEncoding, toEncoding) | ||
@@ -53,59 +51,7 @@ { | ||
function convert(input) { | ||
if (typeof(input) === 'string') { | ||
input = new Buffer(input); | ||
} | ||
if (!(input instanceof Buffer) && input !== FLUSH) { | ||
throw new Error('Bad argument.'); // Not a buffer or a string. | ||
} | ||
var output = new Buffer(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(conv, | ||
input, | ||
input_start, | ||
input_size, | ||
output, | ||
output_start, | ||
output_size, | ||
out); | ||
var input_consumed = out[0]; | ||
var output_consumed = out[1]; | ||
input_start += input_consumed; | ||
input_size -= input_consumed; | ||
output_start += output_consumed; | ||
output_size -= output_consumed; | ||
if (errno) { | ||
if (errno === bindings.EINVAL) { | ||
throw errnoException('EINVAL', 'Incomplete character sequence.'); | ||
} | ||
else if (errno === bindings.EILSEQ) { | ||
throw errnoException('EILSEQ', 'Illegal character sequence.'); | ||
} | ||
else if (errno === bindings.E2BIG) { | ||
output_size += output.length; | ||
var newbuf = new Buffer(output.length * 2); | ||
output.copy(newbuf, 0, 0, output_start); | ||
output = newbuf; | ||
continue; | ||
} | ||
throw 'unexpected error'; | ||
} | ||
if (input !== FLUSH) { | ||
input = FLUSH; | ||
continue; | ||
} | ||
if (output_start < output.length) { | ||
output = output.slice(0, output_start); | ||
} | ||
return output; | ||
} | ||
}; | ||
var convert_ = convert.bind({ conv_: conv }); | ||
var context_ = { trailer: null }; | ||
this.convert = function(input) { | ||
return convert(input); | ||
return convert_(input, null); | ||
}; | ||
@@ -115,3 +61,3 @@ | ||
try { | ||
var buf = convert(input); | ||
var buf = convert_(input, context_); | ||
} | ||
@@ -138,2 +84,79 @@ catch (e) { | ||
function fixEncoding(encoding) | ||
{ | ||
// Convert "utf8" to "utf-8". | ||
return /^utf[^-]/i.test(encoding) ? 'utf-' + encoding.substr(3) : encoding; | ||
} | ||
function convert(input, context) { | ||
if (typeof(input) === 'string') { | ||
input = new Buffer(input); | ||
} | ||
if (!(input instanceof Buffer) && input !== FLUSH) { | ||
throw new Error('Bad argument.'); // Not a buffer or a string. | ||
} | ||
if (context !== null && context.trailer !== null) { | ||
// Prepend input buffer with trailer from last chunk. | ||
var newbuf = new Buffer(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 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_, | ||
input, | ||
input_start, | ||
input_size, | ||
output, | ||
output_start, | ||
output_size, | ||
out); | ||
var input_consumed = out[0]; | ||
var output_consumed = out[1]; | ||
input_start += input_consumed; | ||
input_size -= input_consumed; | ||
output_start += output_consumed; | ||
output_size -= output_consumed; | ||
if (errno) { | ||
if (errno === E2BIG) { | ||
output_size += output.length; | ||
var newbuf = new Buffer(output.length * 2); | ||
output.copy(newbuf, 0, 0, output_start); | ||
output = newbuf; | ||
continue; | ||
} | ||
else if (errno === EILSEQ) { | ||
throw errnoException('EILSEQ', 'Illegal character sequence.'); | ||
} | ||
else if (errno === EINVAL) { | ||
if (context === null || input === FLUSH) { | ||
throw errnoException('EINVAL', 'Incomplete character sequence.'); | ||
} | ||
else { | ||
context.trailer = input.slice(input_start); | ||
return output.slice(0, output_start); | ||
} | ||
} | ||
else { | ||
throw 'unexpected error'; | ||
} | ||
} | ||
if (input !== FLUSH) { | ||
input = FLUSH; | ||
continue; | ||
} | ||
if (output_start < output.length) { | ||
output = output.slice(0, output_start); | ||
} | ||
return output; | ||
} | ||
} | ||
function errnoException(code, errmsg) | ||
@@ -140,0 +163,0 @@ { |
{ | ||
"name": "iconv", | ||
"main": "./lib/iconv", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"description": "Text recoding in JavaScript for fun and profit!", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/bnoordhuis/node-iconv", |
@@ -116,1 +116,33 @@ /* | ||
})(); | ||
(function() { | ||
var ok = false; | ||
var stream = Iconv('utf-8', 'iso-8859-1'); | ||
stream.on('data', function(buf) { | ||
assert.equal(buf.length, 1); | ||
assert.equal(buf[0], 0xA9); | ||
ok = true; | ||
}); | ||
stream.write(Buffer([0xC2])); | ||
stream.write(Buffer([0xA9])); | ||
assert(ok); | ||
})(); | ||
(function() { | ||
var ok = false; | ||
var stream = Iconv('utf-8', 'iso-8859-1'); | ||
stream.once('data', step1); | ||
function step1(buf) { | ||
assert.equal(buf.length, 1); | ||
assert.equal(buf[0], 0xAE); | ||
stream.once('data', step2); | ||
} | ||
function step2(buf) { | ||
assert.equal(buf.length, 1); | ||
assert.equal(buf[0], 0xA9); | ||
ok = true; | ||
} | ||
stream.write(Buffer([0xC2,0xAE,0xC2])); | ||
stream.write(Buffer([0xA9])); | ||
assert(ok); | ||
})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
11426342
405