binding.cc
4.15 KB
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
/*
* 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.
*/
#include "iconv.h"
#include "nan.h"
#include "node_buffer.h"
#include <errno.h>
#include <assert.h>
#include <stdint.h>
#ifndef ICONV_CONST
#define ICONV_CONST
#endif // ICONV_CONST
namespace
{
using v8::Array;
using v8::Boolean;
using v8::FunctionTemplate;
using v8::Handle;
using v8::Integer;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;
struct Iconv
{
static Nan::Persistent<ObjectTemplate> object_template;
iconv_t conv_;
Iconv(iconv_t conv)
{
conv_ = conv;
}
~Iconv()
{
iconv_close(conv_);
}
static void WeakCallback(const Nan::WeakCallbackInfo<Iconv>& data)
{
delete data.GetParameter();
}
static void Initialize(Handle<Object> obj)
{
Local<ObjectTemplate> t = Nan::New<ObjectTemplate>();
t->SetInternalFieldCount(1);
object_template.Reset(t);
obj->Set(Nan::New<String>("make").ToLocalChecked(),
Nan::New<FunctionTemplate>(Make)->GetFunction());
obj->Set(Nan::New<String>("convert").ToLocalChecked(),
Nan::New<FunctionTemplate>(Convert)->GetFunction());
#define EXPORT_ERRNO(err) \
obj->Set(Nan::New<String>(#err).ToLocalChecked(), Nan::New<Integer>(err))
EXPORT_ERRNO(EINVAL);
EXPORT_ERRNO(EILSEQ);
EXPORT_ERRNO(E2BIG);
#undef EXPORT_ERRNO
}
static NAN_METHOD(Make)
{
String::Utf8Value from_encoding(info[0]);
String::Utf8Value to_encoding(info[1]);
iconv_t conv = iconv_open(*to_encoding, *from_encoding);
if (conv == reinterpret_cast<iconv_t>(-1)) {
return info.GetReturnValue().SetNull();
}
Iconv* iv = new Iconv(conv);
Local<Object> obj =
Nan::New<ObjectTemplate>(object_template)->NewInstance();
Nan::SetInternalFieldPointer(obj, 0, iv);
Nan::Persistent<Object> persistent(obj);
persistent.SetWeak(iv, WeakCallback, Nan::WeakCallbackType::kParameter);
info.GetReturnValue().Set(obj);
}
static NAN_METHOD(Convert)
{
Iconv* iv = static_cast<Iconv*>(
Nan::GetInternalFieldPointer(info[0].As<Object>(), 0));
const bool is_flush = info[8]->BooleanValue();
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();
char* output_buf = node::Buffer::Data(info[4].As<Object>());
size_t output_start = info[5]->Uint32Value();
size_t output_size = info[6]->Uint32Value();
Local<Array> rc = info[7].As<Array>();
if (input_buf != NULL) input_buf += input_start;
output_buf += output_start;
size_t input_consumed = input_size;
size_t output_consumed = output_size;
size_t nconv = iconv(iv->conv_,
&input_buf,
&input_size,
&output_buf,
&output_size);
int errorno = 0;
if (nconv == static_cast<size_t>(-1)) {
errorno = errno;
}
input_consumed -= input_size;
output_consumed -= output_size;
rc->Set(0, Nan::New<Integer>(static_cast<uint32_t>(input_consumed)));
rc->Set(1, Nan::New<Integer>(static_cast<uint32_t>(output_consumed)));
info.GetReturnValue().Set(errorno);
}
// Forbid implicit copying.
Iconv(const Iconv&);
void operator=(const Iconv&);
};
Nan::Persistent<ObjectTemplate> Iconv::object_template;
} // namespace
NODE_MODULE(iconv, Iconv::Initialize);