예제 #1
0
        public override UInt32 Serialize(Object instance, Byte[] array, UInt32 offset)
        {
            Object valueAsObject = fieldInfo.GetValue(instance);

            if (valueAsObject == null)
            {
                array[offset]     = 0;
                array[offset + 1] = 0;
                array[offset + 2] = 0;
                array[offset + 3] = 0;
                return(offset + 4);
            }

            Byte[] valueAsArray = (Byte[])valueAsObject;

            array[offset]     = (Byte)(valueAsArray.Length >> 24);
            array[offset + 1] = (Byte)(valueAsArray.Length >> 16);
            array[offset + 2] = (Byte)(valueAsArray.Length >> 8);
            array[offset + 3] = (Byte)(valueAsArray.Length);
            offset           += 4;

            ArrayCopier.Copy(valueAsArray, 0, array, offset, valueAsArray.Length);

            UInt32 valueAsArrayMod4Length = Xdr.UpToNearestMod4((UInt32)valueAsArray.Length);

            for (UInt32 i = (UInt32)valueAsArray.Length; i < valueAsArrayMod4Length; i++)
            {
                array[offset + i] = 0;
            }

            return(offset + valueAsArrayMod4Length);
        }
예제 #2
0
        public String GetLine()
        {
            while (this.nextIndexToCheck < this.dataOffsetLimit)
            {
                if (buffer.array[this.nextIndexToCheck] == '\n')
                {
                    String line = encoding.GetString(buffer.array, (Int32)this.nextStartOfLineOffset, (Int32)
                                                     (this.nextIndexToCheck + ((this.nextIndexToCheck > this.nextStartOfLineOffset && buffer.array[nextIndexToCheck - 1] == '\r') ? -1 : 0) - this.nextStartOfLineOffset));

                    this.nextIndexToCheck++;
                    this.nextStartOfLineOffset = this.nextIndexToCheck;
                    return(line);
                }
                this.nextIndexToCheck++;
            }

            //
            // Move remaining data to the beginning of the buffer
            //
            if (this.nextStartOfLineOffset <= 0 || this.nextStartOfLineOffset >= this.dataOffsetLimit)
            {
                return(null);
            }

            UInt32 copyLength = this.dataOffsetLimit - this.nextStartOfLineOffset;

            ArrayCopier.Copy(buffer.array, this.nextStartOfLineOffset, buffer.array, 0, copyLength);
            this.nextStartOfLineOffset = 0;
            this.nextIndexToCheck      = 0;
            this.dataOffsetLimit       = copyLength;

            return(null);
        }
예제 #3
0
 public override UInt32 Deserialize(Object instance, Byte[] array, UInt32 offset, UInt32 offsetLimit)
 {
     Byte[] value = new Byte[fixedLength];
     ArrayCopier.Copy(array, offset, value, 0, fixedLength);
     fieldInfo.SetValue(instance, value);
     return(offset + fixedLength);
 }
예제 #4
0
        public override UInt32 Serialize(Object instance, Byte[] array, UInt32 offset)
        {
            if (dataLength <= 0)
            {
                return(offset);
            }

            Object valueAsObject = fieldInfo.GetValue(instance);

            if (valueAsObject == null)
            {
                Array.Clear(array, (Int32)offset, (Int32)dataLength);
            }
            else
            {
                Byte[] valueAsArray = (Byte[])valueAsObject;

                if (valueAsArray.Length != dataLength)
                {
                    throw new InvalidOperationException(String.Format("The XdrOpaqueFixedLength length is {0}, but your the byte array for field '{1}' length is {2}",
                                                                      dataLength, fieldInfo.Name, valueAsArray.Length));
                }

                ArrayCopier.Copy(valueAsArray, 0, array, offset, dataLength);
            }

            // Add Padding
            for (UInt32 i = dataLength; i < dataLengthNearestContainingMod4; i++)
            {
                array[i] = 0;
            }

            return(offset + dataLengthNearestContainingMod4);
        }
