Permalink
Browse files

Merge branch 'master' into encoding-buffer

  • Loading branch information...
2 parents 275370d + c8c638a commit 42e3c7eeaa65cfba899e80c10a0a85f2c04cae1c @langpavel committed Sep 13, 2012
Showing with 108 additions and 96 deletions.
  1. +19 −0 doc/api/buffer.markdown
  2. +5 −0 lib/buffer.js
  3. +9 −4 src/node_buffer.h
  4. +4 −71 src/node_crypto.cc
  5. +4 −0 test/simple/test-buffer.js
  6. +67 −0 test/simple/test-crypto-padding-aes256.js
  7. +0 −21 tools/install.py
@@ -108,6 +108,25 @@ Decodes and returns a string from buffer data encoded with `encoding`
See `buffer.write()` example, above.
+### buf.toJSON()
+
+Returns a JSON-representation of the Buffer instance, which is identical to the
+output for JSON Arrays. `JSON.stringify` implicitly calls this function when
+stringifying a Buffer instance.
+
+Example:
+
+ var buf = new Buffer('test');
+ var json = JSON.stringify(buf);
+
+ console.log(json);
+ // '[116,101,115,116]'
+
+ var copy = new Buffer(JSON.parse(json));
+
+ console.log(copy);
+ // <Buffer 74 65 73 74>
+
### buf[index]
<!--type=property-->
View
@@ -411,6 +411,11 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
};
+Buffer.prototype.toJSON = function() {
+ return Array.prototype.slice.call(this, 0);
+};
+
+
// toString(encoding, start=0, end=buffer.length)
Buffer.prototype.toString = function(encoding, start, end) {
encoding = String(encoding || 'utf8').toLowerCase();
View
@@ -72,16 +72,21 @@ class NODE_EXTERN Buffer: public ObjectWrap {
static bool HasInstance(v8::Handle<v8::Value> val);
- static inline char* Data(v8::Handle<v8::Object> obj) {
- return (char*)obj->GetIndexedPropertiesExternalArrayData();
+ static inline char* Data(v8::Handle<v8::Value> val) {
+ assert(val->IsObject());
+ void* data = val.As<v8::Object>()->GetIndexedPropertiesExternalArrayData();
+ return reinterpret_cast<char*>(data);
}
static inline char* Data(Buffer *b) {
return Buffer::Data(b->handle_);
}
- static inline size_t Length(v8::Handle<v8::Object> obj) {
- return (size_t)obj->GetIndexedPropertiesExternalArrayDataLength();
+ static inline size_t Length(v8::Handle<v8::Value> val) {
+ assert(val->IsObject());
+ int len = val.As<v8::Object>()
+ ->GetIndexedPropertiesExternalArrayDataLength();
+ return static_cast<size_t>(len);
}
static inline size_t Length(Buffer *b) {
View
@@ -2153,66 +2153,6 @@ static int LengthWithoutIncompleteUtf8(char* buffer, int len) {
}
-// local decrypt final without strict padding check
-// to work with php mcrypt
-// see http://www.mail-archive.com/openssl-dev@openssl.org/msg19927.html
-int local_EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx,
- unsigned char *out,
- int *outl) {
- int i,b;
- int n;
-
- *outl=0;
- b=ctx->cipher->block_size;
-
- if (ctx->flags & EVP_CIPH_NO_PADDING) {
- if(ctx->buf_len) {
- EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
- return 0;
- }
- *outl = 0;
- return 1;
- }
-
- if (b > 1) {
- if (ctx->buf_len || !ctx->final_used) {
- EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH);
- return(0);
- }
-
- if (b > (int)(sizeof(ctx->final) / sizeof(ctx->final[0]))) {
- EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
- return(0);
- }
-
- n=ctx->final[b-1];
-
- if (n > b) {
- EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
- return(0);
- }
-
- for (i=0; i<n; i++) {
- if (ctx->final[--b] != n) {
- EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
- return(0);
- }
- }
-
- n=ctx->cipher->block_size-n;
-
- for (i=0; i<n; i++) {
- out[i]=ctx->final[i];
- }
- *outl=n;
- } else {
- *outl=0;
- }
-
- return(1);
-}
-
-
class Cipher : public ObjectWrap {
public:
static void Initialize (v8::Handle<v8::Object> target) {
@@ -2618,9 +2558,8 @@ class Decipher : public ObjectWrap {
NODE_SET_PROTOTYPE_METHOD(t, "init", DecipherInit);
NODE_SET_PROTOTYPE_METHOD(t, "initiv", DecipherInitIv);
NODE_SET_PROTOTYPE_METHOD(t, "update", DecipherUpdate);
- NODE_SET_PROTOTYPE_METHOD(t, "final", DecipherFinal<false>);
- // This is completely undocumented:
- NODE_SET_PROTOTYPE_METHOD(t, "finaltol", DecipherFinal<true>);
+ NODE_SET_PROTOTYPE_METHOD(t, "final", DecipherFinal);
+ NODE_SET_PROTOTYPE_METHOD(t, "finaltol", DecipherFinal); // remove someday
NODE_SET_PROTOTYPE_METHOD(t, "setAutoPadding", SetAutoPadding);
target->Set(String::NewSymbol("Decipher"), t->GetFunction());
@@ -2710,7 +2649,6 @@ class Decipher : public ObjectWrap {
}
// coverity[alloc_arg]
- template <bool TOLERATE_PADDING>
int DecipherFinal(unsigned char** out, int *out_len) {
int r;
@@ -2721,11 +2659,7 @@ class Decipher : public ObjectWrap {
}
*out = new unsigned char[EVP_CIPHER_CTX_block_size(&ctx)];
- if (TOLERATE_PADDING) {
- r = local_EVP_DecryptFinal_ex(&ctx,*out,out_len);
- } else {
- r = EVP_CipherFinal_ex(&ctx,*out,out_len);
- }
+ r = EVP_CipherFinal_ex(&ctx,*out,out_len);
EVP_CIPHER_CTX_cleanup(&ctx);
initialised_ = false;
return r;
@@ -2975,7 +2909,6 @@ class Decipher : public ObjectWrap {
return Undefined();
}
- template <bool TOLERATE_PADDING>
static Handle<Value> DecipherFinal(const Arguments& args) {
HandleScope scope;
@@ -2985,7 +2918,7 @@ class Decipher : public ObjectWrap {
int out_len = -1;
Local<Value> outString;
- int r = cipher->DecipherFinal<TOLERATE_PADDING>(&out_value, &out_len);
+ int r = cipher->DecipherFinal(&out_value, &out_len);
assert(out_value != NULL);
assert(out_len != -1);
@@ -748,3 +748,7 @@ Buffer(Buffer(0), 0, 0);
'new gnu gun' ].forEach(function(enc) {
assert.equal(Buffer.isEncoding(enc), false);
});
+
+
+// GH-3905
+assert.equal(JSON.stringify(Buffer('test')), '[116,101,115,116]');
@@ -0,0 +1,67 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// 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.
+
+var common = require('../common');
+var assert = require('assert');
+
+try {
+ var crypto = require('crypto');
+} catch (e) {
+ console.log('Not compiled with OpenSSL support.');
+ process.exit();
+}
+
+function aes256(decipherFinal) {
+ var iv = new Buffer('00000000000000000000000000000000', 'hex');
+ var key = new Buffer('0123456789abcdef0123456789abcdef' +
+ '0123456789abcdef0123456789abcdef', 'hex');
+
+ function encrypt(val, pad) {
+ var c = crypto.createCipheriv('aes256', key, iv);
+ c.setAutoPadding(pad);
+ return c.update(val, 'utf8') + c.final('binary');
+ }
+
+ function decrypt(val, pad) {
+ var c = crypto.createDecipheriv('aes256', key, iv);
+ c.setAutoPadding(pad);
+ return c.update(val, 'binary') + c[decipherFinal]('utf8');
+ }
+
+ // echo 0123456789abcdef0123456789abcdef \
+ // | openssl enc -e -aes256 -nopad -K <key> -iv <iv> \
+ // | openssl enc -d -aes256 -nopad -K <key> -iv <iv>
+ var plaintext = '0123456789abcdef0123456789abcdef'; // multiple of block size
+ var encrypted = encrypt(plaintext, false);
+ var decrypted = decrypt(encrypted, false);
+ assert.equal(decrypted, plaintext);
+
+ // echo 0123456789abcdef0123456789abcde \
+ // | openssl enc -e -aes256 -K <key> -iv <iv> \
+ // | openssl enc -d -aes256 -K <key> -iv <iv>
+ plaintext = '0123456789abcdef0123456789abcde'; // not a multiple
+ encrypted = encrypt(plaintext, true);
+ decrypted = decrypt(encrypted, true);
+ assert.equal(decrypted, plaintext);
+}
+
+aes256('final');
+aes256('finaltol');
View
@@ -114,27 +114,6 @@ def npm_files(action):
assert(0) # unhandled action type
def files(action):
- action(['deps/uv/include/uv.h',
- 'deps/v8/include/v8-debug.h',
- 'deps/v8/include/v8-preparser.h',
- 'deps/v8/include/v8-profiler.h',
- 'deps/v8/include/v8-testing.h',
- 'deps/v8/include/v8.h',
- 'deps/v8/include/v8stdint.h',
- 'src/eio-emul.h',
- 'src/ev-emul.h',
- 'src/node.h',
- 'src/node_buffer.h',
- 'src/node_object_wrap.h',
- 'src/node_version.h'],
- 'include/node/')
- action(['deps/uv/include/uv-private/eio.h',
- 'deps/uv/include/uv-private/ev.h',
- 'deps/uv/include/uv-private/ngx-queue.h',
- 'deps/uv/include/uv-private/tree.h',
- 'deps/uv/include/uv-private/uv-unix.h',
- 'deps/uv/include/uv-private/uv-win.h'],
- 'include/node/uv-private/')
action(['doc/node.1'], 'share/man/man1/')
action(['out/Release/node'], 'bin/node')

0 comments on commit 42e3c7e

Please sign in to comment.