Ejemplo n.º 1
0
        static public byte[] Unpack(byte[] iBuf)
        {
            var PureMemStream = new MemoryStream(iBuf);
            var MS            = new QuickStream(PureMemStream);

            if (MS.ReadString(6) != "JXSDA\x1a")
            {
                Fout("Data is not JXSDA packed data"); return(null);
            }
            var ubit     = MS.ReadByte();
            var pbit     = MS.ReadByte();
            var rest     = MS.ReadByte();
            var dsiz     = MS.ReadInt();
            var dict     = new Dictionary <long, long>();
            var restbank = new List <byte>();

            P($"Unpack data: ubit:{ubit}; pbit:{pbit}; rest:{rest}; dsiz:{dsiz}");
            for (int i = 0; i < dsiz; ++i)
            {
                switch (ubit)
                {
                case 64:
                    dict[i] = MS.ReadLong();
                    break;

                case 32:
                    dict[i] = MS.ReadInt();
                    break;

                case 16:
                    dict[i] = MS.ReadShort();
                    break;

                default:
                    Fout($"Invalid ubit value {ubit} while reading dictionary");
                    return(null);
                }
            }
            for (byte i = 0; i < rest; i++)
            {
                restbank.Add(MS.ReadByte());
            }
            var UPWrite = new MemoryStream();
            var U       = new QuickStream(UPWrite);

            while (!MS.EOF)
            {
                try {
                    long value = 0;
                    switch (pbit)
                    {
                    case 32:
                        value = MS.ReadInt();
                        break;

                    case 16:
                        value = MS.ReadShort();
                        break;

                    case 8:
                        value = MS.ReadByte();
                        break;

                    default:
                        Fout($"Invalud pbit value {pbit} in reading bytes to unpack");
                        break;
                    }
                    if (!dict.ContainsKey(value))
                    {
                        Fout($"Undefined dictionary index {value}/{value:X2}");
                        return(null);
                    }
                    switch (ubit)
                    {
                    case 64:
                        U.WriteLong(dict[value]);
                        break;

                    case 32:
                        U.WriteInt((int)dict[value]);
                        break;

                    case 16:
                        U.WriteShort((short)dict[value]);
                        break;

                    default:
                        Fout($"Invalud ubit value ({ubit}) in writing bytes in unpacking");
                        return(null);
                    }
                } catch (Exception e) {
                    Fout($".NET error unpacking: {e.Message}");
                    return(null);
                }
            }
            foreach (byte b in restbank)
            {
                U.WriteByte(b);
            }
            var result = UPWrite.ToArray();

            MS.Close();
            U.Close();
            return(result); // temp
        }
Ejemplo n.º 2
0
        }                 //End Function

        public void ReadFromBytes(byte[] b)
        {
            // This is a new approach for GINI.

            // The BlitzMax variant doesn't even support it.

            // This has not yet been tested as there is no writer for it yet.

            // I just need to find time to fully complete this experiment XD

            //g.init1st()



            var bt = new QuickStream(new MemoryStream(b)); //bytes.NewReader(b)

            var head = bt.ReadString(5);

            if (head != "GINI\x1a")
            {
                throw new Exception("The buffer read is not a GINI binary");
            }

            while (true)
            {
                var tag = bt.ReadByte();

                switch (tag)
                {
                case 1:     // Basic Variable

                    var k = bt.ReadString();

                    var v = bt.ReadString();

                    D(k, v);

                    break;

                case 2:     // Create list

                    var cklst = bt.ReadString();

                    CL(cklst, false);

                    break;

                case 3:     // Add to list

                    var kl = bt.ReadString();

                    var kv = bt.ReadString();

                    Add(kl, kv);

                    break;

                case 4:     // link list

                    var list2link = bt.ReadString();

                    var list2link2 = bt.ReadString();

                    list2link = list2link2;

                    break;

                case 255:

                    bt.Close();

                    return;

                default:

                    throw new Exception("ERROR! Unknown tag: {tag}");
                }
            } // infinite loop
        }     // func