#define OPENSSL_CONST
#endif
-#define ASSERT_IS_STRING_OR_BUFFER(val) do { \
+#define THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(val) \
+ do { \
if (!Buffer::HasInstance(val) && !val->IsString()) { \
return env->ThrowTypeError("Not a string or buffer"); \
} \
} while (0)
-#define ASSERT_IS_BUFFER(val) do { \
+#define THROW_AND_RETURN_IF_NOT_BUFFER(val) \
+ do { \
if (!Buffer::HasInstance(val)) { \
return env->ThrowTypeError("Not a buffer"); \
} \
}
if (args.Length() >= 2) {
- ASSERT_IS_BUFFER(args[1]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
size_t passlen = Buffer::Length(args[1]);
pass = new char[passlen + 1];
memcpy(pass, Buffer::Data(args[1]), passlen);
return env->ThrowTypeError("Bad argument");
}
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
size_t slen = Buffer::Length(args[0]);
char* sbuf = new char[slen];
memcpy(sbuf, Buffer::Data(args[0]), slen);
return env->ThrowError("Must give cipher-type, key, and iv as argument");
}
- ASSERT_IS_BUFFER(args[1]);
- ASSERT_IS_BUFFER(args[2]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[2]);
const node::Utf8Value cipher_type(env->isolate(), args[0]);
ssize_t key_len = Buffer::Length(args[1]);
void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
CipherBase* cipher = Unwrap<CipherBase>(args.Holder());
- ASSERT_IS_STRING_OR_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
unsigned char* out = nullptr;
bool r;
return env->ThrowError("Must give hashtype string, key as arguments");
}
- ASSERT_IS_BUFFER(args[1]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
const node::Utf8Value hash_type(env->isolate(), args[0]);
const char* buffer_data = Buffer::Data(args[1]);
Hmac* hmac = Unwrap<Hmac>(args.Holder());
- ASSERT_IS_STRING_OR_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
// Only copy the data if we have to, because it's a string
bool r;
Hash* hash = Unwrap<Hash>(args.Holder());
- ASSERT_IS_STRING_OR_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
// Only copy the data if we have to, because it's a string
bool r;
Sign* sign = Unwrap<Sign>(args.Holder());
- ASSERT_IS_STRING_OR_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
// Only copy the data if we have to, because it's a string
Error err;
node::Utf8Value passphrase(env->isolate(), args[2]);
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
size_t buf_len = Buffer::Length(args[0]);
char* buf = Buffer::Data(args[0]);
Verify* verify = Unwrap<Verify>(args.Holder());
- ASSERT_IS_STRING_OR_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[0]);
// Only copy the data if we have to, because it's a string
Error err;
Verify* verify = Unwrap<Verify>(args.Holder());
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
char* kbuf = Buffer::Data(args[0]);
ssize_t klen = Buffer::Length(args[0]);
- ASSERT_IS_STRING_OR_BUFFER(args[1]);
+ THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1]);
+
// BINARY works for both buffers and binary strings.
enum encoding encoding = BINARY;
if (args.Length() >= 3) {
void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
char* kbuf = Buffer::Data(args[0]);
ssize_t klen = Buffer::Length(args[0]);
- ASSERT_IS_BUFFER(args[1]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
char* buf = Buffer::Data(args[1]);
ssize_t len = Buffer::Length(args[1]);
if (args.Length() == 0) {
return env->ThrowError("First argument must be other party's public key");
} else {
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]),
if (args.Length() == 0) {
return env->ThrowError("First argument must be public key");
} else {
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
diffieHellman->dh->pub_key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]), 0);
if (args.Length() == 0) {
return env->ThrowError("First argument must be private key");
} else {
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
diffieHellman->dh->priv_key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]),
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
ECDH* ecdh = Unwrap<ECDH>(args.Holder());
ECDH* ecdh = Unwrap<ECDH>(args.Holder());
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
BIGNUM* priv = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(args[0].As<Object>())),
ECDH* ecdh = Unwrap<ECDH>(args.Holder());
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0].As<Object>()),
Buffer::Length(args[0].As<Object>()));
goto err;
}
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
passlen = Buffer::Length(args[0]);
if (passlen < 0) {
type_error = "Bad password";
goto err;
}
- ASSERT_IS_BUFFER(args[1]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[1]);
pass = static_cast<char*>(malloc(passlen));
if (pass == nullptr) {
if (args.Length() < 1)
return env->ThrowTypeError("Missing argument");
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
size_t length = Buffer::Length(args[0]);
if (length == 0)
if (args.Length() < 1)
return env->ThrowTypeError("Missing argument");
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
size_t length = Buffer::Length(args[0]);
if (length == 0)
if (args.Length() < 1)
return env->ThrowTypeError("Missing argument");
- ASSERT_IS_BUFFER(args[0]);
+ THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
size_t len = Buffer::Length(args[0]);
if (len == 0)