fix const params, lessen variable scope and fixing func arguments names

This commit is contained in:
iceman1001 2024-05-14 11:10:12 +02:00
commit 3430e20459
3 changed files with 129 additions and 75 deletions

View file

@ -113,11 +113,14 @@ kvsprintf(char const *fmt, void *arg, int radix, va_list ap) {
for (;;) { for (;;) {
padc = ' '; padc = ' ';
width = 0; width = 0;
while ((ch = (u_char) * fmt++) != '%' || stop) { while ((ch = (u_char) * fmt++) != '%' || stop) {
PCHAR(ch); PCHAR(ch);
if (ch == '\0') if (ch == '\0') {
return (retval); return (retval);
}
} }
percent = fmt - 1; percent = fmt - 1;
qflag = 0; qflag = 0;
lflag = 0; lflag = 0;
@ -178,36 +181,43 @@ reswitch:
for (n = 0;; ++fmt) { for (n = 0;; ++fmt) {
n = n * 10 + ch - '0'; n = n * 10 + ch - '0';
ch = *fmt; ch = *fmt;
if (ch < '0' || ch > '9') if (ch < '0' || ch > '9') {
break; break;
}
} }
if (dot) if (dot) {
dwidth = n; dwidth = n;
else } else {
width = n; width = n;
}
goto reswitch; goto reswitch;
case 'b': case 'b':
num = (u_int)va_arg(ap, int); num = (u_int)va_arg(ap, int);
p = va_arg(ap, char *); p = va_arg(ap, char *);
for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) {
PCHAR(*q--); PCHAR(*q--);
}
if (num == 0) if (num == 0) {
break; break;
}
for (tmp = 0; *p;) { for (tmp = 0; *p;) {
n = *p++; n = *p++;
if (num & (1 << (n - 1))) { if (num & (1 << (n - 1))) {
PCHAR(tmp ? ',' : '<'); PCHAR(tmp ? ',' : '<');
for (; (n = *p) > ' '; ++p) for (; (n = *p) > ' '; ++p) {
PCHAR(n); PCHAR(n);
}
tmp = 1; tmp = 1;
} else } else {
for (; *p > ' '; ++p) for (; *p > ' '; ++p) {};
continue; }
} }
if (tmp)
if (tmp) {
PCHAR('>'); PCHAR('>');
}
break; break;
case 'c': case 'c':
PCHAR(va_arg(ap, int)); PCHAR(va_arg(ap, int));
@ -215,15 +225,19 @@ reswitch:
case 'D': case 'D':
up = va_arg(ap, u_char *); up = va_arg(ap, u_char *);
p = va_arg(ap, char *); p = va_arg(ap, char *);
if (!width) if (!width) {
width = 16; width = 16;
}
while (width--) { while (width--) {
PCHAR(hex2ascii(*up >> 4)); PCHAR(hex2ascii(*up >> 4));
PCHAR(hex2ascii(*up & 0x0f)); PCHAR(hex2ascii(*up & 0x0f));
up++; up++;
if (width) if (width) {
for (q = p; *q; q++) for (q = p; *q; q++) {
PCHAR(*q); PCHAR(*q);
}
}
} }
break; break;
case 'd': case 'd':
@ -235,8 +249,9 @@ reswitch:
if (hflag) { if (hflag) {
hflag = 0; hflag = 0;
cflag = 1; cflag = 1;
} else } else {
hflag = 1; hflag = 1;
}
goto reswitch; goto reswitch;
case 'j': case 'j':
jflag = 1; jflag = 1;
@ -245,8 +260,9 @@ reswitch:
if (lflag) { if (lflag) {
lflag = 0; lflag = 0;
qflag = 1; qflag = 1;
} else } else {
lflag = 1; lflag = 1;
}
goto reswitch; goto reswitch;
case 'n': case 'n':
if (jflag) if (jflag)
@ -278,29 +294,38 @@ reswitch:
goto reswitch; goto reswitch;
case 'r': case 'r':
base = radix; base = radix;
if (sign) if (sign) {
goto handle_sign; goto handle_sign;
}
goto handle_nosign; goto handle_nosign;
case 's': case 's':
p = va_arg(ap, char *); p = va_arg(ap, char *);
if (p == NULL) if (p == NULL) {
p = "(null)"; p = "(null)";
if (!dot) }
if (!dot) {
n = strlen(p); n = strlen(p);
else } else {
for (n = 0; n < dwidth && p[n]; n++) for (n = 0; n < dwidth && p[n]; n++) {};
continue; }
width -= n; width -= n;
if (!ladjust && width > 0) if (!ladjust && width > 0) {
while (width--) while (width--) {
PCHAR(padc); PCHAR(padc);
while (n--) }
}
while (n--) {
PCHAR(*p++); PCHAR(*p++);
if (ladjust && width > 0) }
while (width--)
if (ladjust && width > 0) {
while (width--) {
PCHAR(padc); PCHAR(padc);
}
}
break; break;
case 't': case 't':
tflag = 1; tflag = 1;
@ -361,22 +386,31 @@ number:
neg = 1; neg = 1;
num = -(intmax_t)num; num = -(intmax_t)num;
} }
p = ksprintn(nbuf, num, base, &tmp, upper);
if (sharpflag && num != 0) {
if (base == 8)
tmp++;
else if (base == 16)
tmp += 2;
}
if (neg)
tmp++;
if (!ladjust && padc != '0' && width p = ksprintn(nbuf, num, base, &tmp, upper);
&& (width -= tmp) > 0)
while (width--) if (sharpflag && num != 0) {
if (base == 8) {
tmp++;
} else if (base == 16) {
tmp += 2;
}
}
if (neg) {
tmp++;
}
if (!ladjust && padc != '0' && width && (width -= tmp) > 0) {
while (width--) {
PCHAR(padc); PCHAR(padc);
if (neg) }
}
if (neg) {
PCHAR('-'); PCHAR('-');
}
if (sharpflag && num != 0) { if (sharpflag && num != 0) {
if (base == 8) { if (base == 8) {
PCHAR('0'); PCHAR('0');
@ -385,21 +419,28 @@ number:
PCHAR('x'); PCHAR('x');
} }
} }
if (!ladjust && width && (width -= tmp) > 0)
while (width--)
PCHAR(padc);
while (*p) if (!ladjust && width && (width -= tmp) > 0) {
while (width--) {
PCHAR(padc);
}
}
while (*p) {
PCHAR(*p--); PCHAR(*p--);
}
if (ladjust && width && (width -= tmp) > 0) if (ladjust && width && (width -= tmp) > 0) {
while (width--) while (width--) {
PCHAR(padc); PCHAR(padc);
}
}
break; break;
default: default:
while (percent < fmt) while (percent < fmt) {
PCHAR(*percent++); PCHAR(*percent++);
}
/* /*
* Since we ignore an formatting argument it is no * Since we ignore an formatting argument it is no
* longer safe to obey the remaining formatting * longer safe to obey the remaining formatting

View file

@ -63,13 +63,15 @@ int bf_generator_set_charset(generator_context_t *ctx, uint8_t charsets) {
int bf_generate(generator_context_t *ctx) { int bf_generate(generator_context_t *ctx) {
switch (ctx->mode) { switch (ctx->mode) {
case BF_MODE_RANGE: case BF_MODE_RANGE: {
return _bf_generate_mode_range(ctx); return _bf_generate_mode_range(ctx);
case BF_MODE_CHARSET: }
case BF_MODE_CHARSET: {
return _bf_generate_mode_charset(ctx); return _bf_generate_mode_charset(ctx);
}
case BF_MODE_SMART: case BF_MODE_SMART: {
return _bf_generate_mode_smart(ctx); return _bf_generate_mode_smart(ctx);
}
} }
return BF_GENERATOR_ERROR; return BF_GENERATOR_ERROR;
@ -81,23 +83,27 @@ int bf_generate(generator_context_t *ctx) {
// returns -1 if incrementing reaches its end // returns -1 if incrementing reaches its end
int bf_array_increment(uint8_t *data, uint8_t data_len, uint8_t modulo) { int bf_array_increment(uint8_t *data, uint8_t data_len, uint8_t modulo) {
uint8_t prev_value;
// check if we reached max value already // check if we reached max value already
uint8_t i; uint8_t i;
for (i = 0; i < data_len; i++) for (i = 0; i < data_len; i++) {
if (data[i] < modulo - 1) if (data[i] < modulo - 1) {
break; break;
}
}
if (i == data_len) if (i == data_len) {
return -1; return -1;
}
for (uint8_t pos = data_len - 1;; pos--) { for (uint8_t pos = data_len - 1;; pos--) {
prev_value = ++data[pos];
uint8_t prev_value = ++data[pos];
data[pos] = data[pos] % modulo; data[pos] = data[pos] % modulo;
if (prev_value == data[pos])
if (prev_value == data[pos]) {
return 0; return 0;
else if (pos == 0) { } else if (pos == 0) {
// we cannot carryover to next byte // we cannot carryover to next byte
// with the max value check in place before, we should not reach this place // with the max value check in place before, we should not reach this place
return -1; return -1;
@ -108,12 +114,12 @@ int bf_array_increment(uint8_t *data, uint8_t data_len, uint8_t modulo) {
} }
// get current key casted to 32 bit // get current key casted to 32 bit
uint32_t bf_get_key32(generator_context_t *ctx) { uint32_t bf_get_key32(const generator_context_t *ctx) {
return ctx->current_key & 0xFFFFFFFF; return ctx->current_key & 0xFFFFFFFF;
} }
// get current key casted to 48 bit // get current key casted to 48 bit
uint64_t bf_get_key48(generator_context_t *ctx) { uint64_t bf_get_key48(const generator_context_t *ctx) {
return ctx->current_key & 0xFFFFFFFFFFFF; return ctx->current_key & 0xFFFFFFFFFFFF;
} }
@ -127,8 +133,9 @@ void bf_generator_clear(generator_context_t *ctx) {
int _bf_generate_mode_range(generator_context_t *ctx) { int _bf_generate_mode_range(generator_context_t *ctx) {
if (ctx->key_length != BF_KEY_SIZE_32 && ctx->key_length != BF_KEY_SIZE_48) if (ctx->key_length != BF_KEY_SIZE_32 && ctx->key_length != BF_KEY_SIZE_48) {
return BF_GENERATOR_ERROR; return BF_GENERATOR_ERROR;
}
if (ctx->current_key >= ctx->range_high) { if (ctx->current_key >= ctx->range_high) {
return BF_GENERATOR_END; return BF_GENERATOR_END;
@ -152,8 +159,9 @@ int _bf_generate_mode_charset(generator_context_t *ctx) {
return BF_GENERATOR_ERROR; return BF_GENERATOR_ERROR;
} }
if (ctx->flag1) if (ctx->flag1) {
return BF_GENERATOR_END; return BF_GENERATOR_END;
}
uint8_t key_byte = 0; uint8_t key_byte = 0;
ctx->current_key = 0; ctx->current_key = 0;
@ -162,32 +170,35 @@ int _bf_generate_mode_charset(generator_context_t *ctx) {
ctx->current_key |= (uint64_t) ctx->charset[ctx->pos[key_byte]] << ((ctx->key_length - key_byte - 1) * 8); ctx->current_key |= (uint64_t) ctx->charset[ctx->pos[key_byte]] << ((ctx->key_length - key_byte - 1) * 8);
} }
if (bf_array_increment(ctx->pos, ctx->key_length, ctx->charset_length) == -1) if (bf_array_increment(ctx->pos, ctx->key_length, ctx->charset_length) == -1) {
// set flag1 to emit value last time and end generation on next call // set flag1 to emit value last time and end generation on next call
ctx->flag1 = true; ctx->flag1 = true;
}
return BF_GENERATOR_NEXT; return BF_GENERATOR_NEXT;
} }
int _bf_generate_mode_smart(generator_context_t *ctx) { int _bf_generate_mode_smart(generator_context_t *ctx) {
int ret;
while (1) { while (1) {
if (smart_generators[ctx->smart_mode_stage] == NULL) if (smart_generators[ctx->smart_mode_stage] == NULL) {
return BF_GENERATOR_END; return BF_GENERATOR_END;
}
ret = smart_generators[ctx->smart_mode_stage](ctx); int ret = smart_generators[ctx->smart_mode_stage](ctx);
switch (ret) { switch (ret) {
case BF_GENERATOR_NEXT: case BF_GENERATOR_NEXT: {
return ret; return ret;
case BF_GENERATOR_ERROR: }
case BF_GENERATOR_ERROR: {
return ret; return ret;
case BF_GENERATOR_END: }
case BF_GENERATOR_END: {
ctx->smart_mode_stage++; ctx->smart_mode_stage++;
bf_generator_clear(ctx); bf_generator_clear(ctx);
continue; continue;
}
} }
} }
} }
@ -197,8 +208,9 @@ int smart_generator_byte_repeat(generator_context_t *ctx) {
// key consists of repeated single byte // key consists of repeated single byte
uint32_t current_byte = ctx->counter1; uint32_t current_byte = ctx->counter1;
if (current_byte > 0xFF) if (current_byte > 0xFF) {
return BF_GENERATOR_END; return BF_GENERATOR_END;
}
ctx->current_key = 0; ctx->current_key = 0;
@ -213,8 +225,9 @@ int smart_generator_msb_byte_only(generator_context_t *ctx) {
// key of one byte (most significant one) and all others being zero // key of one byte (most significant one) and all others being zero
uint32_t current_byte = ctx->counter1; uint32_t current_byte = ctx->counter1;
if (current_byte > 0xFF) if (current_byte > 0xFF) {
return BF_GENERATOR_END; return BF_GENERATOR_END;
}
ctx->current_key = (uint64_t)current_byte << ((ctx->key_length - 1) * 8); ctx->current_key = (uint64_t)current_byte << ((ctx->key_length - 1) * 8);

View file

@ -87,8 +87,8 @@ int _bf_generate_mode_range(generator_context_t *ctx);
int _bf_generate_mode_charset(generator_context_t *ctx); int _bf_generate_mode_charset(generator_context_t *ctx);
int _bf_generate_mode_smart(generator_context_t *ctx); int _bf_generate_mode_smart(generator_context_t *ctx);
int bf_array_increment(uint8_t *data, uint8_t data_len, uint8_t modulo); int bf_array_increment(uint8_t *data, uint8_t data_len, uint8_t modulo);
uint32_t bf_get_key32(generator_context_t *ctx); uint32_t bf_get_key32(const generator_context_t *ctx);
uint64_t bf_get_key48(generator_context_t *ctx); uint64_t bf_get_key48(const generator_context_t *ctx);
// smart mode // smart mode
typedef int (smart_generator_t)(generator_context_t *ctx); typedef int (smart_generator_t)(generator_context_t *ctx);