Split a byte array at a delimiter

前端 未结 6 2024
遥遥无期
遥遥无期 2021-01-02 15:49

I\'m having a bit of an issue and, the other questions here didn\'t help me much.

I am a security student and I am trying to write a crypter for a project. For those

6条回答
  •  死守一世寂寞
    2021-01-02 16:16

    I know I'm really, really late to the party, but... You can of course modify this to return a List easily if preferred. I left comments/writelines in case it would be helpful... This may not be the most optimal/optimized code but works well for my specific use case and I thought I would share.

        public static byte[][] SplitBytesByDelimiter(byte[] data, byte delimiter)
        {
            if (data == null) throw new ArgumentNullException(nameof(data));
            if (data.Length < 1) return null;
    
            List retList = new List();
    
            int start = 0;
            int pos = 0;
            byte[] remainder = null; // in case data found at end without terminating delimiter
    
            while (true)
            {
                // Console.WriteLine("pos " + pos + " start " + start);
                if (pos >= data.Length) break;
    
                if (data[pos] == delimiter)
                {
                    // Console.WriteLine("delimiter found at pos " + pos + " start " + start);
    
                    // separator found
                    if (pos == start)
                    {
                        // Console.WriteLine("first char is delimiter, skipping");
                        // skip if first character is delimiter
                        pos++;
                        start++;
                        if (pos >= data.Length)
                        {
                            // last character is a delimiter, yay!
                            remainder = null;
                            break;
                        }
                        else
                        {
                            // remainder exists
                            remainder = new byte[data.Length - start];
                            Buffer.BlockCopy(data, start, remainder, 0, (data.Length - start));
                            continue;
                        }
                    }
                    else
                    {
                        // Console.WriteLine("creating new byte[] at pos " + pos + " start " + start);
                        byte[] ba = new byte[(pos - start)];
                        Buffer.BlockCopy(data, start, ba, 0, (pos - start));
                        retList.Add(ba);
    
                        start = pos + 1;
                        pos = start;
    
                        if (pos >= data.Length)
                        {
                            // last character is a delimiter, yay!
                            remainder = null;
                            break;
                        }
                        else
                        {
                            // remainder exists
                            remainder = new byte[data.Length - start];
                            Buffer.BlockCopy(data, start, remainder, 0, (data.Length - start));
                        }
                    }
                }
                else
                {
                    // payload character, continue;
                    pos++;
                }
            }
    
            if (remainder != null)
            {
                // Console.WriteLine("adding remainder");
                retList.Add(remainder);
            }
    
            return retList.ToArray();
        }
    

提交回复
热议问题