示例#1
0
文件: Bytes.cs 项目: AgentTy/General
        public static IEnumerable <ByteFinderResult> FindBytesBetween(byte[] src, string strFindStart, string strFindEnd, Encoding objEncoder)
        {
            ByteFinderResult objLastResult = new ByteFinderResult();

            while (objLastResult.Index > -1)
            {
                objLastResult = FindBytesOnce(src, objLastResult.Index + 1, strFindStart, objEncoder);
                var objEndResult = FindBytesOnce(src, objLastResult.Index + objLastResult.Length, strFindEnd, objEncoder);
                if (!objEndResult.Found && strFindEnd == "\r\n")
                {
                    //If end of line is what i'm looking for, understand that the end of the file is also acceptable
                    if (objLastResult.Index + objLastResult.Length + 2 == src.LongLength) //If I'm at end of file
                    {
                        objEndResult.Index  = src.Length;
                        objEndResult.Length = 0;
                    }
                }
                if (objLastResult.Index > -1 && objEndResult.Index > -1)
                {
                    objLastResult.Index  = objLastResult.Index + objLastResult.Length;
                    objLastResult.Length = objEndResult.Index - objLastResult.Index;
                    yield return(objLastResult);
                }
            }
        }
示例#2
0
文件: Bytes.cs 项目: AgentTy/General
        public static ByteFinderResult FindBytesOnceFromEnd(string strOptionalKey, byte[] src, int intStartIndex, string strFind, Encoding objEncoder, int intEndIndex = 0)
        {
            ByteFinderResult objResult = new ByteFinderResult();

            byte[] find = objEncoder.GetBytes(strFind);
            objResult.Length = find.Length;
            objResult.Key    = strOptionalKey;

            int index      = -1;
            int matchIndex = find.Length - 1;

            // handle the complete source array
            for (int i = intStartIndex; i >= intEndIndex; i--)
            {
                if (src[i] == find[matchIndex])
                {
                    if (matchIndex == 0)
                    {
                        index = i;
                        break;
                    }
                    matchIndex--;
                }
                else
                {
                    matchIndex = find.Length - 1;
                }
            }
            objResult.Index = index;
            return(objResult);
        }
示例#3
0
文件: Bytes.cs 项目: AgentTy/General
        public static byte[] ReplaceBytes(byte[] src, ByteFinderResult objFind, byte[] repl)
        {
            if (repl == null && !objFind.EraseIfNullValue)
            {
                return(src); //Bugs can be caused by replacing with NULL, namely when setting key MIME Headers, so I return unchanged here.
            }
            else if (repl == null)
            {
                repl = new byte[0];
            }

            //Add Prefix/Suffix to replacement value
            if (objFind.Prefix != null && objFind.Suffix != null)
            {
                repl = CombineBytes(objFind.Prefix, repl, objFind.Suffix);
            }
            else if (objFind.Prefix != null)
            {
                repl = CombineBytes(objFind.Prefix, repl);
            }
            else if (objFind.Suffix != null)
            {
                repl = CombineBytes(repl, objFind.Suffix);
            }

            byte[] dst = null;
            if (objFind.Found)
            {
                int intFindLength = objFind.Length;
                if (objFind.OneTimeLength != null)
                {
                    intFindLength         = (int)objFind.OneTimeLength;
                    objFind.OneTimeLength = null;
                }
                dst = new byte[src.Length - intFindLength + repl.Length];
                // before found array
                Buffer.BlockCopy(src, 0, dst, 0, objFind.Index);
                // repl copy
                Buffer.BlockCopy(repl, 0, dst, objFind.Index, repl.Length);
                // rest of src array
                Buffer.BlockCopy(
                    src,
                    objFind.Index + intFindLength,
                    dst,
                    objFind.Index + repl.Length,
                    src.Length - (objFind.Index + intFindLength));
            }
            else if (objFind.InsertIfNotFound)
            {
                dst = InsertBytes(src, repl);
            }
            return(dst);
        }
