mirror of https://github.com/lukechilds/node.git
Browse Source
Conflicts: AUTHORS ChangeLog deps/uv/ChangeLog deps/uv/config-unix.mk deps/uv/src/unix/stream.c deps/uv/src/version.c deps/uv/uv.gyp src/node.cc src/node_buffer.cc src/node_crypto.cc src/node_version.h src/stream_wrap.cc src/stream_wrap.hv0.11.3-release
32 changed files with 1378 additions and 891 deletions
@ -0,0 +1,86 @@ |
|||
// throughput benchmark
|
|||
// creates a single hasher, then pushes a bunch of data through it
|
|||
var common = require('../common.js'); |
|||
var crypto = require('crypto'); |
|||
|
|||
var bench = common.createBenchmark(main, { |
|||
writes: [500], |
|||
algo: [ 'sha256', 'md5' ], |
|||
type: ['asc', 'utf', 'buf'], |
|||
out: ['hex', 'binary', 'buffer'], |
|||
len: [2, 1024, 102400, 1024 * 1024], |
|||
api: ['legacy', 'stream'] |
|||
}); |
|||
|
|||
function main(conf) { |
|||
var api = conf.api; |
|||
if (api === 'stream' && process.version.match(/^v0\.[0-8]\./)) { |
|||
console.error('Crypto streams not available until v0.10'); |
|||
// use the legacy, just so that we can compare them.
|
|||
api = 'legacy'; |
|||
} |
|||
|
|||
var crypto = require('crypto'); |
|||
var assert = require('assert'); |
|||
|
|||
var message; |
|||
var encoding; |
|||
switch (conf.type) { |
|||
case 'asc': |
|||
message = new Array(conf.len + 1).join('a'); |
|||
encoding = 'ascii'; |
|||
break; |
|||
case 'utf': |
|||
message = new Array(conf.len / 2 + 1).join('ü'); |
|||
encoding = 'utf8'; |
|||
break; |
|||
case 'buf': |
|||
message = new Buffer(conf.len); |
|||
message.fill('b'); |
|||
break; |
|||
default: |
|||
throw new Error('unknown message type: ' + conf.type); |
|||
} |
|||
|
|||
var fn = api === 'stream' ? streamWrite : legacyWrite; |
|||
|
|||
bench.start(); |
|||
fn(conf.algo, message, encoding, conf.writes, conf.len, conf.out); |
|||
} |
|||
|
|||
function legacyWrite(algo, message, encoding, writes, len, outEnc) { |
|||
var written = writes * len; |
|||
var bits = written * 8; |
|||
var gbits = bits / (1024 * 1024 * 1024); |
|||
|
|||
while (writes-- > 0) { |
|||
var h = crypto.createHash(algo); |
|||
h.update(message, encoding); |
|||
var res = h.digest(outEnc); |
|||
|
|||
// include buffer creation costs for older versions
|
|||
if (outEnc === 'buffer' && typeof res === 'string') |
|||
res = new Buffer(res, 'binary'); |
|||
} |
|||
|
|||
bench.end(gbits); |
|||
} |
|||
|
|||
function streamWrite(algo, message, encoding, writes, len, outEnc) { |
|||
var written = writes * len; |
|||
var bits = written * 8; |
|||
var gbits = bits / (1024 * 1024 * 1024); |
|||
|
|||
while (writes-- > 0) { |
|||
var h = crypto.createHash(algo); |
|||
|
|||
if (outEnc !== 'buffer') |
|||
h.setEncoding(outEnc); |
|||
|
|||
h.write(message, encoding); |
|||
h.end(); |
|||
h.read(); |
|||
} |
|||
|
|||
bench.end(gbits); |
|||
} |
@ -0,0 +1,77 @@ |
|||
// throughput benchmark
|
|||
// creates a single hasher, then pushes a bunch of data through it
|
|||
var common = require('../common.js'); |
|||
var crypto = require('crypto'); |
|||
|
|||
var bench = common.createBenchmark(main, { |
|||
writes: [500], |
|||
algo: [ 'sha256', 'md5' ], |
|||
type: ['asc', 'utf', 'buf'], |
|||
len: [2, 1024, 102400, 1024 * 1024], |
|||
api: ['legacy', 'stream'] |
|||
}); |
|||
|
|||
function main(conf) { |
|||
var api = conf.api; |
|||
if (api === 'stream' && process.version.match(/^v0\.[0-8]\./)) { |
|||
console.error('Crypto streams not available until v0.10'); |
|||
// use the legacy, just so that we can compare them.
|
|||
api = 'legacy'; |
|||
} |
|||
|
|||
var crypto = require('crypto'); |
|||
var assert = require('assert'); |
|||
|
|||
var message; |
|||
var encoding; |
|||
switch (conf.type) { |
|||
case 'asc': |
|||
message = new Array(conf.len + 1).join('a'); |
|||
encoding = 'ascii'; |
|||
break; |
|||
case 'utf': |
|||
message = new Array(conf.len / 2 + 1).join('ü'); |
|||
encoding = 'utf8'; |
|||
break; |
|||
case 'buf': |
|||
message = new Buffer(conf.len); |
|||
message.fill('b'); |
|||
break; |
|||
default: |
|||
throw new Error('unknown message type: ' + conf.type); |
|||
} |
|||
|
|||
var fn = api === 'stream' ? streamWrite : legacyWrite; |
|||
|
|||
bench.start(); |
|||
fn(conf.algo, message, encoding, conf.writes, conf.len); |
|||
} |
|||
|
|||
function legacyWrite(algo, message, encoding, writes, len) { |
|||
var written = writes * len; |
|||
var bits = written * 8; |
|||
var gbits = bits / (1024 * 1024 * 1024); |
|||
var h = crypto.createHash(algo); |
|||
|
|||
while (writes-- > 0) |
|||
h.update(message, encoding); |
|||
|
|||
h.digest(); |
|||
|
|||
bench.end(gbits); |
|||
} |
|||
|
|||
function streamWrite(algo, message, encoding, writes, len) { |
|||
var written = writes * len; |
|||
var bits = written * 8; |
|||
var gbits = bits / (1024 * 1024 * 1024); |
|||
var h = crypto.createHash(algo); |
|||
|
|||
while (writes-- > 0) |
|||
h.write(message, encoding); |
|||
|
|||
h.end(); |
|||
h.read(); |
|||
|
|||
bench.end(gbits); |
|||
} |
@ -0,0 +1,65 @@ |
|||
date: Tue May 14 14:32:31 PDT 2013 |
|||
version: 0.10.6 |
|||
category: release |
|||
title: Node v0.10.6 (Stable) |
|||
slug: node-v0-10-6-stable |
|||
|
|||
2013.05.14, Version 0.10.6 (Stable) |
|||
|
|||
* module: Deprecate require.extensions (isaacs) |
|||
|
|||
* stream: make Readable.wrap support objectMode, empty streams (Daniel Moore) |
|||
|
|||
* child_process: fix handle delivery (Ben Noordhuis) |
|||
|
|||
* crypto: Fix performance regression (isaacs) |
|||
|
|||
* src: DRY string encoding/decoding (isaacs) |
|||
|
|||
|
|||
Source Code: http://nodejs.org/dist/v0.10.6/node-v0.10.6.tar.gz |
|||
|
|||
Macintosh Installer (Universal): http://nodejs.org/dist/v0.10.6/node-v0.10.6.pkg |
|||
|
|||
Windows Installer: http://nodejs.org/dist/v0.10.6/node-v0.10.6-x86.msi |
|||
|
|||
Windows x64 Installer: http://nodejs.org/dist/v0.10.6/x64/node-v0.10.6-x64.msi |
|||
|
|||
Windows x64 Files: http://nodejs.org/dist/v0.10.6/x64/ |
|||
|
|||
Linux 32-bit Binary: http://nodejs.org/dist/v0.10.6/node-v0.10.6-linux-x86.tar.gz |
|||
|
|||
Linux 64-bit Binary: http://nodejs.org/dist/v0.10.6/node-v0.10.6-linux-x64.tar.gz |
|||
|
|||
Solaris 32-bit Binary: http://nodejs.org/dist/v0.10.6/node-v0.10.6-sunos-x86.tar.gz |
|||
|
|||
Solaris 64-bit Binary: http://nodejs.org/dist/v0.10.6/node-v0.10.6-sunos-x64.tar.gz |
|||
|
|||
Other release files: http://nodejs.org/dist/v0.10.6/ |
|||
|
|||
Website: http://nodejs.org/docs/v0.10.6/ |
|||
|
|||
Documentation: http://nodejs.org/docs/v0.10.6/api/ |
|||
|
|||
Shasums: |
|||
|
|||
``` |
|||
24982edc3b6aafd019273fa5e8a2031353314b56 node-v0.10.6-darwin-x64.tar.gz |
|||
e208c5dc83864a7f35f9df60ee35642bc7dd689c node-v0.10.6-darwin-x86.tar.gz |
|||
ab2ad473f5aa0f1c5adb50b9ea47fd05010bca2c node-v0.10.6-linux-x64.tar.gz |
|||
29cdac417449f088e6e6fa67d57c9205d8bff6c5 node-v0.10.6-linux-x86.tar.gz |
|||
66e3a9e53af6d8f27c690a77c329a2bd108965ac node-v0.10.6-sunos-x64.tar.gz |
|||
05bda089f4f702deddb8e593653676ede5f0e10b node-v0.10.6-sunos-x86.tar.gz |
|||
6ceb80be28c63f2c57e8479755d206400b205c46 node-v0.10.6-x86.msi |
|||
cd0acf9b332c30aba6a72979d3373e342fad6b95 node-v0.10.6.pkg |
|||
fa06101af8890eeaf997bd2620d7742b71a7223c node-v0.10.6.tar.gz |
|||
a2a2befa62b3cd2da9c2e51204df017e0f0c0cae node.exe |
|||
8401647a2f8fb3486fa08d7e603822ae12cf6dee node.exp |
|||
8ab923eb23584310874a4f63d71244cca5bfc0f8 node.lib |
|||
b9042fec324b202853f7f1d8b1d26ea49d944913 node.pdb |
|||
647a2ea899113e14d8f0894aa969ffd3a5d407c6 x64/node-v0.10.6-x64.msi |
|||
021048aa29fce5ce200b22896fd5f1b053f0d40c x64/node.exe |
|||
adbdc6112b3172c259a3fa9e07b7d456a0c65beb x64/node.exp |
|||
226af9033a41c96c68ade96cecedcf1289414424 x64/node.lib |
|||
cca201bfe38713ffde4a0cad70cb3a5325097257 x64/node.pdb |
|||
``` |
@ -0,0 +1,87 @@ |
|||
date: Mon May 13 15:53:06 PDT 2013 |
|||
version: 0.11.2 |
|||
category: release |
|||
title: Node v0.11.2 (Unstable) |
|||
slug: node-v0-11-2-unstable |
|||
|
|||
2013.05.13, Version 0.11.2 (Unstable) |
|||
|
|||
* uv: Upgrade to 0.11.2 |
|||
|
|||
* V8: Upgrade to 3.19.0 |
|||
|
|||
* npm: Upgrade to 1.2.21 |
|||
|
|||
* build: Makefile should respect configure --prefix (Timothy J Fontaine) |
|||
|
|||
* cluster: use round-robin load balancing (Ben Noordhuis) |
|||
|
|||
* debugger, cluster: each worker has new debug port (Miroslav Bajtoš) |
|||
|
|||
* debugger: `restart` with custom debug port (Miroslav Bajtoš) |
|||
|
|||
* debugger: breakpoints in scripts not loaded yet (Miroslav Bajtoš) |
|||
|
|||
* event: EventEmitter#setMaxListeners() returns this (Sam Roberts) |
|||
|
|||
* events: add EventEmitter.defaultMaxListeners (Ben Noordhuis) |
|||
|
|||
* install: Support $(PREFIX) install target directory prefix (Olof Johansson) |
|||
|
|||
* os: Include netmask in os.networkInterfaces() (Ben Kelly) |
|||
|
|||
* path: add path.isAbsolute(path) (Ryan Doenges) |
|||
|
|||
* stream: Guarantee ordering of 'finish' event (isaacs) |
|||
|
|||
* streams: introduce .cork/.uncork/._writev (Fedor Indutny) |
|||
|
|||
* vm: add support for timeout argument (Andrew Paprocki) |
|||
|
|||
|
|||
Source Code: http://nodejs.org/dist/v0.11.2/node-v0.11.2.tar.gz |
|||
|
|||
Macintosh Installer (Universal): http://nodejs.org/dist/v0.11.2/node-v0.11.2.pkg |
|||
|
|||
Windows Installer: http://nodejs.org/dist/v0.11.2/node-v0.11.2-x86.msi |
|||
|
|||
Windows x64 Installer: http://nodejs.org/dist/v0.11.2/x64/node-v0.11.2-x64.msi |
|||
|
|||
Windows x64 Files: http://nodejs.org/dist/v0.11.2/x64/ |
|||
|
|||
Linux 32-bit Binary: http://nodejs.org/dist/v0.11.2/node-v0.11.2-linux-x86.tar.gz |
|||
|
|||
Linux 64-bit Binary: http://nodejs.org/dist/v0.11.2/node-v0.11.2-linux-x64.tar.gz |
|||
|
|||
Solaris 32-bit Binary: http://nodejs.org/dist/v0.11.2/node-v0.11.2-sunos-x86.tar.gz |
|||
|
|||
Solaris 64-bit Binary: http://nodejs.org/dist/v0.11.2/node-v0.11.2-sunos-x64.tar.gz |
|||
|
|||
Other release files: http://nodejs.org/dist/v0.11.2/ |
|||
|
|||
Website: http://nodejs.org/docs/v0.11.2/ |
|||
|
|||
Documentation: http://nodejs.org/docs/v0.11.2/api/ |
|||
|
|||
Shasums: |
|||
|
|||
``` |
|||
ddc85fd6ed70057c64d7c9cd64bb94f28596d163 node-v0.11.2-darwin-x64.tar.gz |
|||
9893a3a3598d2e5ed24bfee8642b72c37808dbae node-v0.11.2-darwin-x86.tar.gz |
|||
f81189c30aa268f2b43572e1795fedd50f3495c3 node-v0.11.2-linux-x64.tar.gz |
|||
6322bf3be78f907a3b5e06f38af1b33c52957612 node-v0.11.2-linux-x86.tar.gz |
|||
3becca01532e104081ca51a265f07e77b6e9e25f node-v0.11.2-sunos-x64.tar.gz |
|||
9489238384edb456d9a603e5bef1128dfafe69b1 node-v0.11.2-sunos-x86.tar.gz |
|||
76421e22cff4d4f4d1cb2ce3e3566e2c9004cdee node-v0.11.2-x86.msi |
|||
a23d607f7b433197533cd6d88c981c75463efff8 node-v0.11.2.pkg |
|||
1d1080598431062ccb4bbbf7ecbb7596fe664c67 node-v0.11.2.tar.gz |
|||
b45a04167d32887c32a2479c4567af394627c8ad node.exe |
|||
c65ce6e073e173ae5769fe4dd9ff83f2f56ce05d node.exp |
|||
31f569697cb8447492e3172e614c3c4cfff81d09 node.lib |
|||
c98f8a717ef9d660ff3d45e86e2ee396ca02e721 node.pdb |
|||
7caabd3a774c96a8126f10d2e184727bd5160526 x64/node-v0.11.2-x64.msi |
|||
3b049227e3c392fdb88de9a5da7ad1ec14c82d17 x64/node.exe |
|||
c95f9746e180c064a5225ab83cca604bf918e59a x64/node.exp |
|||
78c94386c312ded2f7cb0c84951535b67e36fecf x64/node.lib |
|||
ad774b472a3cfa03374aac2d1dac19f9599ad2f8 x64/node.pdb |
|||
``` |
@ -0,0 +1,622 @@ |
|||
// 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.
|
|||
|
|||
#include "string_bytes.h" |
|||
|
|||
#include <assert.h> |
|||
#include <string.h> // memcpy |
|||
#include <limits.h> |
|||
|
|||
#include "node.h" |
|||
#include "node_buffer.h" |
|||
#include "v8.h" |
|||
|
|||
namespace node { |
|||
|
|||
using v8::Local; |
|||
using v8::Handle; |
|||
using v8::HandleScope; |
|||
using v8::Object; |
|||
using v8::String; |
|||
using v8::Value; |
|||
|
|||
|
|||
//// Base 64 ////
|
|||
|
|||
#define base64_encoded_size(size) ((size + 2 - ((size + 2) % 3)) / 3 * 4) |
|||
|
|||
|
|||
// Doesn't check for padding at the end. Can be 1-2 bytes over.
|
|||
static inline size_t base64_decoded_size_fast(size_t size) { |
|||
size_t 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); |
|||
} |
|||
} |
|||
|
|||
return size; |
|||
} |
|||
|
|||
static inline size_t base64_decoded_size(const char* src, size_t size) { |
|||
size = base64_decoded_size_fast(size); |
|||
|
|||
const char* end = src + size; |
|||
// check for trailing padding (1 or 2 bytes)
|
|||
if (size > 0) { |
|||
if (end[-1] == '=') size--; |
|||
if (size > 0 && end[-2] == '=') size--; |
|||
} |
|||
|
|||
return size; |
|||
} |
|||
|
|||
|
|||
// supports regular and URL-safe base64
|
|||
static const int unbase64_table[] = |
|||
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -2, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, |
|||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
|||
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
|||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, |
|||
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
|||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
|||
}; |
|||
#define unbase64(x) unbase64_table[(uint8_t)(x)] |
|||
|
|||
|
|||
static inline size_t base64_decode(char *buf, |
|||
size_t len, |
|||
const char *src, |
|||
const size_t srcLen) { |
|||
char a, b, c, d; |
|||
char* dst = buf; |
|||
char* dstEnd = buf + len; |
|||
const char* srcEnd = src + srcLen; |
|||
|
|||
while (src < srcEnd && dst < dstEnd) { |
|||
int remaining = srcEnd - src; |
|||
|
|||
while (unbase64(*src) < 0 && src < srcEnd) src++, remaining--; |
|||
if (remaining == 0 || *src == '=') break; |
|||
a = unbase64(*src++); |
|||
|
|||
while (unbase64(*src) < 0 && src < srcEnd) src++, remaining--; |
|||
if (remaining <= 1 || *src == '=') break; |
|||
b = unbase64(*src++); |
|||
|
|||
*dst++ = (a << 2) | ((b & 0x30) >> 4); |
|||
if (dst == dstEnd) break; |
|||
|
|||
while (unbase64(*src) < 0 && src < srcEnd) src++, remaining--; |
|||
if (remaining <= 2 || *src == '=') break; |
|||
c = unbase64(*src++); |
|||
|
|||
*dst++ = ((b & 0x0F) << 4) | ((c & 0x3C) >> 2); |
|||
if (dst == dstEnd) break; |
|||
|
|||
while (unbase64(*src) < 0 && src < srcEnd) src++, remaining--; |
|||
if (remaining <= 3 || *src == '=') break; |
|||
d = unbase64(*src++); |
|||
|
|||
*dst++ = ((c & 0x03) << 6) | (d & 0x3F); |
|||
} |
|||
|
|||
return dst - buf; |
|||
} |
|||
|
|||
|
|||
//// HEX ////
|
|||
|
|||
static inline unsigned hex2bin(char c) { |
|||
if (c >= '0' && c <= '9') return c - '0'; |
|||
if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); |
|||
if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); |
|||
return static_cast<unsigned>(-1); |
|||
} |
|||
|
|||
|
|||
static inline size_t hex_decode(char *buf, |
|||
size_t len, |
|||
const char *src, |
|||
const size_t srcLen) { |
|||
size_t i; |
|||
for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) { |
|||
unsigned a = hex2bin(src[i * 2 + 0]); |
|||
unsigned b = hex2bin(src[i * 2 + 1]); |
|||
if (!~a || !~b) return i; |
|||
buf[i] = a * 16 + b; |
|||
} |
|||
|
|||
return i; |
|||
} |
|||
|
|||
|
|||
size_t StringBytes::Write(char* buf, |
|||
size_t buflen, |
|||
Handle<Value> val, |
|||
enum encoding encoding, |
|||
int* chars_written) { |
|||
|
|||
HandleScope scope; |
|||
size_t len = 0; |
|||
bool is_buffer = Buffer::HasInstance(val); |
|||
|
|||
// sometimes we use 'binary' when we mean 'buffer'
|
|||
if (is_buffer && (encoding == BINARY || encoding == BUFFER)) { |
|||
// fast path, copy buffer data
|
|||
Local<Object> valObj = Local<Object>::New(val.As<Object>()); |
|||
const char* data = Buffer::Data(valObj); |
|||
size_t size = Buffer::Length(valObj); |
|||
size_t len = size < buflen ? size : buflen; |
|||
memcpy(buf, data, len); |
|||
return len; |
|||
} |
|||
|
|||
Local<String> str = val->ToString(); |
|||
|
|||
int flags = String::NO_NULL_TERMINATION | |
|||
String::HINT_MANY_WRITES_EXPECTED; |
|||
|
|||
switch (encoding) { |
|||
case ASCII: |
|||
len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), |
|||
0, |
|||
buflen, |
|||
flags); |
|||
if (chars_written != NULL) { |
|||
*chars_written = len; |
|||
} |
|||
break; |
|||
|
|||
case UTF8: |
|||
len = str->WriteUtf8(buf, buflen, chars_written, flags); |
|||
break; |
|||
|
|||
case UCS2: |
|||
len = str->Write(reinterpret_cast<uint16_t*>(buf), 0, buflen, flags); |
|||
if (chars_written != NULL) { |
|||
*chars_written = len; |
|||
} |
|||
len = len * sizeof(uint16_t); |
|||
break; |
|||
|
|||
case BASE64: { |
|||
String::AsciiValue value(str); |
|||
len = base64_decode(buf, buflen, *value, value.length()); |
|||
if (chars_written != NULL) { |
|||
*chars_written = len; |
|||
} |
|||
break; |
|||
} |
|||
|
|||
case BINARY: |
|||
case BUFFER: { |
|||
// TODO(isaacs): THIS IS AWFUL!!!
|
|||
uint16_t* twobytebuf = new uint16_t[buflen]; |
|||
|
|||
len = str->Write(twobytebuf, 0, buflen, flags); |
|||
|
|||
for (size_t i = 0; i < buflen && i < len; i++) { |
|||
unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]); |
|||
buf[i] = b[0]; |
|||
} |
|||
|
|||
if (chars_written != NULL) { |
|||
*chars_written = len; |
|||
} |
|||
|
|||
delete[] twobytebuf; |
|||
break; |
|||
} |
|||
|
|||
case HEX: { |
|||
String::AsciiValue value(str); |
|||
len = hex_decode(buf, buflen, *value, value.length()); |
|||
if (chars_written != NULL) { |
|||
*chars_written = len * 2; |
|||
} |
|||
break; |
|||
} |
|||
|
|||
default: |
|||
assert(0 && "unknown encoding"); |
|||
break; |
|||
} |
|||
|
|||
return len; |
|||
} |
|||
|
|||
|
|||
// Quick and dirty size calculation
|
|||
// Will always be at least big enough, but may have some extra
|
|||
// UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes
|
|||
size_t StringBytes::StorageSize(Handle<Value> val, enum encoding encoding) { |
|||
HandleScope scope; |
|||
size_t data_size = 0; |
|||
bool is_buffer = Buffer::HasInstance(val); |
|||
|
|||
if (is_buffer && (encoding == BUFFER || encoding == BINARY)) { |
|||
return Buffer::Length(val); |
|||
} |
|||
|
|||
Local<String> str = val->ToString(); |
|||
|
|||
switch (encoding) { |
|||
case BINARY: |
|||
case BUFFER: |
|||
case ASCII: |
|||
data_size = str->Length(); |
|||
break; |
|||
|
|||
case UTF8: |
|||
// A single UCS2 codepoint never takes up more than 3 utf8 bytes.
|
|||
// It is an exercise for the caller to decide when a string is
|
|||
// long enough to justify calling Size() instead of StorageSize()
|
|||
data_size = 3 * str->Length(); |
|||
break; |
|||
|
|||
case UCS2: |
|||
data_size = str->Length() * sizeof(uint16_t); |
|||
break; |
|||
|
|||
case BASE64: |
|||
data_size = base64_decoded_size_fast(str->Length()); |
|||
break; |
|||
|
|||
case HEX: |
|||
assert(str->Length() % 2 == 0 && "invalid hex string length"); |
|||
data_size = str->Length() / 2; |
|||
break; |
|||
|
|||
default: |
|||
assert(0 && "unknown encoding"); |
|||
break; |
|||
} |
|||
|
|||
return data_size; |
|||
} |
|||
|
|||
|
|||
size_t StringBytes::Size(Handle<Value> val, enum encoding encoding) { |
|||
HandleScope scope; |
|||
size_t data_size = 0; |
|||
bool is_buffer = Buffer::HasInstance(val); |
|||
|
|||
if (is_buffer && (encoding == BUFFER || encoding == BINARY)) { |
|||
return Buffer::Length(val); |
|||
} |
|||
|
|||
Local<String> str = val->ToString(); |
|||
|
|||
switch (encoding) { |
|||
case BINARY: |
|||
case BUFFER: |
|||
case ASCII: |
|||
data_size = str->Length(); |
|||
break; |
|||
|
|||
case UTF8: |
|||
data_size = str->Utf8Length(); |
|||
break; |
|||
|
|||
case UCS2: |
|||
data_size = str->Length() * sizeof(uint16_t); |
|||
break; |
|||
|
|||
case BASE64: { |
|||
String::AsciiValue value(str); |
|||
data_size = base64_decoded_size(*value, value.length()); |
|||
break; |
|||
} |
|||
|
|||
case HEX: |
|||
data_size = str->Length() / 2; |
|||
break; |
|||
|
|||
default: |
|||
assert(0 && "unknown encoding"); |
|||
break; |
|||
} |
|||
|
|||
return data_size; |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
static bool contains_non_ascii_slow(const char* buf, size_t len) { |
|||
for (size_t i = 0; i < len; ++i) { |
|||
if (buf[i] & 0x80) return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
|
|||
static bool contains_non_ascii(const char* src, size_t len) { |
|||
if (len < 16) { |
|||
return contains_non_ascii_slow(src, len); |
|||
} |
|||
|
|||
const unsigned bytes_per_word = sizeof(void*); |
|||
const unsigned align_mask = bytes_per_word - 1; |
|||
const unsigned unaligned = reinterpret_cast<uintptr_t>(src) & align_mask; |
|||
|
|||
if (unaligned > 0) { |
|||
const unsigned n = bytes_per_word - unaligned; |
|||
if (contains_non_ascii_slow(src, n)) return true; |
|||
src += n; |
|||
len -= n; |
|||
} |
|||
|
|||
|
|||
#if BITS_PER_LONG == 64 |
|||
const uintptr_t mask = 0x8080808080808080ll; |
|||
#else |
|||
const uintptr_t mask = 0x80808080l; |
|||
#endif |
|||
|
|||
const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src); |
|||
|
|||
for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) { |
|||
if (srcw[i] & mask) return true; |
|||
} |
|||
|
|||
const unsigned remainder = len & align_mask; |
|||
if (remainder > 0) { |
|||
const size_t offset = len - remainder; |
|||
if (contains_non_ascii_slow(src + offset, remainder)) return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
|
|||
static void force_ascii_slow(const char* src, char* dst, size_t len) { |
|||
for (size_t i = 0; i < len; ++i) { |
|||
dst[i] = src[i] & 0x7f; |
|||
} |
|||
} |
|||
|
|||
|
|||
static void force_ascii(const char* src, char* dst, size_t len) { |
|||
if (len < 16) { |
|||
force_ascii_slow(src, dst, len); |
|||
return; |
|||
} |
|||
|
|||
const unsigned bytes_per_word = sizeof(void*); |
|||
const unsigned align_mask = bytes_per_word - 1; |
|||
const unsigned src_unalign = reinterpret_cast<uintptr_t>(src) & align_mask; |
|||
const unsigned dst_unalign = reinterpret_cast<uintptr_t>(dst) & align_mask; |
|||
|
|||
if (src_unalign > 0) { |
|||
if (src_unalign == dst_unalign) { |
|||
const unsigned unalign = bytes_per_word - src_unalign; |
|||
force_ascii_slow(src, dst, unalign); |
|||
src += unalign; |
|||
dst += unalign; |
|||
len -= src_unalign; |
|||
} else { |
|||
force_ascii_slow(src, dst, len); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
#if BITS_PER_LONG == 64 |
|||
const uintptr_t mask = ~0x8080808080808080ll; |
|||
#else |
|||
const uintptr_t mask = ~0x80808080l; |
|||
#endif |
|||
|
|||
const uintptr_t* srcw = reinterpret_cast<const uintptr_t*>(src); |
|||
uintptr_t* dstw = reinterpret_cast<uintptr_t*>(dst); |
|||
|
|||
for (size_t i = 0, n = len / bytes_per_word; i < n; ++i) { |
|||
dstw[i] = srcw[i] & mask; |
|||
} |
|||
|
|||
const unsigned remainder = len & align_mask; |
|||
if (remainder > 0) { |
|||
const size_t offset = len - remainder; |
|||
force_ascii_slow(src + offset, dst + offset, remainder); |
|||
} |
|||
} |
|||
|
|||
|
|||
static size_t base64_encode(const char* src, |
|||
size_t slen, |
|||
char* dst, |
|||
size_t dlen) { |
|||
// We know how much we'll write, just make sure that there's space.
|
|||
assert(dlen >= base64_encoded_size(slen) && |
|||
"not enough space provided for base64 encode"); |
|||
|
|||
dlen = base64_encoded_size(slen); |
|||
|
|||
unsigned a; |
|||
unsigned b; |
|||
unsigned c; |
|||
unsigned i; |
|||
unsigned k; |
|||
unsigned n; |
|||
|
|||
static const char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|||
"abcdefghijklmnopqrstuvwxyz" |
|||
"0123456789+/"; |
|||
|
|||
i = 0; |
|||
k = 0; |
|||
n = slen / 3 * 3; |
|||
|
|||
while (i < n) { |
|||
a = src[i + 0] & 0xff; |
|||
b = src[i + 1] & 0xff; |
|||
c = src[i + 2] & 0xff; |
|||
|
|||
dst[k + 0] = table[a >> 2]; |
|||
dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; |
|||
dst[k + 2] = table[((b & 0x0f) << 2) | (c >> 6)]; |
|||
dst[k + 3] = table[c & 0x3f]; |
|||
|
|||
i += 3; |
|||
k += 4; |
|||
} |
|||
|
|||
if (n != slen) { |
|||
switch (slen - n) { |
|||
case 1: |
|||
a = src[i + 0] & 0xff; |
|||
dst[k + 0] = table[a >> 2]; |
|||
dst[k + 1] = table[(a & 3) << 4]; |
|||
dst[k + 2] = '='; |
|||
dst[k + 3] = '='; |
|||
break; |
|||
|
|||
case 2: |
|||
a = src[i + 0] & 0xff; |
|||
b = src[i + 1] & 0xff; |
|||
dst[k + 0] = table[a >> 2]; |
|||
dst[k + 1] = table[((a & 3) << 4) | (b >> 4)]; |
|||
dst[k + 2] = table[(b & 0x0f) << 2]; |
|||
dst[k + 3] = '='; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return dlen; |
|||
} |
|||
|
|||
|
|||
static size_t hex_encode(const char* src, size_t slen, char* dst, size_t dlen) { |
|||
// We know how much we'll write, just make sure that there's space.
|
|||
assert(dlen >= slen * 2 && |
|||
"not enough space provided for hex encode"); |
|||
|
|||
dlen = slen * 2; |
|||
for (uint32_t i = 0, k = 0; k < dlen; i += 1, k += 2) { |
|||
static const char hex[] = "0123456789abcdef"; |
|||
uint8_t val = static_cast<uint8_t>(src[i]); |
|||
dst[k + 0] = hex[val >> 4]; |
|||
dst[k + 1] = hex[val & 15]; |
|||
} |
|||
|
|||
return dlen; |
|||
} |
|||
|
|||
|
|||
|
|||
Local<Value> StringBytes::Encode(const char* buf, |
|||
size_t buflen, |
|||
enum encoding encoding) { |
|||
HandleScope scope; |
|||
|
|||
assert(buflen <= Buffer::kMaxLength); |
|||
if (!buflen && encoding != BUFFER) |
|||
return scope.Close(String::Empty()); |
|||
|
|||
Local<String> val; |
|||
switch (encoding) { |
|||
case BUFFER: |
|||
return scope.Close( |
|||
Buffer::New(static_cast<const char*>(buf), buflen)->handle_); |
|||
|
|||
case ASCII: |
|||
if (contains_non_ascii(buf, buflen)) { |
|||
char* out = new char[buflen]; |
|||
force_ascii(buf, out, buflen); |
|||
val = String::New(out, buflen); |
|||
delete[] out; |
|||
} else { |
|||
val = String::New(buf, buflen); |
|||
} |
|||
break; |
|||
|
|||
case UTF8: |
|||
val = String::New(buf, buflen); |
|||
break; |
|||
|
|||
case BINARY: { |
|||
// TODO(isaacs) use ExternalTwoByteString?
|
|||
const unsigned char *cbuf = reinterpret_cast<const unsigned char*>(buf); |
|||
uint16_t * twobytebuf = new uint16_t[buflen]; |
|||
for (size_t i = 0; i < buflen; i++) { |
|||
// XXX is the following line platform independent?
|
|||
twobytebuf[i] = cbuf[i]; |
|||
} |
|||
val = String::New(twobytebuf, buflen); |
|||
delete[] twobytebuf; |
|||
break; |
|||
} |
|||
|
|||
case BASE64: { |
|||
size_t dlen = base64_encoded_size(buflen); |
|||
char* dst = new char[dlen]; |
|||
|
|||
size_t written = base64_encode(buf, buflen, dst, dlen); |
|||
assert(written == dlen); |
|||
|
|||
val = String::New(dst, dlen); |
|||
delete[] dst; |
|||
break; |
|||
} |
|||
|
|||
case UCS2: { |
|||
const uint16_t* data = reinterpret_cast<const uint16_t*>(buf); |
|||
val = String::New(data, buflen / 2); |
|||
break; |
|||
} |
|||
|
|||
case HEX: { |
|||
size_t dlen = buflen * 2; |
|||
char* dst = new char[dlen]; |
|||
size_t written = hex_encode(buf, buflen, dst, dlen); |
|||
assert(written == dlen); |
|||
|
|||
val = String::New(dst, dlen); |
|||
delete[] dst; |
|||
break; |
|||
} |
|||
|
|||
default: |
|||
assert(0 && "unknown encoding"); |
|||
break; |
|||
} |
|||
|
|||
return scope.Close(val); |
|||
} |
|||
|
|||
} // namespace node
|
@ -0,0 +1,65 @@ |
|||
// 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.
|
|||
|
|||
#ifndef SRC_STRING_BYTES_H_ |
|||
#define SRC_STRING_BYTES_H_ |
|||
|
|||
// Decodes a v8::Handle<v8::String> or Buffer to a raw char*
|
|||
|
|||
#include "v8.h" |
|||
#include "node.h" |
|||
|
|||
namespace node { |
|||
|
|||
using v8::Handle; |
|||
using v8::Local; |
|||
using v8::String; |
|||
using v8::Value; |
|||
|
|||
class StringBytes { |
|||
public: |
|||
// Fast, but can be 2 bytes oversized for Base64, and
|
|||
// as much as triple UTF-8 strings <= 65536 chars in length
|
|||
static size_t StorageSize(Handle<Value> val, enum encoding enc); |
|||
|
|||
// Precise byte count, but slightly slower for Base64 and
|
|||
// very much slower for UTF-8
|
|||
static size_t Size(Handle<Value> val, enum encoding enc); |
|||
|
|||
// Write the bytes from the string or buffer into the char*
|
|||
// returns the number of bytes written, which will always be
|
|||
// <= buflen. Use StorageSize/Size first to know how much
|
|||
// memory to allocate.
|
|||
static size_t Write(char* buf, |
|||
size_t buflen, |
|||
Handle<Value> val, |
|||
enum encoding enc, |
|||
int* chars_written = NULL); |
|||
|
|||
// Take the bytes in the src, and turn it into a Buffer or String.
|
|||
static Local<Value> Encode(const char* buf, |
|||
size_t buflen, |
|||
enum encoding encoding); |
|||
}; |
|||
|
|||
} // namespace node
|
|||
|
|||
#endif // SRC_STRING_BYTES_H_
|
Loading…
Reference in new issue