-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.cc
160 lines (141 loc) · 5.5 KB
/
process.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
/**
* Copyright (c) 2017 Chris Baker <[email protected]>
*
* 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 <node.h>
#ifdef __APPLE__
#include "apple.h"
#endif
namespace process {
using v8::Array;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Handle;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void process_open(const FunctionCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
if (args.Length() < 1) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected process id as argument")));
}
else if (!args[0]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected process id to be a number")));
}
else {
platform_handle_t handle;
if (platform_process_open(args[0]->IntegerValue(), &handle) != PLATFORM_SUCCESS) {
isolate->ThrowException(Exception::Error(
String::NewFromUtf8(isolate, "Unable to open process")));
}
else {
Local<Integer> result = Integer::New(isolate, handle);
args.GetReturnValue().Set(result);
}
}
}
void process_read(const FunctionCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
if (args.Length() < 3) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate,
"Expected handle, address, and size as arguments")));
}
else if (!args[0]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected process handle to be a number")));
}
else if (!args[1]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected address to be a number")));
}
else if (!args[2]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected size to be a number")));
}
else {
platform_handle_t handle = args[0]->IntegerValue();
platform_address_t address = args[1]->IntegerValue();
platform_size_t size = args[2]->IntegerValue();
platform_size_t count = size;
uint8_t *buffer = (uint8_t *) malloc(size);
if (platform_process_read(handle, address, size, (platform_ptr_t) buffer, &count) != KERN_SUCCESS) {
free(buffer);
isolate->ThrowException(Exception::Error(
String::NewFromUtf8(isolate, "Unable to read process")));
}
else {
Local<Array> result = Array::New(isolate, count);
for (platform_size_t i = 0; i < count; i++) {
result->Set(i, Integer::NewFromUnsigned(isolate, buffer[i] & 0xff));
}
args.GetReturnValue().Set(result);
}
}
}
void process_write(const FunctionCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
if (args.Length() < 3) {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate,
"Expected handle, address, and bytes as arguments")));
}
else if (!args[0]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected process handle to be a number")));
}
else if (!args[1]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected address to be a number")));
}
else if (!args[2]->IsArray()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Expected bytes to be an array")));
}
else {
platform_handle_t handle = args[0]->IntegerValue();
platform_address_t address = args[1]->IntegerValue();
Handle<Array> array = Handle<Array>::Cast(args[2]);
platform_size_t length = array->Length();
uint8_t *data = (uint8_t *) malloc(length);
for (platform_size_t i = 0; i < length; i++) {
data[i] = (uint8_t) array->Get(i)->IntegerValue();
}
if (platform_process_write(handle, address, (platform_ptr_t) data, length) != KERN_SUCCESS) {
free(data);
isolate->ThrowException(Exception::Error(
String::NewFromUtf8(isolate, "Unable to write process")));
}
else {
free(data);
}
}
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "open", process_open);
NODE_SET_METHOD(exports, "read", process_read);
NODE_SET_METHOD(exports, "write", process_write);
}
NODE_MODULE(process, init)
};