forked from len0rd/rockbox
sbtools: fix output printing
The code used printf instead of the provided printf functions, resulting in strange output. Change-Id: I2c7c2531d8d54ecdea97e8c189d18d351320ca7d
This commit is contained in:
parent
97459def3c
commit
c0aba07f1a
3 changed files with 60 additions and 25 deletions
|
@ -22,6 +22,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
bool g_debug = false;
|
bool g_debug = false;
|
||||||
|
@ -193,6 +194,15 @@ void clear_keys()
|
||||||
g_key_array = NULL;
|
g_key_array = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void misc_std_printf(void *user, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
(void) user;
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
vprintf(fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
bool add_keys_from_file(const char *key_file)
|
bool add_keys_from_file(const char *key_file)
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
|
@ -233,7 +243,7 @@ bool add_keys_from_file(const char *key_file)
|
||||||
if(g_debug)
|
if(g_debug)
|
||||||
{
|
{
|
||||||
printf("Add key: ");
|
printf("Add key: ");
|
||||||
print_key(&k, true);
|
print_key(NULL, misc_std_printf, &k, true);
|
||||||
}
|
}
|
||||||
add_keys(&k, 1);
|
add_keys(&k, 1);
|
||||||
/* request at least one space character before next key, or end of file */
|
/* request at least one space character before next key, or end of file */
|
||||||
|
@ -253,45 +263,45 @@ bool add_keys_from_file(const char *key_file)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_hex(byte *data, int len, bool newline)
|
void print_hex(void *user, misc_printf_t printf, byte *data, int len, bool newline)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < len; i++)
|
for(int i = 0; i < len; i++)
|
||||||
printf("%02X ", data[i]);
|
printf(user, "%02X ", data[i]);
|
||||||
if(newline)
|
if(newline)
|
||||||
printf("\n");
|
printf(user, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_key(struct crypto_key_t *key, bool newline)
|
void print_key(void *user, misc_printf_t printf, struct crypto_key_t *key, bool newline)
|
||||||
{
|
{
|
||||||
switch(key->method)
|
switch(key->method)
|
||||||
{
|
{
|
||||||
case CRYPTO_KEY:
|
case CRYPTO_KEY:
|
||||||
print_hex(key->u.key, 16, false);
|
print_hex(user, printf, key->u.key, 16, false);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_USBOTP:
|
case CRYPTO_USBOTP:
|
||||||
printf("USB-OTP(%04x:%04x)", key->u.vid_pid >> 16, key->u.vid_pid & 0xffff);
|
printf(user, "USB-OTP(%04x:%04x)", key->u.vid_pid >> 16, key->u.vid_pid & 0xffff);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_NONE:
|
case CRYPTO_NONE:
|
||||||
printf("none");
|
printf(user, "none");
|
||||||
break;
|
break;
|
||||||
case CRYPTO_XOR_KEY:
|
case CRYPTO_XOR_KEY:
|
||||||
print_hex(&key->u.xor_key[0].key[0], 64, false);
|
print_hex(user, printf, &key->u.xor_key[0].key[0], 64, false);
|
||||||
print_hex(&key->u.xor_key[1].key[0], 64, false);
|
print_hex(user, printf, &key->u.xor_key[1].key[0], 64, false);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("unknown");
|
printf(user, "unknown");
|
||||||
}
|
}
|
||||||
if(newline)
|
if(newline)
|
||||||
printf("\n");
|
printf(user, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
char OFF[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
|
const char OFF[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
|
||||||
|
|
||||||
char GREY[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
|
const char GREY[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
|
||||||
char RED[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
|
const char RED[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
|
||||||
char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
|
const char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
|
||||||
char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
|
const char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
|
||||||
char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
|
const char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
|
||||||
|
|
||||||
static bool g_color_enable = true;
|
static bool g_color_enable = true;
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,10 @@ typedef struct crypto_key_t *key_array_t;
|
||||||
int g_nr_keys;
|
int g_nr_keys;
|
||||||
key_array_t g_key_array;
|
key_array_t g_key_array;
|
||||||
|
|
||||||
|
typedef void (*misc_printf_t)(void *user, const char *fmt, ...);
|
||||||
|
|
||||||
|
void misc_std_printf(void *user, const char *fmt, ...);
|
||||||
|
|
||||||
void *memdup(const void *p, size_t len);
|
void *memdup(const void *p, size_t len);
|
||||||
void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug, size_t aug_cnt);
|
void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug, size_t aug_cnt);
|
||||||
void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity,
|
void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity,
|
||||||
|
@ -50,14 +54,14 @@ void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity,
|
||||||
void generate_random_data(void *buf, size_t sz);
|
void generate_random_data(void *buf, size_t sz);
|
||||||
void *xmalloc(size_t s);
|
void *xmalloc(size_t s);
|
||||||
int convxdigit(char digit, byte *val);
|
int convxdigit(char digit, byte *val);
|
||||||
void print_hex(byte *data, int len, bool newline);
|
void print_hex(void *user, misc_printf_t printf, byte *data, int len, bool newline);
|
||||||
void add_keys(key_array_t ka, int kac);
|
void add_keys(key_array_t ka, int kac);
|
||||||
bool parse_key(char **str, struct crypto_key_t *key);
|
bool parse_key(char **str, struct crypto_key_t *key);
|
||||||
bool add_keys_from_file(const char *key_file);
|
bool add_keys_from_file(const char *key_file);
|
||||||
void print_key(struct crypto_key_t *key, bool newline);
|
void print_key(void *user, misc_printf_t printf, struct crypto_key_t *key, bool newline);
|
||||||
void clear_keys();
|
void clear_keys();
|
||||||
|
|
||||||
typedef char color_t[];
|
typedef const char color_t[];
|
||||||
|
|
||||||
extern color_t OFF, GREY, RED, GREEN, YELLOW, BLUE;
|
extern color_t OFF, GREY, RED, GREEN, YELLOW, BLUE;
|
||||||
void color(color_t c);
|
void color(color_t c);
|
||||||
|
|
|
@ -674,6 +674,25 @@ struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t si
|
||||||
#undef fatal
|
#undef fatal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct printer_t
|
||||||
|
{
|
||||||
|
void *user;
|
||||||
|
sb_color_printf cprintf;
|
||||||
|
const char *color;
|
||||||
|
bool error;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void sb_printer(void *user, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
struct printer_t *p = user;
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
char buffer[1024];
|
||||||
|
vsnprintf(buffer, sizeof(buffer), fmt, args);
|
||||||
|
p->cprintf(p->user, p->error, p->color, "%s", buffer);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, void *u,
|
struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, void *u,
|
||||||
sb_color_printf cprintf, enum sb_error_t *err)
|
sb_color_printf cprintf, enum sb_error_t *err)
|
||||||
{
|
{
|
||||||
|
@ -686,8 +705,9 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
cprintf(u, true, GREY, __VA_ARGS__); \
|
cprintf(u, true, GREY, __VA_ARGS__); \
|
||||||
sb_free(sb_file); \
|
sb_free(sb_file); \
|
||||||
return NULL; } while(0)
|
return NULL; } while(0)
|
||||||
|
struct printer_t printer = {.user = u, .cprintf = cprintf, .color = OFF, .error = false };
|
||||||
#define print_hex(c, p, len, nl) \
|
#define print_hex(c, p, len, nl) \
|
||||||
do { printf(c, ""); print_hex(p, len, nl); } while(0)
|
do { printer.color = c; print_hex(&printer, sb_printer, p, len, nl); } while(0)
|
||||||
|
|
||||||
struct sha_1_params_t sha_1_params;
|
struct sha_1_params_t sha_1_params;
|
||||||
sb_file = xmalloc(sizeof(struct sb_file_t));
|
sb_file = xmalloc(sizeof(struct sb_file_t));
|
||||||
|
@ -798,8 +818,8 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
|
||||||
{
|
{
|
||||||
printf(RED, " Key %d\n", i),
|
printf(RED, " Key %d\n", i),
|
||||||
printf(GREEN, " Key: ");
|
printf(GREEN, " Key: ");
|
||||||
printf(YELLOW, "");
|
printer.color = YELLOW;
|
||||||
print_key(&g_key_array[i], true);
|
print_key(&printer, sb_printer, &g_key_array[i], true);
|
||||||
printf(GREEN, " CBC-MAC: ");
|
printf(GREEN, " CBC-MAC: ");
|
||||||
/* check it */
|
/* check it */
|
||||||
byte zero[16];
|
byte zero[16];
|
||||||
|
@ -1137,8 +1157,9 @@ void sb_free(struct sb_file_t *file)
|
||||||
void sb_dump(struct sb_file_t *file, void *u, sb_color_printf cprintf)
|
void sb_dump(struct sb_file_t *file, void *u, sb_color_printf cprintf)
|
||||||
{
|
{
|
||||||
#define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
|
#define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
|
||||||
|
struct printer_t printer = {.user = u, .cprintf = cprintf, .color = OFF, .error = false };
|
||||||
#define print_hex(c, p, len, nl) \
|
#define print_hex(c, p, len, nl) \
|
||||||
do { printf(c, ""); print_hex(p, len, nl); } while(0)
|
do { printer.color = c; print_hex(&printer, sb_printer, p, len, nl); } while(0)
|
||||||
|
|
||||||
#define TREE RED
|
#define TREE RED
|
||||||
#define HEADER GREEN
|
#define HEADER GREEN
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue