コード例 #1
0
        public override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
        {
            if ((bytes == null) ||
                (chars == null) ||
                (charCount < 0) ||
                (byteCount < 0))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes);
            }
            Contract.EndContractBlock();

            int bytesWritten;

            if (charCount > 0)
            {
                if (byteCount == 0)
                {
                    // Definitely not enough space, early bail
                    EncodingForwarder.ThrowBytesOverflow(this);
                }
                int charactersConsumed;
                if (!EncodingForwarder.TryEncode(chars, charCount, bytes, byteCount, out charactersConsumed, out bytesWritten))
                {
                    // Not all ASCII, GetBytesFallback for remaining conversion
                    bytesWritten += GetBytesFallback(chars + charactersConsumed, charCount - charactersConsumed, bytes + bytesWritten, byteCount - bytesWritten, null);
                }
            }
            else
            {
                // Nothing to encode
                bytesWritten = 0;
            }

            return(bytesWritten);
        }
コード例 #2
0
        private unsafe byte[] GetBytesValidated(char *input, int charCount)
        {
            int remaining = 0;

            // Assume string is all ASCII and size array for that
            byte[] bytes = new byte[charCount];

            int bytesWritten;

            fixed(byte *output = &bytes[0])
            {
                int charactersConsumed;

                if (!EncodingForwarder.TryEncode(input, charCount, output, charCount, out charactersConsumed, out bytesWritten))
                {
                    // Not all ASCII, get the byte count for the remaining encoded conversion
                    remaining = GetByteCount(input + charactersConsumed, charCount - charactersConsumed, null);
                }
            }

            if (remaining > 0)
            {
                // Not all ASCII, fallback to slower path for remaining encoding
                var encoded = ResizeGetRemainingBytes(input, charCount, ref bytes, bytesWritten, remaining);
                Debug.Assert(encoded == remaining);
            }

            return(bytes);
        }
コード例 #3
0
        public override unsafe int GetByteCount(char[] chars, int index, int count, bool flush)
        {
            // Validate input parameters
            if ((chars == null) ||
                (index < 0) ||
                (count < 0) ||
                (chars.Length - index < count))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, index, count);
            }
            Contract.EndContractBlock();

            // Avoid empty input problem
            if (chars.Length == 0)
            {
                chars = new char[1];
            }

            // Just call the pointer version
            int result = -1;

            fixed(char *pChars = &chars[0])
            {
                result = GetByteCountValidated(pChars + index, count, flush);
            }

            return(result);
        }
コード例 #4
0
        public override byte[] GetBytes(char[] chars, int index, int count)
        {
            if ((chars == null) ||
                (index < 0) ||
                (count < 0) ||
                (chars.Length - index < count))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, index, count);
            }
            Contract.EndContractBlock();

            return(GetBytesValidated(chars, index, count));
        }
コード例 #5
0
        public unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, bool flush)
        {
            // Validate parameters
            if ((bytes == null) ||
                (chars == null) ||
                (charCount < 0) ||
                (byteCount < 0))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes);
            }
            Contract.EndContractBlock();

            return(GetBytesValidated(chars, charCount, bytes, byteCount, flush));
        }
コード例 #6
0
        // Encodes a range of characters in a character array into a range of bytes
        // in a byte array. An exception occurs if the byte array is not large
        // enough to hold the complete encoding of the characters. The
        // GetByteCount method can be used to determine the exact number of
        // bytes that will be produced for a given range of characters.
        // Alternatively, the GetMaxByteCount method can be used to
        // determine the maximum number of bytes that will be produced for a given
        // number of characters, regardless of the actual character values.
        public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
        {
            if ((chars == null) ||
                (bytes == null) ||
                (charIndex < 0) ||
                (charCount < 0) ||
                (chars.Length - charIndex < charCount) ||
                (byteIndex < 0 || byteIndex > bytes.Length))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charIndex, charCount, bytes);
            }
            Contract.EndContractBlock();

            // Note that byteCount is the # of bytes to decode, not the size of the array
            int byteCount = bytes.Length - byteIndex;
            int bytesWritten;

            if (charCount > 0)
            {
                if (byteCount == 0)
                {
                    // Definitely not enough space, early bail
                    EncodingForwarder.ThrowBytesOverflow(this);
                }

                fixed(char *pInput = &chars[0])
                fixed(byte *pOutput = &bytes[0])
                {
                    char *input  = pInput + charIndex;
                    byte *output = pOutput + byteIndex;
                    int   charactersConsumed;

                    if (!EncodingForwarder.TryEncode(input, charCount, output, byteCount, out charactersConsumed, out bytesWritten))
                    {
                        // Not all ASCII, GetBytesFallback for remaining conversion
                        bytesWritten += GetBytesFallback(input + charactersConsumed, charCount - charactersConsumed, output + bytesWritten, byteCount - bytesWritten, null);
                    }
                }
            }
            else
            {
                // Nothing to encode
                bytesWritten = 0;
            }

            return(bytesWritten);
        }
