|
| 1 | +#include <node.h> |
| 2 | +#include <node_buffer.h> |
| 3 | +#include <assert.h> |
| 4 | +#include <string.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <ctype.h> // isdigit |
| 7 | +#include <arpa/inet.h> // htons, htonl |
| 8 | + |
| 9 | +using namespace v8; |
| 10 | +using namespace node; |
| 11 | + |
| 12 | +#define GET_OFFSET(a) (a)->IsInt32() ? (a)->IntegerValue() : -1; |
| 13 | +#define OUT_OF_BOUNDS ThrowException(Exception::Error(String::New("Out of bounds"))) |
| 14 | + |
| 15 | +class Binary : public ObjectWrap { |
| 16 | + public: |
| 17 | + static void Initialize (v8::Handle<v8::Object> target) |
| 18 | + { |
| 19 | + HandleScope scope; |
| 20 | + Local<FunctionTemplate> t = FunctionTemplate::New(New); |
| 21 | + t->InstanceTemplate()->SetInternalFieldCount(1); |
| 22 | + NODE_SET_PROTOTYPE_METHOD(t, "pack", Pack); |
| 23 | + NODE_SET_PROTOTYPE_METHOD(t, "unpack", Unpack); |
| 24 | + target->Set(String::NewSymbol("Binary"), t->GetFunction()); |
| 25 | + } |
| 26 | + |
| 27 | + protected: |
| 28 | + |
| 29 | + static Handle<Value> New (const Arguments& args) |
| 30 | + { |
| 31 | + HandleScope scope; |
| 32 | + Binary *binary = new Binary(); |
| 33 | + binary->Wrap(args.This()); |
| 34 | + return args.This(); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + // buffer.unpack(format, index, Buffer); |
| 39 | + // Starting at 'index', unpacks binary from the buffer into an array. |
| 40 | + // 'format' is a string |
| 41 | + // |
| 42 | + // FORMAT RETURNS |
| 43 | + // N uint32_t a 32bit unsigned integer in network byte order |
| 44 | + // n uint16_t a 16bit unsigned integer in network byte order |
| 45 | + // o uint8_t a 8bit unsigned integer |
| 46 | + static Handle<Value> Unpack(const Arguments &args) { |
| 47 | + HandleScope scope; |
| 48 | + Binary *binary = ObjectWrap::Unwrap<Binary>(args.This()); |
| 49 | + |
| 50 | + if (!args[0]->IsString()) { |
| 51 | + return ThrowException(Exception::TypeError(String::New("Argument must be a string"))); |
| 52 | + } |
| 53 | + if (!Buffer::HasInstance(args[2])) { |
| 54 | + return ThrowException(Exception::Error(String::New("Second argument should be a Buffer"))); |
| 55 | + } |
| 56 | + Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[2]->ToObject()); |
| 57 | + |
| 58 | + String::AsciiValue format(args[0]->ToString()); |
| 59 | + uint32_t index = args[1]->Uint32Value(); |
| 60 | + |
| 61 | + Local<Array> array = Array::New(format.length()); |
| 62 | + |
| 63 | + uint8_t uint8; |
| 64 | + uint16_t uint16; |
| 65 | + uint32_t uint32; |
| 66 | + |
| 67 | + int pos; |
| 68 | + pos = 0; |
| 69 | + for (int i = 0; i < format.length(); i++) { |
| 70 | + switch ((*format)[i]) { |
| 71 | + // 32bit unsigned integer in network byte order |
| 72 | + case 'N': |
| 73 | + if (index + 3 >= buffer->length()) return OUT_OF_BOUNDS; |
| 74 | + uint32 = htonl(*(uint32_t*)(buffer->data() + index)); |
| 75 | + array->Set(Integer::New(pos++), Integer::NewFromUnsigned(uint32)); |
| 76 | + index += 4; |
| 77 | + break; |
| 78 | + // 16bit unsigned integer in network byte order |
| 79 | + case 'n': |
| 80 | + if (index + 1 >= buffer->length()) return OUT_OF_BOUNDS; |
| 81 | + uint16 = htons(*(uint16_t*)(buffer->data() + index)); |
| 82 | + array->Set(Integer::New(pos++), Integer::NewFromUnsigned(uint16)); |
| 83 | + index += 2; |
| 84 | + break; |
| 85 | + // a single octet, unsigned. |
| 86 | + case 'o': |
| 87 | + if (index >= buffer->length()) return OUT_OF_BOUNDS; |
| 88 | + uint8 = (uint8_t)buffer->data()[index]; |
| 89 | + array->Set(Integer::New(pos++), Integer::NewFromUnsigned(uint8)); |
| 90 | + index += 1; |
| 91 | + break; |
| 92 | + case 's': |
| 93 | + char num[32]; |
| 94 | + int j; |
| 95 | + j=0; |
| 96 | + while(isdigit((*format)[++i])) { |
| 97 | + num[j++] = (*format)[i]; |
| 98 | + } |
| 99 | + num[j] = '\0'; |
| 100 | + int size; |
| 101 | + size = atoi(num); |
| 102 | + if (index + size > buffer->length()) return OUT_OF_BOUNDS; |
| 103 | + // read string |
| 104 | + char * newstr; |
| 105 | + newstr = (char*)malloc(size + 1); |
| 106 | + strncpy(newstr, buffer->data() + index, size); |
| 107 | + *(newstr+size) = '\0'; |
| 108 | + array->Set(Integer::New(pos++), String::New(newstr)); |
| 109 | + index += size; |
| 110 | + free(newstr); |
| 111 | + i--; |
| 112 | + break; |
| 113 | + default: |
| 114 | + return ThrowException(Exception::Error(String::New("Unknown format character"))); |
| 115 | + } |
| 116 | + } |
| 117 | + return scope.Close(array); |
| 118 | + } |
| 119 | + |
| 120 | + static Handle<Value> Pack (const Arguments& args) |
| 121 | + { |
| 122 | + HandleScope scope; |
| 123 | + Binary *binary = ObjectWrap::Unwrap<Binary>(args.This()); |
| 124 | + |
| 125 | + if (!args[0]->IsArray()) { |
| 126 | + return ThrowException(Exception::Error(String::New("First argument should be an Array"))); |
| 127 | + } |
| 128 | + if (!Buffer::HasInstance(args[1])) { |
| 129 | + return ThrowException(Exception::Error(String::New("Second argument should be a Buffer"))); |
| 130 | + } |
| 131 | + |
| 132 | + Buffer * buffer = ObjectWrap::Unwrap<Buffer>(args[1]->ToObject()); |
| 133 | + size_t off = args[2]->Int32Value(); |
| 134 | + size_t len = buffer->length(); |
| 135 | + if (off >= len) { |
| 136 | + return OUT_OF_BOUNDS; |
| 137 | + } |
| 138 | + |
| 139 | + char * start = (char*)buffer->data(); |
| 140 | + char * buf = start + off; |
| 141 | + |
| 142 | + Local<Array> a = Local<Array>::Cast(args[0]); |
| 143 | + int val = 0; |
| 144 | + for (int i = 0; i < a->Length(); i++) { |
| 145 | + v8::Handle<v8::Object> fields = a->Get(i)->ToObject(); |
| 146 | + v8::Handle<v8::Array> props = fields->GetPropertyNames(); |
| 147 | + v8::String::Utf8Value type(props->Get(0)); |
| 148 | + Handle<String> name = props->Get(0)->ToString(); |
| 149 | + val = 0; |
| 150 | + if(strcmp(*type, "int16") == 0) { |
| 151 | + val = (int) fields->Get(name)->ToInteger()->Value(); |
| 152 | + *buf++ = (0xff00 & val) >> 8; |
| 153 | + *buf++ = 0xff & val; |
| 154 | + } |
| 155 | + else if(strcmp(*type, "int32") == 0) { |
| 156 | + val = (int) fields->Get(name)->ToInteger()->Value(); |
| 157 | + *buf++ = (0xff000000 & val) >> 24; |
| 158 | + *buf++ = (0xff0000 & val) >> 16; |
| 159 | + *buf++ = (0xff00 & val) >> 8; |
| 160 | + *buf++ = 0xff & val; |
| 161 | + } |
| 162 | + else if(strcmp(*type, "int") == 0) { |
| 163 | + *buf++ = fields->Get(name)->ToInteger()->Value(); |
| 164 | + } |
| 165 | + else if(strcmp(*type, "string") == 0) { |
| 166 | + String::Utf8Value value(fields->Get(name)); |
| 167 | + strcpy(buf, *value); |
| 168 | + buf += value.length(); |
| 169 | + } |
| 170 | + else { |
| 171 | + return ThrowException(Exception::Error(String::New("Unknown Type"))); |
| 172 | + } |
| 173 | + } |
| 174 | + Local<Integer> length = Integer::New(buf-(start+off)); |
| 175 | + return scope.Close(length); |
| 176 | + } |
| 177 | + |
| 178 | + Binary () : ObjectWrap () |
| 179 | + { |
| 180 | + } |
| 181 | + |
| 182 | + ~Binary () |
| 183 | + { |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +extern "C" void |
| 188 | +init (Handle<Object> target) |
| 189 | +{ |
| 190 | + HandleScope scope; |
| 191 | + Binary::Initialize(target); |
| 192 | +} |
0 commit comments