Added hex to Object and string_utf8.

This commit is contained in:
Jari Sundell
2026-06-03 21:22:16 +02:00
committed by GitHub
parent d0fc48cf7b
commit f1cfe8ad72
7 changed files with 180 additions and 71 deletions
+16 -5
View File
@@ -16,6 +16,7 @@
#include "torrent/exceptions.h"
#include "torrent/object.h"
#include "utils/functional.h"
#include "utils/base64.h"
namespace rpc {
@@ -62,18 +63,28 @@ json_to_object(const json& value) {
}
}
// Note: Only throw rpc_errors here.
json
object_to_json(const torrent::Object& object) noexcept {
object_to_json(const torrent::Object& object) {
switch (object.type()) {
case torrent::Object::TYPE_VALUE:
return object.as_value();
case torrent::Object::TYPE_STRING:
if (object.flags() & torrent::Object::flag_base64) {
std::vector<uint8_t> binary_data;
torrent::utils::transform_from_hex(object.as_string(), binary_data);
if (object.flags() & torrent::Object::flag_as_binary) {
return json::binary(binary_data);
// We should optimize our imported json library to support copying base64 strings directly.
if (object.flags() & torrent::Object::flag_base64) {
auto binary_data = utils::base64_to_vector_unsafe(object.as_string());
if (!binary_data.has_value())
throw rpc_error(JSONRPC_INTERNAL_ERROR, "invalid base64 string in base64-as-binary object");
return json::binary(*binary_data);
}
return json::binary({object.as_string().begin(), object.as_string().end()});
}
return object.as_string();
+13 -1
View File
@@ -278,8 +278,20 @@ object_to_xmlrpc(xmlrpc_env* env, const torrent::Object& object) {
case torrent::Object::TYPE_STRING:
{
if (object.flags() & torrent::Object::flag_base64)
if (object.flags() & torrent::Object::flag_binary) {
// This causes decode-and-reencode for base64, as XMLRPC-C doesn't allow us to pass base64 strings.
if (object.flags() & torrent::Object::flag_base64) {
auto binary_data = utils::base64_to_vector_unsafe(object.as_string());
if (!binary_data.has_value())
throw torrent::input_error("invalid base64 string in base64-as-binary object");
return xmlrpc_base64_new(env, (const char*)binary_data->data(), binary_data->size());
}
return xmlrpc_base64_new(env, object.as_string().c_str(), object.as_string().size());
}
#ifdef XMLRPC_HAVE_I8
// The versions that support I8 do implicit utf-8 validation.
+11 -2
View File
@@ -152,9 +152,17 @@ void
print_object_xml(const torrent::Object& obj, tinyxml2::XMLPrinter* printer) {
switch (obj.type()) {
case torrent::Object::TYPE_STRING:
if (obj.flags() & torrent::Object::flag_base64) {
if (obj.flags() & torrent::Object::flag_as_binary) {
// It is optimal for tinyxml2 to pass base64 strings directly from torrent::string_utf8 sources.
if (obj.flags() & torrent::Object::flag_base64) {
printer->OpenElement("base64", true);
printer->PushText(obj.as_string().c_str());
printer->CloseElement(true);
break;
}
printer->OpenElement("base64", true);
printer->PushText(obj.as_string().c_str());
printer->PushText(utils::openssl_base64_encode(obj.as_string()).c_str());
printer->CloseElement(true);
break;
}
@@ -169,6 +177,7 @@ print_object_xml(const torrent::Object& obj, tinyxml2::XMLPrinter* printer) {
printer->PushText(std::to_string(obj.as_value()).c_str());
printer->CloseElement(true);
break;
case torrent::Object::TYPE_LIST:
printer->OpenElement("array", true);
printer->OpenElement("data", true);