예제 #5
0
        public override UInt32 Deserialize(Object instance, Byte[] array, UInt32 offset, UInt32 maxOffset)
        {
            Byte[] data = new Byte[dataLength];
            ArrayCopier.Copy(array, offset, data, 0, dataLength);

            fieldInfo.SetValue(instance, data);

            return(offset + dataLengthNearestContainingMod4);
        }
예제 #6
0
 public UInt32 Serialize(Byte[] array, UInt32 offset)
 {
     if (this.bytes == null)
     {
         return(offset);
     }
     ArrayCopier.Copy(this.bytes, this.offset, array, offset, this.length);
     return(offset + this.length);
 }
예제 #7
0
파일: Buf.cs 프로젝트: blinds52/More
 public static void EnsureCapacityCopySomeData <T>(ref T[] array, UInt32 dataLength, UInt32 capacity)
 {
     if (array.Length < capacity)
     {
         T[] newArray = new T[CalculateNewSize((UInt32)array.Length, capacity)];
         ArrayCopier.Copy(array, newArray, dataLength);
         array = newArray;
     }
 }
예제 #8
0
        /*
         * public void ShiftRight(UInt32 shiftAmount, UInt32 offset, UInt32 length)
         * {
         *  // Shift the beginning of the request to compensate for a smaller Content-Length
         *  if (shift > 0)
         *  {
         *      while (offset < length)
         *      {
         *          bytes[
         *      }
         *      var offset = contentLengthOffset - 1;
         *      while (true)
         *      {
         *          builder.bytes[offset + shift] = builder.bytes[offset];
         *          if (offset == 0)
         *              break;
         *          offset--;
         *      }
         *  }
         *  return shift;
         * }
         */

        public void EnsureTotalCapacity(UInt32 capacity)
        {
            if (bytes.Length < capacity)
            {
                UInt32 newLength = (UInt32)bytes.Length * 2U;
                if (newLength < capacity)
                {
                    newLength = capacity;
                }
                var newBytes = new Byte[newLength];
                ArrayCopier.Copy(bytes, newBytes, contentLength);
                bytes = newBytes;
            }
        }
예제 #9
0
        public UInt32 Deserialize(Byte[] array, UInt32 offset, UInt32 offsetLimit)
        {
            UInt32 length = offsetLimit - offset;

            if (length <= 0)
            {
                this.bytes  = null;
                this.offset = 0;
                this.length = 0;
                return(offset);
            }

            this.bytes = new Byte[length];
            ArrayCopier.Copy(array, offset, this.bytes, 0, length);

            this.offset = 0;
            this.length = length;

            return(offset + length);
        }
예제 #10
0
        public override UInt32 Deserialize(Object instance, Byte[] array, UInt32 offset, UInt32 maxOffset)
        {
            UInt32 length = array.BigEndianReadUInt32(offset);

            offset += 4;

            if (length == 0)
            {
                fieldInfo.SetValue(instance, null);
                return(offset);
            }

            Byte[] data = new Byte[length];
            ArrayCopier.Copy(array, offset, data, 0, length);

            fieldInfo.SetValue(instance, data);

            UInt32 lengthMod4 = Xdr.UpToNearestMod4(length);

            return(offset + lengthMod4);
        }
예제 #11
0
        public override UInt32 Serialize(Object instance, Byte[] array, UInt32 offset)
        {
            Slice <Byte> segment = (Slice <Byte>)fieldInfo.GetValue(instance);

            array[offset]     = (Byte)(segment.length >> 24);
            array[offset + 1] = (Byte)(segment.length >> 16);
            array[offset + 2] = (Byte)(segment.length >> 8);
            array[offset + 3] = (Byte)(segment.length);
            offset           += 4;

            ArrayCopier.Copy(segment.array, segment.offset, array, offset, segment.length);

            UInt32 valueAsArrayMod4Length = Xdr.UpToNearestMod4(segment.length);

            for (UInt32 i = segment.length; i < valueAsArrayMod4Length; i++)
            {
                array[offset + i] = 0;
            }

            return(offset + valueAsArrayMod4Length);
        }
