Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Allow window size and polynomial to be configured from javascript #24

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
var fs = require('fs')
var crypto = require('crypto')
var args = require('minimist')(process.argv.slice(2))
var args = require('minimist')(process.argv.slice(2), {string: "polynomial"})
var rabin = require('./')(args)
var offset = 0
var rs = fs.createReadStream(args._[0])
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ function Rabin (opts) {
var avgBits = +opts.bits || 12
var min = +opts.min || 8 * 1024
var max = +opts.max || 32 * 1024
var w = +opts.window || 64
var polynomial = opts.polynomial || "0x3DA3358B4DC173"
this.rabin = rabin.rabin()
this.rabin.configure(avgBits, min, max)
this.rabin.configure(avgBits, min, max, w, polynomial)
this.nextCb = null
this.buffers = new BufferList()
this.pending = []
Expand Down
6 changes: 3 additions & 3 deletions src/rabin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void calc_tables(struct rabin_t *h) {
uint64_t hash = 0;

hash = append_byte(hash, (uint8_t)b, h->polynomial);
for (int i = 0; i < WINSIZE-1; i++) {
for (int i = 0; i < h->winsize-1; i++) {
hash = append_byte(hash, 0, h->polynomial);
}
out_table[b] = hash;
Expand Down Expand Up @@ -86,12 +86,12 @@ void rabin_slide(struct rabin_t *h, uint8_t b) {
uint8_t out = h->window[h->wpos];
h->window[h->wpos] = b;
h->digest = (h->digest ^ out_table[out]);
h->wpos = (h->wpos +1 ) % WINSIZE;
h->wpos = (h->wpos +1 ) % h->winsize;
rabin_append(h, b);
}

void rabin_reset(struct rabin_t *h) {
for (int i = 0; i < WINSIZE; i++)
for (int i = 0; i < h->winsize; i++)
h->window[i] = 0;
h->digest = 0;
h->wpos = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/rabin.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define _RABIN_H

#include <stdint.h>
#include <vector>

#define WINSIZE 64

struct rabin_t {
uint8_t window[WINSIZE];
std::vector<uint8_t> window;
uint64_t wpos;
uint64_t count;
uint64_t pos;
Expand All @@ -22,6 +23,7 @@ struct rabin_t {
uint64_t minsize;
uint64_t maxsize;
uint64_t mask;
int winsize;
};

struct rabin_t *rabin_init(struct rabin_t *h);
Expand Down
24 changes: 22 additions & 2 deletions src/rabin_wrap.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdlib.h>
#include <sstream>
#include <node.h>
#include <v8.h>
#include "rabin_wrap.h"

static Nan::Persistent<FunctionTemplate> rabin_constructor;
Expand Down Expand Up @@ -46,17 +49,34 @@ NAN_METHOD(RabinWrap::Configure) {
if (!info[0]->IsNumber()) return Nan::ThrowError("first arg must be a number");
if (!info[1]->IsNumber()) return Nan::ThrowError("second arg must be a number");
if (!info[2]->IsNumber()) return Nan::ThrowError("third arg must be a number");
if (!info[3]->IsNumber()) return Nan::ThrowError("fourth arg must be a number");
if (!info[4]->IsString()) return Nan::ThrowError("fifth arg must be a string");

struct rabin_t *handle = &(self->handle);

handle->average_bits = info[0]->Uint32Value();
handle->minsize = info[1]->Uint32Value();
handle->maxsize = info[2]->Uint32Value();
handle->winsize = info[3]->Uint32Value();
handle->window.reserve(info[3]->Uint32Value());

// Open a pull request if you need these to be configurable
handle->mask = ((1<<handle->average_bits)-1);
handle->polynomial = 0x3DA3358B4DC173LL;
handle->polynomial_degree = 53;

// Convert string representation of polynomial to 64bit integer
String::Utf8Value utf8(info[4]->ToString());
std::stringstream ss;
char* ps = *utf8;
if (utf8.length() > 2 && ps[0] == '0' && ps[1] == 'x') {
ss << std::hex << (ps + 2);
} else {
ss << ps;
}
ss >> handle->polynomial;

uint64_t index = handle->polynomial;
handle->polynomial_degree = 0;
while (index >>= 1) ++handle->polynomial_degree;
handle->polynomial_shift = (handle->polynomial_degree-8);

rabin_init(handle);
Expand Down