Now it can generate chars until UINT_MAX
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
chars
|
||||||
2
Makefile
2
Makefile
@@ -2,4 +2,4 @@ all: main.c strtoui.h
|
|||||||
gcc -Wall main.c strtoui.h -o chars -lsodium
|
gcc -Wall main.c strtoui.h -o chars -lsodium
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm chars main.o
|
rm chars
|
||||||
|
|||||||
36
main.c
36
main.c
@@ -4,6 +4,9 @@
|
|||||||
#include "sodium.h"
|
#include "sodium.h"
|
||||||
#include "strtoui.h"
|
#include "strtoui.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps a number that is between i_min and i_max to the range o_min and o_max
|
||||||
|
*/
|
||||||
int map(int i, int i_min, int i_max, int o_min, int o_max) {
|
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);
|
double r = (double)(o_max - o_min) / (double)(i_max - i_min);
|
||||||
int j = (double) (i - i_min) * r + o_min;
|
int j = (double) (i - i_min) * r + o_min;
|
||||||
@@ -11,32 +14,35 @@ int map(int i, int i_min, int i_max, int o_min, int o_max) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
//if doesnt have any argument just show a usage info
|
||||||
if(argc == 1) {
|
if(argc == 1) {
|
||||||
puts("Usage: chars [length]");
|
puts("Usage:\tchars [length]");
|
||||||
puts(" Max length is 1024");
|
puts("\t0 > lenght > UINT_MAX");
|
||||||
|
puts("Description:");
|
||||||
|
puts("\t Generates a random string with the specified length");
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//read the length argument and parse it to a an unsigned int
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
enum strtoui_error error = strtoui(&len, argv[1], 10);
|
enum strtoui_error error = strtoui(&len, argv[1], 10);
|
||||||
if(error != STRTOUI_SUCCESS){
|
if(error == STRTOUI_OVERFLOW){
|
||||||
fputs("Bad input\n", stderr);
|
fputs("The input is too big\n", stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}else if (len > 1024) {
|
}else if(error == STRTOUI_UNDERFLOW) {
|
||||||
fputs("The length is to big\n", stderr);
|
fputs("The input must be bigger than 0\n", stderr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}else if(error == STRTOUI_INCONVERTIBLE) {
|
||||||
|
fputs("The input isn't valid\n", stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
char lower_range = 0x20;
|
//prints random numbers in the bounds specified as chars
|
||||||
char upper_range= 0x7e;
|
char lower_bound = 0x20;
|
||||||
|
char upper_bound= 0x7e;
|
||||||
char data[len];
|
|
||||||
randombytes_buf(data, sizeof len);
|
|
||||||
|
|
||||||
for(int i = 0; i < len; i++) {
|
for(int i = 0; i < len; i++) {
|
||||||
int random = randombytes_uniform(upper_range);
|
int random = randombytes_uniform(upper_bound);
|
||||||
data[i] = map(random, 0, upper_range, lower_range, upper_range);
|
printf("%c", map(random, 0, upper_bound, lower_bound, upper_bound));
|
||||||
printf("%c", data[i]);
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ enum strtoui_error strtoui(unsigned int *out, char *s, int base) {
|
|||||||
if(s[0] == '\0' || isspace(s[0]))
|
if(s[0] == '\0' || isspace(s[0]))
|
||||||
return STRTOUI_INCONVERTIBLE;
|
return STRTOUI_INCONVERTIBLE;
|
||||||
long l = strtol(s, &end, base);
|
long l = strtol(s, &end, base);
|
||||||
if(l > INT_MAX || (errno == ERANGE && l == LONG_MAX))
|
if(l > UINT_MAX || (errno == ERANGE && l == LONG_MAX))
|
||||||
return STRTOUI_OVERFLOW;
|
return STRTOUI_OVERFLOW;
|
||||||
if(l < 0|| (errno == ERANGE && l == LONG_MIN))
|
if(l < 0|| (errno == ERANGE && l == LONG_MIN))
|
||||||
return STRTOUI_UNDERFLOW;
|
return STRTOUI_UNDERFLOW;
|
||||||
|
|||||||
Reference in New Issue
Block a user