Ejemplo n.º 1
0
bool ConfigShared::set_option(Slice name, Slice value) {
  if (value.empty()) {
    return config_pmc_->erase(name.str()) != 0;
  } else {
    return config_pmc_->set(name.str(), value.str()) != 0;
  }
}
Ejemplo n.º 2
0
        /// Cook a cookie for use
        ///
        /// \param result  ref to memory cell that receives cookie if status is
        ///                success
        ///
        /// \note this call should be a pretty rare because it actually does
        ///       lookup for an entry in database
        Status cook(const Slice &name, Cookie &cookie)
        {
            Status s;
            assert( !name.empty() );

            std::string v;
            s = meta.Get(name, v);
            if (s.ok())
            {
                if (Cookie::corrupted(v))
                { return Status::Corruption("Invalid sandwich mapping entry"); }
                cookie = v;
                return s;
            }
            else if (s.IsNotFound())
            {
                Cookie nextCookie;
                s = seq.Next(nextCookie);
                if (s.ok() && nextCookie == 0) s = seq.Next(nextCookie); // skip meta part
                if (s.ok()) s = meta.Put(name, nextCookie);
                if (s.ok()) cookie = nextCookie;
                return s;
            }
            else
            { return s; }
        }
Ejemplo n.º 3
0
/** Determine whether a signature is canonical.
    Canonical signatures are important to protect against signature morphing
    attacks.
    @param vSig the signature data
    @param sigLen the length of the signature
    @param strict_param whether to enforce strictly canonical semantics

    @note For more details please see:
    https://ripple.com/wiki/Transaction_Malleability
    https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
    https://github.com/sipa/bitcoin/commit/58bc86e37fda1aec270bccb3df6c20fbd2a6591c
*/
boost::optional<ECDSACanonicality>
ecdsaCanonicality (Slice const& sig)
{
    static uint264 const G(
        "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");

    // The format of a signature should be:
    // <30> <len> [ <02> <lenR> <R> ] [ <02> <lenS> <S> ]
    if ((sig.size() < 8) || (sig.size() > 72))
        return boost::none;
    if ((sig[0] != 0x30) || (sig[1] != (sig.size() - 2)))
        return boost::none;
    Slice p = sig + 2;
    auto r = sigPart(p);
    auto s = sigPart(p);
    if (! r || ! s || ! p.empty())
        return boost::none;

    uint264 R(sliceToHex(*r));
    uint264 S(sliceToHex(*s));

    if (R >= G)
        return boost::none;
    if (S >= G)
        return boost::none;
    // (R,S) and (R,G-S) are canonical,
    // but is fully canonical when S <= G-S
    auto const Sp = G - S;
    if (S > Sp)
        return ECDSACanonicality::canonical;
    return ECDSACanonicality::fullyCanonical;
}
Ejemplo n.º 4
0
static void signal_safe_write_data(Slice data) {
#if TD_PORT_POSIX
  while (!data.empty()) {
    auto res = write(2, data.begin(), data.size());
    if (res < 0 && errno == EINTR) {
      continue;
    }
    if (res <= 0) {
      break;
    }

    if (res > 0) {
      data.remove_prefix(res);
    }
  }
#elif TD_PORT_WINDOWS
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
  HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
  DWORD bytes_written;
  WriteFile(stderr_handle, data.data(), static_cast<DWORD>(data.size()), &bytes_written, nullptr);
#else
// there is no stderr
#endif
#endif
}
Ejemplo n.º 5
0
bool
RsaSha256::parsePayload (Slice s)
{
    // The payload may not be empty
    if (!s.empty() && detail::parsePayloadHelper (s, modulus_, signature_))
        return true;

    // Clear the state
    modulus_.clear();
    signature_.clear();
    return false;
}
Ejemplo n.º 6
0
        DB open(const DBEnv &env, const DBTxn &txn, const char *fname, const char *dbname, DBTYPE dbtype, uint32_t flags, int mode) const {
            ::DB *db;
            int r = db_create(&db, env.env(), 0);
            handle_ft_retval(r);

            if (_readpagesize) {
                r = db->set_readpagesize(db, _readpagesize);
                handle_ft_retval(r);
            }

            if (_compression_method >= 0) {
                r = db->set_compression_method(db, TOKU_COMPRESSION_METHOD(_compression_method));
                handle_ft_retval(r);
            }

            if (_fanout) {
                r = db->set_fanout(db, _fanout);
                handle_ft_retval(r);
            }

            if (_memcmp_magic) {
                r = db->set_memcmp_magic(db, _memcmp_magic);
                handle_ft_retval(r);
            }

            if (_pagesize) {
                r = db->set_pagesize(db, _pagesize);
                handle_ft_retval(r);
            }

            const DBTxn *txnp = &txn;
            DBTxn writeTxn;
            if (txn.is_read_only()) {
                writeTxn = DBTxn(env, DB_SERIALIZABLE);
                txnp = &writeTxn;
            }

            r = db->open(db, txnp->txn(), fname, dbname, dbtype, flags, mode);
            handle_ft_retval(r);

            if (!_descriptor.empty()) {
                DBT desc = _descriptor.dbt();
                r = db->change_descriptor(db, txnp->txn(), &desc, DB_UPDATE_CMP_DESCRIPTOR);
                handle_ft_retval(r);
            }

            if (txn.is_read_only()) {
                writeTxn.commit();
            }

            return DB(db, true);
        }
