Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
crypto: Remove many unnecessary toObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 23, 2012
1 parent d7da20c commit f2fa97f
Showing 1 changed file with 46 additions and 62 deletions.
108 changes: 46 additions & 62 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,8 @@ static BIO* LoadBIO (Handle<Value> v) {
String::Utf8Value s(v);
r = BIO_write(bio, *s, s.length());
} else if (Buffer::HasInstance(v)) {
Local<Object> buffer_obj = v->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(v);
size_t buffer_length = Buffer::Length(v);
r = BIO_write(bio, buffer_data, buffer_length);
}

Expand Down Expand Up @@ -659,7 +658,7 @@ Handle<Value> SecureContext::LoadPKCS12(const Arguments& args) {
if (args.Length() >= 2) {
ASSERT_IS_BUFFER(args[1]);

int passlen = Buffer::Length(args[1]->ToObject());
int passlen = Buffer::Length(args[1]);
if (passlen < 0) {
BIO_free(in);
return ThrowException(Exception::TypeError(
Expand Down Expand Up @@ -1283,9 +1282,8 @@ Handle<Value> Connection::EncIn(const Arguments& args) {
String::New("Second argument should be a buffer")));
}

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

size_t off = args[1]->Int32Value();
if (off >= buffer_length) {
Expand Down Expand Up @@ -1330,9 +1328,8 @@ Handle<Value> Connection::ClearOut(const Arguments& args) {
String::New("Second argument should be a buffer")));
}

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

size_t off = args[1]->Int32Value();
if (off >= buffer_length) {
Expand Down Expand Up @@ -1403,9 +1400,8 @@ Handle<Value> Connection::EncOut(const Arguments& args) {
String::New("Second argument should be a buffer")));
}

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

size_t off = args[1]->Int32Value();
if (off >= buffer_length) {
Expand Down Expand Up @@ -1443,9 +1439,8 @@ Handle<Value> Connection::ClearIn(const Arguments& args) {
String::New("Second argument should be a buffer")));
}

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

