Exemplo n.º 1
0
        static Huffman()
        {
            //Add 1st DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode1,
                maxcode       = maxcode1,
                valptr        = valptr1,
                huffval       = huffval1,
                maxcodelength = maxcodelength1
            };
            Console.WriteLine("DHT 1 hashcode" + DHT.GetHashCode());
            Console.WriteLine(string.Join(", ", DHT.huffval[0]));
            DHTs.Add(DHT);


            //Add 2nd DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode2,
                maxcode       = maxcode2,
                valptr        = valptr2,
                huffval       = huffval2,
                maxcodelength = maxcodelength2
            };
            DHTs.Add(DHT);

            //Add 3rd DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode3,
                maxcode       = maxcode3,
                valptr        = valptr3,
                huffval       = huffval3,
                maxcodelength = maxcodelength3
            };
            DHTs.Add(DHT);

            //Add 4th DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode4,
                maxcode       = maxcode4,
                valptr        = valptr4,
                huffval       = huffval4,
                maxcodelength = maxcodelength4
            };
            DHTs.Add(DHT);

            //Add 5th DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode5,
                maxcode       = maxcode5,
                valptr        = valptr5,
                huffval       = huffval5,
                maxcodelength = maxcodelength5
            };
            DHTs.Add(DHT);

            //Add 6th DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode6,
                maxcode       = maxcode6,
                valptr        = valptr6,
                huffval       = huffval6,
                maxcodelength = maxcodelength6
            };
            DHTs.Add(DHT);

            //Add 7th DHT
            DHT = new DHTStruct()
            {
                mincode       = mincode7,
                maxcode       = maxcode7,
                valptr        = valptr7,
                huffval       = huffval7,
                maxcodelength = maxcodelength7
            };
            DHTs.Add(DHT);

            //Add DHTs from file, if any
            String path = "Huffman.json";

            // See if file exists, if exists meaning Huffman values are there
            if (File.Exists(path))
            {
                Console.WriteLine("Huffman table file exists");
                //Check if file is valid
                try
                {
                    using (StreamReader r = new StreamReader(path))
                    {
                        String json   = r.ReadToEnd();
                        var    jarray = JsonConvert.DeserializeObject <List <DHTStruct> >(json);

                        //Console.WriteLine("jarray:  " + jarray[0].huffval);

                        foreach (DHTStruct dhtRecord in jarray)
                        {
                            bool containsItem = DHTs.Any(item => item.id == dhtRecord.id);
                            if (!containsItem)
                            {
                                DHTs.Add(dhtRecord);
                            }
                        }
                        //DHTs.Union(jarray);
                    }

                    Console.WriteLine("Huffman tables count" + DHTs.Count);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error - " + e.Message);
                }
            }
        }
