1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2008 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using System; 34 using System.Collections; 35 using System.Collections.Generic; 36 using System.IO; 37 using System.Security; 38 using System.Text; 39 #if !NET35 40 using System.Threading; 41 using System.Threading.Tasks; 42 #endif 43 #if NET35 44 using Google.Protobuf.Compatibility; 45 #endif 46 47 namespace Google.Protobuf 48 { 49 /// <summary> 50 /// Immutable array of bytes. 51 /// </summary> 52 public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString> 53 { 54 private static readonly ByteString empty = new ByteString(new byte[0]); 55 56 private readonly byte[] bytes; 57 58 /// <summary> 59 /// Unsafe operations that can cause IO Failure and/or other catastrophic side-effects. 60 /// </summary> 61 internal static class Unsafe 62 { 63 /// <summary> 64 /// Constructs a new ByteString from the given byte array. The array is 65 /// *not* copied, and must not be modified after this constructor is called. 66 /// </summary> FromBytes(byte[] bytes)67 internal static ByteString FromBytes(byte[] bytes) 68 { 69 return new ByteString(bytes); 70 } 71 } 72 73 /// <summary> 74 /// Internal use only. Ensure that the provided array is not mutated and belongs to this instance. 75 /// </summary> AttachBytes(byte[] bytes)76 internal static ByteString AttachBytes(byte[] bytes) 77 { 78 return new ByteString(bytes); 79 } 80 81 /// <summary> 82 /// Constructs a new ByteString from the given byte array. The array is 83 /// *not* copied, and must not be modified after this constructor is called. 84 /// </summary> ByteString(byte[] bytes)85 private ByteString(byte[] bytes) 86 { 87 this.bytes = bytes; 88 } 89 90 /// <summary> 91 /// Returns an empty ByteString. 92 /// </summary> 93 public static ByteString Empty 94 { 95 get { return empty; } 96 } 97 98 /// <summary> 99 /// Returns the length of this ByteString in bytes. 100 /// </summary> 101 public int Length 102 { 103 get { return bytes.Length; } 104 } 105 106 /// <summary> 107 /// Returns <c>true</c> if this byte string is empty, <c>false</c> otherwise. 108 /// </summary> 109 public bool IsEmpty 110 { 111 get { return Length == 0; } 112 } 113 114 #if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY 115 /// <summary> 116 /// Provides read-only access to the data of this <see cref="ByteString"/>. 117 /// No data is copied so this is the most efficient way of accessing. 118 /// </summary> 119 public ReadOnlySpan<byte> Span 120 { 121 [SecuritySafeCritical] 122 get 123 { 124 return new ReadOnlySpan<byte>(bytes); 125 } 126 } 127 128 /// <summary> 129 /// Provides read-only access to the data of this <see cref="ByteString"/>. 130 /// No data is copied so this is the most efficient way of accessing. 131 /// </summary> 132 public ReadOnlyMemory<byte> Memory 133 { 134 [SecuritySafeCritical] 135 get 136 { 137 return new ReadOnlyMemory<byte>(bytes); 138 } 139 } 140 #endif 141 142 /// <summary> 143 /// Converts this <see cref="ByteString"/> into a byte array. 144 /// </summary> 145 /// <remarks>The data is copied - changes to the returned array will not be reflected in this <c>ByteString</c>.</remarks> 146 /// <returns>A byte array with the same data as this <c>ByteString</c>.</returns> ToByteArray()147 public byte[] ToByteArray() 148 { 149 return (byte[]) bytes.Clone(); 150 } 151 152 /// <summary> 153 /// Converts this <see cref="ByteString"/> into a standard base64 representation. 154 /// </summary> 155 /// <returns>A base64 representation of this <c>ByteString</c>.</returns> ToBase64()156 public string ToBase64() 157 { 158 return Convert.ToBase64String(bytes); 159 } 160 161 /// <summary> 162 /// Constructs a <see cref="ByteString" /> from the Base64 Encoded String. 163 /// </summary> FromBase64(string bytes)164 public static ByteString FromBase64(string bytes) 165 { 166 // By handling the empty string explicitly, we not only optimize but we fix a 167 // problem on CF 2.0. See issue 61 for details. 168 return bytes == "" ? Empty : new ByteString(Convert.FromBase64String(bytes)); 169 } 170 171 /// <summary> 172 /// Constructs a <see cref="ByteString"/> from data in the given stream, synchronously. 173 /// </summary> 174 /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position 175 /// at the start of the call.</remarks> 176 /// <param name="stream">The stream to copy into a ByteString.</param> 177 /// <returns>A ByteString with content read from the given stream.</returns> FromStream(Stream stream)178 public static ByteString FromStream(Stream stream) 179 { 180 ProtoPreconditions.CheckNotNull(stream, nameof(stream)); 181 int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0; 182 var memoryStream = new MemoryStream(capacity); 183 stream.CopyTo(memoryStream); 184 #if NETSTANDARD1_1 || NETSTANDARD2_0 185 byte[] bytes = memoryStream.ToArray(); 186 #else 187 // Avoid an extra copy if we can. 188 byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray(); 189 #endif 190 return AttachBytes(bytes); 191 } 192 193 #if !NET35 194 /// <summary> 195 /// Constructs a <see cref="ByteString"/> from data in the given stream, asynchronously. 196 /// </summary> 197 /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position 198 /// at the start of the call.</remarks> 199 /// <param name="stream">The stream to copy into a ByteString.</param> 200 /// <param name="cancellationToken">The cancellation token to use when reading from the stream, if any.</param> 201 /// <returns>A ByteString with content read from the given stream.</returns> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))202 public async static Task<ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken)) 203 { 204 ProtoPreconditions.CheckNotNull(stream, nameof(stream)); 205 int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0; 206 var memoryStream = new MemoryStream(capacity); 207 // We have to specify the buffer size here, as there's no overload accepting the cancellation token 208 // alone. But it's documented to use 81920 by default if not specified. 209 await stream.CopyToAsync(memoryStream, 81920, cancellationToken); 210 #if NETSTANDARD1_1 || NETSTANDARD2_0 211 byte[] bytes = memoryStream.ToArray(); 212 #else 213 // Avoid an extra copy if we can. 214 byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray(); 215 #endif 216 return AttachBytes(bytes); 217 } 218 #endif 219 220 /// <summary> 221 /// Constructs a <see cref="ByteString" /> from the given array. The contents 222 /// are copied, so further modifications to the array will not 223 /// be reflected in the returned ByteString. 224 /// This method can also be invoked in <c>ByteString.CopyFrom(0xaa, 0xbb, ...)</c> form 225 /// which is primarily useful for testing. 226 /// </summary> CopyFrom(params byte[] bytes)227 public static ByteString CopyFrom(params byte[] bytes) 228 { 229 return new ByteString((byte[]) bytes.Clone()); 230 } 231 232 /// <summary> 233 /// Constructs a <see cref="ByteString" /> from a portion of a byte array. 234 /// </summary> CopyFrom(byte[] bytes, int offset, int count)235 public static ByteString CopyFrom(byte[] bytes, int offset, int count) 236 { 237 byte[] portion = new byte[count]; 238 ByteArray.Copy(bytes, offset, portion, 0, count); 239 return new ByteString(portion); 240 } 241 242 #if GOOGLE_PROTOBUF_SUPPORT_SYSTEM_MEMORY 243 /// <summary> 244 /// Constructs a <see cref="ByteString" /> from a read only span. The contents 245 /// are copied, so further modifications to the span will not 246 /// be reflected in the returned <see cref="ByteString" />. 247 /// </summary> 248 [SecuritySafeCritical] CopyFrom(ReadOnlySpan<byte> bytes)249 public static ByteString CopyFrom(ReadOnlySpan<byte> bytes) 250 { 251 return new ByteString(bytes.ToArray()); 252 } 253 #endif 254 255 /// <summary> 256 /// Creates a new <see cref="ByteString" /> by encoding the specified text with 257 /// the given encoding. 258 /// </summary> CopyFrom(string text, Encoding encoding)259 public static ByteString CopyFrom(string text, Encoding encoding) 260 { 261 return new ByteString(encoding.GetBytes(text)); 262 } 263 264 /// <summary> 265 /// Creates a new <see cref="ByteString" /> by encoding the specified text in UTF-8. 266 /// </summary> CopyFromUtf8(string text)267 public static ByteString CopyFromUtf8(string text) 268 { 269 return CopyFrom(text, Encoding.UTF8); 270 } 271 272 /// <summary> 273 /// Returns the byte at the given index. 274 /// </summary> 275 public byte this[int index] 276 { 277 get { return bytes[index]; } 278 } 279 280 /// <summary> 281 /// Converts this <see cref="ByteString"/> into a string by applying the given encoding. 282 /// </summary> 283 /// <remarks> 284 /// This method should only be used to convert binary data which was the result of encoding 285 /// text with the given encoding. 286 /// </remarks> 287 /// <param name="encoding">The encoding to use to decode the binary data into text.</param> 288 /// <returns>The result of decoding the binary data with the given decoding.</returns> ToString(Encoding encoding)289 public string ToString(Encoding encoding) 290 { 291 return encoding.GetString(bytes, 0, bytes.Length); 292 } 293 294 /// <summary> 295 /// Converts this <see cref="ByteString"/> into a string by applying the UTF-8 encoding. 296 /// </summary> 297 /// <remarks> 298 /// This method should only be used to convert binary data which was the result of encoding 299 /// text with UTF-8. 300 /// </remarks> 301 /// <returns>The result of decoding the binary data with the given decoding.</returns> ToStringUtf8()302 public string ToStringUtf8() 303 { 304 return ToString(Encoding.UTF8); 305 } 306 307 /// <summary> 308 /// Returns an iterator over the bytes in this <see cref="ByteString"/>. 309 /// </summary> 310 /// <returns>An iterator over the bytes in this object.</returns> GetEnumerator()311 public IEnumerator<byte> GetEnumerator() 312 { 313 return ((IEnumerable<byte>) bytes).GetEnumerator(); 314 } 315 316 /// <summary> 317 /// Returns an iterator over the bytes in this <see cref="ByteString"/>. 318 /// </summary> 319 /// <returns>An iterator over the bytes in this object.</returns> IEnumerable.GetEnumerator()320 IEnumerator IEnumerable.GetEnumerator() 321 { 322 return GetEnumerator(); 323 } 324 325 /// <summary> 326 /// Creates a CodedInputStream from this ByteString's data. 327 /// </summary> CreateCodedInput()328 public CodedInputStream CreateCodedInput() 329 { 330 // We trust CodedInputStream not to reveal the provided byte array or modify it 331 return new CodedInputStream(bytes); 332 } 333 334 /// <summary> 335 /// Compares two byte strings for equality. 336 /// </summary> 337 /// <param name="lhs">The first byte string to compare.</param> 338 /// <param name="rhs">The second byte string to compare.</param> 339 /// <returns><c>true</c> if the byte strings are equal; false otherwise.</returns> operator ==(ByteString lhs, ByteString rhs)340 public static bool operator ==(ByteString lhs, ByteString rhs) 341 { 342 if (ReferenceEquals(lhs, rhs)) 343 { 344 return true; 345 } 346 if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) 347 { 348 return false; 349 } 350 if (lhs.bytes.Length != rhs.bytes.Length) 351 { 352 return false; 353 } 354 for (int i = 0; i < lhs.Length; i++) 355 { 356 if (rhs.bytes[i] != lhs.bytes[i]) 357 { 358 return false; 359 } 360 } 361 return true; 362 } 363 364 /// <summary> 365 /// Compares two byte strings for inequality. 366 /// </summary> 367 /// <param name="lhs">The first byte string to compare.</param> 368 /// <param name="rhs">The second byte string to compare.</param> 369 /// <returns><c>false</c> if the byte strings are equal; true otherwise.</returns> operator !=(ByteString lhs, ByteString rhs)370 public static bool operator !=(ByteString lhs, ByteString rhs) 371 { 372 return !(lhs == rhs); 373 } 374 375 /// <summary> 376 /// Compares this byte string with another object. 377 /// </summary> 378 /// <param name="obj">The object to compare this with.</param> 379 /// <returns><c>true</c> if <paramref name="obj"/> refers to an equal <see cref="ByteString"/>; <c>false</c> otherwise.</returns> Equals(object obj)380 public override bool Equals(object obj) 381 { 382 return this == (obj as ByteString); 383 } 384 385 /// <summary> 386 /// Returns a hash code for this object. Two equal byte strings 387 /// will return the same hash code. 388 /// </summary> 389 /// <returns>A hash code for this object.</returns> GetHashCode()390 public override int GetHashCode() 391 { 392 int ret = 23; 393 foreach (byte b in bytes) 394 { 395 ret = (ret * 31) + b; 396 } 397 return ret; 398 } 399 400 /// <summary> 401 /// Compares this byte string with another. 402 /// </summary> 403 /// <param name="other">The <see cref="ByteString"/> to compare this with.</param> 404 /// <returns><c>true</c> if <paramref name="other"/> refers to an equal byte string; <c>false</c> otherwise.</returns> Equals(ByteString other)405 public bool Equals(ByteString other) 406 { 407 return this == other; 408 } 409 410 /// <summary> 411 /// Used internally by CodedOutputStream to avoid creating a copy for the write 412 /// </summary> WriteRawBytesTo(CodedOutputStream outputStream)413 internal void WriteRawBytesTo(CodedOutputStream outputStream) 414 { 415 outputStream.WriteRawBytes(bytes, 0, bytes.Length); 416 } 417 418 /// <summary> 419 /// Copies the entire byte array to the destination array provided at the offset specified. 420 /// </summary> CopyTo(byte[] array, int position)421 public void CopyTo(byte[] array, int position) 422 { 423 ByteArray.Copy(bytes, 0, array, position, bytes.Length); 424 } 425 426 /// <summary> 427 /// Writes the entire byte array to the provided stream 428 /// </summary> WriteTo(Stream outputStream)429 public void WriteTo(Stream outputStream) 430 { 431 outputStream.Write(bytes, 0, bytes.Length); 432 } 433 } 434 }