示例#4
0
文件: Bytes.cs 项目: AgentTy/General
        public static IEnumerable <ByteFinderResult> FindBytes(string strOptionalKey, byte[] src, string strFind, Encoding objEncoder)
        {
            ByteFinderResult objLastResult = new ByteFinderResult();

            while (objLastResult.Index > -1 && (objLastResult.Index + 1 < src.Length))
            {
                objLastResult = FindBytesOnce(strOptionalKey, src, objLastResult.Index + 1, strFind, objEncoder);
                if (objLastResult.Index > -1)
                {
                    yield return(objLastResult);
                }
            }
        }
示例#5
0
文件: Bytes.cs 项目: AgentTy/General
        public ByteFinderResult Clone()
        {
            ByteFinderResult objNew = new ByteFinderResult();

            objNew.Key = this.Key;
            //objNew.ReplacementValue = this.ReplacementValue; //Let the value stay Null
            objNew.Index            = this.Index;
            objNew.Length           = this.Length;
            objNew.OneTimeLength    = this.OneTimeLength;
            objNew.InsertIfNotFound = this.InsertIfNotFound;
            objNew.EraseIfNullValue = this.EraseIfNullValue;
            objNew.Prefix           = this.Prefix;
            objNew.Suffix           = this.Suffix;
            return(objNew);
        }
示例#6
0
文件: Bytes.cs 项目: AgentTy/General
        public static IEnumerable <ByteFinderResult> FindBytesBetween(string strOptionalKey, byte[] src, string strFindStart, string strFindEnd, Encoding objEncoder)
        {
            ByteFinderResult objLastResult = new ByteFinderResult();

            while (objLastResult.Index > -1 && (objLastResult.Index + 1 < src.Length))
            {
                objLastResult = FindBytesOnce(strOptionalKey, src, objLastResult.Index + 1, strFindStart, objEncoder);
                var objEndResult = FindBytesOnce(strOptionalKey, src, objLastResult.Index + 1, strFindEnd, objEncoder);
                if (objLastResult.Index > -1 && objEndResult.Index > -1)
                {
                    objLastResult.Index  = objLastResult.Index + objLastResult.Length;
                    objLastResult.Length = objEndResult.Index - objLastResult.Index;
                    yield return(objLastResult);
                }
            }
        }
示例#7
0
文件: Bytes.cs 项目: AgentTy/General
        public static ByteFinderResult FindLineStartingWith(string strOptionalKey, byte[] src, int intStartIndex, string strFind, Encoding objEncoder)
        {
            ByteFinderResult objResult = new ByteFinderResult();

            byte[] bytNewLine = objEncoder.GetBytes("\r\n");
            byte[] find       = objEncoder.GetBytes(strFind);
            objResult.Key = strOptionalKey;

            int index      = -1;
            int length     = 0;
            int matchIndex = 0;

            // handle the complete source array
            for (int i = intStartIndex; i < src.Length; i++)
            {
                if (src[i] == find[matchIndex])
                {
                    if (matchIndex == (find.Length - 1))
                    {
                        index = i - matchIndex;
                        //Count to the end of the line
                        length = find.Length;
                        while (src[i] != bytNewLine[bytNewLine.Length - 1])
                        {
                            i++;
                            length++;
                        }
                        length = length - bytNewLine.Length; //Subtract the NewLine constant
                        break;
                    }
                    matchIndex++;
                }
                else
                {
                    matchIndex = 0;
                    //Skip to the next line
                    while (src[i] != bytNewLine[bytNewLine.Length - 1] && i < src.Length - 1)
                    {
                        i++;
                    }
                }
            }
            objResult.Index  = index;
            objResult.Length = length;
            return(objResult);
        }
示例#8
0
文件: Bytes.cs 项目: AgentTy/General
 public static byte[] ReplaceBytes(byte[] src, ByteFinderResult objFind, string strReplace, Encoding objEncoder)
 {
     return(ReplaceBytes(src, objFind, objEncoder.GetBytes(strReplace)));
 }