예제 #12
0
        public UInt32 GetLineBytes(out UInt32 outOffset)
        {
            while (this.nextIndexToCheck < this.dataOffsetLimit)
            {
                if (buffer.array[this.nextIndexToCheck] == '\n')
                {
                    outOffset = this.nextStartOfLineOffset;
                    UInt32 nextLineLength = this.nextIndexToCheck - this.nextStartOfLineOffset;
                    if (this.nextIndexToCheck > this.nextStartOfLineOffset && buffer.array[nextIndexToCheck - 1] == '\r')
                    {
                        nextLineLength--;
                    }

                    this.nextIndexToCheck++;
                    this.nextStartOfLineOffset = this.nextIndexToCheck;
                    return(nextLineLength);
                }
                this.nextIndexToCheck++;
            }

            //
            // Move remaining data to the beginning of the buffer
            //
            if (this.nextStartOfLineOffset <= 0 || this.nextStartOfLineOffset >= this.dataOffsetLimit)
            {
                outOffset = 0;
                return(0);
            }

            UInt32 copyLength = this.dataOffsetLimit - this.nextStartOfLineOffset;

            ArrayCopier.Copy(buffer.array, this.nextStartOfLineOffset, buffer.array, 0, copyLength);
            this.nextStartOfLineOffset = 0;
            this.nextIndexToCheck      = 0;
            this.dataOffsetLimit       = copyLength;

            outOffset = 0;
            return(0);
        }
예제 #13
0
 public static SByte[] CreateSubSByteArray(this Byte[] bytes, UInt32 offset, UInt32 length)
 {
     SByte[] subArray = new SByte[length];
     ArrayCopier.Copy(bytes, offset, subArray, 0, length);
     return(subArray);
 }
예제 #14
0
 public void Add(Byte[] data)
 {
     buffer.EnsureCapacityCopyAllData(this.dataOffsetLimit + (UInt32)data.Length);
     ArrayCopier.Copy(data, 0, buffer.array, this.dataOffsetLimit, data.Length);
     this.dataOffsetLimit += (UInt32)data.Length;
 }
예제 #15
0
 public override UInt32 Serialize(Object instance, Byte[] array, UInt32 offset)
 {
     Byte[] objBytes = (Byte[])fieldInfo.GetValue(instance);
     ArrayCopier.Copy(objBytes, 0, array, offset, fixedLength);
     return(offset + fixedLength);
 }
예제 #16
0
 public void Append(Byte[] content)
 {
     EnsureTotalCapacity(contentLength + (UInt32)content.Length);
     ArrayCopier.Copy(content, 0, bytes, contentLength, content.Length);
     contentLength += (UInt32)content.Length;
 }
예제 #17
0
 public void Add(Byte[] data, UInt32 offset, UInt32 length)
 {
     buffer.EnsureCapacityCopyAllData(this.dataOffsetLimit + length);
     ArrayCopier.Copy(data, offset, buffer.array, this.dataOffsetLimit, length);
     this.dataOffsetLimit += length;
 }
예제 #18
0
 public void Append(Byte[] content, UInt32 offset, UInt32 length)
 {
     EnsureTotalCapacity(contentLength + length);
     ArrayCopier.Copy(content, offset, bytes, contentLength, length);
     contentLength += length;
 }
