private unsafe void CreateFirstSubstData(string s, IIS7WorkerRequest iis7WorkerRequest, System.Text.Encoder encoder) { IntPtr ptr; int num = 0; int length = s.Length; if (length > 0) { fixed(char *str = ((char *)s)) { char *chars = str; int size = encoder.GetByteCount(chars, length, true); ptr = iis7WorkerRequest.AllocateRequestMemory(size); if (ptr != IntPtr.Zero) { num = encoder.GetBytes(chars, length, (byte *)ptr, size, true); } } } else { ptr = iis7WorkerRequest.AllocateRequestMemory(1); } if (ptr == IntPtr.Zero) { throw new OutOfMemoryException(); } this._firstSubstData = ptr; this._firstSubstDataSize = num; }
public static int Hash(string data, Encoder enc) { var arr = data.ToCharArray(); int count = enc.GetByteCount(arr, 0, arr.Length, false); var bytes = new byte[count]; enc.GetBytes(arr, 0, arr.Length, bytes, 0, false); return Hash(bytes); }
public static byte[] EncodeString(string vValue) { string str = vValue; System.Text.Encoder encoder = m_encoding.GetEncoder(); char[] chars = str.ToCharArray(); byte[] bytes = new byte[encoder.GetByteCount(chars, 0, chars.Length, false)]; encoder.GetBytes(chars, 0, chars.Length, bytes, 0, true); return(Mogo.RPC.Utils.FillLengthHead(bytes)); }
/// <summary> /// Add a standard EXIF field to the image /// </summary> /// <param name="image"></param> /// <param name="field"></param> /// <param name="fieldValue"></param> private static void WriteEXIFField(Image image, ExifField field, string fieldValue) { Encoding asciiEncoding = new ASCIIEncoding(); System.Text.Encoder encoder = asciiEncoding.GetEncoder(); char[] tagTextChars = fieldValue.ToCharArray(); int byteCount = encoder.GetByteCount(tagTextChars, 0, tagTextChars.Length, true); byte[] tagTextBytes = new byte[byteCount]; encoder.GetBytes(tagTextChars, 0, tagTextChars.Length, tagTextBytes, 0, true); if (image.PropertyItems != null && image.PropertyItems.Length > 0) { PropertyItem propertyItem = image.PropertyItems[0]; propertyItem.Id = (int)field; propertyItem.Type = 2; // ASCII propertyItem.Len = tagTextBytes.Length; propertyItem.Value = tagTextBytes; image.SetPropertyItem(propertyItem); } }
// GET api/<controller> public HttpResponseMessage Get(string filename, string ext) { if (filename == null) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } string filePath = HostingEnvironment.MapPath("~/Videos/") + filename + "." + ext; if (Request.Headers.Range != null) { try { System.Text.Encoder stringEncoder = Encoding.UTF8.GetEncoder(); byte[] stringBytes = new byte[stringEncoder.GetByteCount(filePath.ToCharArray(), 0, filePath.Length, true)]; stringEncoder.GetBytes(filePath.ToCharArray(), 0, filePath.Length, stringBytes, 0, true); MD5CryptoServiceProvider MD5Enc = new MD5CryptoServiceProvider(); string hash = BitConverter.ToString(MD5Enc.ComputeHash(stringBytes)).Replace("-", string.Empty); HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent); partialResponse.Headers.AcceptRanges.Add("bytes"); partialResponse.Headers.ETag = new EntityTagHeaderValue("\"" + hash + "\""); var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, new MediaTypeHeaderValue("video/mp4")); return(partialResponse); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } else { return(new HttpResponseMessage(HttpStatusCode.RequestedRangeNotSatisfiable)); } }
// WOS 1926509: ASP.NET: WriteSubstitution in integrated mode needs to support callbacks that return String.Empty private unsafe void CreateFirstSubstData(String s, IIS7WorkerRequest iis7WorkerRequest, Encoder encoder) { Debug.Assert(s != null, "s != null"); IntPtr pbBuffer; int numBytes = 0; int cch = s.Length; if (cch > 0) { fixed (char * pch = s) { int cbBuffer = encoder.GetByteCount(pch, cch, true /*flush*/); pbBuffer = iis7WorkerRequest.AllocateRequestMemory(cbBuffer); if (pbBuffer != IntPtr.Zero) { numBytes = encoder.GetBytes(pch, cch, (byte*)pbBuffer, cbBuffer, true /*flush*/); } } } else { // deal with empty string pbBuffer = iis7WorkerRequest.AllocateRequestMemory(1); } if (pbBuffer == IntPtr.Zero) { throw new OutOfMemoryException(); } _firstSubstData = pbBuffer; _firstSubstDataSize = numBytes; }
protected static byte[] StringToByteArray(string str, Encoder enc) { var ca = str.ToCharArray(); var ret = new byte[enc.GetByteCount(ca, 0, ca.Length, false)]; int charsUsed, bytesUsed; bool completed; enc.Convert(ca, 0, ca.Length, ret, 0, ret.Length, true, out charsUsed, out bytesUsed, out completed); return ret; }
static bool GetNextValue(string charset, Encoder encoder, HexEncoder hex, char[] chars, ref int index, ref byte[] bytes, ref byte[] encoded, int maxLength, out string value) { int length = chars.Length - index; if (length < maxLength) { switch (GetEncodeMethod (chars, index, length)) { case EncodeMethod.Quote: value = MimeUtils.Quote (new string (chars, index, length)); index += length; return false; case EncodeMethod.None: value = new string (chars, index, length); index += length; return false; } } length = Math.Min (maxLength, length); int ratio, count, n; do { count = encoder.GetByteCount (chars, index, length, true); if (count > maxLength && length > 1) { ratio = (int) Math.Round ((double) count / (double) length); length -= Math.Max ((count - maxLength) / ratio, 1); continue; } if (bytes.Length < count) Array.Resize<byte> (ref bytes, count); count = encoder.GetBytes (chars, index, length, bytes, 0, true); // Note: the first chunk needs to be encoded in order to declare the charset if (index > 0 || charset == "us-ascii") { var method = GetEncodeMethod (bytes, count); if (method == EncodeMethod.Quote) { value = MimeUtils.Quote (Encoding.ASCII.GetString (bytes, 0, count)); index += length; return false; } if (method == EncodeMethod.None) { value = Encoding.ASCII.GetString (bytes, 0, count); index += length; return false; } } n = hex.EstimateOutputLength (count); if (encoded.Length < n) Array.Resize<byte> (ref encoded, n); // only the first value gets a charset declaration int charsetLength = index == 0 ? charset.Length + 2 : 0; n = hex.Encode (bytes, 0, count, encoded); if (n > 3 && (charsetLength + n) > maxLength) { int x = 0; for (int i = n - 1; i >= 0 && charsetLength + i >= maxLength; i--) { if (encoded[i] == (byte) '%') x--; else x++; } ratio = (int) Math.Round ((double) count / (double) length); length -= Math.Max (x / ratio, 1); continue; } if (index == 0) value = charset + "''" + Encoding.ASCII.GetString (encoded, 0, n); else value = Encoding.ASCII.GetString (encoded, 0, n); index += length; return true; } while (true); }
/// <summary> /// Determines the number of bytes that result from an encoding operation /// </summary> /// <param name="encoder">The encoder performing the encoding operation</param> /// <param name="span">The input characters</param> /// <param name="flush">True to flush the encoder, otherwise False</param> /// <returns>The number of bytes that would be output</returns> public static unsafe int GetByteCount(this Encoder encoder, ReadOnlySpan <char> span, bool flush) { fixed(char *pSpan = &MemoryMarshal.GetReference(span)) return(encoder.GetByteCount(pSpan, span.Length, flush)); }
public static int GetByteCount(Encoder encoder, ReadOnlySpan <char> span, bool flush) => encoder.GetByteCount(span, flush);
private static int WriteWithUInt16LengthPrefix(byte[] buffer, ref int offset, Char[] value, Encoder coder) { #if DEBUG if (buffer == null) throw new ArgumentNullException("buffer"); if (value == null) throw new ArgumentNullException("value"); if (coder == null) throw new ArgumentNullException("coder"); #endif int ofs = offset; int count = coder.GetByteCount(value, 0, value.Length, true); int bytesUsed; int charsUsed; bool completed; Write(buffer, ref ofs, (UInt16)count); coder.Convert(value, 0, value.Length, buffer, ofs, buffer.Length - ofs, true , out charsUsed , out bytesUsed , out completed); if (!completed) throw new ArgumentException(String.Format( Properties.Resources.Error_BufferOutOfSpace), "buffer"); offset = ofs + bytesUsed; return bytesUsed + 2; }