internal unsafe static void Memcpy(byte *pDest, int destIndex, byte[] src, int srcIndex, int len)
        {
            if (len == 0)
            {
                return;
            }

            fixed(byte *ptr = src)
            {
                Buffer.Memcpy(pDest + destIndex, ptr + srcIndex, len);
            }
        }
        internal unsafe static void Memcpy(byte[] dest, int destIndex, byte *src, int srcIndex, int len)
        {
            if (len == 0)
            {
                return;
            }

            fixed(byte *ptr = dest)
            {
                Buffer.Memcpy(ptr + destIndex, src + srcIndex, len);
            }
        }
Example #3
0
        // Copies the source String (byte buffer) to the destination IntPtr memory allocated with len bytes.
        internal static unsafe void InternalCopy(String src, IntPtr dest, int len)
        {
            if (len == 0)
                return;

            fixed(char *charPtr = &src._firstChar)
            {
                byte *srcPtr = (byte *)charPtr;
                byte *dstPtr = (byte *)dest;

                Buffer.Memcpy(dstPtr, srcPtr, len);
            }
        }
Example #4
0
        private static unsafe string Ctor(sbyte *value, int startIndex, int length, Encoding enc)
        {
            if (enc == null)
            {
                return(new string(value, startIndex, length));
            }

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
            }

            if (value == null)
            {
                if (length == 0)
                {
                    return(Empty);
                }

                throw new ArgumentNullException(nameof(value));
            }

            byte *pStart = (byte *)(value + startIndex);

            if (pStart < value)
            {
                // overflow check
                throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_PartialWCHAR);
            }

            byte[] copyOfValue = new byte[length];
            fixed(byte *pCopyOfValue = copyOfValue)
            {
                try
                {
                    Buffer.Memcpy(dest: pCopyOfValue, src: pStart, len: length);
                }
                catch (Exception)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_PartialWCHAR);
                }
            }

            return(enc.GetString(copyOfValue));
        }
Example #5
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public void AppendString(string stringToAppend)
        {
            if (String.IsNullOrEmpty(stringToAppend))
            {
                return;
            }

            if ((m_totalSize - m_length) < stringToAppend.Length)
            {
                throw new IndexOutOfRangeException();
            }

            fixed(char *pointerToString = stringToAppend)
            {
                Buffer.Memcpy((byte *)(m_buffer + m_length), (byte *)pointerToString, stringToAppend.Length * sizeof(char));
            }

            m_length += stringToAppend.Length;
            Contract.Assert(m_length <= m_totalSize, "Buffer has been overflowed!");
        }
Example #6
0
        public unsafe void AppendString(string stringToAppend)
        {
            if (string.IsNullOrEmpty(stringToAppend))
            {
                return;
            }
            if (this.m_totalSize - this.m_length < stringToAppend.Length)
            {
                throw new IndexOutOfRangeException();
            }
            string str   = stringToAppend;
            char * chPtr = (char *)str;

            if ((IntPtr)chPtr != IntPtr.Zero)
            {
                chPtr += RuntimeHelpers.OffsetToStringData;
            }
            Buffer.Memcpy((byte *)(this.m_buffer + this.m_length), (byte *)chPtr, stringToAppend.Length * 2);
            str           = (string)null;
            this.m_length = this.m_length + stringToAppend.Length;
        }
        public unsafe void AppendString(string stringToAppend)
        {
            if (string.IsNullOrEmpty(stringToAppend))
            {
                return;
            }
            if (this.m_totalSize - this.m_length < stringToAppend.Length)
            {
                throw new IndexOutOfRangeException();
            }

            fixed(string text = stringToAppend)
            {
                char *ptr = text;

                if (ptr != null)
                {
                    ptr += RuntimeHelpers.OffsetToStringData / 2;
                }
                Buffer.Memcpy((byte *)(this.m_buffer + this.m_length), (byte *)ptr, stringToAppend.Length * 2);
            }

            this.m_length += stringToAppend.Length;
        }
Example #8
0
        unsafe static private String CreateString(sbyte *value, int startIndex, int length, Encoding enc)
        {
            if (enc == null)
            {
                return(new String(value, startIndex, length)); // default to ANSI
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
            }
            if ((value + startIndex) < value)
            {
                // overflow check
                throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_PartialWCHAR);
            }

            byte[] b = new byte[length];

            try
            {
                Buffer.Memcpy(b, 0, (byte *)value, startIndex, length);
            }
            catch (NullReferenceException)
            {
                // If we got a NullReferencException. It means the pointer or
                // the index is out of range
                throw new ArgumentOutOfRangeException(nameof(value),
                                                      SR.ArgumentOutOfRange_PartialWCHAR);
            }

            return(enc.GetString(b));
        }
Example #9
0
 static unsafe void memcpy(byte *dest, byte *src, int size)
 {
     Buffer.Memcpy(dest, src, size);
 }
Example #10
0
 internal static unsafe void wstrcpy(char *dmem, char *smem, int charCount)
 {
     Buffer.Memcpy((byte *)dmem, (byte *)smem, charCount * 2); // 2 used everywhere instead of sizeof(char)
 }
Example #11
0
        //
        // IRI normalization for strings containing characters that are not allowed or
        // escaped characters that should be unescaped in the context of the specified Uri component.
        //
        internal static unsafe string EscapeUnescapeIri(char *pInput, int start, int end, UriComponents component)
        {
            char[] dest  = new char[end - start];
            byte[] bytes = null;

            // Pin the array to do pointer accesses
            GCHandle destHandle = GCHandle.Alloc(dest, GCHandleType.Pinned);
            char *   pDest      = (char *)destHandle.AddrOfPinnedObject();

            const int percentEncodingLen     = 3; // Escaped UTF-8 will take 3 chars: %AB.
            const int bufferCapacityIncrease = 30 * percentEncodingLen;
            int       bufferRemaining        = 0;

            int  next       = start;
            int  destOffset = 0;
            char ch;
            bool escape        = false;
            bool surrogatePair = false;

            for (; next < end; ++next)
            {
                escape        = false;
                surrogatePair = false;

                if ((ch = pInput[next]) == '%')
                {
                    if (next + 2 < end)
                    {
                        ch = UriHelper.EscapedAscii(pInput[next + 1], pInput[next + 2]);

                        // Do not unescape a reserved char
                        if (ch == Uri.c_DummyChar || ch == '%' || CheckIsReserved(ch, component) || UriHelper.IsNotSafeForUnescape(ch))
                        {
                            // keep as is
                            Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                            pDest[destOffset++] = pInput[next++];
                            Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                            pDest[destOffset++] = pInput[next++];
                            Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                            pDest[destOffset++] = pInput[next];
                            continue;
                        }
                        else if (ch <= '\x7F')
                        {
                            Debug.Assert(ch < 0xFF, "Expecting ASCII character.");
                            Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                            //ASCII
                            pDest[destOffset++] = ch;
                            next += 2;
                            continue;
                        }
                        else
                        {
                            // possibly utf8 encoded sequence of unicode

                            // check if safe to unescape according to Iri rules

                            Debug.Assert(ch < 0xFF, "Expecting ASCII character.");

                            int startSeq  = next;
                            int byteCount = 1;
                            // lazy initialization of max size, will reuse the array for next sequences
                            if ((object)bytes == null)
                            {
                                bytes = new byte[end - next];
                            }

                            bytes[0] = (byte)ch;
                            next    += 3;
                            while (next < end)
                            {
                                // Check on exit criterion
                                if ((ch = pInput[next]) != '%' || next + 2 >= end)
                                {
                                    break;
                                }

                                // already made sure we have 3 characters in str
                                ch = UriHelper.EscapedAscii(pInput[next + 1], pInput[next + 2]);

                                //invalid hex sequence ?
                                if (ch == Uri.c_DummyChar)
                                {
                                    break;
                                }
                                // character is not part of a UTF-8 sequence ?
                                else if (ch < '\x80')
                                {
                                    break;
                                }
                                else
                                {
                                    //a UTF-8 sequence
                                    bytes[byteCount++] = (byte)ch;
                                    next += 3;
                                }

                                Debug.Assert(ch < 0xFF, "Expecting ASCII character.");
                            }
                            next--; // for loop will increment


                            // Using encoder with no replacement fall-back will skip all invalid UTF-8 sequences.
                            Encoding noFallbackCharUTF8 = (Encoding)Encoding.UTF8.Clone();
                            noFallbackCharUTF8.EncoderFallback = new EncoderReplacementFallback("");
                            noFallbackCharUTF8.DecoderFallback = new DecoderReplacementFallback("");

                            char[] unescapedChars = new char[bytes.Length];
                            int    charCount      = noFallbackCharUTF8.GetChars(bytes, 0, byteCount, unescapedChars, 0);


                            if (charCount != 0)
                            {
                                // If invalid sequences were present in the original escaped string, we need to
                                // copy the escaped versions of those sequences.
                                // Decoded Unicode values will be kept only when they are allowed by the URI/IRI RFC
                                // rules.
                                UriHelper.MatchUTF8Sequence(pDest, dest, ref destOffset, unescapedChars, charCount, bytes,
                                                            byteCount, component == UriComponents.Query, true);
                            }
                            else
                            {
                                // copy escaped sequence as is
                                for (int i = startSeq; i <= next; ++i)
                                {
                                    Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                                    pDest[destOffset++] = pInput[i];
                                }
                            }
                        }
                    }
                    else
                    {
                        Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                        pDest[destOffset++] = pInput[next];
                    }
                }
                else if (ch > '\x7f')
                {
                    // unicode

                    char ch2;

                    if ((Char.IsHighSurrogate(ch)) && (next + 1 < end))
                    {
                        ch2    = pInput[next + 1];
                        escape = !CheckIriUnicodeRange(ch, ch2, ref surrogatePair, component == UriComponents.Query);
                        if (!escape)
                        {
                            // copy the two chars
                            Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                            pDest[destOffset++] = pInput[next++];
                            Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                            pDest[destOffset++] = pInput[next];
                        }
                    }
                    else
                    {
                        if (CheckIriUnicodeRange(ch, component == UriComponents.Query))
                        {
                            if (!Uri.IsBidiControlCharacter(ch) || !UriParser.DontKeepUnicodeBidiFormattingCharacters)
                            {
                                // copy it
                                Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                                pDest[destOffset++] = pInput[next];
                            }
                        }
                        else
                        {
                            // escape it
                            escape = true;
                        }
                    }
                }
                else
                {
                    // just copy the character
                    Debug.Assert(dest.Length > destOffset, "Buffer overrun detected");
                    pDest[destOffset++] = pInput[next];
                }

                if (escape)
                {
                    const int maxNumberOfBytesEncoded = 4;

                    if (bufferRemaining < maxNumberOfBytesEncoded * percentEncodingLen)
                    {
                        int newBufferLength = 0;

                        checked
                        {
                            // may need more memory since we didn't anticipate escaping
                            newBufferLength  = dest.Length + bufferCapacityIncrease;
                            bufferRemaining += bufferCapacityIncrease;
                        }

                        char[] newDest = new char[newBufferLength];

                        fixed(char *pNewDest = newDest)
                        {
#if !UT_PUBLIC_DEPENDS
                            Buffer.Memcpy((byte *)pNewDest, (byte *)pDest, destOffset * sizeof(char));
#else
                            for (int idx = 0; idx < destOffset; idx++)
                            {
                                pNewDest[idx] = pDest[idx];
                            }
#endif
                        }

                        if (destHandle.IsAllocated)
                        {
                            destHandle.Free();
                        }

                        dest = newDest;

                        // re-pin new dest[] array
                        destHandle = GCHandle.Alloc(dest, GCHandleType.Pinned);
                        pDest      = (char *)destHandle.AddrOfPinnedObject();
                    }

                    byte[] encodedBytes = new byte[maxNumberOfBytesEncoded];
                    fixed(byte *pEncodedBytes = encodedBytes)
                    {
                        int encodedBytesCount = Encoding.UTF8.GetBytes(pInput + next, surrogatePair ? 2 : 1, pEncodedBytes, maxNumberOfBytesEncoded);

                        Debug.Assert(encodedBytesCount <= maxNumberOfBytesEncoded, "UTF8 encoder should not exceed specified byteCount");

                        bufferRemaining -= encodedBytesCount * percentEncodingLen;

                        for (int count = 0; count < encodedBytesCount; ++count)
                        {
                            UriHelper.EscapeAsciiChar((char)encodedBytes[count], dest, ref destOffset);
                        }
                    }
                }
            }

            if (destHandle.IsAllocated)
            {
                destHandle.Free();
            }

            Debug.Assert(destOffset <= dest.Length, "Buffer overrun detected");
            return(new string(dest, 0, destOffset));
        }