initial commit <3
This commit is contained in:
5
Makefile
Normal file
5
Makefile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
all: main.c strtoui.h
|
||||||
|
gcc -Wall main.c strtoui.h -o chars -lsodium
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm chars main.o
|
||||||
44
main.c
Normal file
44
main.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "stdio.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "limits.h"
|
||||||
|
#include "sodium.h"
|
||||||
|
#include "strtoui.h"
|
||||||
|
|
||||||
|
int map(int i, int i_min, int i_max, int o_min, int o_max) {
|
||||||
|
double r = (double)(o_max - o_min) / (double)(i_max - i_min);
|
||||||
|
int j = (double) (i - i_min) * r + o_min;
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if(argc == 1) {
|
||||||
|
puts("Usage: chars [length]");
|
||||||
|
puts(" Max length is 1024");
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int len;
|
||||||
|
enum strtoui_error error = strtoui(&len, argv[1], 10);
|
||||||
|
if(error != STRTOUI_SUCCESS){
|
||||||
|
fputs("Bad input\n", stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}else if (len > 1024) {
|
||||||
|
fputs("The length is to big\n", stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char lower_range = 0x20;
|
||||||
|
char upper_range= 0x7e;
|
||||||
|
|
||||||
|
char data[len];
|
||||||
|
randombytes_buf(data, sizeof len);
|
||||||
|
|
||||||
|
for(int i = 0; i < len; i++) {
|
||||||
|
int random = randombytes_uniform(upper_range);
|
||||||
|
data[i] = map(random, 0, upper_range, lower_range, upper_range);
|
||||||
|
printf("%c", data[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
31
strtoui.h
Normal file
31
strtoui.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef STRTOUI_H
|
||||||
|
#define STRTOUI_H
|
||||||
|
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "ctype.h"
|
||||||
|
#include "limits.h"
|
||||||
|
#include "errno.h"
|
||||||
|
|
||||||
|
enum strtoui_error{
|
||||||
|
STRTOUI_SUCCESS,
|
||||||
|
STRTOUI_OVERFLOW,
|
||||||
|
STRTOUI_UNDERFLOW,
|
||||||
|
STRTOUI_INCONVERTIBLE
|
||||||
|
};
|
||||||
|
|
||||||
|
enum strtoui_error strtoui(unsigned int *out, char *s, int base) {
|
||||||
|
char *end;
|
||||||
|
if(s[0] == '\0' || isspace(s[0]))
|
||||||
|
return STRTOUI_INCONVERTIBLE;
|
||||||
|
long l = strtol(s, &end, base);
|
||||||
|
if(l > INT_MAX || (errno == ERANGE && l == LONG_MAX))
|
||||||
|
return STRTOUI_OVERFLOW;
|
||||||
|
if(l < 0|| (errno == ERANGE && l == LONG_MIN))
|
||||||
|
return STRTOUI_UNDERFLOW;
|
||||||
|
if(*end != '\0')
|
||||||
|
return STRTOUI_INCONVERTIBLE;
|
||||||
|
*out = l;
|
||||||
|
return STRTOUI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user