Ejemplo n.º 7
0
tl_object_ptr<td_api::OptionValue> ConfigShared::get_option_value_object(Slice value) {
  if (value.empty()) {
    return make_tl_object<td_api::optionValueEmpty>();
  }

  switch (value[0]) {
    case 'B':
      if (value == "Btrue") {
        return make_tl_object<td_api::optionValueBoolean>(true);
      }
      if (value == "Bfalse") {
        return make_tl_object<td_api::optionValueBoolean>(false);
      }
      break;
    case 'I':
      return make_tl_object<td_api::optionValueInteger>(to_integer<int32>(value.substr(1)));
    case 'S':
      return make_tl_object<td_api::optionValueString>(value.substr(1).str());
  }

  return make_tl_object<td_api::optionValueString>(value.str());
}
Ejemplo n.º 8
0
	void smart_fill_value(const Slice& value, ValueObject& valueobject)
	{
		if (value.empty())
		{
			valueobject.type = EMPTY;
			return;
		}
		int64_t intv;
		double doublev;
		char first_char = value.data()[0];
		if (first_char != '+' && value.data()[0] != '-'
		        && (first_char < '0' || first_char > '9'))
		{
			valueobject.type = RAW;
			char* v = const_cast<char*>(value.data());
			valueobject.v.raw = new Buffer(v, 0, value.size());
		}
		else
		{
			if (raw_toint64(value.data(), value.size(), intv))
			{
				valueobject.type = INTEGER;
				valueobject.v.int_v = intv;
			}
			else if (raw_todouble(value.data(), value.size(), doublev))
			{
				valueobject.type = DOUBLE;
				valueobject.v.double_v = doublev;
			}
			else
			{
				valueobject.type = RAW;
				char* v = const_cast<char*>(value.data());
				valueobject.v.raw = new Buffer(v, 0, value.size());
			}
		}

	}
