/
node_manifestparser_plist.cc
263 lines (212 loc) · 7.05 KB
/
node_manifestparser_plist.cc
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* (The MIT License)
*
* Copyright (c) 2012 Peter Sunde <peter.sunde@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the 'Software'), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <v8.h>
#include <node.h>
#include <uv.h>
#include <node_buffer.h>
#include <plist/plist.h>
#include <streambuf>
#include <fstream>
#include "./node_async_shim.h"
// Seriously google?
using v8::Handle;
using v8::String;
using v8::Value;
using v8::TryCatch;
using v8::HandleScope;
using v8::Persistent;
using v8::Function;
using v8::Context;
using v8::Arguments;
using v8::Exception;
using v8::Local;
using v8::Object;
using v8::Undefined;
using v8::Null;
using node::Buffer;
using node::FatalException;
using std::string;
using std::ifstream;
using std::istreambuf_iterator;
using std::ios;
using std::basic_ifstream;
namespace plist {
struct parsexml {
// Input data, can either be a buffer or a filename
char* input;
// Type of the above
int type;
// Error message
std::string ex;
// Final xml output
std::string finalXml;
// Javascript callback
Persistent<Function> callback;
// Length of input
size_t length;
};
const char* INPUT_IS_BINARY = "bplist00";
#define UNWRAP \
HandleScope scope;
static inline char *V8GetString(v8::Local<v8::Value> value) {
if (value->IsString()) {
std::string inputString(*v8::String::Utf8Value(value));
char *convertedString = reinterpret_cast<char*&>(inputString);
return convertedString;
}
return NULL;
}
async_rtn parse_plist_binary(uv_work_t *req) {
// Decode unit of work
parsexml *r = reinterpret_cast<parsexml*>(req->data);
/// Check if we need to open a file
if (r->type == 1) {
// Actual contents of file
char *plist_xml = NULL;
// Converted xml
char *plist_xml_converted = NULL;
// Size of plist_xml
uint32_t size_in = 0;
// Size of plist_xml_converted
uint32_t size_out = 0;
// Plist structure
plist_t plist = NULL;
// Try to open xml file for reading
std::ifstream fileStream(r->input);
// Container for xml
std::string contents;
// Check if opening file for reading failed
if (!fileStream.is_open()) {
r->ex = "Unable to open file for reading: ";
r->ex = r->input;
return;
}
// Seek until end
fileStream.seekg(0, std::ios::end);
// Pre-allocate memory
contents.reserve(fileStream.tellg());
fileStream.seekg(0, std::ios::beg);
// Get xml
contents.assign((std::istreambuf_iterator<char>(fileStream)),
std::istreambuf_iterator<char>());
// Close file
fileStream.close();
// Convert to char*
plist_xml = reinterpret_cast<char*&>(contents);
// Length of xml read
size_in = contents.length();
// Check if binary xml
if (memcmp(plist_xml, INPUT_IS_BINARY, 8) == 0) {
// Convert binary to plist
plist_from_bin(plist_xml, size_in, &plist);
// Convert plist to xml
plist_to_xml(plist, &plist_xml_converted, &size_out);
// Output xml
r->finalXml = std::string(plist_xml_converted);
} else {
// Output xml
r->finalXml = std::string(plist_xml);
}
// Release memory
plist_free(plist);
} else if (r->type == 2) {
// Plist structure
plist_t plist = NULL;
// Parsed content of file
char *plist_xml = NULL;
// Actual size of converted xml
uint32_t size_out = 0;
// Check if binary xml
if (memcmp(r->input, INPUT_IS_BINARY, 8) == 0) {
plist_from_bin(r->input, r->length, &plist);
plist_to_xml(plist, &plist_xml, &size_out);
r->finalXml = plist_xml;
} else {
// This kinda sux, but it works
plist_from_xml(r->input, r->length, &plist);
plist_to_xml(plist, &plist_xml, &size_out);
r->finalXml = plist_xml;
}
// Release memory
plist_free(plist);
}
}
async_rtn parse_plist_binary_AFTER(uv_work_t *req) {
UNWRAP;
parsexml *r = reinterpret_cast<parsexml*>(req->data);
Handle<Value> argv[1];
// Output error message
if (r->ex.empty()) {
argv[0] = Null();
argv[1] = String::New(r->finalXml.c_str());
// Output parsed xml
} else {
argv[0] = Exception::Error(String::New(r->ex.c_str()));
argv[1] = Null();
}
// Catch any exceptions
TryCatch try_catch;
// Call callback with result
r->callback->Call(Context::GetCurrent()->Global(), 2, argv);
// Throw on error
if (try_catch.HasCaught())
FatalException(try_catch);
// Release memory
r->callback.Dispose();
delete r;
}
Handle<Value> node_parse_plist(const Arguments& args) {
UNWRAP;
// Must be two arguments
if (args.Length() != 2) {
THROW(Exception::TypeError, "Excepts two arguments");
}
// Last argument must be a callback
if (!args[1]->IsFunction()) {
THROW(Exception::TypeError, "Last argument must a valid function.");
}
// Our async parser structure
parsexml *request = new parsexml;
// Callback when we are finished
request->callback = Persistent<Function>::New(
Local<Function>::Cast(args[1]));
// Check single file
if (args[0]->IsString()) {
request->input = V8GetString(args[0]->ToString());
request->type = 1;
// Check if buffer
} else if (Buffer::HasInstance(args[0])) {
Local<Object> buffer = args[0]->ToObject();
request->input = Buffer::Data(buffer);
request->length = (size_t) Buffer::Length(buffer);
request->type = 2;
} else {
THROW(Exception::TypeError, "Must be either string or buffer.");
}
BEGIN_ASYNC(request, parse_plist_binary, parse_plist_binary_AFTER);
return Undefined();
}
void InitPlistParser(Handle<Object> target) {
UNWRAP;
// Functions
NODE_SET_METHOD(target, "parse", node_parse_plist);
}
} // plist namespace