Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::Blob Class Reference

The Blob class is a static class that holds helper functions for the Blob type. More...

#include <duckdb.hpp>

Static Public Member Functions

static DUCKDB_API idx_t GetStringSize (string_t blob)
 Returns the string size of a blob -> string conversion.
static DUCKDB_API void ToString (string_t blob, char *output)
 Converts a blob to a string, writing the output to the designated output string. The string needs to have space for at least GetStringSize(blob) bytes.
static DUCKDB_API string ToString (string_t blob)
 Convert a blob object to a string.
static DUCKDB_API bool TryGetBlobSize (string_t str, idx_t &result_size, CastParameters &parameters)
 Returns the blob size of a string -> blob conversion.
static DUCKDB_API idx_t GetBlobSize (string_t str)
static DUCKDB_API idx_t GetBlobSize (string_t str, CastParameters &parameters)
static DUCKDB_API void ToBlob (string_t str, data_ptr_t output)
 Convert a string to a blob. This function should ONLY be called after calling GetBlobSize, since it does NOT perform data validation.
static DUCKDB_API string ToBlob (string_t str)
 Convert a string object to a blob.
static DUCKDB_API string ToBlob (string_t str, CastParameters &parameters)
static DUCKDB_API string ToBase64 (string_t blob)
static DUCKDB_API idx_t ToBase64Size (string_t blob)
 Returns the string size of a blob -> base64 conversion.
static DUCKDB_API void ToBase64 (string_t blob, char *output)
 Converts a blob to a base64 string, output should have space for at least ToBase64Size(blob) bytes.
static DUCKDB_API string FromBase64 (string_t blob)
static DUCKDB_API idx_t FromBase64Size (string_t str)
 Returns the string size of a base64 string -> blob conversion.
static DUCKDB_API void FromBase64 (string_t str, data_ptr_t output, idx_t output_size)
 Converts a base64 string to a blob, output should have space for at least FromBase64Size(blob) bytes.

Static Public Attributes

static constexpr const char * HEX_TABLE = "0123456789ABCDEF"
static const int HEX_MAP [256]
static constexpr const char * BASE64_MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 map of index -> base64 character
static constexpr const char BASE64_PADDING = '='
 padding character used in base64 encoding

Detailed Description

The Blob class is a static class that holds helper functions for the Blob type.

Member Function Documentation

◆ GetStringSize()

idx_t duckdb::Blob::GetStringSize ( string_t blob)
static

Returns the string size of a blob -> string conversion.

113136 {
113137 auto data = const_data_ptr_cast(blob.GetData());
113138 auto len = blob.GetSize();
113139 idx_t str_len = 0;
113140 for (idx_t i = 0; i < len; i++) {
113141 if (IsRegularCharacter(data[i])) {
113142 // ascii characters are rendered as-is
113143 str_len++;
113144 } else {
113145 // non-ascii characters are rendered as hexadecimal (e.g. \x00)
113146 str_len += 4;
113147 }
113148 }
113149 return str_len;
113150}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the caller graph for this function:

◆ ToString() [1/2]

void duckdb::Blob::ToString ( string_t blob,
char * output )
static

Converts a blob to a string, writing the output to the designated output string. The string needs to have space for at least GetStringSize(blob) bytes.

113152 {
113153 auto data = const_data_ptr_cast(blob.GetData());
113154 auto len = blob.GetSize();
113155 idx_t str_idx = 0;
113156 for (idx_t i = 0; i < len; i++) {
113157 if (IsRegularCharacter(data[i])) {
113158 // ascii characters are rendered as-is
113159 output[str_idx++] = UnsafeNumericCast<char>(data[i]);
113160 } else {
113161 auto byte_a = data[i] >> 4;
113162 auto byte_b = data[i] & 0x0F;
113163 D_ASSERT(byte_a >= 0 && byte_a < 16);
113164 D_ASSERT(byte_b >= 0 && byte_b < 16);
113165 // non-ascii characters are rendered as hexadecimal (e.g. \x00)
113166 output[str_idx++] = '\\';
113167 output[str_idx++] = 'x';
113168 output[str_idx++] = Blob::HEX_TABLE[byte_a];
113169 output[str_idx++] = Blob::HEX_TABLE[byte_b];
113170 }
113171 }
113172 D_ASSERT(str_idx == GetStringSize(blob));
113173}
static DUCKDB_API idx_t GetStringSize(string_t blob)
Returns the string size of a blob -> string conversion.
Definition duckdb.cpp:113136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ToString() [2/2]