Ejemplo n.º 9
0
Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
  data->clear();
  SequentialFile* file;
  Status s = env->NewSequentialFile(fname, &file);
  if (!s.ok()) {
    return s;
  }
  static const int kBufferSize = 8192;
  char* space = new char[kBufferSize];
  while (true) {
    Slice fragment;
    s = file->Read(kBufferSize, &fragment, space);
    if (!s.ok()) {
      break;
    }
    data->append(fragment.data(), fragment.size());
    if (fragment.empty()) {
      break;
    }
  }
  delete[] space;
  delete file;
  return s;
}
Ejemplo n.º 10
0
Status VersionEdit::DecodeFrom(const Slice& src) {
  Clear();
  Slice input = src;
  const char* msg = NULL;
  uint32_t tag;

  // Temporary storage for parsing
  int level;
  uint64_t number;
  FileMetaData f;
  Slice str;
  InternalKey key;

  while (msg == NULL && GetVarint32(&input, &tag)) {
    switch (tag) {
      case kComparator:
        if (GetLengthPrefixedSlice(&input, &str)) {
          comparator_ = str.ToString();
          has_comparator_ = true;
        } else {
          msg = "comparator name";
        }
        break;

      case kLogNumber:
        if (GetVarint64(&input, &log_number_)) {
          has_log_number_ = true;
        } else {
          msg = "log number";
        }
        break;

      case kPrevLogNumber:
        if (GetVarint64(&input, &prev_log_number_)) {
          has_prev_log_number_ = true;
        } else {
          msg = "previous log number";
        }
        break;

      case kNextFileNumber:
        if (GetVarint64(&input, &next_file_number_)) {
          has_next_file_number_ = true;
        } else {
          msg = "next file number";
        }
        break;

      case kLastSequence:
        if (GetVarint64(&input, &last_sequence_)) {
          has_last_sequence_ = true;
        } else {
          msg = "last sequence number";
        }
        break;

      case kCompactPointer:
        if (GetLevel(&input, &level) &&
            GetInternalKey(&input, &key)) {
          compact_pointers_.push_back(std::make_pair(level, key));
        } else {
          msg = "compaction pointer";
        }
        break;

      case kDeletedFile:
        if (GetLevel(&input, &level) &&
            GetVarint64(&input, &number)) {
          deleted_files_.insert(std::make_pair(level, number));
        } else {
          msg = "deleted file";
        }
        break;

      case kNewFile:
        if (GetLevel(&input, &level) &&
            GetVarint64(&input, &f.number) &&
            GetVarint64(&input, &f.file_size) &&
            GetInternalKey(&input, &f.smallest) &&
            GetInternalKey(&input, &f.largest)) {
          new_files_.push_back(std::make_pair(level, f));
        } else {
          msg = "new-file entry";
        }
        break;

      default:
        msg = "unknown tag";
        break;
    }
  }

  if (msg == NULL && !input.empty()) {
    msg = "invalid tag";
  }

  Status result;
  if (msg != NULL) {
    result = Status::Corruption("VersionEdit", msg);
  }
  return result;
}
Ejemplo n.º 11
0
    void testDynamic ()
    {
        testcase ("Dynamic");

        Slice m = makeSlice (knownMessage);

        std::string test = "aaabc";

        RsaSha256 f;
        BEAST_EXPECT (f.sign (goodKey, m));

        {
            auto const f2 = loadFulfillment(makeSlice(to_blob(f)));
            BEAST_EXPECT (f2);
            BEAST_EXPECT (f == *f2);
        }

        // Generate and verify the condition for this fulfillment:
        auto const c = f.condition();

        {
            auto c1 = loadCondition (to_string(c));
            BEAST_EXPECT (c1);
            BEAST_EXPECT (c == *c1);

            auto c2 = loadCondition (makeSlice(to_blob(c)));
            BEAST_EXPECT (c2);
            BEAST_EXPECT (c == *c2);
        }

        // First check against incorrect conditions, using both
        // correct, incorrect and empty buffers.
        char const* const ccs[] =
        {
            "cc:0:3:PWh2oBRt6FdusjlahY3hIT0bksZbd53zozHP1aRYRUY:256",
            "cc:1:25:XkflBmyISKuevH8-850LuMrzN-HT1Ds9zKUEzaZ2Wk0:103",
            "cc:2:2b:d3O4epRCo_3rj17Bf3v8hp5ig7vq84ivPok07T9Rdl0:146",
            "cc:3:11:Mjmrcm06fOo-3WOEZu9YDSNfqmn0lj4iOsTVEurtCdI:518",
            "cc:4:20:O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik:96"
        };

        for (auto cc : ccs)
        {
            auto nc = loadCondition (cc);

            if (BEAST_EXPECT (nc && nc != c))
            {
                check (f, nc.get(), makeSlice(test), m);
                check (f, nc.get(), m, m);
                check (f, nc.get(), { }, m);
            }
        }

        // Now try against the correct condition with various
        // buffers - most are incorrect, some are correct.
        do
        {
            for (Slice t = makeSlice (test); !t.empty(); t += 1)
                check (f, c, t, m);
        } while (std::next_permutation(test.begin(), test.end()));

        // And with an empty buffer:
        check (f, c, Slice { }, m);
    }
