8000 iconv, bugfix: decode error when iconv does not exist. · fibjs/fibjs@96ca1cf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 96ca1cf

Browse files
committed
iconv, bugfix: decode error when iconv does not exist.
1 parent 4cb10eb commit 96ca1cf

File tree

5 files changed

+66
-101
lines changed

5 files changed

+66
-101
lines changed

fibjs/src/encoding/encoding_iconv.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,6 @@ result_t encoding_iconv::decode(const char* data, size_t sz, exlib::string& retV
159159
if (ucs_decode(data, sz, retVal) == 0)
160160
return 0;
161161

162-
if (!m_iconv_de) {
163-
m_iconv_de = iconv_open("utf-8", m_charset.c_str());
164-
if (m_iconv_de == (iconv_t)(-1)) {
165-
m_iconv_de = NULL;
166-
return CHECK_ERROR(Runtime::setError("encoding: Unknown charset."));
167-
}
168-
}
169-
170162
int32_t _sz;
171163
UErrorCode errorCode = U_ZERO_ERROR;
172164

@@ -178,29 +170,6 @@ result_t encoding_iconv::decode(const char* data, size_t sz, exlib::string& retV
178170
return 0;
179171
}
180172

181-
exlib::string strBuf;
182-
183-
strBuf.resize(sz * 2);
184-
char* output_buf = strBuf.c_buffer();
185-
size_t output_size = strBuf.length();
186-
187-
size_t n = iconv((iconv_t)m_iconv_de, &data, &sz, &output_buf, &output_size);
188-
189-
if (n == (size_t)-1)
190-
return CHECK_ERROR(Runtime::setError("encoding: convert error."));
191-
192-
strBuf.resize(strBuf.length() - output_size);
193-
194-
retVal = strBuf;
195-
196-
return 0;
197-
}
198-
199-
result_t encoding_iconv::decode(const exlib::string& data, exlib::string& retVal)
200-
{
201-
if (ucs_decode(data, retVal) == 0)
202-
return 0;
203-
204173
if (!m_iconv_de) {
205174
m_iconv_de = iconv_open("utf-8", m_charset.c_str());
206175
if (m_iconv_de == (iconv_t)(-1)) {
@@ -209,15 +178,13 @@ result_t encoding_iconv::decode(const exlib::string& data, exlib::string& retVal
209178
}
210179
}
211180

212-
size_t sz = data.length();
213-
const char* ptr = data.c_str();
214181
exlib::string strBuf;
215182

216183
strBuf.resize(sz * 2);
217184
char* output_buf = strBuf.c_buffer();
218185
size_t output_size = strBuf.length();
219186

220-
size_t n = iconv((iconv_t)m_iconv_de, &ptr, &sz, &output_buf, &output_size);
187+
size_t n = iconv((iconv_t)m_iconv_de, &data, &sz, &output_buf, &output_size);
221188

222189
if (n == (size_t)-1)
223190
return CHECK_ERROR(Runtime::setError("encoding: convert error."));
@@ -229,12 +196,15 @@ result_t encoding_iconv::decode(const exlib::string& data, exlib::string& retVal
229196
return 0;
230197
}
231198

232-
result_t encoding_iconv::decode(Buffer_base* data, exlib::string& retVal)
199+
result_t encoding_iconv::decode(const exlib::string& data, exlib::string& retVal)
233200
{
234-
exlib::string strData;
235-
data->toString(strData);
201+
return decode(data.c_str(), data.length(), retVal);
202+
}
236203

237-
return decode(strData, retVal);
204+
result_t encoding_iconv::decode(Buffer_base* data, exlib::string& retVal)
205+
{
206+
Buffer* buf = Buffer::Cast(data);
207+
return decode((const char*)buf->data(), buf->length(), retVal);
238208
}
239209

240210
bool encoding_iconv::is_encoding(exlib::string charset)

test/buffer_test.js

F438
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ describe('Buffer', () => {
384384
assert.equal(Buffer.isEncoding('binary'), true);
385385
assert.equal(Buffer.isEncoding('latin1'), true);
386386

387-
if (!process.env.QEMU_LD_PREFIX)
388-
assert.equal(Buffer.isEncoding('EUC-JP'), true);
387+
assert.equal(Buffer.isEncoding('EUC-JP'), true);
389388
});
390389

391390
it('@iterator', () => {
@@ -1020,12 +1019,11 @@ describe('Buffer', () => {
10201019
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
10211020
});
10221021

1023-
if (Buffer.isEncoding("EUC-JP"))
1024-
it('charset', () => {
1025-
assert.equal(new Buffer("哈哈哈").toString(), "哈哈哈");
1026-
assert.equal(new Buffer("哈哈哈哈", "EUC-JP").hex(), "d2fdd2fdd2fdd2fd");
1027-
assert.equal(new Buffer("哈哈哈", "EUC-JP").toString("EUC-JP"), "哈哈哈");
1028-
});
1022+
it('charset', () => {
1023+
assert.equal(new Buffer("哈哈哈").toString(), "哈哈哈");
1024+
assert.equal(new Buffer("哈哈哈哈", "EUC-JP").hex(), "d2fdd2fdd2fdd2fd");
1025+
assert.equal(new Buffer("哈哈哈", "EUC-JP").toString("EUC-JP"), "哈哈哈");
1026+
});
10291027

10301028
it('forEach', () => {
10311029
var buf = new Buffer([1, 2, 3, 4, 5]);

test/buffered_test.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,34 @@ describe("buffered stream", () => {
104104
f.close();
105105
});
106106

107-
if (Buffer.isEncoding("EUC-JP"))
108-
it("charset", () => {
109-
fs.unlink(path.join(__dirname, "test0000" + base_port));
107+
it("charset", () => {
108+
fs.unlink(path.join(__dirname, "test0000" + base_port));
110109

111-
f = fs.openFile(path.join(__dirname, "test0000" + base_port), "w+");
112-
var r = new io.BufferedStream(f);
113-
r.EOL = '\r\n';
110+
f = fs.openFile(path.join(__dirname, "test0000" + base_port), "w+");
111+
var r = new io.BufferedStream(f);
112+
r.EOL = '\r\n';
114113

115-
assert.equal(r.charset, "utf-8");
114+
assert.equal(r.charset, "utf-8");
116115

117-
f.write("哈哈哈\r\n");
118-
f.rewind();
119-
assert.equal(r.readLine(), "哈哈哈");
116+
f.write("哈哈哈\r\n");
117+
f.rewind();
118+
assert.equal(r.readLine(), "哈哈哈");
120119

121-
r.charset = "EUC-JP";
120+
r.charset = "EUC-JP";
122121

123-
f.rewind();
124-
f.truncate(0);
125-
r.writeText("我是好人");
126-
r.writeLine("哈哈哈");
127-
f.rewind();
128-
assert.equal(f.readAll().toString("EUC-JP"), "我是好人哈哈哈\r\n");
122+
f.rewind();
123+
f.truncate(0);
124+
r.writeText("我是好人");
125+
r.writeLine("哈哈哈");
126+
f.rewind();
127+
assert.equal(f.readAll().toString("EUC-JP"), "我是好人哈哈哈\r\n");
129128

130-
f.rewind();
131-
assert.equal(r.readText(8), "我是好人");
132-
assert.equal(r.readLine(), "哈哈哈");
129+
f.rewind();
130+
assert.equal(r.readText(8), "我是好人");
131+
assert.equal(r.readLine(), "哈哈哈");
133132

134-
f.close();
135-
});
133+
f.close();
134+
});
136135
});
137136

138137
require.main === module && test.run(console.DEBUG);

test/unzip_test.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,24 +252,23 @@ describe("zip", () => {
252252
assert.equal(zipfile.read('password.txt', password).toString(), 'password test');
253253
})
254254

255-
if (Buffer.isEncoding("gbk"))
256-
it("zip with codec", () => {
257-
zipfile = zip.open(path.join(__dirname, 'unzip_test.zip' + vmid), 'w', "gbk");
258-
var buf = new Buffer('codec test');
259-
zipfile.write(buf, '密码.txt');
260-
zipfile.close();
255+
it("zip with codec", () => {
256+
zipfile = zip.open(path.join(__dirname, 'unzip_test.zip' + vmid), 'w', "gbk");
257+
var buf = new Buffer('codec test');
258+
zipfile.write(buf, '密码.txt');
259+
zipfile.close();
260+
261+
zipfile = zip.open(path.join(__dirname, 'unzip_test.zip' + vmid), "r", "gbk");
262+
assert.equal('密码.txt', zipfile.namelist()[0]);
263+
assert.equal('密码.txt', zipfile.infolist()[0].filename);
264+
assert.equal('密码.txt', zipfile.getinfo('密码.txt').filename);
265+
assert.equal(zipfile.read('密码.txt').toString(), 'codec test');
266+
assert.equal(zipfile.readAll()[< 8000 /span>0].filename, '密码.txt');
261267

262-
zipfile = zip.open(path.join(__dirname, 'unzip_test.zip' + vmid), "r", "gbk");
263-
assert.equal('密码.txt', zipfile.namelist()[0]);
264-
assert.equal('密码.txt', zipfile.infolist()[0].filename);
265-
assert.equal('密码.txt', zipfile.getinfo('密码.txt').filename);
266-
assert.equal(zipfile.read('密码.txt').toString(), 'codec test');
267-
assert.equal(zipfile.readAll()[0].filename, '密码.txt');
268-
269-
zipfile.extractAll(pathname);
270-
efile3 = pathname + '/密码.txt';
271-
assert.equal(fs.exists(efile3), true);
272-
})
268+
zipfile.extractAll(pathname);
269+
efile3 = pathname + '/密码.txt';
270+
assert.equal(fs.exists(efile3), true);
271+
})
273272

274273
it("bugfix: read from empty file", () => {
275274
zip.open(new io.MemoryStream());

test/xml_test.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ describe('xml', () => {
252252
assert.equal(e.ownerDocument, xdoc);
253253
xdoc.appendChild(e);
254254
assert.equal(e.ownerDocument, xdoc);
255-
255+
256256
assert.equal(e.nodeType, 1);
257257
assert.equal(e.nodeName, 'aaa');
258258
assert.equal(e.tagName, 'aaa');
@@ -930,24 +930,23 @@ describe('xml', () => {
930930
assert.equal(hdoc.body.innerHTML, "&lt;img&gt;&lt;br&gt;");
931931
});
932932

933-
if (Buffer.isEncoding("EUC-JP"))
934-
it("charset", () => {
935-
var data = new Buffer('<html><meta charset=EUC-JP>哈哈哈哈', "EUC-JP");
936-
var doc = xml.parse(data, "text/html");
937-
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
933+
it("charset", () => {
934+
var data = new Buffer('<html><meta charset=EUC-JP>哈哈哈哈', "EUC-JP");
935+
var doc = xml.parse(data, "text/html");
936+
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
938937

939-
var data = new Buffer('<html><meta http-equiv=content-type content="text/html; charset=EUC-JP">哈哈哈哈', "EUC-JP");
940-
var doc = xml.parse(data, "text/html");
941-
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
938+
var data = new Buffer('<html><meta http-equiv=content-type content="text/html; charset=EUC-JP">哈哈哈哈', "EUC-JP");
939+
var doc = xml.parse(data, "text/html");
940+
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
942941

943-
var data = new Buffer('<html><meta content="text/html; charset=EUC-JP" http-equiv=content-type>哈哈哈哈', "EUC-JP");
944-
var doc = xml.parse(data, "text/html");
945-
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
942+
var data = new Buffer('<html><meta content="text/html; charset=EUC-JP" http-equiv=content-type>哈哈哈哈', "EUC-JP");
943+
var doc = xml.parse(data, "text/html");
944+
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
946945

947-
var data = new Buffer('<html><meta http-equiv=content-type content="text/html; test=111; charset=EUC-JP; ccc=222">哈哈哈哈', "EUC-JP");
948-
var doc = xml.parse(data, "text/html");
949-
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
950-
});
946+
var data = new Buffer('<html><meta http-equiv=content-type content="text/html; test=111; charset=EUC-JP; ccc=222">哈哈哈哈', "EUC-JP");
947+
var doc = xml.parse(data, "text/html");
948+
assert.equal(doc.documentElement.textContent, "哈哈哈哈");
949+
});
951950
});
952951
});
953952

0 commit comments

Comments
 (0)
0