OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [arch/] [or32/] [tools/] [comp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 862 simons
#include <stdio.h>
2
 
3
unsigned char src[5000000];
4
unsigned char dest[10000000];
5
unsigned char dec[10000000];
6
 
7
#define CRC(x) (crc = (crc << 5) | (x))
8
 
9
unsigned long compress (unsigned char *in, unsigned char *out, unsigned long size)
10
{
11
  unsigned char hist[0x10000] = {0};
12
  unsigned char *mask;
13
  unsigned short hash = 0;
14
  unsigned i, b;
15
  unsigned long csize = 5;
16
  unsigned long crc = 0;
17
 
18
  *out++ = (size >> 24) & 0xff;
19
  *out++ = (size >> 16) & 0xff;
20
  *out++ = (size >> 8) & 0xff;
21
  *out++ = (size >> 0) & 0xff;
22
  mask = out++;
23
  *mask = 0;
24
  for (i = 0, b = 0; i < size; i++) {
25
    CRC(*in);
26
    if (*in == hist[hash]) *mask |= 1 << b;
27
    else {
28
      *out++ = *in;
29
      csize++;
30
    }
31
    hist[hash] = *in;
32
    hash <<= 8;
33
    hash |= *in;
34
    if (++b == 8) {
35
      mask = out++;
36
      csize++;
37
      *mask = 0;
38
      b = 0;
39
    }
40
    in++;
41
  }
42
  printf ("original CRC = %08x\n", crc);
43
  return csize;
44
}
45
 
46
unsigned long decompress (unsigned char *in, unsigned char *out)
47
{
48
  unsigned char hist[0x10000] = {0};
49
  unsigned long size;
50
  int b = 0, i;
51
  unsigned char *mask;
52
  unsigned short hash = 0;
53
  unsigned long crc = 0;
54
 
55
  CRC(*in); size = (*in++) << 24;
56
  CRC(*in); size |= (*in++) << 16;
57
  CRC(*in); size |= (*in++) << 8;
58
  CRC(*in); size |= (*in++) << 0;
59
  CRC(*in); mask = in++;
60
  for (i = 0; i < size; i++) {
61
    unsigned char c;
62
    if ((*mask >> b) & 1) c = hist[hash];
63
    else CRC(c = *in++);
64
    *out++ = c;
65
    hist[hash] = c;
66
    hash <<= 8;
67
    hash |= c;
68
    hash &= 0xffff;
69
    if (++b == 8) {
70
      CRC(*in);
71
      mask = in++;
72
      b = 0;
73
    }
74
  }
75
  printf ("compressed CRC = %08x\n", crc);
76
  return size;
77
}
78
 
79
int main (int argc, char *argv[])
80
{
81
  FILE *fi;
82
  unsigned short hash = 0;
83
  int i, size;
84
  char tmp[50];
85
  unsigned long crc = 0;
86
  if (argc != 2) return -1;
87
 
88
  fi = fopen (argv[1], "rb");
89
  if (!fi) return 1;
90
  size = fread (src, 1, sizeof (src), fi);
91
  fclose (fi);
92
  printf ("original size = %i (%08x)\n", size, size);
93
  size = compress (src, dest, size);
94
  printf ("compressed size = %i (%08x)\n", size, size);
95
  sprintf (tmp, "%s.cmp", argv[1]);
96
  fi = fopen (tmp, "wb+");
97
  fwrite (dest, 1, size, fi);
98
  fclose (fi);
99
  size = decompress (dest, dec);
100
  printf ("decompressed size = %i (%08x)\n", size, size);
101
  for (i = 0; i < size; i++)
102
    if (src[i] != dec[i]) {
103
      printf ("error!\n");
104
      return 1;
105
    }
106
#if 0
107
  fi = fopen ("fl_img.dec", "wb+");
108
  fwrite (dec, 1, size, fi);
109
  fclose (fi);
110
#endif
111
  return 0;
112
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.