Exemple #1
0
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();
        }
    }
}
 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;
 }
Exemple #3
0
inline BinaryData BinaryColumn::get(size_t ndx) const noexcept
{
    REALM_ASSERT_DEBUG(ndx < size());
    if (root_is_leaf()) {
        bool is_big = m_array->get_context_flag();
        BinaryData ret;
        if (!is_big) {
            // Small blobs root leaf
            ArrayBinary* leaf = static_cast<ArrayBinary*>(m_array.get());
            ret = leaf->get(ndx);
        }
        else {
            // Big blobs root leaf
            ArrayBigBlobs* leaf = static_cast<ArrayBigBlobs*>(m_array.get());
            ret = leaf->get(ndx);
        }
        if (!m_nullable && ret.is_null())
            return BinaryData("", 0); // return empty string (non-null)
        return ret;
    }

    // Non-leaf root
    std::pair<MemRef, size_t> p = m_array->get_bptree_leaf(ndx);
    const char* leaf_header = p.first.get_addr();
    size_t ndx_in_leaf = p.second;
    Allocator& alloc = m_array->get_alloc();
    bool is_big = Array::get_context_flag_from_header(leaf_header);
    if (!is_big) {
        // Small blobs
        return ArrayBinary::get(leaf_header, ndx_in_leaf, alloc);
    }
    // Big blobs
    return ArrayBigBlobs::get(leaf_header, ndx_in_leaf, alloc);
}
Exemple #4
0
// 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));
    }
}
BinaryData ZeroConfContainer::getNewZCkey()
{
   uint32_t newId = topId_.fetch_add(1, memory_order_relaxed);
   BinaryData newKey = READHEX("ffff");
   newKey.append(WRITE_UINT32_BE(newId));

   return newKey;
}
 /// 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);
    }
 }
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());
}
CryptoPP::ECP CryptoECDSA::Get_secp256k1_ECP(void)
{
   static bool firstRun = true;
   static CryptoPP::Integer intN;
   static CryptoPP::Integer inta;
   static CryptoPP::Integer intb;

   static BinaryData N;
   static BinaryData a;
   static BinaryData b;

   if(firstRun)
   {
      firstRun = false;
      N = BinaryData::CreateFromHex(
            "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
      a = BinaryData::CreateFromHex(
            "0000000000000000000000000000000000000000000000000000000000000000");
      b = BinaryData::CreateFromHex(
            "0000000000000000000000000000000000000000000000000000000000000007");

      intN.Decode( N.getPtr(),  N.getSize(),  UNSIGNED);
      inta.Decode( a.getPtr(),  a.getSize(),  UNSIGNED);
      intb.Decode( b.getPtr(),  b.getSize(),  UNSIGNED);
   }

   
   return CryptoPP::ECP(intN, inta, intb);
}
Exemple #9
0
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);
    }
}
Exemple #10
0
bool BinaryData::startsWith(BinaryData const & matchStr) const
{
   if(matchStr.getSize() > getSize())
      return false;

   for(uint32_t i=0; i<matchStr.getSize(); i++)
      if(matchStr[i] != (*this)[i])
         return false;

   return true;
}
   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 ZeroConfContainer::updateZCinDB(const vector<BinaryData>& keysToWrite, 
   const vector<BinaryData>& keysToDelete)
{
   //should run in its own thread to make sure we can get a write tx
   DB_SELECT dbs = BLKDATA;
   if (db_->getDbType() != ARMORY_DB_SUPER)
      dbs = HISTORY;

   LMDBEnv::Transaction tx;
   db_->beginDBTransaction(&tx, dbs, LMDB::ReadWrite);

   for (auto& key : keysToWrite)
   {
      StoredTx zcTx;
      zcTx.createFromTx(txMap_[key], true, true);
      db_->putStoredZC(zcTx, key);
   }

   for (auto& key : keysToDelete)
   {
      BinaryData keyWithPrefix;
      if (key.getSize() == 6)
      {
         keyWithPrefix.resize(7);
         uint8_t* keyptr = keyWithPrefix.getPtr();
         keyptr[0] = DB_PREFIX_ZCDATA;
         memcpy(keyptr + 1, key.getPtr(), 6);
      }
      else
         keyWithPrefix = key;

      LDBIter dbIter(db_->getIterator(dbs));

      if (!dbIter.seekTo(keyWithPrefix))
         continue;

      vector<BinaryData> ktd;

      do
      {
         BinaryDataRef thisKey = dbIter.getKeyRef();
         if (!thisKey.startsWith(keyWithPrefix))
            break;

         ktd.push_back(thisKey);
      } 
      while (dbIter.advanceAndRead(DB_PREFIX_ZCDATA));

      for (auto Key : ktd)
         db_->deleteValue(dbs, Key);
   }
}
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);
    }
}
Exemple #14
0
BinaryData DBUtils::getMissingHashesKey(uint32_t id)
{
   BinaryData bd;
   bd.resize(4);

   id &= 0x00FFFFFF; //24bit ids top
   id |= DB_PREFIX_MISSING_HASHES << 24;
   
   auto keyPtr = (uint32_t*)bd.getPtr();
   *keyPtr = id;

   return bd;
}
	StaticMesh::StaticMesh(const Path& path)
	{
		const BinaryData file{ path };
		auto* cursor = file.get_data();

		// Get the number of vertices
		const auto numVerts = *reinterpret_cast<const uint32*>(cursor);

		cursor += sizeof(uint32);

		// Load in vertices
		this->vertices = Array<Vertex>(reinterpret_cast<const Vertex*>(cursor), numVerts);
	}
