Ejemplo n.º 1
0
        public override byte[] Expand(byte[] inputbuffer, int realsize)
        {
            byte[]      ret = new byte[realsize];
            int         pos = 0;
            QuickStream bi  = QuickStream.StreamFromBytes(inputbuffer);

            bi.ReadByte(); // First byte had to be ignored... A little issue that came up in the first draft... silly me!
            while (!bi.EOF)
            {
                byte xbyte = bi.ReadByte();
                byte xkeer = bi.ReadByte();
                for (byte i = 0; i < xkeer; ++i)
                {
                    if (pos >= realsize)
                    {
                        JCR6.JERROR = $"JXSRCCA: Pos {i} expanded over the realsize length {pos}/{realsize}. Is this entry corrupted?";
                        System.Console.WriteLine("ERROR! " + JCR6.JERROR);
                        bi.Close();
                        return(null);
                    }
                    //System.Console.Write(qstr.Chr(xbyte));
                    ret[pos] = xbyte;
                    pos++;
                }
            }
            bi.Close();
            return(ret);
        }
Ejemplo n.º 2
0
        string RL(QuickStream BT, bool trim = true)
        {
            var  r = new StringBuilder();
            byte b = 0;

            while (true)
            {
                if (BT.EOF)
                {
                    break;
                }
                b = BT.ReadByte();
                if (b == 10)
                {
                    break;
                }
                if (b != 13)
                {
                    r.Append((char)b);
                }
            }
            if (trim)
            {
                return(r.ToString().Trim());
            }
            return(r.ToString());
        }
Ejemplo n.º 3
0
        static string GetLine(QuickStream bin)
        {
            var ret   = "";
            var wt    = 0;
            var allow = true;

            while (true)
            {
                var x = bin.ReadByte();
                if (x == 10 && qstr.Left(ret, 1) == "#")
                {
                    ret = "";
                }
                if (x == 10 && ret.Trim() != "")
                {
                    break;
                }
                if (x == 10)
                {
                    allow = true;
                }
                if (allow)
                {
                    ret += qstr.Chr((byte)x);
                }
                if (bin.EOF)
                {
                    break;
                }
                wt++;
                if (wt > 10000)
                {
                    //Console.WriteLine($"Is a string loading cycle this long normal?\n{ret.Length}>{ret}");
                    Process(bin);
                    wt = 0;
                }
#if MaxLine
                if (ret.Length > MaxLine)
                {
                    Console.WriteLine($"WARNING! There's a line exceeding the maximum allowed size of {MaxLine} characters! Gonna ignore it!");
                    allow = false;
                    ret   = "";
                }
                    #endif
            }
            Process(bin);
            return(ret.Trim());
        }
Ejemplo n.º 4
0
        public override TJCRDIR Dir(string file)
        {
            var         ret = new TJCRDIR();
            QuickStream bt  = null;

            try {
                bt = QuickStream.ReadFile(file);
                if (bt.ReadString(header.Length) != header)
                {
                    throw new Exception("AR archive with incorrect header");
                }
                while (!bt.EOF)
                {
                    var e = new TJCREntry();
                    e.Entry = bt.ReadNullTerminatedString(16).Trim(); if (qstr.Suffixed(e.Entry, "/"))
                    {
                        e.Entry = qstr.Left(e.Entry, e.Entry.Length - 1);
                    }
                    Chat($"File: \"{e.Entry}\"");
                    e.dataint["__TimeStamp"] = qstr.ToInt(bt.ReadNullTerminatedString(12)); Chat($"TimeStamp: {e.dataint["__TimeStamp"]}!");
                    Chat($"OwnerID:  {bt.ReadNullTerminatedString(6)}"); // OwnerID -- Not relevant or supported by JCR6 (yet)
                    Chat($"GroupID:  {bt.ReadNullTerminatedString(6)}"); // GroupID -- Not relevant or supported by JCR6 (yet)
                    Chat($"FileMode: {bt.ReadNullTerminatedString(8)}"); // Permisisons -- Not yet relevant, but might be added later!
                    e.Size           = qstr.ToInt(bt.ReadNullTerminatedString(10)); Chat($"Size: {e.Size}");
                    e.CompressedSize = e.Size;
                    e.Storage        = "Store";
                    e.MainFile       = file;
                    //if (bt.ReadString(2) != ending) throw new Exception("End of file record is not 600A");
                    var e1 = bt.ReadByte();
                    var e2 = bt.ReadByte();
                    if (e1 != 0x60)
                    {
                        throw new Exception($"0x60 expected, but got {e1.ToString("X2")}");
                    }
                    if (e2 != 0x0a)
                    {
                        throw new Exception($"0x0a expected, but got {e1.ToString("X2")}");
                    }
                    ret.Entries[e.Entry.ToUpper()] = e;
                    e.Offset     = (int)bt.Position;
                    bt.Position += e.Size;
                    byte b;
                    do
                    {
                        b = bt.ReadByte();
                    } while (b == 10);
                    bt.Position--;
                }
                return(ret);
            } catch (Exception NETERROR) {
#if DEBUG
                JCR6.JERROR = $"{NETERROR.Message}\n{NETERROR.StackTrace}";
#else
                JCR6.JERROR = NETERROR.Message;
#endif
                return(null);
            } finally {
                if (bt != null)
                {
                    bt.Close();
                }
            }
        }
