Permalink
Browse files

fix length calculation error when Buffer as first argument

  • Loading branch information...
1 parent e782a30 commit 0abc42abebc5decaa000cdb1c6a9a192ca3ff15b @yyfrankyy yyfrankyy committed Nov 8, 2012
Showing with 18 additions and 1 deletion.
  1. +8 −0 binding.cc
  2. +1 −1 package.json
  3. +9 −0 test.js
View
8 binding.cc
@@ -21,6 +21,7 @@
#include <v8.h>
#include <string.h>
#include <sstream>
+#include <node_buffer.h>
#include "city.h"
#ifdef __SSE4_2__
#include "citycrc.h"
@@ -30,6 +31,7 @@
#define MAX_128_HASH_LEN MAX_64_HASH_LEN*2
using namespace v8;
+using node::Buffer;
inline uint32 Uint64High32(const uint64 &x) {
return (uint32)((x >> 32) & 0xFFFFFFFF);
@@ -235,6 +237,12 @@ node_CityHash64(const Arguments& args) {
const char* str = *data;
size_t len = data.length();
+ if (Buffer::HasInstance(args[0])) {
+ Local<Object> obj = args[0]->ToObject();
+ str = Buffer::Data(obj);
+ len = Buffer::Length(obj);
+ }
+
uint64 hash;
if(args_len == 1) {
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "cityhash",
- "version": "0.0.2",
+ "version": "0.0.3",
"main": "./build/Release/node-cityhash",
"description": "NodeJS binding for Google CityHash.",
"dependencies": [ ],
View
9 test.js
@@ -88,6 +88,15 @@ assertEqual({
value: '15738392108067291633'
}, cityhash.hash64('Hello', 87392039), 'Hash64 for "hello" with seed 87392039');
+var b = new Buffer(4)
+b.writeUInt32LE(7771789, 0)
+assertEqual({
+ low: 3883967176,
+ high: 3780832496,
+ uint64: true,
+ value:'16238551925858017992'
+ }, cityhash.hash64(b, "2310915232984335893"), 'Hash64 for Buffer');
+
assertEqual({
low: 2569634289,
high: 3664379964,

0 comments on commit 0abc42a

Please sign in to comment.