string duckdb::Blob::ToString ( string_t blob)
static

Convert a blob object to a string.

113175 {
113176 auto str_len = GetStringSize(blob);
113177 auto buffer = make_unsafe_uniq_array_uninitialized<char>(str_len);
113178 Blob::ToString(blob, buffer.get());
113179 return string(buffer.get(), str_len);
113180}
static DUCKDB_API void ToString(string_t blob, char *output)
Converts a blob to a string, writing the output to the designated output string. The string needs to ...
Definition duckdb.cpp:113152
Here is the call graph for this function:

◆ TryGetBlobSize()

bool duckdb::Blob::TryGetBlobSize ( string_t str,
idx_t & result_size,
CastParameters & parameters )
static

Returns the blob size of a string -> blob conversion.

113182 {
113183 auto data = const_data_ptr_cast(str.GetData());
113184 auto len = str.GetSize();
113185 str_len = 0;
113186 for (idx_t i = 0; i < len; i++) {
113187 if (data[i] == '\\') {
113188 if (i + 3 >= len) {
113189 string error = StringUtil::Format("Invalid hex escape code encountered in string -> blob conversion of "
113190 "string \"%s\": unterminated escape code at end of blob",
113191 str.GetString());
113192 HandleCastError::AssignError(error, parameters);
113193 return false;
113194 }
113195 if (data[i + 1] != 'x' || Blob::HEX_MAP[data[i + 2]] < 0 || Blob::HEX_MAP[data[i + 3]] < 0) {
113196 string error = StringUtil::Format(
113197 "Invalid hex escape code encountered in string -> blob conversion of string \"%s\": %s",
113198 str.GetString(), string(const_char_ptr_cast(data) + i, 4));
113199 HandleCastError::AssignError(error, parameters);
113200 return false;
113201 }
113202 str_len++;
113203 i += 3;
113204 } else if (data[i] <= 127) {
113205 str_len++;
113206 } else {
113207 string error = StringUtil::Format(
113208 "Invalid byte encountered in STRING -> BLOB conversion of string \"%s\". All non-ascii characters "
113209 "must be escaped with hex codes (e.g. \\xAA)",
113210 str.GetString());
113211 HandleCastError::AssignError(error, parameters);
113212 return false;
113213 }
113214 }
113215 return true;
113216}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
Here is the call graph for this function:

◆ GetBlobSize() [1/2]

idx_t duckdb::Blob::GetBlobSize ( string_t str)
static
113218 {
113219 CastParameters parameters;
113220 return GetBlobSize(str, parameters);
113221}

◆ GetBlobSize() [2/2]

idx_t duckdb::Blob::GetBlobSize ( string_t str,
CastParameters & parameters )
static
113223 {
113224 idx_t str_len;
113225 auto result = Blob::TryGetBlobSize(str, str_len, parameters);
113226 if (!result) {
113227 throw InternalException("Blob::TryGetBlobSize failed but no exception was thrown!?");
113228 }
113229 return str_len;
113230}
static DUCKDB_API bool TryGetBlobSize(string_t str, idx_t &result_size, CastParameters &parameters)
Returns the blob size of a string -> blob conversion.
Definition duckdb.cpp:113182

◆ ToBlob() [1/3]

void duckdb::Blob::ToBlob ( string_t str,
data_ptr_t output )
static

Convert a string to a blob. This function should ONLY be called after calling GetBlobSize, since it does NOT perform data validation.