コード例 #7
0
        internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
        {
            // Just need to Assert, this is called by internal EncoderNLS and parameters should already be checked
            Debug.Assert(this != null);
            Debug.Assert(bytes != null);
            Debug.Assert(chars != null);
            Debug.Assert(charCount >= 0);
            Debug.Assert(byteCount >= 0);

            int bytesWritten;
            int charactersConsumed = 0;

            if (((encoder?.InternalHasFallbackBuffer ?? false) &&
                 (encoder.FallbackBuffer.Remaining > 0)) ||
                (charCount > byteCount))
            {
                // Data already in Fallback buffer, so straight to GetBytesFallback
                bytesWritten = GetBytesFallback(chars, charCount, bytes, byteCount, encoder);
            }
            else if (charCount > 0)
            {
                if (byteCount == 0)
                {
                    // Definitely not enough space, early bail
                    EncodingForwarder.ThrowBytesOverflow(this);
                }
                if (!EncodingForwarder.TryEncode(chars, charCount, bytes, byteCount, out charactersConsumed, out bytesWritten))
                {
                    // Not all ASCII, use GetBytesFallback for remaining conversion
                    bytesWritten += GetBytesFallback(chars + charactersConsumed, charCount - charactersConsumed, bytes + bytesWritten, byteCount - bytesWritten, encoder);
                }
            }
            else
            {
                // Nothing to encode
                bytesWritten = 0;
            }

            if (encoder != null)
            {
                encoder.m_charsUsed += charactersConsumed;
            }

            return(bytesWritten);
        }
コード例 #8
0
        // This is the version that uses pointers.  We call the base encoding worker function
        // after setting our appropriate internal variables.  This is getting bytes
        public override unsafe void Convert(char *chars, int charCount,
                                            byte *bytes, int byteCount, bool flush,
                                            out int charsUsed, out int bytesUsed, out bool completed)
        {
            // Validate input parameters
            if ((bytes == null) ||
                (chars == null) ||
                (charCount < 0) ||
                (byteCount < 0))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes);
            }
            Contract.EndContractBlock();

            StartConversion(flush);

            // Do conversion
            bytesUsed = this.m_encoding.GetBytes(chars, charCount, bytes, byteCount, this);

            FinishConversion(charCount, flush, out charsUsed, out completed);
        }
コード例 #9
0
        public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
                                            byte[] bytes, int byteIndex, bool flush)
        {
            // Validate parameters
            if ((chars == null) ||
                (bytes == null) ||
                (charIndex < 0) ||
                (charCount < 0) ||
                (chars.Length - charIndex < charCount) ||
                (byteIndex < 0 || byteIndex > bytes.Length))
            {
                EncodingForwarder.ThrowValidationFailedException(chars, charIndex, charCount, bytes);
            }
            Contract.EndContractBlock();

            int byteCount = bytes.Length - byteIndex;

            if (charCount > 0 && byteCount == 0)
            {
                // Definitely not enough space, early bail
                EncodingForwarder.ThrowBytesOverflow(m_encoding);
            }

            if (chars.Length == 0)
            {
                chars = new char[1];
            }
            if (bytes.Length == 0)
            {
                bytes = new byte[1];

                // Just call pointer version
                fixed(char *pChars = &chars[0])
                fixed(byte *pBytes = &bytes[0])
                {
                    return(GetBytesValidated(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, flush));
                }
        }
コード例 #10
0
 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
                              char[] chars, int charIndex)
 {
     return(EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex));
 }
コード例 #11
0
 public override unsafe int GetCharCount(byte *bytes, int count)
 {
     return(EncodingForwarder.GetCharCount(this, bytes, count));
 }
コード例 #12
0
        // Returns the number of characters produced by decoding a range of bytes
        // in a byte array.

        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            return(EncodingForwarder.GetCharCount(this, bytes, index, count));
        }
コード例 #13
0
 public override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
 {
     return(EncodingForwarder.GetBytes(this, chars, charCount, bytes, byteCount));
 }
コード例 #14
0
 public override int GetBytes(String s, int charIndex, int charCount,
                              byte[] bytes, int byteIndex)
 {
     return(EncodingForwarder.GetBytes(this, s, charIndex, charCount, bytes, byteIndex));
 }
コード例 #15
0
 public override unsafe int GetByteCount(char *chars, int count)
 {
     return(EncodingForwarder.GetByteCount(this, chars, count));
 }
コード例 #16
0
        // Returns a string containing the decoded representation of a range of
        // bytes in a byte array.

        public override string GetString(byte[] bytes, int byteIndex, int byteCount)
        {
            return(EncodingForwarder.GetString(this, bytes, byteIndex, byteCount));
        }
コード例 #17
0
        // NOTE: Many methods in this class forward to EncodingForwarder for
        // validating arguments/wrapping the unsafe methods in this class
        // which do the actual work. That class contains
        // shared logic for doing this which is used by
        // ASCIIEncoding, EncodingNLS, UnicodeEncoding, UTF32Encoding,
        // UTF7Encoding, and UTF8Encoding.
        // The reason the code is separated out into a static class, rather
        // than a base class which overrides all of these methods for us
        // (which is what EncodingNLS is for internal Encodings) is because
        // that's really more of an implementation detail so it's internal.
        // At the same time, C# doesn't allow a public class subclassing an
        // internal/private one, so we end up having to re-override these
        // methods in all of the public Encodings + EncodingNLS.

        // Returns the number of bytes required to encode a range of characters in
        // a character array.

        public override int GetByteCount(char[] chars, int index, int count)
        {
            return(EncodingForwarder.GetByteCount(this, chars, index, count));
        }
コード例 #18
0
 public override int GetByteCount(string chars)
 {
     return(EncodingForwarder.GetByteCount(this, chars));
 }
コード例 #19
0
 public unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
 {
     return(EncodingForwarder.GetChars(this, bytes, byteCount, chars, charCount));
 }
コード例 #20
0
 public override String GetString(byte[] bytes, int index, int count)
 {
     return(EncodingForwarder.GetString(this, bytes, index, count));
 }
コード例 #21
0
 public override int GetByteCount(String s)
 {
     return(EncodingForwarder.GetByteCount(this, s));
 }