예제 #19
0
        // This function is highly tested
        public void HandleData(Socket socket, Byte[] bytes, UInt32 offset, UInt32 offsetLimit)
        {
            switch (state)
            {
            case State.Initial:
            {
                while (offset < offsetLimit)
                {
                    //
                    // Only a few bytes of the length were received
                    //
                    if (offsetLimit - offset < 4)
                    {
                        copiedFramentDataLength = offsetLimit - offset;
                        copiedFragmentData      = new Byte[4];
                        for (int i = 0; i < copiedFramentDataLength; i++)
                        {
                            this.copiedFragmentData[i] = bytes[offset + i];
                        }
                        state = State.PartialLengthReceived;
                        return;
                    }

                    Boolean isLastFragment = (bytes[offset] & 0x80) == 0x80;
                    if (!isLastFragment)
                    {
                        throw new NotSupportedException("Multifragment records are not supported");
                    }

                    Int32 fragmentLength =
                        (0x7F000000 & (bytes[offset] << 24)) |
                        (0x00FF0000 & (bytes[offset + 1] << 16)) |
                        (0x0000FF00 & (bytes[offset + 2] << 8)) |
                        (0x000000FF & (bytes[offset + 3]));

                    offset += 4;

                    UInt32 fragmentBytesAvailable = offsetLimit - offset;

                    if (fragmentBytesAvailable < fragmentLength)
                    {
                        this.copiedFragmentData = new Byte[fragmentLength];
                        ArrayCopier.Copy(bytes, offset, copiedFragmentData, 0, fragmentBytesAvailable);
                        this.copiedFramentDataLength = fragmentBytesAvailable;
                        state = State.LengthReceived;
                        return;
                    }

                    recordHandler(clientString, socket, bytes, offset, (UInt32)fragmentLength);
                    offset += (UInt32)fragmentLength;
                }
            }
                return;

            case State.PartialLengthReceived:
            {
                while (true)
                {
                    copiedFragmentData[copiedFramentDataLength] = bytes[offset];
                    offset++;
                    if (copiedFramentDataLength == 3)
                    {
                        break;
                    }
                    copiedFramentDataLength++;
                    if (offset >= offsetLimit)
                    {
                        return;
                    }
                }

                Boolean isLastFragment = (copiedFragmentData[0] & 0x80) == 0x80;
                if (!isLastFragment)
                {
                    throw new NotSupportedException("Multifragment records are not supported");
                }

                Int32 fragmentLength =
                    (0x7F000000 & (copiedFragmentData[0] << 24)) |
                    (0x00FF0000 & (copiedFragmentData[1] << 16)) |
                    (0x0000FF00 & (copiedFragmentData[2] << 8)) |
                    (0x000000FF & (copiedFragmentData[3]));

                UInt32 fragmentBytesAvailable = offsetLimit - offset;

                if (fragmentBytesAvailable < fragmentLength)
                {
                    this.copiedFragmentData = new Byte[fragmentLength];
                    ArrayCopier.Copy(bytes, offset, copiedFragmentData, 0, fragmentBytesAvailable);
                    this.copiedFramentDataLength = fragmentBytesAvailable;
                    state = State.LengthReceived;
                    return;
                }

                recordHandler(clientString, socket, bytes, offset, (UInt32)fragmentLength);
                offset += (UInt32)fragmentLength;

                state = State.Initial;
                goto case State.Initial;
            }

            case State.LengthReceived:
            {
                UInt32 fragmentBytesAvailable = offsetLimit - offset;
                UInt32 fragmentBytesNeeded    = (UInt32)copiedFragmentData.Length - copiedFramentDataLength;

                if (fragmentBytesAvailable < fragmentBytesNeeded)
                {
                    ArrayCopier.Copy(bytes, offset, copiedFragmentData, copiedFramentDataLength, fragmentBytesAvailable);
                    copiedFramentDataLength += fragmentBytesAvailable;
                    return;
                }
                else
                {
                    ArrayCopier.Copy(bytes, offset, copiedFragmentData, copiedFramentDataLength, fragmentBytesNeeded);

                    recordHandler(clientString, socket, copiedFragmentData, 0, (UInt32)copiedFragmentData.Length);
                    offset += fragmentBytesNeeded;

                    this.copiedFragmentData = null;

                    this.state = State.Initial;
                    goto case State.Initial;
                }
            }
            }
        }