[misc]add misc code

This commit is contained in:
JianjunJiang 2024-08-24 11:03:03 +08:00
parent 33d37b28d0
commit de5cd0cc4f
4 changed files with 114 additions and 77 deletions

77
main.c
View File

@ -1,82 +1,5 @@
#include <rock.h>
static uint64_t file_save(const char * filename, void * buf, uint64_t len)
{
FILE * out = fopen(filename, "wb");
int r;
if(!out)
{
perror("Failed to open output file");
exit(-1);
}
r = fwrite(buf, len, 1, out);
fclose(out);
return r;
}
static void * file_load(const char * filename, uint64_t * len)
{
uint64_t offset = 0, bufsize = 8192;
char * buf = malloc(bufsize);
FILE * in;
if(strcmp(filename, "-") == 0)
in = stdin;
else
in = fopen(filename, "rb");
if(!in)
{
perror("Failed to open input file");
exit(-1);
}
while(1)
{
uint64_t len = bufsize - offset;
uint64_t n = fread(buf + offset, 1, len, in);
offset += n;
if(n < len)
break;
bufsize *= 2;
buf = realloc(buf, bufsize);
if(!buf)
{
perror("Failed to resize load_file() buffer");
exit(-1);
}
}
if(len)
*len = offset;
if(in != stdin)
fclose(in);
return buf;
}
static void hexdump(uint32_t addr, void * buf, size_t len)
{
unsigned char * p = buf;
size_t i, j;
for(j = 0; j < len; j += 16)
{
printf("%08x: ", (uint32_t)(addr + j));
for(i = 0; i < 16; i++)
{
if(j + i < len)
printf("%02x ", p[j + i]);
else
printf(" ");
}
putchar(' ');
for(i = 0; i < 16; i++)
{
if(j + i >= len)
putchar(' ');
else
putchar(isprint(p[j + i]) ? p[j + i] : '.');
}
printf("\r\n");
}
}
static const char * manufacturer[] = {
"Samsung",
"Toshiba",

94
misc.c Normal file
View File

@ -0,0 +1,94 @@
#include <misc.h>
uint64_t file_save(const char * filename, void * buf, uint64_t len)
{
FILE * out = fopen(filename, "wb");
int r;
if(!out)
{
perror("Failed to open output file");
exit(-1);
}
r = fwrite(buf, len, 1, out);
fclose(out);
return r;
}
void * file_load(const char * filename, uint64_t * len)
{
uint64_t offset = 0, bufsize = 8192;
char * buf = malloc(bufsize);
FILE * in;
if(strcmp(filename, "-") == 0)
in = stdin;
else
in = fopen(filename, "rb");
if(!in)
{
perror("Failed to open input file");
exit(-1);
}
while(1)
{
uint64_t len = bufsize - offset;
uint64_t n = fread(buf + offset, 1, len, in);
offset += n;
if(n < len)
break;
bufsize *= 2;
buf = realloc(buf, bufsize);
if(!buf)
{
perror("Failed to resize load_file() buffer");
exit(-1);
}
}
if(len)
*len = offset;
if(in != stdin)
fclose(in);
return buf;
}
static inline unsigned char hex_to_bin(char c)
{
if((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
if((c >= '0') && (c <= '9'))
return c - '0';
if((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
return 0;
}
unsigned char hex_string(const char * s, int o)
{
return (hex_to_bin(s[o]) << 4) | hex_to_bin(s[o + 1]);
}
void hexdump(uint32_t addr, void * buf, size_t len)
{
unsigned char * p = buf;
size_t i, j;
for(j = 0; j < len; j += 16)
{
printf("%08x: ", (uint32_t)(addr + j));
for(i = 0; i < 16; i++)
{
if(j + i < len)
printf("%02x ", p[j + i]);
else
printf(" ");
}
putchar(' ');
for(i = 0; i < 16; i++)
{
if(j + i >= len)
putchar(' ');
else
putchar(isprint(p[j + i]) ? p[j + i] : '.');
}
printf("\r\n");
}
}

19
misc.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __MISC_H__
#define __MISC_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <x.h>
uint64_t file_save(const char * filename, void * buf, uint64_t len);
void * file_load(const char * filename, uint64_t * len);
unsigned char hex_string(const char * s, int o);
void hexdump(uint32_t addr, void * buf, size_t len);
#ifdef __cplusplus
}
#endif
#endif /* __MISC_H__ */

1
rock.h
View File

@ -9,6 +9,7 @@ extern "C" {
#include <rc4.h>
#include <crc16.h>
#include <crc32.h>
#include <misc.h>
#include <progress.h>
struct chip_t {