mirror of
https://github.com/sigmasternchen/libsfuid
synced 2025-03-16 00:19:07 +00:00
made decoding much faster
It's still pretty slow though. I assume because of the pow operation. I'll take a look at it later.
This commit is contained in:
parent
9822208d98
commit
30648e7b5e
1 changed files with 16 additions and 13 deletions
29
main.c
29
main.c
|
@ -9,7 +9,8 @@
|
||||||
|
|
||||||
typedef __uint128_t uint128_t;
|
typedef __uint128_t uint128_t;
|
||||||
|
|
||||||
char* charset = "a-zA-Z0-9";
|
const char* charset = "a-zA-Z0-9";
|
||||||
|
int inverseCharset[(1 << 7)] = {-1};
|
||||||
unsigned int resultLength = 10;
|
unsigned int resultLength = 10;
|
||||||
unsigned int offset = 5;
|
unsigned int offset = 5;
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
|
@ -49,19 +50,13 @@ unsigned long long convertFromString(const char* string) {
|
||||||
|
|
||||||
for (int i = 0; i < stringLength; i++) {
|
for (int i = 0; i < stringLength; i++) {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for(int j = 0; j < charsetLength; j++) {
|
int tmp = inverseCharset[string[i]];
|
||||||
if (charset[j] == string[i]) {
|
if (tmp < 0) {
|
||||||
found = true;
|
|
||||||
unsigned long long val = ((unsigned long long) pow(charsetLength, stringLength - i - 1));
|
|
||||||
//printf("position %d, digit %d, value %d\n", i, j, val);;
|
|
||||||
result += ((unsigned long long) j) * val;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
error("Parsing error.\nUnexpected character '%c'.\n", string[i]);
|
error("Parsing error.\nUnexpected character '%c'.\n", string[i]);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
unsigned long long val = ((unsigned long long) pow(charsetLength, stringLength - i - 1));
|
||||||
|
result += ((unsigned long long) tmp) * val;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -248,7 +243,7 @@ void prepareCharset() {
|
||||||
verbose("prepareing charset\n");
|
verbose("prepareing charset\n");
|
||||||
|
|
||||||
int size = strlen(charset);
|
int size = strlen(charset);
|
||||||
char* tmp;
|
const char* tmp;
|
||||||
|
|
||||||
for(int i = 0; i < ((sizeof shortcuts) / (sizeof shortcuts[0])); i++) {
|
for(int i = 0; i < ((sizeof shortcuts) / (sizeof shortcuts[0])); i++) {
|
||||||
tmp = charset;
|
tmp = charset;
|
||||||
|
@ -268,7 +263,8 @@ void prepareCharset() {
|
||||||
for (int i = 0; i < ((sizeof shortcuts) / (sizeof shortcuts[0])); i++) {
|
for (int i = 0; i < ((sizeof shortcuts) / (sizeof shortcuts[0])); i++) {
|
||||||
tmp = strstr(inflatedCharset, shortcuts[i].needle);
|
tmp = strstr(inflatedCharset, shortcuts[i].needle);
|
||||||
if ((tmp != NULL) && ((first == NULL) || (tmp < first))) {
|
if ((tmp != NULL) && ((first == NULL) || (tmp < first))) {
|
||||||
first = tmp;
|
// we declared tmp as const char* but now we are using it for char
|
||||||
|
first = (char*) tmp;
|
||||||
replace = shortcuts[i].replacement;
|
replace = shortcuts[i].replacement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -285,6 +281,12 @@ void prepareCharset() {
|
||||||
charset = inflatedCharset;
|
charset = inflatedCharset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prepareInverseCharset() {
|
||||||
|
for(int i = 0; i < strlen(charset); i++) {
|
||||||
|
inverseCharset[charset[i]] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void help(const char* name) {
|
void help(const char* name) {
|
||||||
printf("SYNOPSIS: %s [OPTIONS] INPUT\n\n", name);
|
printf("SYNOPSIS: %s [OPTIONS] INPUT\n\n", name);
|
||||||
printf("options: \n");
|
printf("options: \n");
|
||||||
|
@ -357,6 +359,7 @@ int main(int argc, char** argv) {
|
||||||
settings_t settings = getSettings();
|
settings_t settings = getSettings();
|
||||||
|
|
||||||
if (decodeMode) {
|
if (decodeMode) {
|
||||||
|
prepareInverseCharset();
|
||||||
verbose("decode mode\n");
|
verbose("decode mode\n");
|
||||||
const char* string = argv[optind];
|
const char* string = argv[optind];
|
||||||
verbose("string: %s\n", string);
|
verbose("string: %s\n", string);
|
||||||
|
|
Loading…
Reference in a new issue