예제 #1
0
bool decodeString(folly::ByteRange &br, char *src, int64_t max,
                  std::string &str) {
  int64_t strLen = folly::decodeVarint(br);
  int64_t off = br.start() - (uint8_t *)src;
  if (off + strLen > max) {
    LOG(ERROR) << "Not enough room with " << max << " to decode " << strLen
               << " at " << off;
    return false;
  }
  str.assign((const char *)(br.start()), strLen);
  br.advance(strLen);
  return true;
}
예제 #2
0
// MD5 encode using openssl
std::string md5Encode(folly::ByteRange text) {
  static_assert(MD5_DIGEST_LENGTH == 16, "");

  unsigned char digest[MD5_DIGEST_LENGTH];
  MD5(text.begin(), text.size(), digest);

  // convert digest to hex string
  std::ostringstream ss;
  ss << std::hex << std::setfill('0');
  for(int i = 0; i < MD5_DIGEST_LENGTH; i++) {
    ss << std::setw(2) << (unsigned int)digest[i];
  }
  return ss.str();
}
예제 #3
0
// Base64 encode using openssl
std::string base64Encode(folly::ByteRange text) {
  std::string result;
  BIO *b64 = BIO_new(BIO_f_base64());
  if (b64 == nullptr) {
    return result;
  }
  BIO *bmem = BIO_new(BIO_s_mem());
  if (bmem == nullptr) {
    BIO_free_all(b64);
    return result;
  }
  BUF_MEM *bptr;

  // chain base64 filter with the memory buffer
  // so that text will be encoded by base64 and flushed to buffer
  BIO *chain = BIO_push(b64, bmem);
  if (chain == nullptr) {
    BIO_free_all(b64);
    return result;
  }
  BIO_set_flags(chain, BIO_FLAGS_BASE64_NO_NL);
  BIO_write(chain, text.begin(), text.size());
  if (BIO_flush(chain) != 1) {
    BIO_free_all(chain);
    return result;
  }

  BIO_get_mem_ptr(chain, &bptr);

  if (bptr && bptr->length > 0) {
    result = std::string((char *)bptr->data, bptr->length);
  }

  // free the whole BIO chain (b64 and mem)
  BIO_free_all(chain);
  return result;
}
예제 #4
0
void AsciiSerializedRequest::addString(folly::ByteRange range) {
  assert(iovsCount_ < kMaxIovs);
  iovs_[iovsCount_].iov_base = const_cast<unsigned char*>(range.begin());
  iovs_[iovsCount_].iov_len = range.size();
  ++iovsCount_;
}