void MemoryStream::Write(ByteArray& buffer, int32 offset, int32 count)
      {
      if(buffer.IsNull())
        throw ArgumentNullException(L"buffer");

      if (offset < 0 || count < 0)
        throw ArgumentOutOfRangeException ();

      if((int32)buffer.Length() - offset < count)
        throw ArgumentException(L"offset+count", L"The size of the buffer is less than offset + count.");

      CheckIfClosedThrowDisposed ();

      if(!CanWrite())
        throw NotSupportedException(L"Cannot write to this stream.");

      // reordered to avoid possible integer overflow
      if(_position > _length - count)
        Expand(_position + count);

      Buffer::BlockCopy(buffer, offset, (*_internalBuffer), _position, count);
      _position += count;
      if(_position >= _length)
        _length = _position;
      }
Example #2
0
  int Convert::ToBase64CharArray(ByteArray& inArray, int offsetIn, int length, CharArray& outArray, int offsetOut)
    {
    using namespace Security;
    if(inArray.IsNull())
      throw ArgumentNullException(L"inArray");
    if (outArray.IsNull())
      throw ArgumentNullException(L"outArray");
    if(offsetIn < 0 || length < 0 || offsetOut < 0)
      throw ArgumentOutOfRangeException(L"offsetIn, length, offsetOut < 0");
    // avoid integer overflow
    if(offsetIn > (int)inArray.Length() - length)
      throw ArgumentOutOfRangeException(L"offsetIn + length > array.Length");

    // note: normally ToBase64Transform doesn't support multiple block processing
    ByteArray outArr = Cryptography::Base64Helper::TransformFinalBlock(inArray, offsetIn, length);

    Text::ASCIIEncoding enc;    
    CharArray cOutArr = enc.GetChars(outArr);

    // avoid integer overflow
    if(offsetOut > (int)(outArray.Length() - cOutArr.Length()) )
      throw ArgumentOutOfRangeException(L"offsetOut + cOutArr.Length > outArray.Length");

    outArray.Base(offsetOut);
    for(int32 i = 0; i < (int32)cOutArr.Length(); ++i)
      outArray[i] = cOutArr[i];
    outArray.Base(0);

    return (int)cOutArr.Length();
    }
Example #3
0
// Check whether the other ByteArray is equal to this.
bool ByteArray::Equal(const ByteArray &other) const {
	if (Length() != other.Length()) {
		return false;
	}
	if (Length() == 0 && other.Length() == 0) {
		// If both are empty, ensure that they are either
		// both null, or both non-null.
		return IsNull() == other.IsNull();
	}
	if (memcmp(ConstData(), other.ConstData(), Length()) == 0) {
		return true;
	}
	return false;
}
    int MemoryStream::Read(ByteArray& buffer, int offset, int count)
      {
      if(buffer.IsNull())
        throw ArgumentNullException(L"buffer");

      if(offset < 0 || count < 0)
        throw ArgumentOutOfRangeException(L"offset or count less than zero.");

      if((int32)buffer.Length() - offset < count )
        throw ArgumentException (L"offset+count", L"The size of the buffer is less than offset + count.");

      CheckIfClosedThrowDisposed ();

      if(_position >= _length || count == 0)
        return 0;

      if(_position > _length - count)
        count = _length - _position;

      Buffer::BlockCopy((*_internalBuffer), _position, buffer, offset, count);

      _position += count;
      return count;
      }