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

Function encoding_base64_encode

encoding/encoding.c:721–753  ·  view source on GitHub ↗

* @brief Encodes binary data into Base64 format. * * This function encodes a given input string into Base64 format. Base64 encoding * represents binary data in an ASCII string format by translating it into a radix-64 * representation. The output string is null-terminated. * * @param input Pointer to the input binary data. * @param length Length of the input data. * * @return A dynamica

Source from the content-addressed store, hash-verified

719
720 bool ok = (q == 0 && pad == 0) || (q == 2 && pad == 2) || (q == 3 && pad == 1);
721 if (!ok) {
722 ENCODING_LOG("[encoding_base64_decode] Error: Invalid length or padding.");
723 free(decoded);
724 return NULL;
725 }
726 if (q == 2) {
727 decoded[j++] = (char)((quad[0] << 2) | (quad[1] >> 4));
728 }
729 else if (q == 3) {
730 decoded[j++] = (char)((quad[0] << 2) | (quad[1] >> 4));
731 decoded[j++] = (char)(((quad[1] & 0x0F) << 4) | (quad[2] >> 2));
732 }
733
734 decoded[j] = '\0';
735 if (out_len) {
736 *out_len = j;
737 }
738
739 ENCODING_LOG("[encoding_base64_decode] Decoding completed successfully.");
740 return decoded;
741}
742
743
744/**
745 * @brief Decodes a Base64 encoded string back to binary data.
746 *
747 * Back-compat wrapper over `encoding_base64_decode_ex` that discards the decoded
748 * length. Use `encoding_base64_decode_ex` when the payload may be binary (the
749 * NUL-terminated `char*` return cannot convey the length of data containing
750 * embedded `0x00` bytes). The input length must be a multiple of 4.
751 *
752 * @param input Pointer to the Base64 encoded input string.
753 * @param length Length of the Base64 encoded string. Must be a multiple of 4.
754 * @return A dynamically allocated buffer containing the decoded binary data.
755 * Returns NULL if an error occurs (e.g., memory allocation failure, invalid input length).
756 *

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected