MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / string_base64_encode

Function string_base64_encode

string/std_string.c:3065–3107  ·  view source on GitHub ↗

* @brief Encodes a String object into Base64 format. * * This function takes a String object and encodes its content into Base64 format. * The encoded result is returned as a new String object. If the input String object * or its dataStr is NULL, the function returns NULL and logs an error. * * @param input The String object to encode. Must not be NULL. * * @return A new String object co

Source from the content-addressed store, hash-verified

3063 * @return A new String object containing the Base64-encoded data, or NULL if an error occurs.
3064 */
3065String* string_base64_encode(const String *input) {
3066 STRING_LOG("[string_base64_encode]: Function start.");
3067
3068 if (input == NULL) {
3069 STRING_LOG("[string_base64_encode]: Error - The String object is NULL.");
3070 return NULL;
3071 }
3072 if (input->dataStr == NULL) {
3073 STRING_LOG("[string_base64_encode]: Error - The dataStr of String object is NULL.");
3074 return NULL;
3075 }
3076
3077 String *encoded = string_create("");
3078 if (encoded == NULL) {
3079 STRING_LOG("[string_base64_encode]: Error - Failed to create encoded String object.");
3080 return NULL;
3081 }
3082
3083 int val = 0, valb = -6;
3084 size_t i;
3085 for (i = 0; i < input->size; i++) {
3086 unsigned char c = input->dataStr[i];
3087 val = (val << 8) + c;
3088 valb += 8;
3089 while (valb >= 0) {
3090 string_push_back(encoded, base64_chars[(val >> valb) & 0x3F]);
3091 valb -= 6;
3092 }
3093 }
3094
3095 if (valb > -6) {
3096 string_push_back(encoded, base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
3097 }
3098
3099 while (encoded->size % 4) {
3100 string_push_back(encoded, '=');
3101 }
3102
3103 STRING_LOG("[string_base64_encode]: Successfully encoded input.");
3104 STRING_LOG("[string_base64_encode]: Function end.");
3105
3106 return encoded;
3107}
3108
3109
3110/**

Callers

nothing calls this directly

Calls 2

string_createFunction · 0.85
string_push_backFunction · 0.85

Tested by

no test coverage detected