epg/util.c

281 lines
3.8 KiB
C

/*
* Humax EPG Tool
* by af123, 2011
*/
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <strings.h>
#include <stdarg.h>
#include <xconv.h>
#include "lint.h"
void
uncompress_epg(char **epg, unsigned int *epglen)
{
char *new;
unsigned int newlen;
if ((new = (char *)
freeview_huffman_to_string((unsigned char *)(*epg),
*epglen, &newlen)))
{
free(*epg);
*epg = new;
*epglen = newlen;
}
}
void
iso6937_convert(char **str, unsigned int *len)
{
char dst[0x200];
int newlen;
newlen = xconv(*str, dst, sizeof(dst));
if (newlen)
{
free(*str);
*str = strdup(dst);
*len = newlen;
}
}
#ifdef sun
char *
strcasestr (char *h, char *n)
{
char *hp, *np = n, *match = 0;
if(!*np) {
return np;
}
for (hp = h; *hp; hp++) {
if (toupper(*hp) == toupper(*np)) {
if (!match) {
match = hp;
}
if(!*++np) {
return match;
}
} else {
if (match) {
match = 0;
np = n;
}
}
}
return NULL;
}
#endif
void
safeprintf(char *fmt, ...)
{
char buf[0x400];
char *p;
va_list argp;
int len;
va_start(argp, fmt);
len = vsprintf(buf, fmt, argp);
va_end(argp);
for (p = buf; p < buf + len; p++)
if (!isprint(*p) && *p != '\t' && *p != '\n')
*p = '.';
printf("%.*s", len, buf);
}
time_t
mjd(uint16_t day, int h, int m, int s)
{
time_t tm;
struct tm *t;
tm = MJD_TO_UNIX(day);
t = gmtime(&tm);
t->tm_hour = h;
t->tm_min = m;
t->tm_sec = s;
return mktime(t);
}
char *
ctime_nl(time_t *tm)
{
static char buf[26];
strcpy(buf, ctime(tm));
*strchr(buf, '\n') = '\0';
return buf;
}
inline 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];
}
inline uint16_t
read_uint16(uint8_t *p, int le)
{
if (le)
return p[1] << 8 | p[0];
else
return p[0] << 8 | p[1];
}
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");
}
}
char *
hexstr(uint8_t *s, uint32_t len)
{
static char buf[0x1000];
static char buf2[0x1000];
uint16_t off;
if (!s)
return "";
if (!len)
len = strlen((char *)s);
memset(buf, '\0', sizeof(buf));
memset(buf2, '\0', sizeof(buf));
for (off = 0; off < len; off += 16)
{
uint32_t i;
for (i = off; i - off < 16; i++)
{
if (i < len)
{
sprintf(buf + i * 3, "%02x ", s[i] & 0xff);
if (isprint(s[i] & 0xff))
buf2[i] = s[i];
else
buf2[i] = '.';
}
}
}
strcat(buf, " (");
strcat(buf, buf2);
strcat(buf, ")");
return buf;
}
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