Removed previous stuff

This commit is contained in:
John Newby 2019-08-06 15:55:39 +02:00
parent 018c4c9325
commit 6229badcae
5 changed files with 0 additions and 234 deletions

View File

@ -1,26 +0,0 @@
DEBUG ?= 0
ifeq ($(DEBUG), 1)
CFLAGS =-DDEBUG=1
else
CFLAGS=-DNDEBUG -Ofast
endif
CC = gcc
LIBS = -llibecrecover.so
LDPATH = -Ltarget/debug
INCLUDEPATH = -Iinclude
%.o: %.c $(DEPS)
$(CC) $(CFLAGS) -c -o $(INCLUDEPATH) $@ $<
all: test erl_ecrecover
test: src/test.c src/base64.c target/release/libecrecover.so
$(CC) -o $@ $^ $(INCLUDEPATH) $(CFLAGS) $(LDPATH)
./test
erl_ecrecover: src/erl_ecrecover.c src/base64.c src/erl_comm.c target/debug/libecrecover.so
$(CC) -o$ $@ $^ $(INCLUDEPATH) $(CFLAGS) $(LDPATH)
clean:
rm -f erl_ecrecover test

View File

@ -1,77 +0,0 @@
#include "base64.h"
#include <stdlib.h>
char *bin2hex(unsigned char *p, int len)
{
char *hex = malloc(((2*len) + 1));
char *r = hex;
while(len && p)
{
(*r) = ((*p) & 0xF0) >> 4;
(*r) = ((*r) <= 9 ? '0' + (*r) : 'a' - 10 + (*r));
r++;
(*r) = ((*p) & 0x0F);
(*r) = ((*r) <= 9 ? '0' + (*r) : 'a' - 10 + (*r));
r++;
p++;
len--;
}
*r = '\0';
return hex;
}
unsigned char *hex2bin(const char *str)
{
int len, h;
unsigned char *result, *err, *p, c;
err = malloc(1);
*err = 0;
if (!str)
return err;
if (!*str)
return err;
len = 0;
p = (unsigned char*) str;
while (*p++)
len++;
result = malloc((len/2)+1);
h = !(len%2) * 4;
p = result;
*p = 0;
c = *str;
while(c)
{
if(('0' <= c) && (c <= '9'))
*p += (c - '0') << h;
else if(('A' <= c) && (c <= 'F'))
*p += (c - 'A' + 10) << h;
else if(('a' <= c) && (c <= 'f'))
*p += (c - 'a' + 10) << h;
else
return err;
str++;
c = *str;
if (h)
h = 0;
else
{
h = 4;
p++;
*p = 0;
}
}
return result;
}

View File

@ -1,63 +0,0 @@
// -*- C -*-
#include "erl_comm.h"
#include <unistd.h>
int read_cmd(byte *buf)
{
int len;
if (read_exact(buf, 2) != 2)
return(-1);
len = (buf[0] << 8) | buf[1];
DEBUG_PRINTF("Len is %d\n", len);
if(len > ECRECOVER_BUFFER_MAX - 1) {
DEBUG_PRINTF("Avoiding overflow\n");
return 0; // let's not overflow.
}
int read = read_exact(buf, len);
DEBUG_PRINTF("read_cmd returning %d\n", read);
return read;
}
int write_cmd(byte *buf, int len)
{
byte li;
li = (len >> 8) & 0xff;
write_exact(&li, 1);
li = len & 0xff;
write_exact(&li, 1);
return write_exact(buf, len);
}
int read_exact(byte *buf, int len)
{
int i, got=0;
DEBUG_PRINTF("Read exact\n");
do {
if ((i = read(0, buf+got, len-got)) <= 0) {
DEBUG_PRINTF("got=%d len=%d i=%d\n", got, len, i);
return(i);
}
got += i;
} while (got<len);
DEBUG_PRINTF("Read %d returning %d\n", got, len);
return(len);
}
int write_exact(byte *buf, int len)
{
int i, wrote = 0;
do {
if ((i = write(1, buf+wrote, len-wrote)) <= 0)
return (i);
wrote += i;
} while (wrote<len);
return (len);
}

View File

@ -1,45 +0,0 @@
// -*- C -*-
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "ecrecover.h"
#include "erl_comm.h"
#include "base64.h"
#define OUTPUT_LENGTH 32
int main(int arc, char **argv)
{
int fn, arg, len;
byte buf[ECRECOVER_BUFFER_MAX];
while ((len = read_cmd(buf)) > 0) {
DEBUG_PRINTF("len %d\n", len);
fn = buf[0];
DEBUG_PRINTF("Function %d len %d\n", fn, len);
buf[len] = '\0';
if (fn == 1) {
int result;
byte *input = hex2bin(buf+1);
byte *output = malloc(ECRECOVER_BUFFER_MAX);
result = ecrecover(input, output);
free(input);
DEBUG_PRINTF("Return value is %d", result);
if(result) {
byte *hex_result = bin2hex(output, OUTPUT_LENGTH);
strcpy(buf, hex_result);
free(hex_result);
DEBUG_PRINTF("result is %s len %ld\n", buf, strlen(buf));
} else {
strcpy("failed", buf);
}
free(output);
} else {
strcpy("function not found", buf);
}
DEBUG_PRINTF("Returning %s\n", buf);
write_cmd(buf, strlen(buf));
}
}

View File

@ -1,23 +0,0 @@
// -*-C-*-
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "../include/ecrecover.h"
char *bin2hex(unsigned char*, int);
unsigned char *hex2bin(const char*);
#define OUTPUT_LENGTH 32
int main(int arc, char **argv)
{
// copied from Ethereum tests
unsigned char *input = hex2bin("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03");
unsigned char *expected = "000000000000000000000000c08b5542d177ac6686946920409741463a15dddb";
unsigned char *output = malloc(OUTPUT_LENGTH * sizeof(unsigned char));
ecrecover(input, output);
assert(strcmp(bin2hex(output, OUTPUT_LENGTH), expected) == 0);
printf("Test passed\n");
}