// 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 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(); }
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; }
void TLSConnectionPrivate::Write(const ByteArray &buf) { // If called from within the runloop's thread, allow the operation // to go through immediately. unsigned long us = uv_thread_self(); unsigned long it = thread_id_.load(); if (us == it) { int nread = SSL_write(ssl_, reinterpret_cast<const void *>(buf.ConstData()), buf.Length()); if (nread < 0) { int SSLerr = SSL_get_error(ssl_, nread); this->ShutdownError(OpenSSLUtils::ErrorFromOpenSSLErrorCode(SSLerr)); } else if (nread == 0) { this->ShutdownRemote(); } // If called from another thread, add it to the write queue and // inform the runloop that there are new bytes to be written. } else if (it > 0) { uv_mutex_lock(&wqlock_); wq_.push(buf); uv_mutex_unlock(&wqlock_); uv_async_send(&wqasync_); } }
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; }
bool ProcessInstance::SetParameterValue( const Variant& value, const ProcessParameter& parameter, size_type rowIndex ) { if ( !value.IsValid() ) return false; uint32 apiType = (*API->Process->GetParameterType)( parameter.Handle() ) & PTYPE_TYPE_MASK; if ( apiType == 0 ) throw APIFunctionError( "GetParameterType" ); if ( apiType == PTYPE_TABLE ) throw Error( "ProcessInstance::SetParameterValue(): Invoked for a table parameter" ); if ( apiType == PTYPE_STRING ) { String s = value.ToString(); return (*API->Process->SetParameterValue)( handle, parameter.Handle(), rowIndex, s.c_str(), s.Length() ) != api_false; } if ( apiType == PTYPE_BLOCK ) { ByteArray a = value.ToByteArray(); return (*API->Process->SetParameterValue)( handle, parameter.Handle(), rowIndex, a.Begin(), a.Length() ) != api_false; } union { uint32 u32; int32 i32; uint64 u64; int64 i64; float f; double d; api_bool b; api_enum e; } apiValue; switch ( apiType ) { case PTYPE_UINT8: case PTYPE_UINT16: case PTYPE_UINT32: apiValue.u32 = value.ToUInt(); break; case PTYPE_INT8: case PTYPE_INT16: case PTYPE_INT32: apiValue.i32 = value.ToInt(); break; case PTYPE_UINT64: apiValue.u64 = value.ToUInt64(); break; case PTYPE_INT64: apiValue.i64 = value.ToInt64(); break; case PTYPE_FLOAT: apiValue.f = value.ToFloat(); break; case PTYPE_DOUBLE: apiValue.d = value.ToDouble(); break; case PTYPE_BOOL: apiValue.b = api_bool( value.ToBoolean() ); break; case PTYPE_ENUM: apiValue.e = api_enum( value.ToInt() ); break; default: throw Error( "ProcessParameter::SetParameterValue(): Internal error: Unknown parameter type" ); } return (*API->Process->SetParameterValue)( handle, parameter.Handle(), rowIndex, &apiValue, 1 ) != api_false; }
// Adds a buffer containing one or more PEM-encoded // root certificates to the X509PEMVerifier. // // If the certificate (or one of the certificates) could not be // parsed AddPEM will return immediately, resulting in all of the // certificates up to the bad certicate being added to the verifier. bool X509PEMVerifier::AddPEM(const ByteArray &buf) { BIO *mem = BIO_new_mem_buf(static_cast<void *>(const_cast<char *>(buf.ConstData())), buf.Length()); (void) BIO_set_close(mem, BIO_NOCLOSE); int ncerts = 0; while (1) { X509 *x = PEM_read_bio_X509_AUX(mem, nullptr, nullptr, nullptr); if (x == nullptr) { return false; } X509_STORE_add_cert(store_, x); X509_free(x); ncerts++; } return true; }
void ExternalProcess::Write( const ByteArray& data ) { if ( !data.IsEmpty() ) if ( (*API->ExternalProcess->WriteToExternalProcess)( handle, data.Begin(), data.Length() ) == api_false ) throw APIFunctionError( "WriteToExternalProcess" ); }