00001 #include "freesat_huffman.h"
00002
00003 #include <qstring.h>
00004
00005 struct fsattab {
00006 unsigned int value;
00007 short bits;
00008 char next;
00009 };
00010
00011 #define START '\0'
00012 #define STOP '\0'
00013 #define ESCAPE '\1'
00014
00015 #include "freesat_tables.h"
00016
00017 QString freesat_huffman_to_string(const unsigned char *src, uint size)
00018 {
00019 if (src[1] == 1 || src[1] == 2)
00020 {
00021 QByteArray uncompressed(size * 3);
00022 int p = 0;
00023 unsigned value = 0, byte = 2, bit = 0;
00024 while (byte < 6 && byte < size)
00025 {
00026 value |= src[byte] << ((5-byte) * 8);
00027 byte++;
00028 }
00029 char lastch = START;
00030
00031 do
00032 {
00033 bool found = false;
00034 unsigned bitShift = 0;
00035 char nextCh = STOP;
00036 if (lastch == ESCAPE)
00037 {
00038 found = true;
00039
00040
00041 nextCh = (value >> 24) & 0xff;
00042 bitShift = 8;
00043 if ((nextCh & 0x80) == 0)
00044 {
00045 if (nextCh < ' ')
00046 nextCh = STOP;
00047 lastch = nextCh;
00048 }
00049 }
00050 else
00051 {
00052 unsigned indx = (unsigned)lastch;
00053 if (src[1] == 2)
00054 indx |= 0x80;
00055 for (unsigned j = fsat_index[indx]; j < fsat_index[indx+1]; j++)
00056 {
00057 unsigned mask = 0, maskbit = 0x80000000;
00058 for (short kk = 0; kk < fsat_table[j].bits; kk++)
00059 {
00060 mask |= maskbit;
00061 maskbit >>= 1;
00062 }
00063 if ((value & mask) == fsat_table[j].value)
00064 {
00065 nextCh = fsat_table[j].next;
00066 bitShift = fsat_table[j].bits;
00067 found = true;
00068 lastch = nextCh;
00069 break;
00070 }
00071 }
00072 }
00073 if (found)
00074 {
00075 if (nextCh != STOP && nextCh != ESCAPE)
00076 {
00077 if (p >= uncompressed.count())
00078 uncompressed.resize(p+10);
00079 uncompressed[p++] = nextCh;
00080 }
00081
00082 for (unsigned b = 0; b < bitShift; b++)
00083 {
00084 value = (value << 1) & 0xfffffffe;
00085 if (byte < size)
00086 value |= (src[byte] >> (7-bit)) & 1;
00087 if (bit == 7)
00088 {
00089 bit = 0;
00090 byte++;
00091 }
00092 else bit++;
00093 }
00094 }
00095 else
00096 {
00097
00098 QString result = QString::fromUtf8(uncompressed, p);
00099 result.append("...");
00100 return result;
00101 }
00102 } while (lastch != STOP && byte < size+4);
00103
00104 return QString::fromUtf8(uncompressed, p);
00105 }
00106 else return QString("");
00107 }