Updates to OEMCrypto API, OPK, ODK, and unit tests. See the file CHANGELOG.md for details.
48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
// Copyright 2022 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
|
|
#include "string_format.h"
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <memory>
|
|
|
|
namespace wvutil {
|
|
|
|
bool FormatString(std::string* out, const char* fmt, ...) {
|
|
if (out == nullptr || fmt == nullptr) return false;
|
|
|
|
va_list vlist;
|
|
va_start(vlist, fmt);
|
|
const bool result = VFormatString(out, fmt, vlist);
|
|
va_end(vlist);
|
|
return result;
|
|
}
|
|
|
|
bool VFormatString(std::string* out, const char* fmt, va_list vlist) {
|
|
if (out == nullptr || fmt == nullptr) return false;
|
|
|
|
va_list vlist_copy;
|
|
va_copy(vlist_copy, vlist);
|
|
const int desired_size = vsnprintf(nullptr, 0, fmt, vlist_copy);
|
|
va_end(vlist_copy);
|
|
|
|
if (desired_size < 0) return false;
|
|
const size_t buffer_size =
|
|
static_cast<size_t>(desired_size) + 1; // +1 for null
|
|
std::unique_ptr<char[]> buffer(new char[buffer_size]);
|
|
|
|
const int actual_size = vsnprintf(buffer.get(), buffer_size, fmt, vlist);
|
|
|
|
if (actual_size != desired_size) return false;
|
|
|
|
out->assign(buffer.get(), actual_size);
|
|
return true;
|
|
}
|
|
|
|
} // namespace wvutil
|