113232 {
113233 auto data = const_data_ptr_cast(str.GetData());
113234 auto len = str.GetSize();
113235 idx_t blob_idx = 0;
113236 for (idx_t i = 0; i < len; i++) {
113237 if (data[i] == '\\') {
113238 int byte_a = Blob::HEX_MAP[data[i + 2]];
113239 int byte_b = Blob::HEX_MAP[data[i + 3]];
113240 D_ASSERT(i + 3 < len);
113241 D_ASSERT(byte_a >= 0 && byte_b >= 0);
113242 D_ASSERT(data[i + 1] == 'x');
113243 output[blob_idx++] = UnsafeNumericCast<data_t>((byte_a << 4) + byte_b);
113244 i += 3;
113245 } else if (data[i] <= 127) {
113246 output[blob_idx++] = data_t(data[i]);
113247 } else {
113248 throw ConversionException("Invalid byte encountered in STRING -> BLOB conversion. All non-ascii characters "
113249 "must be escaped with hex codes (e.g. \\xAA)");
113250 }
113251 }
113252 D_ASSERT(blob_idx == GetBlobSize(str));
113253}
uint8_t data_t
data pointers
Definition duckdb.hpp:246
Here is the caller graph for this function:

◆ ToBlob() [2/3]

string duckdb::Blob::ToBlob ( string_t str)
static

Convert a string object to a blob.

113255 {
113256 CastParameters parameters;
113257 return Blob::ToBlob(str, parameters);
113258}
static DUCKDB_API void ToBlob(string_t str, data_ptr_t output)
Convert a string to a blob. This function should ONLY be called after calling GetBlobSize,...
Definition duckdb.cpp:113232
Here is the call graph for this function:

◆ ToBlob() [3/3]

string duckdb::Blob::ToBlob ( string_t str,
CastParameters & parameters )
static
113260 {
113261 auto blob_len = GetBlobSize(str, parameters);
113262 auto buffer = make_unsafe_uniq_array_uninitialized<char>(blob_len);
113263 Blob::ToBlob(str, data_ptr_cast(buffer.get()));
113264 return string(buffer.get(), blob_len);
113265}

◆ ToBase64() [1/2]

string duckdb::Blob::ToBase64 ( string_t blob)
static
113268 {
113269 auto base64_size = Blob::ToBase64Size(blob);
113270 auto data = make_uniq_array<char>(base64_size);
113271 Blob::ToBase64(blob, data.get());
113272 return string(data.get(), base64_size);
113273}
static DUCKDB_API idx_t ToBase64Size(string_t blob)
Returns the string size of a blob -> base64 conversion.
Definition duckdb.cpp:113275

◆ ToBase64Size()

idx_t duckdb::Blob::ToBase64Size ( string_t blob)
static

Returns the string size of a blob -> base64 conversion.

113275 {
113276 // every 4 characters in base64 encode 3 bytes, plus (potential) padding at the end
113277 auto input_size = blob.GetSize();
113278 return ((input_size + 2) / 3) * 4;
113279}

◆ ToBase64() [2/2]

void duckdb::Blob::ToBase64 ( string_t blob,
char * output )
static

Converts a blob to a base64 string, output should have space for at least ToBase64Size(blob) bytes.

113281 {
113282 auto input_data = const_data_ptr_cast(blob.GetData());
113283 auto input_size = blob.GetSize();
113284 idx_t out_idx = 0;
113285 idx_t i;
113286 // convert the bulk of the string to base64
113287 // this happens in steps of 3 bytes -> 4 output bytes
113288 for (i = 0; i + 2 < input_size; i += 3) {
113289 output[out_idx++] = Blob::BASE64_MAP[(input_data[i] >> 2) & 0x3F];
113290 output[out_idx++] = Blob::BASE64_MAP[((input_data[i] & 0x3) << 4) | ((input_data[i + 1] & 0xF0) >> 4)];
113291 output[out_idx++] = Blob::BASE64_MAP[((input_data[i + 1] & 0xF) << 2) | ((input_data[i + 2] & 0xC0) >> 6)];
113292 output[out_idx++] = Blob::BASE64_MAP[input_data[i + 2] & 0x3F];
113293 }
113294
113295 if (i < input_size) {
113296 // there are one or two bytes left over: we have to insert padding
113297 // first write the first 6 bits of the first byte
113298 output[out_idx++] = Blob::BASE64_MAP[(input_data[i] >> 2) & 0x3F];
113299 // now check the character count
113300 if (i == input_size - 1) {
113301 // single byte left over: convert the remainder of that byte and insert padding
113302 output[out_idx++] = Blob::BASE64_MAP[((input_data[i] & 0x3) << 4)];
113303 output[out_idx++] = Blob::BASE64_PADDING;
113304 } else {
113305 // two bytes left over: convert the second byte as well
113306 output[out_idx++] = Blob::BASE64_MAP[((input_data[i] & 0x3) << 4) | ((input_data[i + 1] & 0xF0) >> 4)];
113307 output[out_idx++] = Blob::BASE64_MAP[((input_data[i + 1] & 0xF) << 2)];
113308 }
113309 output[out_idx++] = Blob::BASE64_PADDING;
113310 }
113311}
static constexpr const char * BASE64_MAP
map of index -> base64 character
Definition duckdb.hpp:57305
static constexpr const char BASE64_PADDING
padding character used in base64 encoding
Definition duckdb.hpp:57307

