示例#1
0
 internal LosWriter()
 {
     _charBuffer = (char[])_charBufferAllocator.GetBuffer();
     _size       = _charBuffer.Length;
     _freePos    = 0;
     _recyclable = true;
 }
示例#2
0
        /// <devdoc>
        ///    <para>Deserializes a LOS formatted object from a string.</para>
        /// </devdoc>
        public object Deserialize(string input)
        {
#if NO_BASE64
            char[] data = input.ToCharArray();
#else
            byte[] dataBytes = Convert.FromBase64String(input);

            int dataLength = -1;
            if (EnableViewStateMac)
            {
                try {
                    dataBytes = MachineKeySection.GetDecodedData(dataBytes, _macKey, 0, dataBytes.Length, ref dataLength);
                }
                catch (Exception e) {
                    PerfCounters.IncrementCounter(AppPerfCounter.VIEWSTATE_MAC_FAIL);
                    ViewStateException.ThrowMacValidationError(e, input);
                }
            }

            if (dataLength == -1)
            {
                dataLength = dataBytes.Length;
            }

            char[] data = EncodingInstance.GetChars(dataBytes, 0, dataLength);
#endif


            // clear or allocate name and type tables.
            //
            if (_deserializedTypeTable == null)
            {
                _deserializedTypeTable      = new ArrayList();
                _deserializedConverterTable = new ListDictionary();
            }
            else
            {
                _deserializedTypeTable.Clear();
                _deserializedConverterTable.Clear();
            }

            _builder    = (char[])_charBufferAllocator.GetBuffer();
            _recyclable = true;

            // DeserializeValueInternal is recursive, so we just kick this off
            // starting at 0
            _current             = 0;
            _deserializationData = data;
            object ret = DeserializeValueInternal();

            if (_recyclable)
            {
                _charBufferAllocator.ReuseBuffer(_builder);
            }

            return(ret);
        }
        /// <include file='doc\LOSFormatter.uex' path='docs/doc[@for="LosFormatter.Deserialize2"]/*' />
        /// <devdoc>
        ///    <para>Deserializes a LOS formatted object from a string.</para>
        /// </devdoc>
        public object Deserialize(string input)
        {
#if NO_BASE64
            char[] data = input.ToCharArray();
#else
            byte[] dataBytes = Convert.FromBase64String(input);

            int dataLength = -1;
            if (EnableViewStateMac)
            {
                try {
                    dataBytes = MachineKey.GetDecodedData(dataBytes, _macKey, 0, dataBytes.Length, ref dataLength);
                }
                catch {
                    throw new HttpException(HttpRuntime.FormatResourceString(SR.Invalid_Viewstate));
                }
            }

            if (dataLength == -1)
            {
                dataLength = dataBytes.Length;
            }

            char[] data = EncodingInstance.GetChars(dataBytes, 0, dataLength);
#endif


            // clear or allocate name and type tables.
            //
            if (_deserializedTypeTable == null)
            {
                _deserializedTypeTable      = new ArrayList();
                _deserializedConverterTable = new ListDictionary();
            }
            else
            {
                _deserializedTypeTable.Clear();
                _deserializedConverterTable.Clear();
            }

            _builder    = (char[])_charBufferAllocator.GetBuffer();
            _recyclable = true;

            // DeserializeValueInternal is recursive, so we just kick this off
            // starting at 0
            _current             = 0;
            _deserializationData = data;
            object ret = DeserializeValueInternal();

            if (_recyclable)
            {
                _charBufferAllocator.ReuseBuffer(_builder);
            }

            return(ret);
        }
示例#4
0
        internal /*public*/ void CompleteTransforms(TextWriter output, bool enableMac, byte[] macKey)
        {
            int len = 0;

            // convert to bytes
            if (_recyclable)
            {
                // still using the original recyclable char buffer
                // -- can use recyclable byte buffer
                _byteBuffer = (byte[])_byteBufferAllocator.GetBuffer();

                if (_freePos > 0)
                {
                    len = Encoding.UTF8.GetBytes(_charBuffer, 0, _freePos, _byteBuffer, 0);
                }

                // do the mac encoding if requested
                if (enableMac)
                {
                    // the size of the output array depends on the key length and encryption type
                    // so we can't use the recyclable buffers after this
                    byte[] data       = MachineKeySection.GetEncodedData(_byteBuffer, macKey, 0, ref len);
                    string serialized = Convert.ToBase64String(data, 0, len);
                    output.Write(serialized);
                }
                else
                {
                    char[] base64chars = (char[])_charBufferAllocatorBase64.GetBuffer();
                    len = Convert.ToBase64CharArray(_byteBuffer, 0, len, base64chars, 0);
                    output.Write(base64chars, 0, len);
                    _charBufferAllocatorBase64.ReuseBuffer(base64chars);
                }
            }
            else
            {
                _byteBuffer = Encoding.UTF8.GetBytes(_charBuffer, 0, _freePos);


                len = _byteBuffer.Length;
                if (enableMac)
                {
                    _byteBuffer = MachineKeySection.GetEncodedData(_byteBuffer, macKey, 0, ref len);
                }

                string serialized = Convert.ToBase64String(_byteBuffer);
                output.Write(serialized);
            }
        }
 private char[] GetDecodedCharBuffer(Encoding encoding, ref int len)
 {
     if (this._charBuffer == null)
     {
         if (len == 0)
         {
             this._charBuffer = new char[0];
         }
         else if (this._recyclable)
         {
             this._charBuffer = (char[])s_CharBufferAllocator.GetBuffer();
             len = encoding.GetChars(this._byteBuffer, this._offset, len, this._charBuffer, 0);
         }
         else
         {
             this._charBuffer = encoding.GetChars(this._byteBuffer, this._offset, len);
             len = this._charBuffer.Length;
         }
     }
     return(this._charBuffer);
 }