Ejemplo n.º 12
0
    void testKnown ()
    {
        testcase ("Known");

        Slice m = makeSlice (knownMessage);
        std::string test = "aaabc";

        // Load and test string and binary and text
        // serialization & deserialization
        auto const f = loadFulfillment(knownFulfillment);
        BEAST_EXPECT (f);
        BEAST_EXPECT (to_string (*f) == knownFulfillment);

        {
            auto const f2 = loadFulfillment(makeSlice(to_blob(*f)));
            BEAST_EXPECT (f2);
            BEAST_EXPECT (*f == *f2);
        }

        // Verify the condition for this fulfillment and test
        // binary and text serialization & deserialization
        auto const c = f->condition();
        BEAST_EXPECT (to_string(c) == knownCondition);

        {
            auto c1 = loadCondition (knownCondition);
            BEAST_EXPECT (c1);
            BEAST_EXPECT (c == *c1);

            auto c2 = loadCondition (makeSlice(to_blob(c)));
            BEAST_EXPECT (c2);
            BEAST_EXPECT (c == *c2);
        }

        // First check against incorrect conditions, using both
        // correct, incorrect and empty buffers.
        char const* const ccs[] =
        {
            "cc:0:3:PWh2oBRt6FdusjlahY3hIT0bksZbd53zozHP1aRYRUY:256",
            "cc:1:25:XkflBmyISKuevH8-850LuMrzN-HT1Ds9zKUEzaZ2Wk0:103",
            "cc:2:2b:d3O4epRCo_3rj17Bf3v8hp5ig7vq84ivPok07T9Rdl0:146",
            "cc:3:11:Mjmrcm06fOo-3WOEZu9YDSNfqmn0lj4iOsTVEurtCdI:518",
            "cc:4:20:O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik:96"
        };

        for (auto cc : ccs)
        {
            auto nc = loadCondition (cc);

            if (BEAST_EXPECT (nc && nc != c))
            {
                check (*f, nc.get(), makeSlice(test), m);
                check (*f, nc.get(), m, m);
                check (*f, nc.get(), { }, m);
            }
        }

        // Now try against the correct condition with various
        // buffers - most are incorrect, some are correct.
        do
        {
            for (Slice t = makeSlice (test); !t.empty(); t += 1)
                check (*f, c, t, m);
        } while (std::next_permutation(test.begin(), test.end()));

        // And with an empty buffer:
        check (*f, c, Slice { }, m);

        // Under the existing spec, multiple messages sharing
        // the same key should generate the same fulfillment:
        {
            std::array<std::uint8_t, 3> aaa {{ 'a', 'a', 'a' }};
            std::array<std::uint8_t, 3> bbb {{ 'b', 'b', 'b' }};

            RsaSha256 f1;
            BEAST_EXPECT (f1.sign (goodKey, makeSlice (aaa)));

            RsaSha256 f2;
            BEAST_EXPECT (f2.sign (goodKey, makeSlice (bbb)));

            BEAST_EXPECT (f1.condition () == f2.condition ());
        }
    }