Ejemplo n.º 5
0
        public override TJCRDIR Dir(string file)
        {
            QuickStream BT = null;

            try {
                BT = QuickStream.ReadFile(file);
                var    ret = new TJCRDIR();
                string s;
                do
                {
                    if (BT.EOF)
                    {
                        throw new Exception("JQL heading not found!");
                    }
                    s = RL(BT);
                } while (s == "" || qstr.Prefixed(s, "#"));
                if (s != "JQL")
                {
                    throw new Exception("JQL not properly headed!");
                }
                var optional = true;
                var author   = "";
                var notes    = "";
                while (!BT.EOF)
                {
                    s = RL(BT);
                    var c = new QP(s);
                    if (s != "" && (!qstr.Prefixed(s, "#")))
                    {
                        switch (c.commando)
                        {
                        case "REQUIRED":
                        case "REQ":
                            optional = false;
                            break;

                        case "OPTIONAL":
                        case "OPT":
                            optional = true;
                            break;

                        case "PATCH": {
                            var to = c.parameter.IndexOf('>');
                            if (to < 0)
                            {
                                var p = JCR6.Dir(c.parameter);
                                if (p == null)
                                {
                                    if (optional)
                                    {
                                        break;
                                    }
                                    throw new Exception($"Patch error {JCR6.JERROR}");
                                }
                                ret.Patch(p);
                            }
                            else
                            {
                                var rw = c.parameter.Substring(0, to).Trim().Replace("\\", "/");
                                var tg = c.parameter.Substring(to + 1).Trim().Replace("\\", "/");
                                var p  = JCR6.Dir(rw);
                                if (p == null)
                                {
                                    if (optional)
                                    {
                                        break;
                                    }
                                    throw new Exception($"Patch error {JCR6.JERROR}");
                                }
                                ret.Patch(p, tg);
                            }
                            break;
                        }

                        case "AUTHOR":
                        case "AUT":
                            author = c.parameter;
                            break;

                        case "NOTES":
                        case "NTS":
                            notes = c.parameter;
                            break;

                        case "RAW": {
                            var p  = c.parameter.IndexOf('>');
                            var rw = c.parameter.Replace("\\", "/");
                            var tg = rw;
                            if (p >= 0)
                            {
                                rw = c.parameter.Substring(0, p).Trim().Replace("\\", "/");
                                tg = c.parameter.Substring(p + 1).Trim().Replace("\\", "/");
                            }
                            if (tg.Length > 1 && tg[1] == ':')
                            {
                                tg = tg.Substring(2);
                            }
                            while (tg[1] == '/')
                            {
                                tg = tg.Substring(1);
                            }
                            if (rw == "")
                            {
                                throw new Exception("RAW no original");
                            }
                            if (tg == "")
                            {
                                throw new Exception("RAW no target");
                            }
                            if (!File.Exists(rw))
                            {
                                if (optional)
                                {
                                    break;
                                }
                                throw new Exception($"Required raw file \"{rw}\" doesn't exist!");
                            }
                            var e = new TJCREntry();
                            e.Entry                   = tg;
                            e.MainFile                = rw;
                            e.Storage                 = "Store";
                            e.Offset                  = 0;
                            e.Size                    = (int)new FileInfo(rw).Length;
                            e.CompressedSize          = e.Size;
                            e.Notes                   = notes;
                            e.Author                  = author;
                            ret.Entries[tg.ToUpper()] = e;
                            break;
                        }

                        case "TEXT":
                        case "TXT": {
                            var tg = c.parameter.Trim().Replace("\\", "/");
                            if (tg.Length > 1 && tg[1] == ':')
                            {
                                tg = tg.Substring(2);
                            }
                            while (tg[1] == '/')
                            {
                                tg = tg.Substring(1);
                            }
                            if (tg == "")
                            {
                                throw new Exception("TEXT no target");
                            }
                            var e   = new TJCREntry();
                            var buf = new byte[5];
                            e.Entry    = tg;
                            e.MainFile = file;
                            e.Storage  = "Store";
                            e.Offset   = (int)BT.Position;
                            e.Notes    = notes;
                            e.Author   = author;
                            do
                            {
                                if (BT.EOF)
                                {
                                    throw new Exception("Unexpected end of file (TXT Block not ended)");
                                }
                                for (int i = 0; i < 4; i++)
                                {
                                    buf[i] = buf[i + 1];
                                }
                                buf[4] = BT.ReadByte();
                                //Console.WriteLine(Encoding.UTF8.GetString(buf, 0, buf.Length));
                            } while (Encoding.UTF8.GetString(buf, 0, buf.Length) != "@END@");
                            RL(BT);
                            e.Size                    = (int)(BT.Position - 7) - e.Offset;
                            e.CompressedSize          = e.Size;
                            ret.Entries[tg.ToUpper()] = e;
                            break;
                        }

                        case "COMMENT":
                        case "CMT": {
                            if (c.parameter == "")
                            {
                                throw new Exception("Comment without a name");
                            }
                            var cmt = new StringBuilder("");
                            var l   = "";
                            do
                            {
                                if (BT.EOF)
                                {
                                    throw new Exception("Unexpected end of file (COMMENT block not ended)");
                                }
                                l = RL(BT, false);
                                if (l.Trim() != "@END@")
                                {
                                    cmt.Append($"{l}\n");
                                }
                            } while (l.Trim() != "@END@");
                            ret.Comments[c.parameter] = cmt.ToString();
                            break;
                        }

                        case "IMPORT":
                            ret.PatchFile(c.parameter);
                            break;

                        case "END":
                            return(ret);

                        default: throw new Exception($"Unknown instruction! {c.commando}");
                        }
                    }
                }
                return(ret);
            } catch (Exception e) {
                JCR6.JERROR = $"JQL error: {e.Message}";
#if DEBUG
                Console.WriteLine(e.StackTrace);
#endif
                return(null);
            } finally {
                if (BT != null)
                {
                    BT.Close();
                }
            }
        }