Beispiel #1
0
        public string GetKeyValue(MdictHelper.Tuple <long, long, string> findKey)
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = RecordListStartPosition;

                long h = 0, start = 0, ofset = 0;
                var  findBlock = new MdictHelper.Tuple <long, long>(-1, -1);
                for (int kj = 0; kj < RecordBlockInfoList.Count; kj++)
                {
                    h     += RecordBlockInfoList[kj].Value2;
                    ofset += RecordBlockInfoList[kj].Value1;

                    if (h > findKey.Value1)
                    {
                        findBlock = RecordBlockInfoList[kj];
                        start     = RecordBlockInfoList[kj].Value2 - (h - findKey.Value1);

                        ofset -= RecordBlockInfoList[kj].Value1;

                        break;
                    }
                }

                mdictStream.BaseStream.Position += ofset;

                var findKeyBuffer = new byte[findBlock.Value1];
                mdictStream.BaseStream.Read(findKeyBuffer, 0, findKeyBuffer.Length);

                string findKeyCompressedText = BitConverter.ToString(findKeyBuffer).Replace("-", "");

                string findKeyDecompressedText = MdictHelper.UnZipZlibHexString(findKeyCompressedText.Substring(16));

                Encoding encoder = Headers["Encoding"] == "UTF-16" ? Encoding.Unicode : Encoding.UTF8;

                string findKeyText = encoder.GetString(MdictHelper.HexStringToByteArray(findKeyDecompressedText.Replace("-", "").Substring((int)start * 2, ((int)(findKey.Value2)) * 2))).Replace("\0", "");

                var pattern = new Regex(@"[`](?<id>\d*)[`]",
                                        RegexOptions.Compiled | RegexOptions.IgnoreCase);
                MatchCollection _q = pattern.Matches(findKeyText);
                foreach (Match qm in _q)
                {
                    int id = int.Parse(qm.Groups["id"].Value);
                    findKeyText = findKeyText.Replace("`" + id + "`", StyleSheets.Single(p => p.Key == id).Value);
                }

                mdictStream.Close();

                if (!findKeyText.StartsWith("@@@LINK="))
                {
                    return("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\" <html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" ><style type=\"text/css\">body{background-color:;color:;font-family:\"\";}</style></head><body>" + findKeyText + "</body></html>");
                }
                return(findKeyText);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #2
0
        public bool GetRecordBlocksInfo()
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = RecordBlockInfoStartPosition;

                RecordBlockInfoList = new List <MdictHelper.Tuple <long, long> >();

                int numRecordBlocks     = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int numEntries          = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int recordBlockInfoSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int recordBlockSize     = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);

                for (int ml = 0; ml < numRecordBlocks; ml++)
                {
                    long compressedSize   = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                    long decompressedSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);

                    RecordBlockInfoList.Add(new MdictHelper.Tuple <long, long>(compressedSize, decompressedSize));
                }

                RecordListStartPosition = mdictStream.BaseStream.Position;

                mdictStream.Close();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #3
0
        public byte[] GetKeyValue(MdictHelper.Tuple <long, long, string> findKey)
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = RecordListStartPosition;

                long h = 0, start = 0, ofset = 0;
                var  findBlock = new MdictHelper.Tuple <long, long>(-1, -1);
                for (int kj = 0; kj < RecordBlockInfoList.Count; kj++)
                {
                    h     += RecordBlockInfoList[kj].Value2;
                    ofset += RecordBlockInfoList[kj].Value1;

                    if (h > findKey.Value1)
                    {
                        findBlock = RecordBlockInfoList[kj];
                        start     = RecordBlockInfoList[kj].Value2 - (h - findKey.Value1);

                        ofset -= RecordBlockInfoList[kj].Value1;

                        break;
                    }
                }

                mdictStream.BaseStream.Position += ofset;

                var findKeyBuffer = new byte[findBlock.Value1];
                mdictStream.BaseStream.Read(findKeyBuffer, 0, findKeyBuffer.Length);

                string findKeyCompressedText = BitConverter.ToString(findKeyBuffer).Replace("-", "");

                string findKeyDecompressedText = MdictHelper.UnZipZlibHexString(findKeyCompressedText.Substring(16));

                mdictStream.Close();

                return(MdictHelper.HexStringToByteArray(findKeyDecompressedText.Replace("-", "").Substring((int)start * 2, (int)findKey.Value2 * 2)));
            }
            catch
            {
                return(null);
            }
        }