◆ FromBase64() [1/2]

string duckdb::Blob::FromBase64 ( string_t blob)
static
113324 {
113325 auto decoded_size = Blob::FromBase64Size(blob);
113326 auto data = make_uniq_array<data_t>(decoded_size);
113327 Blob::FromBase64(blob, data.get(), decoded_size);
113328 return string(char_ptr_cast(data.get()), decoded_size);
113329}
static DUCKDB_API idx_t FromBase64Size(string_t str)
Returns the string size of a base64 string -> blob conversion.
Definition duckdb.cpp:113331

◆ FromBase64Size()

idx_t duckdb::Blob::FromBase64Size ( string_t str)
static

Returns the string size of a base64 string -> blob conversion.

113331 {
113332 auto input_data = str.GetData();
113333 auto input_size = str.GetSize();
113334 if (input_size % 4 != 0) {
113335 // valid base64 needs to always be cleanly divisible by 4
113336 throw ConversionException("Could not decode string \"%s\" as base64: length must be a multiple of 4",
113337 str.GetString());
113338 }
113339 if (input_size < 4) {
113340 // empty string
113341 return 0;
113342 }
113343 auto base_size = input_size / 4 * 3;
113344 // check for padding to figure out the length
113345 if (input_data[input_size - 2] == Blob::BASE64_PADDING) {
113346 // two bytes of padding
113347 return base_size - 2;
113348 }
113349 if (input_data[input_size - 1] == Blob::BASE64_PADDING) {
113350 // one byte of padding
113351 return base_size - 1;
113352 }
113353 // no padding
113354 return base_size;
113355}
Here is the caller graph for this function:

◆ FromBase64() [2/2]

void duckdb::Blob::FromBase64 ( string_t str,
data_ptr_t output,
idx_t output_size )
static

Converts a base64 string to a blob, output should have space for at least FromBase64Size(blob) bytes.

113377 {
113378 D_ASSERT(output_size == FromBase64Size(str));
113379 auto input_data = const_data_ptr_cast(str.GetData());
113380 auto input_size = str.GetSize();
113381 if (input_size == 0) {
113382 return;
113383 }
113384 idx_t out_idx = 0;
113385 idx_t i = 0;
113386 for (i = 0; i + 4 < input_size; i += 4) {
113387 auto combined = DecodeBase64Bytes<false>(str, input_data, i);
113388 output[out_idx++] = (combined >> 2 * 8) & 0xFF;
113389 output[out_idx++] = (combined >> 1 * 8) & 0xFF;
113390 output[out_idx++] = (combined >> 0 * 8) & 0xFF;
113391 }
113392 // decode the final four bytes: padding is allowed here
113393 auto combined = DecodeBase64Bytes<true>(str, input_data, i);
113394 output[out_idx++] = (combined >> 2 * 8) & 0xFF;
113395 if (out_idx < output_size) {
113396 output[out_idx++] = (combined >> 1 * 8) & 0xFF;
113397 }
113398 if (out_idx < output_size) {
113399 output[out_idx++] = (combined >> 0 * 8) & 0xFF;
113400 }
113401}
Here is the call graph for this function:

Member Data Documentation

◆ HEX_MAP

const int duckdb::Blob::HEX_MAP
static
Initial value:
= {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}

The documentation for this class was generated from the following files: