#include
#include
#include // malloc, free
#include
#include // memcpy
#include // htons, htonl
#include
#define MIN(a,b) ((a) < (b) ? (a) : (b))
namespace node {
using namespace v8;
#define SLICE_ARGS(start_arg, end_arg) \
if (!start_arg->IsInt32() || !end_arg->IsInt32()) { \
return ThrowException(Exception::TypeError( \
String::New("Bad argument."))); \
} \
int32_t start = start_arg->Int32Value(); \
int32_t end = end_arg->Int32Value(); \
if (start < 0 || end < 0) { \
return ThrowException(Exception::TypeError( \
String::New("Bad argument."))); \
} \
if (!(start <= end)) { \
return ThrowException(Exception::Error( \
String::New("Must have start <= end"))); \
} \
if ((size_t)end > parent->length_) { \
return ThrowException(Exception::Error( \
String::New("end cannot be longer than parent.length"))); \
}
static Persistent length_symbol;
static Persistent chars_written_sym;
static Persistent write_sym;
Persistent Buffer::constructor_template;
static inline size_t base64_decoded_size(const char *src, size_t size) {
const char *const end = src + size;
const int remainder = size % 4;
size = (size / 4) * 3;
if (remainder) {
if (size == 0 && remainder == 1) {
// special case: 1-byte input cannot be decoded
size = 0;
} else {
// non-padded input, add 1 or 2 extra bytes
size += 1 + (remainder == 3);
}
}
// check for trailing padding (1 or 2 bytes)
if (size > 0) {
if (end[-1] == '=') size--;
if (end[-2] == '=') size--;
}
return size;
}
static size_t ByteLength (Handle string, enum encoding enc) {
HandleScope scope;
if (enc == UTF8) {
return string->Utf8Length();
} else if (enc == BASE64) {
String::Utf8Value v(string);
return base64_decoded_size(*v, v.length());
} else {
return string->Length();
}
}
Handle