forked from hummypkg/hmt
177 lines
2.4 KiB
C
177 lines
2.4 KiB
C
/*
|
|
* HMT Tool
|
|
* (c) af123, 2011
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#ifndef _WIN32
|
|
#include <unistd.h>
|
|
#endif
|
|
#include <time.h>
|
|
#include <strings.h>
|
|
#include <stdarg.h>
|
|
|
|
#include "lint.h"
|
|
|
|
uint32_t
|
|
read_uint32(uint8_t *p, int le)
|
|
{
|
|
if (le)
|
|
return p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
|
|
else
|
|
return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
|
|
}
|
|
|
|
uint16_t
|
|
read_uint16(uint8_t *p, int le)
|
|
{
|
|
if (le)
|
|
return p[1] << 8 | p[0];
|
|
else
|
|
return p[0] << 8 | p[1];
|
|
}
|
|
|
|
void
|
|
write_uint32(uint8_t *p, uint32_t val)
|
|
{
|
|
*p++ = val & 0xff;
|
|
*p++ = val >> 8 & 0xff;
|
|
*p++ = val >> 16 & 0xff;
|
|
*p++ = val >> 24 & 0xff;
|
|
}
|
|
|
|
void
|
|
write_uint16(uint8_t *p, uint16_t val)
|
|
{
|
|
*p++ = val & 0xff;
|
|
*p++ = val >> 8 & 0xff;
|
|
}
|
|
|
|
uint8_t *
|
|
memmem(register uint8_t *mem, uint32_t mlen,
|
|
register uint8_t *pat, uint32_t plen)
|
|
{
|
|
while (mlen >= plen)
|
|
{
|
|
if (!memcmp(mem, pat, plen))
|
|
return mem;
|
|
mem++, mlen--;
|
|
}
|
|
return (uint8_t *)NULL;
|
|
}
|
|
|
|
void
|
|
hexdump(uint8_t *s, uint32_t len, uint32_t o)
|
|
{
|
|
uint16_t off;
|
|
|
|
if (!s)
|
|
return;
|
|
|
|
if (!len)
|
|
len = strlen((char *)s);
|
|
|
|
for (off = 0; off < len; off += 16)
|
|
{
|
|
uint32_t i;
|
|
|
|
printf("%08lx: ", (unsigned long)off + o);
|
|
|
|
for (i = off; i - off < 16; i++)
|
|
{
|
|
if (i < len)
|
|
printf("%02x ", s[i] & 0xff);
|
|
else
|
|
printf(" ");
|
|
}
|
|
|
|
printf(" ");
|
|
|
|
for (i = off; i < len && i - off < 16; i++)
|
|
printf("%c", isprint((int)s[i]) ? s[i] : '.');
|
|
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void
|
|
hexstr(uint8_t *s, uint32_t len)
|
|
{
|
|
uint16_t off;
|
|
|
|
if (!s)
|
|
return;
|
|
|
|
if (!len)
|
|
len = strlen((char *)s);
|
|
|
|
for (off = 0; off < len; off += 16)
|
|
{
|
|
uint32_t i;
|
|
|
|
for (i = off; i - off < 16; i++)
|
|
{
|
|
if (i < len)
|
|
printf("%02x", s[i] & 0xff);
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
hexdump_fd(int fd, uint32_t len)
|
|
{
|
|
off_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
uint8_t *buffer;
|
|
|
|
buffer = (uint8_t *)malloc(len);
|
|
|
|
read(fd, buffer, len);
|
|
hexdump(buffer, len, 0);
|
|
|
|
lseek(fd, pos, SEEK_SET);
|
|
}
|
|
|
|
void
|
|
error(char *fmt, ...)
|
|
{
|
|
char buf[0x400];
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
|
vsprintf(buf, fmt, argp);
|
|
|
|
fprintf(stderr, "%s\n", buf);
|
|
va_end(argp);
|
|
exit(1);
|
|
}
|
|
|
|
#if defined(__MINGW32__) || defined(__linux__)
|
|
|
|
size_t
|
|
strlcpy(char *s, const char *t, size_t n)
|
|
{
|
|
const char *o = t;
|
|
|
|
if (n)
|
|
do
|
|
{
|
|
if (!--n)
|
|
{
|
|
*s = 0;
|
|
break;
|
|
}
|
|
} while ((*s++ = *t++));
|
|
if (!n)
|
|
while (*t++);
|
|
return t - o - 1;
|
|
}
|
|
|
|
#endif
|
|
|