Browse files

add async callback to md5_file function

  • Loading branch information...
1 parent b64d1a7 commit be070af32efe92f12612750cc55831b7320f38d2 @brainfucker committed Jan 20, 2010
Showing with 52 additions and 7 deletions.
  1. +1 −1 .lock-wscript
  2. +2 −2 README.md
  3. BIN build/.wafpickle-7
  4. +1 −1 build/config.log
  5. BIN build/default/hashlib.node
  6. BIN build/default/hashlib_2.o
  7. +23 −2 hashlib.cc
  8. +3 −1 makefile
  9. +5 −0 md5.js
  10. +2 −0 test.file
  11. +15 −0 test.js
View
2 .lock-wscript
@@ -2,7 +2,7 @@ argv = ['/usr/local/bin/node-waf', 'configure', 'build']
blddir = '/home/brainfucker/Projects/hashlib/build'
commands = {'dist': 0, 'configure': True, 'distcheck': 0, 'install': 0, 'build': True, 'clean': 0, 'distclean': 0, 'check': 0, 'uninstall': 0}
cwd = '/home/brainfucker/Projects/hashlib'
-environ = {'GDM_KEYBOARD_LAYOUT': 'us', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'LOGNAME': 'brainfucker', 'USER': 'brainfucker', 'PATH': '/var/lib/gems/1.8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/brainfucker/Dropbox/Bin', 'HOME': '/home/brainfucker', 'DISPLAY': ':0.0', 'SSH_AGENT_PID': '1998', 'LANG': 'en_US.UTF-8', 'SPEECHD_PORT': '7560', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'XDG_SESSION_COOKIE': '8143f1ef8b2173edae5c4ea34aed6323-1263635224.732645-323512337', 'SESSION_MANAGER': 'local/brainfucker-laptop:@/tmp/.ICE-unix/1576,unix/brainfucker-laptop:/tmp/.ICE-unix/1576', 'SHLVL': '1', '_': '/usr/local/bin/node-waf', 'DESKTOP_AUTOSTART_ID': '10ab6ac4f4a0b815a9126363523039719900000015760000', 'USERNAME': 'brainfucker', 'SSH_AUTH_SOCK': '/tmp/keyring-XIzwzg/socket.ssh', 'GNOME_KEYRING_SOCKET': '/tmp/keyring-XIzwzg/socket', 'GDMSESSION': 'gnome', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-j1hwFNQETS,guid=08713bc948d5417b745764a04b518b1a', 'ORBIT_SOCKETDIR': '/tmp/orbit-brainfucker', 'XAUTHORITY': '/var/run/gdm/auth-for-brainfucker-woi8gU/database', 'DESKTOP_SESSION': 'gnome', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'GTK_MODULES': 'canberra-gtk-module', 'OLDPWD': '/home/brainfucker/Projects', 'GDM_LANG': 'en_US.UTF-8', 'HISTCONTROL': 'ignoreboth', 'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share/:/usr/share/', 'PWD': '/home/brainfucker/Projects/hashlib', 'GTK_RC_FILES': '/etc/gtk/gtkrc:/home/brainfucker/.gtkrc-1.2-gnome2', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:hl=44;37:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'}
+environ = {'GDM_KEYBOARD_LAYOUT': 'us', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'LOGNAME': 'brainfucker', 'USER': 'brainfucker', 'HOME': '/home/brainfucker', 'PATH': '/var/lib/gems/1.8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/brainfucker/Dropbox/Bin', 'DISPLAY': ':0.0', 'MAKEFLAGS': '', 'SSH_AGENT_PID': '2229', 'LANG': 'en_US.UTF-8', 'SPEECHD_PORT': '7560', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'XAUTHORITY': '/var/run/gdm/auth-for-brainfucker-zWjB0A/database', 'SESSION_MANAGER': 'local/brainfucker-laptop:@/tmp/.ICE-unix/1925,unix/brainfucker-laptop:/tmp/.ICE-unix/1925', 'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share/:/usr/share/', '_': '/usr/bin/make', 'DESKTOP_AUTOSTART_ID': '109b9a019b6a9c4443126392782668975300000019250000', 'MFLAGS': '', 'USERNAME': 'brainfucker', 'MAKELEVEL': '1', 'SSH_AUTH_SOCK': '/tmp/keyring-wLN2Sw/socket.ssh', 'GNOME_KEYRING_SOCKET': '/tmp/keyring-wLN2Sw/socket', 'GDMSESSION': 'gnome', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-cXAVxLuiPR,guid=f3f6b8a7e87ae6d25b4575d14b560210', 'ORBIT_SOCKETDIR': '/tmp/orbit-brainfucker', 'XDG_SESSION_COOKIE': '8143f1ef8b2173edae5c4ea34aed6323-1263927823.695691-168079962', 'DESKTOP_SESSION': 'gnome', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'GTK_MODULES': 'canberra-gtk-module', 'OLDPWD': '/home/brainfucker/Projects/brainfucker-hashlib-b64d1a7', 'GDM_LANG': 'en_US.UTF-8', 'HISTCONTROL': 'ignoreboth', 'SHLVL': '1', 'PWD': '/home/brainfucker/Projects/hashlib', 'GTK_RC_FILES': '/etc/gtk/gtkrc:/home/brainfucker/.gtkrc-1.2-gnome2', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:hl=44;37:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'}
files = []
hash = 0
options = {'compile_targets': None, 'force': False, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'destdir': '', 'keep': False, 'zones': '', 'blddir': '', 'prefix': '/usr/local/', 'jobs': 2, 'srcdir': '', 'check_cxx_compiler': 'g++ icpc sunc++', 'check_c_compiler': 'gcc icc suncc'}
View
4 README.md
@@ -1,7 +1,7 @@
# Hashlib
*Is a FAST nodejs(http://github.com/ry/node/) library for making hashes written in C/C++*
-*Supports: md4, md5, md6, sha, sha1, sha256, sha512*
+*Supports: md4, md5, md6, sha, sha1, sha256, sha512*, also can make md5 hash of files
*Thangs to Vanilla Hsu for speed improvements and md4, sha support*
@@ -32,7 +32,7 @@ You should use `var hashlib = require("hashlib");` (from any path)
sha256(str); // Returns sha256 hash from sting
sha512(str); // Returns sha512 hash from sting
md6(str[, length]); // Returns md6 hash from sting, second parametr is optional
- md5_file(path); // Returns md5 hash from file (callbacks will be soon)
+ md5_file(path[, callback]); // Returns md5 hash from file callback is optional and works async
## Usage:
var hashlib = require('hashlib');
View
BIN build/.wafpickle-7
Binary file not shown.
View
2 build/config.log
@@ -1,4 +1,4 @@
-# project configured on Fri Jan 15 16:58:50 2010 by
+# project configured on Wed Jan 20 20:15:34 2010 by
# waf 1.5.10 (abi 7, python 20604f0 on linux2)
# using /usr/local/bin/node-waf configure build
#
View
BIN build/default/hashlib.node
Binary file not shown.
View
BIN build/default/hashlib_2.o
Binary file not shown.
View
25 hashlib.cc
@@ -1,4 +1,5 @@
#include <v8.h>
+#include <ev.h>
extern "C" {
#include "sha.h"
@@ -195,15 +196,35 @@ Handle<Value> get_md5_file(char * path)
return String::New((char*)hexdigest,32);
}
+void md5_file_callback(int revents, void *args) {
+ Persistent<Object> *data =
+ reinterpret_cast<Persistent<Object>*>(args);
+
+ String::Utf8Value path((*data)->Get(String::New("path")));
+ v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast((*data)->Get(String::New("callback")));
+ Handle<Object> recv = Handle<Object>::Cast((*data)->Get(String::New("recv")));
+ v8::Handle<v8::Value> outArgs[] = {get_md5_file(*path)};
+ callback->Call(recv, 1, outArgs);
+ data->Dispose();
+}
+
Handle<Value>
md5_file(const Arguments& args)
{
HandleScope scope;
String::Utf8Value path(args[0]->ToString());
if (args[1]->IsFunction()) {
- printf("callback's for md5_file not implemented yet");
- return get_md5_file(*path);
+ v8::Local<v8::Object> arguments = v8::Object::New();
+ arguments->Set(String::New("path"),args[0]->ToString());
+ arguments->Set(String::New("callback"),args[1]);
+ arguments->Set(String::New("recv"),args.This());
+ Persistent<Object> *data = new Persistent<Object>();
+ *data = Persistent<Object>::New(arguments);
+
+ ev_once(0, EV_TIMEOUT, 0, md5_file_callback, (void*)data);
+
+ return v8::Boolean::New(true);
} else {
return get_md5_file(*path);
}
View
4 makefile
@@ -1,5 +1,7 @@
all:
node-waf configure build
- @echo "--------\nMODULE FILE SHOULD BE IN ./build/default/\nUSE 'sudo make install' to install it"
+ @echo "--------\nMODULE FILE SHOULD BE IN ./build/default/\nUSE 'sudo make install' to install it\nAlso you can use 'make tests' to run tests"
+tests:
+ node ./test.js
install:
cp ./build/default/hashlib.node /usr/local/lib/node/libraries/hashlib.node
View
5 md5.js
@@ -0,0 +1,5 @@
+var hashlib = require("./build/default/hashlib");
+var sys = require("sys");
+var md5 = require("./test/md5");
+var c=new hashlib.hashlib();
+sys.puts(c.md5('test'));
View
2 test.file
@@ -0,0 +1,2 @@
+Don't cahnge this file,
+it needs for tests.
View
15 test.js
@@ -2,6 +2,8 @@ var hashlib = require("./build/default/hashlib");
var sys = require("sys");
var md5 = require("./test/md5");
+process.chdir(require("path").dirname(process.ARGV[1]));
+
if (hashlib.md5('test')=='098f6bcd4621d373cade4e832627b4f6')
sys.puts('test 1 PASSED');
else
@@ -34,6 +36,17 @@ if (hashlib.sha('test')=='f8d3b312442a67706057aeb45b983221afb4f035')
sys.puts('test 8 PASSED');
else
sys.puts('test 8 FAILS');
+if (hashlib.md5_file('./test.file')=='bc8aeda5b02f054117bd9979908787dc')
+ sys.puts('test 9 PASSED');
+else
+ sys.puts('test 9 FAILS');
+hashlib.md5_file('./test.file',function(value) {
+ if (value=='bc8aeda5b02f054117bd9979908787dc')
+ sys.puts('test 10 PASSED');
+ else
+ sys.puts('test 10 FAILS');
+
+// End of tests
// C++ md5
var m1=new Date().getTime();
@@ -109,3 +122,5 @@ for(i=0;i<100000;i++) {
var m2=new Date().getTime();
var c=m2-m1;
sys.puts('C++ sha512 result is: '+(c));
+
+});

0 comments on commit be070af

Please sign in to comment.