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