#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
}
struct data_in_s {
- unsigned char *start;
- unsigned char *stop;
- unsigned char bitpos;
- unsigned char byte;
+ uint8_t *start;
+ uint8_t *stop;
+ uint8_t bitpos;
+ uint8_t byte;
};
struct data_out_s {
- unsigned char *pos;
- unsigned char *end;
+ uint8_t *pos;
+ uint8_t *end;
};
-unsigned char get_next_in_byte(struct data_in_s *data_in)
+uint8_t get_next_in_byte(struct data_in_s *data_in)
{
- unsigned char byte;
+ uint8_t byte;
if (data_in->stop < data_in->start)
err_exit(__func__);
return byte;
}
-unsigned char get_next_bit(struct data_in_s *data_in)
+uint8_t get_next_bit(struct data_in_s *data_in)
{
- unsigned char bitval;
+ uint8_t bitval;
if (data_in->bitpos == 0x80) {
data_in->byte = get_next_in_byte(data_in);
return 1;
}
-unsigned int get_next_bits(struct data_in_s *data_in, unsigned int bits)
+uint32_t get_next_bits(struct data_in_s *data_in, uint32_t bits)
{
- unsigned int bit;
- unsigned int next_bits;
+ uint32_t bit;
+ uint32_t next_bits;
bit = 1 << (bits - 1);
return next_bits;
}
-void write_byte(unsigned char byte, struct data_out_s *data_out)
+void write_byte(uint8_t byte, struct data_out_s *data_out)
{
if (data_out->pos > data_out->end) {
err_exit(__func__);
void lz_expand(struct data_in_s *data_in, struct data_out_s *data_out)
{
- unsigned int pos;
- unsigned int wordoffset;
- unsigned int i;
- unsigned char byte;
- unsigned int wordlen;
- unsigned char buf[1024];
+ uint32_t pos;
+ uint32_t wordoffset;
+ uint32_t i;
+ uint8_t byte;
+ uint32_t wordlen;
+ uint8_t buf[1024];
pos = 1;
}
}
-void set_next_bit(unsigned char *buf, unsigned int set, unsigned int *currbit) {
- unsigned char *pos;
- unsigned char bitpos;
+void set_next_bit(uint8_t *buf, uint32_t set, uint32_t *currbit) {
+ uint8_t *pos;
+ uint8_t bitpos;
if (set) {
pos = buf + ((*currbit) / 8);
*currbit = *currbit + 1;
}
-void write_bits(unsigned char *buf, unsigned int data, unsigned int bits, unsigned int *currbit) {
- int i;
- unsigned int bitpos;
+void write_bits(uint8_t *buf, uint32_t data, uint32_t bits, uint32_t *currbit) {
+ int32_t i;
+ uint32_t bitpos;
bitpos = 1 << (bits - 1);
}
}
-unsigned char *compress_lz(unsigned char *inbuf, int inlen, int *outlen)
+uint8_t *compress_lz(uint8_t *inbuf, int32_t inlen, int32_t *outlen)
{
- unsigned char *end = inbuf + inlen;
- unsigned char *outbuf;
- unsigned char window[1024];
- int pos = 0;
- int fill = 0;
- unsigned int currbit = 0;
- int offset;
- int wordlen;
- int found;
- int i;
+ uint8_t *end = inbuf + inlen;
+ uint8_t *outbuf;
+ uint8_t window[1024];
+ int32_t pos = 0;
+ int32_t fill = 0;
+ uint32_t currbit = 0;
+ int32_t offset;
+ int32_t wordlen;
+ int32_t found;
+ int32_t i;
if ((outbuf = malloc((inlen * 2) + 4)) == NULL) {
perror("malloc");
}
- *((unsigned int*)outbuf) = LZ_MAGIC;
+ *((uint32_t*)outbuf) = LZ_MAGIC;
currbit = 8 * 8;
while(inbuf < end) {
*outlen = (currbit / 8) + 1;
- *((unsigned int*)(outbuf + 4)) = *outlen;
+ *((uint32_t*)(outbuf + 4)) = *outlen;
return outbuf;
}
/* Checksum is only used for the compressed firmware in 'firmware' */
-unsigned int crc_check(unsigned char *buf, unsigned int len, unsigned int magic)
+uint32_t crc_check(uint8_t *buf, uint32_t len, uint32_t magic)
{
- unsigned int file_crc;
- unsigned int my_len;
- unsigned int crc;
- unsigned int my_magic;
+ uint32_t file_crc;
+ uint32_t my_len;
+ uint32_t crc;
+ uint32_t my_magic;
- my_len = *((unsigned int*)(buf + 0x20));
- my_magic = *((unsigned int*)(buf + 0x24));
+ my_len = *((uint32_t*)(buf + 0x20));
+ my_magic = *((uint32_t*)(buf + 0x24));
if (my_magic != magic) {
printf("\nmagic: 0x%08x <-> 0x%08x\n", my_magic, magic);
return 3;
crc = ~rsb_crc(~0x00, buf, len);
- file_crc = *((unsigned int*)(buf + len));
+ file_crc = *((uint32_t*)(buf + len));
if (file_crc != crc) {
printf("\nChecksums: 0x%08x <-> 0x%08x!\n", crc, file_crc);
return 0;
}
-unsigned char *extract_lz_file(unsigned char *inbuf, unsigned int *outlen , unsigned char check_crc)
+uint8_t *extract_lz_file(uint8_t *inbuf, uint32_t *outlen , uint8_t check_crc)
{
- unsigned char *outbuf;
+ uint8_t *outbuf;
struct data_in_s data_in;
struct data_out_s data_out;
- if (*((unsigned int*)inbuf) != LZ_MAGIC)
+ if (*((uint32_t*)inbuf) != LZ_MAGIC)
err_exit(__func__);
- *outlen = *((unsigned int*)(inbuf + 4));
+ *outlen = *((uint32_t*)(inbuf + 4));
printf(", length: %d", *outlen);
if ((outbuf = malloc(*outlen)) == NULL) {
lz_expand(&data_in, &data_out);
if (check_crc) {
- unsigned int crclen;
- int ret;
+ uint32_t crclen;
+ int32_t ret;
- crclen = *((unsigned int*)(outbuf + 0x20));
+ crclen = *((uint32_t*)(outbuf + 0x20));
if ((ret = crc_check(outbuf, crclen, 0x46335053)) != 0) {
printf("crc_check return: %d\n", ret);