SecureBinaryData CryptoECDSA::InvMod(const SecureBinaryData& m)
{
   static BinaryData N = BinaryData::CreateFromHex(
           "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
   CryptoPP::Integer cppM;
   CryptoPP::Integer cppModulo;
   cppM.Decode(m.getPtr(), m.getSize(), UNSIGNED);
   cppModulo.Decode(N.getPtr(), N.getSize(), UNSIGNED);
   CryptoPP::Integer cppResult = cppM.InverseMod(cppModulo);
   SecureBinaryData result(32);
   cppResult.Encode(result.getPtr(), result.getSize(), UNSIGNED);
   return result;
}
BinaryData CryptoECDSA::ECAddPoints(BinaryData const & Ax, 
                                    BinaryData const & Ay,
                                    BinaryData const & Bx,
                                    BinaryData const & By)
{
   CryptoPP::ECP ecp = Get_secp256k1_ECP();
   CryptoPP::Integer intAx, intAy, intBx, intBy, intCx, intCy;

   intAx.Decode(Ax.getPtr(), Ax.getSize(), UNSIGNED);
   intAy.Decode(Ay.getPtr(), Ay.getSize(), UNSIGNED);
   intBx.Decode(Bx.getPtr(), Bx.getSize(), UNSIGNED);
   intBy.Decode(By.getPtr(), By.getSize(), UNSIGNED);


   BTC_ECPOINT A(intAx, intAy);
   BTC_ECPOINT B(intBx, intBy);

   BTC_ECPOINT C = ecp.Add(A,B);

   BinaryData Cbd(64);
   C.x.Encode(Cbd.getPtr(),    32, UNSIGNED);
   C.y.Encode(Cbd.getPtr()+32, 32, UNSIGNED);

   return Cbd;
}
TxOut BlockDataViewer::getTxOutCopy(
   const BinaryData& txHash, uint16_t index) const
{
   LMDBEnv::Transaction tx;
   db_->beginDBTransaction(&tx, STXO, LMDB::ReadOnly);
      

   BinaryData bdkey = db_->getDBKeyForHash(txHash);

   if (bdkey.getSize() == 0)
      return TxOut();

   return db_->getTxOutCopy(bdkey, index);
}
/////////////////////////////////////////////////////////////////////////////
// Deterministically generate new public key using a chaincode
SecureBinaryData CryptoECDSA::ComputeChainedPublicKey(
                                SecureBinaryData const & binPubKey,
                                SecureBinaryData const & chainCode,
                                SecureBinaryData* multiplierOut)
{
   if(CRYPTO_DEBUG)
   {
      cout << "ComputeChainedPUBLICKey:" << endl;
      cout << "   BinPub: " << binPubKey.toHexStr() << endl;
      cout << "   BinChn: " << chainCode.toHexStr() << endl;
   }
   static SecureBinaryData SECP256K1_ORDER_BE = SecureBinaryData::CreateFromHex(
           "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");

   // Added extra entropy to chaincode by xor'ing with hash256 of pubkey
   BinaryData chainMod  = binPubKey.getHash256();
   BinaryData chainOrig = chainCode.getRawCopy();
   BinaryData chainXor(32);
      
   for(uint8_t i=0; i<8; i++)
   {
      uint8_t offset = 4*i;
      *(uint32_t*)(chainXor.getPtr()+offset) =
                           *(uint32_t*)( chainMod.getPtr()+offset) ^ 
                           *(uint32_t*)(chainOrig.getPtr()+offset);
   }

   // Parse the chaincode as a big-endian integer
   CryptoPP::Integer mult;
   mult.Decode(chainXor.getPtr(), chainXor.getSize(), UNSIGNED);

   // "new" init as "old", to make sure it's initialized on the correct curve
   BTC_PUBKEY oldPubKey = ParsePublicKey(binPubKey); 
   BTC_PUBKEY newPubKey = ParsePublicKey(binPubKey);

   // Let Crypto++ do the EC math for us, serialize the new public key
   newPubKey.SetPublicElement( oldPubKey.ExponentiatePublicElement(mult) );

   if(multiplierOut != NULL)
      (*multiplierOut) = SecureBinaryData(chainXor);

   //LOGINFO << "Computed new chained public key using:";
   //LOGINFO << "   Public key: " << binPubKey.toHexStr().c_str();
   //LOGINFO << "   PubKeyHash: " << chainMod.toHexStr().c_str();
   //LOGINFO << "   Chaincode:  " << chainOrig.toHexStr().c_str();
   //LOGINFO << "   Multiplier: " << chainXor.toHexStr().c_str();

   return CryptoECDSA::SerializePublicKey(newPubKey);
}
TxOut BlockDataViewer::getTxOutCopy(
   const BinaryData& txHash, uint16_t index) const
{
   LMDBEnv::Transaction tx;
   db_->beginDBTransaction(&tx, HISTORY, LMDB::ReadOnly);
      

   BinaryData bdkey;
   db_->getStoredTx_byHash(txHash, nullptr, &bdkey);

   if (bdkey.getSize() == 0)
      return TxOut();

   return db_->getTxOutCopy(bdkey, index);
}
Exemple #21
0
size_t BinaryData::Append(const BinaryData &inData)
{
    if (inData.DataSize() == 0)
        return 0;

    if (m_pBuffer != NULL)
        SetData(m_pBuffer, DataSize());
    if (Size() - DataSize() < inData.DataSize()) {
        mBuffer.resize(Size() + inData.DataSize(), 0);
        mBufferSize = mBuffer.size();
    }
    memcpy_s((void *)(*this + DataSize()), Size() - DataSize(), inData, inData.DataSize());
    mDataSize += inData.DataSize();

    return DataSize();
}
Exemple #22
0
int BinaryData::Compare(const BinaryData & inData) const
{
    int diff((int)DataSize() - (int)inData.DataSize());
    if (!diff && DataSize() > 0)
        diff = memcmp((const void *)*this, (const void *)inData, DataSize());
    return diff;
}
Exemple #23
0
 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;
     });
 }