size_t off = args[1]->Int32Value();
if (off > buffer_length) {
Expand Down Expand Up @@ -1628,7 +1623,7 @@ Handle<Value> Connection::SetSession(const Arguments& args) {
}

ASSERT_IS_BUFFER(args[0]);
ssize_t slen = Buffer::Length(args[0]->ToObject());
ssize_t slen = Buffer::Length(args[0]);

if (slen < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand Down Expand Up @@ -2130,7 +2125,7 @@ class Cipher : public ObjectWrap {
}

ASSERT_IS_BUFFER(args[1]);
ssize_t key_buf_len = Buffer::Length(args[1]->ToObject());
ssize_t key_buf_len = Buffer::Length(args[1]);

if (key_buf_len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand Down Expand Up @@ -2171,15 +2166,15 @@ class Cipher : public ObjectWrap {
}

ASSERT_IS_BUFFER(args[1]);
ssize_t key_len = Buffer::Length(args[1]->ToObject());
ssize_t key_len = Buffer::Length(args[1]);

if (key_len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
return ThrowException(exception);
}

ASSERT_IS_BUFFER(args[2]);
ssize_t iv_len = Buffer::Length(args[2]->ToObject());
ssize_t iv_len = Buffer::Length(args[2]);

if (iv_len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand Down Expand Up @@ -2217,9 +2212,8 @@ class Cipher : public ObjectWrap {

unsigned char *out=0;
int out_len=0, r;
Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char* buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

r = cipher->CipherUpdate(buffer_data, buffer_length, &out, &out_len);

Expand Down Expand Up @@ -2443,7 +2437,7 @@ class Decipher : public ObjectWrap {
}

ASSERT_IS_BUFFER(args[1]);
ssize_t key_len = Buffer::Length(args[1]->ToObject());
ssize_t key_len = Buffer::Length(args[1]);

if (key_len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand Down Expand Up @@ -2482,15 +2476,15 @@ class Decipher : public ObjectWrap {
}

ASSERT_IS_BUFFER(args[1]);
ssize_t key_len = Buffer::Length(args[1]->ToObject());
ssize_t key_len = Buffer::Length(args[1]);

if (key_len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
return ThrowException(exception);
}

ASSERT_IS_BUFFER(args[2]);
ssize_t iv_len = Buffer::Length(args[2]->ToObject());
ssize_t iv_len = Buffer::Length(args[2]);

if (iv_len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand Down Expand Up @@ -2531,9 +2525,8 @@ class Decipher : public ObjectWrap {
char* buf;
// if alloc_buf then buf must be deleted later
bool alloc_buf = false;
Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

buf = buffer_data;
len = buffer_length;
Expand Down Expand Up @@ -2682,7 +2675,7 @@ class Hmac : public ObjectWrap {
}

ASSERT_IS_BUFFER(args[1]);
ssize_t len = Buffer::Length(args[1]->ToObject());
ssize_t len = Buffer::Length(args[1]);

if (len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand All @@ -2694,9 +2687,8 @@ class Hmac : public ObjectWrap {
bool r;

if( Buffer::HasInstance(args[1])) {
Local<Object> buffer_obj = args[1]->ToObject();
char* buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char* buffer_data = Buffer::Data(args[1]);
size_t buffer_length = Buffer::Length(args[1]);

r = hmac->HmacInit(*hashType, buffer_data, buffer_length);
} else {
Expand Down Expand Up @@ -2725,9 +2717,8 @@ class Hmac : public ObjectWrap {

int r;

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

r = hmac->HmacUpdate(buffer_data, buffer_length);

Expand Down Expand Up @@ -2841,9 +2832,8 @@ class Hash : public ObjectWrap {

int r;

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);
r = hash->HashUpdate(buffer_data, buffer_length);

if (!r) {
Expand Down Expand Up @@ -2994,9 +2984,8 @@ class Sign : public ObjectWrap {

int r;

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

r = sign->SignUpdate(buffer_data, buffer_length);

Expand All @@ -3021,7 +3010,7 @@ class Sign : public ObjectWrap {
md_value = new unsigned char[md_len];

ASSERT_IS_BUFFER(args[0]);
ssize_t len = Buffer::Length(args[0]->ToObject());
ssize_t len = Buffer::Length(args[0]);

char* buf = new char[len];
ssize_t written = DecodeWrite(buf, len, args[0], BUFFER);
Expand Down Expand Up @@ -3206,9 +3195,8 @@ class Verify : public ObjectWrap {

int r;

Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);
char *buffer_data = Buffer::Data(args[0]);
size_t buffer_length = Buffer::Length(args[0]);

r = verify->VerifyUpdate(buffer_data, buffer_length);

Expand All @@ -3227,7 +3215,7 @@ class Verify : public ObjectWrap {
Verify *verify = ObjectWrap::Unwrap<Verify>(args.This());

ASSERT_IS_BUFFER(args[0]);
ssize_t klen = Buffer::Length(args[0]->ToObject());
ssize_t klen = Buffer::Length(args[0]);

if (klen < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
Expand All @@ -3239,7 +3227,7 @@ class Verify : public ObjectWrap {
assert(kwritten == klen);

ASSERT_IS_BUFFER(args[1]);
ssize_t hlen = Buffer::Length(args[1]->ToObject());
ssize_t hlen = Buffer::Length(args[1]);

if (hlen < 0) {
delete [] kbuf;
Expand Down Expand Up @@ -3384,10 +3372,9 @@ class DiffieHellman : public ObjectWrap {
if (args[0]->IsInt32()) {
initialized = diffieHellman->Init(args[0]->Int32Value());
} else {
Local<Object> buffer = args[0]->ToObject();
initialized = diffieHellman->Init(
reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer));
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]));
}
}

Expand Down Expand Up @@ -3551,10 +3538,9 @@ class DiffieHellman : public ObjectWrap {
String::New("First argument must be other party's public key")));
} else {
ASSERT_IS_BUFFER(args[0]);
Local<Object> buffer = args[0]->ToObject();
key = BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer), 0);
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]), 0);
}

int dataSize = DH_size(diffieHellman->dh);
Expand Down Expand Up @@ -3623,11 +3609,10 @@ class DiffieHellman : public ObjectWrap {
String::New("First argument must be public key")));
} else {
ASSERT_IS_BUFFER(args[0]);
Local<Object> buffer = args[0]->ToObject();
diffieHellman->dh->pub_key =
BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer), 0);
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]), 0);
}

return args.This();
Expand All @@ -3649,11 +3634,10 @@ class DiffieHellman : public ObjectWrap {
String::New("First argument must be private key")));
} else {
ASSERT_IS_BUFFER(args[0]);
Local<Object> buffer = args[0]->ToObject();
diffieHellman->dh->priv_key =
BN_bin2bn(
reinterpret_cast<unsigned char*>(Buffer::Data(buffer)),
Buffer::Length(buffer), 0);
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
Buffer::Length(args[0]), 0);
}

return args.This();
Expand Down Expand Up @@ -3769,7 +3753,7 @@ Handle<Value> PBKDF2(const Arguments& args) {
}

ASSERT_IS_BUFFER(args[0]);
passlen = Buffer::Length(args[0]->ToObject());
passlen = Buffer::Length(args[0]);
if (passlen < 0) {
type_error = "Bad password";
goto err;
Expand All @@ -3780,7 +3764,7 @@ Handle<Value> PBKDF2(const Arguments& args) {
assert(pass_written == passlen);

ASSERT_IS_BUFFER(args[1]);
saltlen = Buffer::Length(args[1]->ToObject());
saltlen = Buffer::Length(args[1]);
if (saltlen < 0) {
type_error = "Bad salt";
goto err;
Expand Down

0 comments on commit f2fa97f

Please sign in to comment.