Exemplo n.º 2
0
        public void parseDht(Stream fileStream, long dht_point = 0)
        {
            // Set the stream position to after dht marker.
            fileStream.Seek(dht_point + 2, SeekOrigin.Begin);

            Console.WriteLine("Reading DHT marker");
            BinaryReader binReader = new BinaryReader(fileStream);
            //binReader.BaseStream.Position = dht_point*8;
            //read length in BigEndian
            int length = (int)ReverseBytes(binReader.ReadUInt16());

            length -= 2;
            Console.WriteLine("DHT Length (bytes):" + length);
            // Console.WriteLine("fileStream Length:" + fileStream.Length);
            Huff[] _dht  = new Huff[4];
            int    index = 0;


            while (length > 0)
            {
                _dht[index].huffTable = new int[16][];
                _dht[index].huffval   = new List <byte>();
                _dht[index].maxcode   = new int[17];
                _dht[index].mincode   = new int[17];
                _dht[index].valptr    = new int[16];
                _dht[index].BITS      = new int[17];
                _dht[index].huffSize  = new int[163];
                _dht[index].HUFFCODE  = new uint[163];

                byte tableInfo = binReader.ReadByte();

                int tableID = tableInfo & 0x0F;;
                _dht[index].tableID = tableID;

                int ACTable = tableInfo >> 4;
                _dht[index].ACTable = ACTable;
                Console.WriteLine("tableID:" + tableID);
                Console.WriteLine("ACTable:" + ACTable);

                if (tableID > 3)
                {
                    Console.WriteLine("Error - Invalid Huffman Table ID: " + tableID);
                    return;
                }

                uint allSymbols = 0;
                uint numSymbols = 0;
                //Read numOfSymbols from DHT header
                for (uint i = 0; i < 16; i++)
                {
                    numSymbols = binReader.ReadByte();
                    _dht[index].BITS[i + 1] = (int)numSymbols;
                    allSymbols += numSymbols;
                    _dht[index].huffTable[i] = new int[numSymbols];
                    //  _dht[index].numSymbols = numSymbols;
                }

                if (allSymbols > 162)
                {
                    Console.WriteLine("Error - Too many symbols in Huffman table " + allSymbols);
                    return;
                }

                for (int i = 0; i < 16; i++)
                {
                    if (_dht[index].huffTable[i].Length == 0)
                    {
                        continue;
                    }
                    for (int j = 0; j < _dht[index].huffTable[i].Length; j++)
                    {
                        sbyte s = unchecked ((sbyte)binReader.ReadByte());
                        _dht[index].huffTable[i][j] = s;
                        _dht[index].huffval.Add((byte)s);
                    }
                }

                for (int i = 0; i < 16; i++)
                {
                    Console.WriteLine("Huffman table index " + i + ": " + "[{0}]", string.Join(", ", _dht[index].huffTable[i]));
                }

                Console.WriteLine("Huffman table  " + string.Join(", ", _dht[index].huffval));

                //Generate_size_table(huff_size)
                int K = 0, I = 1, J = 1;
                while (true)
                {
                    if (J > _dht[index].BITS[I])
                    {
                        I = I + 1;
                        J = 1;
                    }
                    else
                    {
                        _dht[index].huffSize[K] = I;
                        K = K + 1;
                        J = J + 1;
                        continue;
                    }

                    if (I > 16)
                    {
                        _dht[index].huffSize[K] = 0;
                        break;
                    }
                }

                //for (int k = 0; k < _dht[index].huffSize.Length; k++)
                //{
                //    Console.WriteLine("HuffmanSize table " + _dht[index].huffSize[k]);
                //}

                //Generate_code_table
                K = 0;
                uint CODE = 0b_0;
                int  SI   = _dht[index].huffSize[0];
                while (true)
                {
                    _dht[index].HUFFCODE[K] = CODE;
                    CODE = CODE + 1;
                    K    = K + 1;
                    if (_dht[index].huffSize[K] == SI)
                    {
                        continue;
                    }

                    if (_dht[index].huffSize[K] == 0)
                    {
                        break;
                    }
                    else
                    {
                        CODE = CODE << 1;
                        SI   = SI + 1;
                        while (_dht[index].huffSize[K] != SI)
                        {
                            CODE = CODE << 1;
                            SI   = SI + 1;
                        }
                    }
                }

                //for (int k = 0; k < _dht[index].HUFFCODE.Length; k++)
                //{
                //    Console.WriteLine("HuffmanCode table " + Convert.ToString(_dht[index].HUFFCODE[k], toBase: 2) );
                //}

                //Decoder_tables
                I = 0;
                J = 0;
                while (true)
                {
                    I = I + 1;
                    if (I > 16)
                    {
                        break;
                    }
                    if (_dht[index].BITS[I] == 0)
                    {
                        _dht[index].maxcode[I] = -1;
                    }
                    else
                    {
                        _dht[index].valptr[I - 1] = J;
                        _dht[index].mincode[I]    = (int)_dht[index].HUFFCODE[J];
                        J = J + _dht[index].BITS[I] - 1;
                        _dht[index].maxcode[I] = (int)_dht[index].HUFFCODE[J];
                        J = J + 1;
                    }
                }

                /*for (int k = 1; k < _dht[index].maxcode.Length; k++)
                 * //{
                 * //    Console.WriteLine("HuffmanMaxCode table " + _dht[index].maxcode[k]);
                 * //}
                 *
                 * for (int k = 1; k < _dht[index].mincode.Length; k++)
                 * {
                 *  Console.WriteLine("HuffmanMInCode table " + _dht[index].mincode[k]);
                 * }*/

                for (int k = 0; k < _dht[index].valptr.Length; k++)
                {
                    Console.WriteLine("Huffman valptr table " + _dht[index].valptr[k]);
                }

                _dht[index].maxcodelength = (int)(Math.Log(_dht[index].maxcode.Max(), 2)) + 1;

                Console.WriteLine("MaxCodeLength " + _dht[index].maxcodelength);

                length = length - (int)allSymbols - 17;
                index++;
            }

            //Write everything to file
            String path = "Huffman.json";

            //Check if atleast four Huffman tables were present, otherwise invalid --> Change this limitation in the future
            if (index < 4)
            {
                return;
            }

            //Convert to Huffman.DHTStruct format
            DHTStruct DHTrecord = Huff2DHTStruct(_dht);

            DHTrecord.id    = DHTrecord.GetHashCode();
            DHTrecord.count = 0;
            //List<DHTStruct> DHTsList = new List<DHTStruct>();
            List <DHTStruct> DHTsList = null;

            //DHTsList.Add(DHTrecord);

            //// See if file exists,
            if (File.Exists(path))
            {
                try
                {
                    /// Read in stream and check if DHT is already present
                    using (StreamReader r = new StreamReader(path))
                    {
                        String json = r.ReadToEnd();
                        DHTsList = JsonConvert.DeserializeObject <List <DHTStruct> >(json);
                        bool recordFound = false;
                        //Check if DHTrecord already present in list
                        for (int i = 0; i < DHTsList.Count; i++)
                        {
                            if (DHTsList[i].id == DHTrecord.id)
                            {
                                DHTrecord.count = DHTsList[i].count + 1;
                                DHTsList[i]     = DHTrecord;
                                recordFound     = true;
                            }
                        }

                        // Console.WriteLine("jarray:  " + jarray[0].huffval);
                        if (!recordFound && DHTsList != null)
                        {
                            DHTsList.Add(DHTrecord);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error - " + e.Message);
                    return;
                }
            }

            // Save DHT to Huffman.json
            if (DHTsList == null)
            {
                DHTsList = new List <DHTStruct>();
                DHTsList.Add(DHTrecord);
            }

            JsonSerializer serializer = new JsonSerializer();

            serializer.NullValueHandling = NullValueHandling.Ignore;
            //Create a new file every time
            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);

            fs.Close();
            using (StreamWriter sw = File.AppendText(path))
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, DHTsList.ToArray());
                }
        }