Exemple #24
0
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;
    }
}
BigInteger Helper::CalcLowerU(const BigInteger& A, const BigInteger& B,
                              unsigned int uiPadSize)
{
   // u = SHA1(PAD(A) | PAD(B))
   BinaryData temp;
   Helper::ToBinaryData(A, temp);
   Helper::Pad(temp, uiPadSize);

   BinaryData temp2;
   Helper::ToBinaryData(B, temp2);
   Helper::Pad(temp2, uiPadSize);

   temp.insert(temp.end(), temp2.begin(), temp2.end());

   BinaryData ubin = Sha1(temp);
   return Helper::ToInteger<BigInteger>(ubin);
}
bool CryptoECDSA::ECVerifyPoint(BinaryData const & x,
                                BinaryData const & y)
{
   BTC_PUBKEY cppPubKey;

   CryptoPP::Integer pubX;
   CryptoPP::Integer pubY;
   pubX.Decode(x.getPtr(), x.getSize(), UNSIGNED);
   pubY.Decode(y.getPtr(), y.getSize(), UNSIGNED);
   BTC_ECPOINT publicPoint(pubX, pubY);

   // Initialize the public key with the ECP point just created
   cppPubKey.Initialize(CryptoPP::ASN1::secp256k1(), publicPoint);

   // Validate the public key -- not sure why this needs a PRNG
   BTC_PRNG prng;
   return cppPubKey.Validate(prng, 3);
}
Exemple #27
0
// The unit test that waits for EOS for the
// transcoded file and repeate it again
void TestSchedulePlay::testAlways()
{
    klk::test::printOut( "\nTranscode test (schedule playback) ... ");

    m_scheduler.start();

    // wait for awhile
    u_int count = 4;

    /*
      We can fail one time at startup and it can be not finished
      Thus
      getRunningCount() == count
      or
      getRunningCount() == count -1
      or
      getRunningCount() == count -2
    */
    time_t duration = SCHEDULE_INTERVAL * (count + 2);

    sleep(duration);

    // stop all others
    m_scheduler.stop();

    // check result in the started thread
    m_scheduler.checkResult();

    // check running count
    // all should be in EOS state
    TaskInfoList tasks = getTaskList();
    CPPUNIT_ASSERT(tasks.size() == 1);

    for (TaskInfoList::iterator task = tasks.begin(); task != tasks.end(); task++)
    {
        CPPUNIT_ASSERT((*task)->getRunningCount() >= count);
    }

    // test route
    BinaryData route =
        base::Utils::readWholeDataFromFile(trans::test::OUTPUTROUTE);
    CPPUNIT_ASSERT(route.empty() == false);
}
Exemple #28
0
inline void BinaryColumn::add(BinaryData value)
{
    if (value.is_null() && !m_nullable)
        throw LogicError(LogicError::column_not_nullable);

    size_t row_ndx = realm::npos;
    bool add_zero_term = false;
    size_t num_rows = 1;
    do_insert(row_ndx, value, add_zero_term, num_rows); // Throws
}
Exemple #29
0
bool CHttpClient::ContentHandler(const BinaryData & inData)
{
    UNREFERENCED_PARAMETER(inData);

#ifdef _DEBUG
    _tprintf(_T("%s\n"), inData.HexDump().c_str());
#endif // _DEBUG

    return true;
}
BinaryData CryptoECDSA::ECInverse(BinaryData const & Ax, 
                                  BinaryData const & Ay)
                                  
{
   CryptoPP::ECP ecp = Get_secp256k1_ECP();
   CryptoPP::Integer intAx, intAy, intCx, intCy;

   intAx.Decode(Ax.getPtr(), Ax.getSize(), UNSIGNED);
   intAy.Decode(Ay.getPtr(), Ay.getSize(), UNSIGNED);

   BTC_ECPOINT A(intAx, intAy);
   BTC_ECPOINT C = ecp.Inverse(A);

   BinaryData Cbd(64);
   C.x.Encode(Cbd.getPtr(),    32, UNSIGNED);
   C.y.Encode(Cbd.getPtr()+32, 32, UNSIGNED);

   return Cbd;
}