Beispiel #4
0
        public bool IgnoreKeys()
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = KeyListStartPosition;

                int keyBlockInfoCount      = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int keyBlockInfoEntryCount = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);

                if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0)
                {
                    MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                }

                int keyBlockInfoSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int keyBlockSize     = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);

                if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0)
                {
                    mdictStream.BaseStream.Read(new byte[4], 0, 4);
                }

                mdictStream.BaseStream.Position += keyBlockInfoSize;

                mdictStream.BaseStream.Position += keyBlockSize;

                RecordBlockInfoStartPosition = mdictStream.BaseStream.Position;

                mdictStream.Close();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #5
0
        public bool GetKeys()
        {
            try
            {
                var mdictStream = new StreamReader(Filename);
                mdictStream.BaseStream.Position = KeyListStartPosition;

                //Get KeyBlockInfoList
                var keyBlockInfoList = new List <MdictHelper.Tuple <long, long> >();

                int keyBlockInfoCount      = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int keyBlockInfoEntryCount = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);

                if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0)
                {
                    MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                }

                int keyBlockInfoSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);
                int keyBlockSize     = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth);

                if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0)
                {
                    mdictStream.BaseStream.Read(new byte[4], 0, 4);
                }

                var keyBlockInfoBuffer = new byte[keyBlockInfoSize];
                mdictStream.BaseStream.Read(keyBlockInfoBuffer, 0, keyBlockInfoBuffer.Length);

                try
                {
                    var keyBlockInfoText = BitConverter.ToString(keyBlockInfoBuffer);
                    if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0)
                    {
                        keyBlockInfoText =
                            MdictHelper.UnZipZlibHexString(keyBlockInfoText.Replace("-", "").Substring(NumberWidth * 2))
                            .Replace("-", "");
                    }

                    int i = 0, byteWidth = 2, textTerm = 1;
                    if (float.Parse(Headers["GeneratedByEngineVersion"]) < 2.0)
                    {
                        byteWidth = 1;
                        textTerm  = 0;
                    }

                    while (i < keyBlockInfoText.Length)
                    {
                        i += NumberWidth * 2;

                        int textHeadSize = int.Parse(keyBlockInfoText.Substring(i, byteWidth * 2),
                                                     NumberStyles.HexNumber);

                        i += byteWidth * 2;

                        if (Headers["Encoding"] != "UTF-16")
                        {
                            i += (textHeadSize * 2) + (textTerm * 2);
                        }
                        else
                        {
                            i += ((textHeadSize * 2) + (textTerm * 2)) * 2;
                        }

                        int textTailSize = int.Parse(keyBlockInfoText.Substring(i, byteWidth * 2),
                                                     NumberStyles.HexNumber);

                        i += byteWidth * 2;

                        if (Headers["Encoding"] != "UTF-16")
                        {
                            i += (textTailSize * 2) + (textTerm * 2);
                        }
                        else
                        {
                            i += ((textTailSize * 2) + (textTerm * 2)) * 2;
                        }

                        int keyBlockCompressedSize = int.Parse(keyBlockInfoText.Substring(i, NumberWidth * 2),
                                                               NumberStyles.HexNumber);
                        i += NumberWidth * 2;
                        int keyBlockDecompressedSize = int.Parse(keyBlockInfoText.Substring(i, NumberWidth * 2),
                                                                 NumberStyles.HexNumber);

                        i += NumberWidth * 2;

                        keyBlockInfoList.Add(new MdictHelper.Tuple <long, long>(keyBlockCompressedSize,
                                                                                keyBlockDecompressedSize));
                    }
                }
                catch { }

                //Get KeyList
                var keyBlockBuffer = new byte[keyBlockSize];
                mdictStream.BaseStream.Read(keyBlockBuffer, 0, keyBlockBuffer.Length);

                string keyBlockText = BitConverter.ToString(keyBlockBuffer).Replace("-", "");

                //KeyBlockInfo not exist
                IdxBlockInfoList = new List <MdictHelper.Idx>();
                if (keyBlockInfoList.Count == 0)
                {
                    var keyBlockCompressed = new string[1];

                    switch (keyBlockText.Substring(0, 8))
                    {
                    case "00000000":

                        keyBlockCompressed = new[] { keyBlockText };

                        break;

                    case "01000000":
                        break;

                    case "02000000":

                        var stringSeparators = new[] { "02000000" };
                        keyBlockCompressed = keyBlockText.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);

                        break;
                    }

                    int i = 0;
                    foreach (var kb in keyBlockCompressed)
                    {
                        string keyBlock = MdictHelper.UnZipZlibHexString(kb.Substring(8)).Replace("-", "");

                        var _kbs = MdictHelper.SplitUncompressesBlock(keyBlock, Headers["Encoding"]);
                        var idx  = new MdictHelper.Idx()
                        {
                            Id = i++, FirstEntry = _kbs.Min(p => p.Value2), LastEntry = _kbs.Max(p => p.Value2)
                        };
                        foreach (var _kb in _kbs)
                        {
                            idx.Entries.Add(new MdictHelper.Tuple <long, long, string>(_kb.Value1, -1, _kb.Value2));
                        }
                        IdxBlockInfoList.Add(idx);
                    }
                }
                else
                {
                    int l = 0, i = 0;
                    foreach (var kbi in keyBlockInfoList)
                    {
                        int st = l;
                        int ed = l + ((int)kbi.Value1 * 2);

                        switch (keyBlockText.Substring(0, 8))
                        {
                        case "00000000":
                            var _qf = MdictHelper.SplitUncompressesBlock(keyBlockText.Substring(st + (NumberWidth * 2), ed - (st + (NumberWidth * 2))), Headers["Encoding"]);
                            var idx = new MdictHelper.Idx()
                            {
                                Id = i++, FirstEntry = _qf.Min(p => p.Value2), LastEntry = _qf.Max(p => p.Value2)
                            };
                            foreach (var kj in _qf)
                            {
                                idx.Entries.Add(new MdictHelper.Tuple <long, long, string>(kj.Value1, -1, kj.Value2));
                            }
                            IdxBlockInfoList.Add(idx);
                            break;

                        case "01000000":
                            break;

                        case "02000000":
                            string keyBlock = MdictHelper.UnZipZlibHexString(keyBlockText.Substring(st + (NumberWidth * 2), ed - (st + (NumberWidth * 2)))).Replace("-", "");

                            var _qg  = MdictHelper.SplitUncompressesBlock(keyBlock, Headers["Encoding"]);
                            var idx_ = new MdictHelper.Idx()
                            {
                                Id = i++, FirstEntry = _qg.Min(p => p.Value2), LastEntry = _qg.Max(p => p.Value2)
                            };
                            foreach (var kj in _qg)
                            {
                                idx_.Entries.Add(new MdictHelper.Tuple <long, long, string>(kj.Value1, -1, kj.Value2));
                            }
                            IdxBlockInfoList.Add(idx_);
                            break;
                        }

                        l += ((int)kbi.Value1 * 2);
                    }
                }

                long lastPosition = -1;
                for (int i = IdxBlockInfoList.Count - 1; i >= 0; i--)
                {
                    IdxBlockInfoList[i].Entries.Last().Value2 = lastPosition != -1 ? lastPosition - IdxBlockInfoList[i].Entries.Last().Value1 : -1;
                    for (int j = IdxBlockInfoList[i].Entries.Count - 1 - 1; j >= 0; j--)
                    {
                        IdxBlockInfoList[i].Entries[j].Value2 = IdxBlockInfoList[i].Entries[j + 1].Value1 - IdxBlockInfoList[i].Entries[j].Value1;
                    }
                    lastPosition = IdxBlockInfoList[i].Entries.First().Value1;
                }

                RecordBlockInfoStartPosition = mdictStream.BaseStream.Position;

                mdictStream.Close();

                return(true);
            }
            catch
            {
                return(false);
            }
        }