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