| 1 |
/* |
| 2 |
* Copyright (c) 2013, Ben Noordhuis <[email protected]> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
'use strict'; |
| 18 |
|
| 19 |
exports.Iconv = Iconv; |
| 20 |
|
| 21 |
const stream = require('stream'); |
| 22 |
const util = require('util'); |
| 23 |
|
| 24 |
let bindings; |
| 25 |
try { |
| 26 |
bindings = require('./build/Release/iconv.node'); |
| 27 |
} |
| 28 |
catch (e) { |
| 29 |
if (e.code !== 'MODULE_NOT_FOUND') throw e; |
| 30 |
bindings = require('./build/Debug/iconv.node'); |
| 31 |
} |
| 32 |
|
| 33 |
const E2BIG = bindings.E2BIG | 0; |
| 34 |
const EILSEQ = bindings.EILSEQ | 0; |
| 35 |
const EINVAL = bindings.EINVAL | 0; |
| 36 |
|
| 37 |
// Marker object. |
| 38 |
const FLUSH = Buffer.alloc(0); |
| 39 |
|
| 40 |
function Iconv(fromEncoding, toEncoding) |
| 41 |
{ |
| 42 |
if (!(this instanceof Iconv)) { |
| 43 |
return new Iconv(fromEncoding, toEncoding); |
| 44 |
} |
| 45 |
|
| 46 |
stream.Stream.call(this); |
| 47 |
this.writable = true; |
| 48 |
|
| 49 |
const conv = bindings.make(fixEncoding(fromEncoding), |
| 50 |
fixEncoding(toEncoding)); |
| 51 |
if (conv === null) { |
| 52 |
throw new Error('Conversion from ' + |
| 53 |
fromEncoding + ' to ' + |
| 54 |
toEncoding + ' is not supported.'); |
| 55 |
} |
| 56 |
|
| 57 |
const context_ = { trailer: null }; |
| 58 |
|
| 59 |
this.convert = function(input, encoding) { |
| 60 |
if (typeof(input) === 'string') { |
| 61 |
input = Buffer.from(input, encoding || 'utf8'); |
| 62 |
} |
| 63 |
return convert(conv, input, null); |
| 64 |
}; |
| 65 |
|
| 66 |
this.write = function(input, encoding) { |
| 67 |
if (typeof(input) === 'string') { |
| 68 |
input = Buffer.from(input, encoding || 'utf8'); |
| 69 |
} |
| 70 |
let buf; |
| 71 |
try { |
| 72 |
buf = convert(conv, input, context_); |
| 73 |
} |
| 74 |
catch (e) { |
| 75 |
this.emit('error', e); |
| 76 |
return false; |
| 77 |
} |
| 78 |
if (buf && buf.length !== 0) { |
| 79 |
this.emit('data', buf); |
| 80 |
} |
| 81 |
return true; |
| 82 |
}; |
| 83 |
|
| 84 |
this.end = function(input, encoding) { |
| 85 |
if (typeof(input) !== 'undefined') { |
| 86 |
this.write(input, encoding); |
| 87 |
} |
| 88 |
this.write(FLUSH); |
| 89 |
this.emit('end'); |
| 90 |
}; |
| 91 |
} |
| 92 |
util.inherits(Iconv, stream.Stream); |
| 93 |
|
| 94 |
function fixEncoding(encoding) |
| 95 |
{ |
| 96 |
if (/^windows-31j$/i.test(encoding)) return 'cp932'; |
| 97 |
// Convert "utf8" to "utf-8". |
| 98 |
return /^utf[^-]/i.test(encoding) ? 'utf-' + encoding.substr(3) : encoding; |
| 99 |
} |
| 100 |
|
| 101 |
function convert(conv, input, context) { |
| 102 |
if (!Buffer.isBuffer(input) && input !== FLUSH) { |
| 103 |
throw new Error('Bad argument.'); // Not a buffer or a string. |
| 104 |
} |
| 105 |
if (context !== null && context.trailer !== null && input === FLUSH) { |
| 106 |
throw errnoException('EINVAL', 'Incomplete character sequence.'); |
| 107 |
} |
| 108 |
if (context !== null && context.trailer !== null) { |
| 109 |
if (input.length === 0) { |
| 110 |
// Use the trailer directly when the input is empty, |
| 111 |
// don't allocate a new buffer. |
| 112 |
input = context.trailer; |
| 113 |
context.trailer = null; |
| 114 |
} else { |
| 115 |
// Prepend input buffer with trailer from last chunk. |
| 116 |
const newbuf = Buffer.alloc(context.trailer.length + input.length); |
| 117 |
context.trailer.copy(newbuf, 0, 0, context.trailer.length); |
| 118 |
input.copy(newbuf, context.trailer.length, 0, input.length); |
| 119 |
context.trailer = null; |
| 120 |
input = newbuf; |
| 121 |
} |
| 122 |
} |
| 123 |
let output = Buffer.alloc(input.length * 2); // To a first approximation. |
| 124 |
let input_start = 0; |
| 125 |
let output_start = 0; |
| 126 |
let input_size = input.length; |
| 127 |
let output_size = output.length; |
| 128 |
const inout = [0,0]; |
| 129 |
for (;;) { |
| 130 |
inout[0] = input_size; |
| 131 |
inout[1] = output_size; |
| 132 |
const errno = bindings.convert(input === FLUSH, |
| 133 |
conv, |
| 134 |
input, |
| 135 |
input_start, |
| 136 |
output, |
| 137 |
output_start, |
| 138 |
inout); |
| 139 |
const input_consumed = input_size - inout[0]; |
| 140 |
const output_consumed = output_size - inout[1]; |
| 141 |
input_start += input_consumed; |
| 142 |
input_size -= input_consumed; |
| 143 |
output_start += output_consumed; |
| 144 |
output_size -= output_consumed; |
| 145 |
if (errno) { |
| 146 |
if (errno === E2BIG) { |
| 147 |
output_size += output.length; |
| 148 |
const newbuf = Buffer.alloc(output.length * 2); |
| 149 |
output.copy(newbuf, 0, 0, output_start); |
| 150 |
output = newbuf; |
| 151 |
continue; |
| 152 |
} |
| 153 |
else if (errno === EILSEQ) { |
| 154 |
throw errnoException('EILSEQ', 'Illegal character sequence.'); |
| 155 |
} |
| 156 |
else if (errno === EINVAL) { |
| 157 |
if (context === null || input === FLUSH) { |
| 158 |
throw errnoException('EINVAL', 'Incomplete character sequence.'); |
| 159 |
} |
| 160 |
else { |
| 161 |
context.trailer = input.slice(input_start); |
| 162 |
return output.slice(0, output_start); |
| 163 |
} |
| 164 |
} |
| 165 |
else { |
| 166 |
throw 'unexpected error'; |
| 167 |
} |
| 168 |
} |
| 169 |
if (input !== FLUSH) { |
| 170 |
input = FLUSH; |
| 171 |
continue; |
| 172 |
} |
| 173 |
if (output_start < output.length) { |
| 174 |
output = output.slice(0, output_start); |
| 175 |
} |
| 176 |
return output; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
function errnoException(code, errmsg) |
| 181 |
{ |
| 182 |
const err = new Error(errmsg); |
| 183 |
err.code = code; |
| 184 |
return err; |
| 185 |
} |