inline size_t ChangesetInputStream::next_block(const char*& begin, const char*& end) { for (;;) { if (REALM_UNLIKELY(m_changesets_begin == m_changesets_end)) { if (m_begin_version == m_end_version) return 0; // End of input version_type n = sizeof m_changesets / sizeof m_changesets[0]; version_type avail = m_end_version - m_begin_version; if (n > avail) n = avail; version_type end_version = m_begin_version + n; m_history.get_changesets(m_begin_version, end_version, m_changesets); m_begin_version = end_version; m_changesets_begin = m_changesets; m_changesets_end = m_changesets_begin + n; } BinaryData changeset = *m_changesets_begin++; if (changeset.size() > 0) { begin = changeset.data(); end = changeset.data() + changeset.size(); return changeset.size(); } } }
// Saves the data to a file void Utils::saveData2File(const std::string& fname, const BinaryData& data) { int fd = open(fname.c_str(), O_WRONLY|O_APPEND|O_CREAT); if (fd < 0) { throw Exception(__FILE__, __LINE__, "Error %d in open(): %s", errno, strerror(errno)); } try { size_t wcount = write(fd, data.toVoid(), data.size()); if (wcount != data.size()) { throw Exception(__FILE__, __LINE__, "Error %d in write(): %s", errno, strerror(errno)); } } catch(...) { close(fd); throw; } close(fd); // change permsiisons to rw for owner if (chmod (fname.c_str(), S_IRUSR|S_IWUSR) < 0) { throw Exception(__FILE__, __LINE__, "Error %d in chmod(): %s", errno, strerror(errno)); } }
/// pad binary representation to given size inline void Pad(BinaryData& data, size_t uiTargetSize) { if (data.size() < uiTargetSize) { data.reserve(uiTargetSize); data.insert(data.begin(), uiTargetSize - data.size(), 0); } }
void ToBinaryData(const TIntNumber& n, BinaryData& data) { BinaryData tempData = boost::xint::to_binary(n); // since all operators expect a high byte...low byte order, reverse the data data.reserve(data.size() + tempData.size()); std::copy(tempData.rbegin(), tempData.rend(), std::back_inserter(data)); }
bool next_block(const char*& begin, const char*& end) override { BinaryData block = m_it.get_next(); begin = block.data(); end = begin + block.size(); return begin != end; }
jbyteArray tbl_GetByteArray(JNIEnv* env, jlong nativeTablePtr, jlong columnIndex, jlong rowIndex) { if (!TBL_AND_INDEX_VALID(env, reinterpret_cast<T*>(nativeTablePtr), columnIndex, rowIndex)) return NULL; BinaryData bin = reinterpret_cast<T*>(nativeTablePtr)->get_binary( S(columnIndex), S(rowIndex)); if (bin.size() <= MAX_JSIZE) { jbyteArray jresult = env->NewByteArray(static_cast<jsize>(bin.size())); if (jresult) env->SetByteArrayRegion(jresult, 0, static_cast<jsize>(bin.size()), reinterpret_cast<const jbyte*>(bin.data())); // throws return jresult; } else { ThrowException(env, IllegalArgument, "Length of ByteArray is larger than an Int."); return NULL; } }
OwnedBinaryData Realm::write_copy() { verify_thread(); BinaryData buffer = read_group().write_to_mem(); // Since OwnedBinaryData does not have a constructor directly taking // ownership of BinaryData, we have to do this to avoid copying the buffer return OwnedBinaryData(std::unique_ptr<char[]>((char*)buffer.data()), buffer.size()); }
// Recieves a data portion void TCPSocket::recv(BinaryData& data) { BOOST_ASSERT(m_sock.getDescriptor() >= 0); BOOST_ASSERT(data.empty() == false); try { int count = ::recv(m_sock.getDescriptor(), data.toVoid(), data.size(), 0); if (count < 0) { int saved_errno = errno; if (saved_errno == EPIPE) { throw ClosedConnection(__FILE__, __LINE__, getPeerName()); } else { throw Exception(__FILE__, __LINE__, "Error %d in recv(): %s", saved_errno, strerror(saved_errno)); } } else if (count == 0) { throw ClosedConnection(__FILE__, __LINE__, getPeerName()); } else if (count < static_cast<int>(data.size())) { data.resize(count); } m_rater.updateInput(static_cast<size_t>(count)); } catch(...) { disconnect(); throw; } }
bool next_block(const char*& begin, const char*& end) override { while (m_valid) { BinaryData actual = m_changesets_begin->get_next(); if (actual.size() > 0) { begin = actual.data(); end = actual.data() + actual.size(); return true; } m_changesets_begin++; if (REALM_UNLIKELY(m_changesets_begin == m_changesets_end)) { get_changeset(); } } return false; // End of input }
void Realm::write_copy(StringData path, BinaryData key) { REALM_ASSERT(!key.data() || key.size() == 64); verify_thread(); try { read_group()->write(path, key.data()); } catch (...) { translate_file_exception(path); } }
TIntNumber ToInteger(const BinaryData& data, bool bFromBigEndian = true) { if (!bFromBigEndian) return TIntNumber(data); BinaryData tempData; tempData.reserve(data.size()); std::copy(data.rbegin(), data.rend(), std::back_inserter(tempData)); return TIntNumber(tempData); }
void Realm::write_copy(StringData path, BinaryData key) { if (key.data() && key.size() != 64) { throw InvalidEncryptionKeyException(); } verify_thread(); try { read_group().write(path, key.data()); } catch (...) { translate_file_exception(path); } }
template<> JSValueRef RJSAccessor::from_binary(JSContextRef ctx, BinaryData data) { static JSStringRef bufferString = JSStringCreateWithUTF8CString("buffer"); static JSStringRef uint8ArrayString = JSStringCreateWithUTF8CString("Uint8Array"); size_t byteCount = data.size(); JSValueRef byteCountValue = JSValueMakeNumber(ctx, byteCount); JSObjectRef uint8ArrayContructor = RJSValidatedObjectProperty(ctx, JSContextGetGlobalObject(ctx), uint8ArrayString); JSObjectRef uint8Array = JSObjectCallAsConstructor(ctx, uint8ArrayContructor, 1, &byteCountValue, NULL); for (size_t i = 0; i < byteCount; i++) { JSValueRef num = JSValueMakeNumber(ctx, data[i]); JSObjectSetPropertyAtIndex(ctx, uint8Array, (unsigned)i, num, NULL); } return RJSValidatedObjectProperty(ctx, uint8Array, bufferString); }
REALM_EXPORT size_t object_get_binary(const Object& object, size_t property_ndx, const char*& return_buffer, size_t& return_size, NativeException::Marshallable& ex) { return handle_errors(ex, [&]() { verify_can_get(object); const size_t column_ndx = get_column_index(object, property_ndx); const BinaryData fielddata = object.row().get_binary(column_ndx); if (fielddata.is_null()) return 0; return_buffer = fielddata.data(); return_size = fielddata.size(); return 1; }); }
std::string ToUrlFormat(const BinaryData& d) { std::string r; const int size = d.size(); for (int i = 0; i < size; i++) { unsigned char c = d[i]; if (IsUrlPrintable(c)) { r += std::string(1, c); } else { // Replace c with e.g. %20 - i.e. '%' followed by c in hex. r += std::string(1, '%'); r += ToHexString((unsigned int)c); } } return r; }
inline StringData BinaryColumn::get_string(size_t ndx) const noexcept { BinaryData bin = get(ndx); REALM_ASSERT_3(0, <, bin.size()); return StringData(bin.data(), bin.size()-1); }
BinaryData Helper::Sha1(const BinaryData& data) { ATLASSERT(!data.empty()); return Sha1(&data[0], data.size()); }
void HashDataSha1::Add(const BinaryData& data) { ATLASSERT(data.size() <= std::numeric_limits<unsigned int>::max()); if (!data.empty()) SHA1_Update(m_spContext.get(), &data[0], static_cast<unsigned int>(data.size())); }
jobject CreateJMixedFromMixed(JNIEnv* env, Mixed& mixed) { jclass jMixedClass = GetClassMixed(env); if (jMixedClass == NULL) return NULL; TR((env, "CreateJMixedFromMixed(type %d)\n", mixed.get_type())); switch (mixed.get_type()) { case type_Int: { jmethodID consId = GetMixedMethodID(env, "<init>", "(J)V"); if (consId) return env->NewObject(jMixedClass, consId, mixed.get_int()); } case type_Float: { jmethodID consId = GetMixedMethodID(env, "<init>", "(F)V"); if (consId) return env->NewObject(jMixedClass, consId, mixed.get_float()); } case type_Double: { jmethodID consId = GetMixedMethodID(env, "<init>", "(D)V"); if (consId) return env->NewObject(jMixedClass, consId, mixed.get_double()); } case type_String: { jmethodID consId = GetMixedMethodID(env, "<init>", "(Ljava/lang/String;)V"); if (consId) return env->NewObject(jMixedClass, consId, to_jstring(env, mixed.get_string())); } case type_Bool: { jmethodID consId = GetMixedMethodID(env, "<init>", "(Z)V"); if (consId) return env->NewObject(jMixedClass, consId, mixed.get_bool()); } case type_DateTime: { time_t timeValue = mixed.get_datetime().get_datetime(); jclass jDateClass = env->FindClass("java/util/Date"); if (jDateClass == NULL) { ThrowException(env, ClassNotFound, "Date"); return NULL; } jmethodID jDateConsId = env->GetMethodID(jDateClass, "<init>", "(J)V"); if (jDateConsId == NULL) { ThrowException(env, NoSuchMethod, "Date", "<init>"); return NULL; } jobject jDate = env->NewObject(jDateClass, jDateConsId, static_cast<jlong>(timeValue)); jmethodID consId = GetMixedMethodID(env, "<init>", "(Ljava/util/Date;)V"); if (consId) return env->NewObject(jMixedClass, consId, jDate); } case type_Binary: { BinaryData binaryData = mixed.get_binary(); jmethodID consId = GetMixedMethodID(env, "<init>", "(Ljava/nio/ByteBuffer;)V"); if (consId) { jobject jByteBuffer = env->NewDirectByteBuffer(const_cast<char*>(binaryData.data()), binaryData.size()); return env->NewObject(jMixedClass, consId, jByteBuffer); } } case type_Table: { // param input: Table* t. TR((env, " --Mixed(type_Table)\n")); jmethodID consId = GetMixedMethodID(env, "<init>", "(Lio/realm/internal/ColumnType;)V"); jobject jColumnType = NULL; // GetJColumnTypeFromColumnType(env, type_Table); if (consId) return env->NewObject(jMixedClass, consId, jColumnType); } case type_Mixed: break; case type_Link: break; case type_LinkList: break; } return NULL; }
int64_t operator()(BinaryData v) const { return v.size(); }
/// compare tow binary data blocks for equality inline bool CompareBinaryData(const BinaryData& data1, const BinaryData& data2) { return data1.size() == data2.size() && 0 == memcmp(&data1[0], &data2[0], data1.size()); }