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

Function encoding_base85_encode

encoding/encoding.c:1729–1777  ·  view source on GitHub ↗

* @brief Encodes binary data into a Base85 encoded string. * * Base85 is an encoding method used to encode binary data into ASCII text, often used * in contexts where binary data needs to be handled as text, such as in Adobe's PostScript. * * @param input Pointer to the binary data to be encoded. * @param length The size of the binary data in bytes. * * @return A dynamically allocated st

Source from the content-addressed store, hash-verified

1727 * Base85 decoding interprets the encoded text and converts it back into the binary data
1728 * it originally represented.
1729 *
1730 * @param input Pointer to the Base85 encoded string.
1731 * @param length The length of the encoded string.
1732 *
1733 * @return A dynamically allocated array containing the decoded binary data.
1734 * Returns NULL if an error occurs (e.g., invalid input, memory allocation failure).
1735 *
1736 * @note The caller is responsible for freeing the returned decoded data.
1737 */
1738uint8_t* encoding_base85_decode(const char* input, size_t length) {
1739 if (input == NULL && length > 0) {
1740 ENCODING_LOG("[encoding_base85_decode] Error: Invalid input or length");
1741 return NULL;
1742 }
1743
1744 ENCODING_LOG("[encoding_base85_decode] Starting Base85 decoding");
1745
1746 size_t decoded_max_length = length * 4 + 4;
1747 uint8_t* decoded = (uint8_t*) malloc(decoded_max_length);
1748 if (!decoded) {
1749 ENCODING_LOG("[encoding_base85_decode] Error: Memory allocation failed for decoded string");
1750 return NULL;
1751 }
1752
1753 size_t input_index = 0;
1754 size_t decoded_index = 0;
1755 while (input_index < length) {
1756 if (isspace((unsigned char)input[input_index])) {
1757 input_index++; // Skip whitespace
1758 continue;
1759 }
1760 if (input[input_index] == 'z') {
1761 // Special case 'z' represents four zero bytes
1762 memset(decoded + decoded_index, 0, 4);
1763 decoded_index += 4;
1764 input_index++;
1765 continue;
1766 }
1767
1768 uint64_t acc = 0;
1769 int count = 0;
1770 /* Whitespace inside a group (wrapped Ascii85 lines) is skipped
1771 without using up one of the group's 5 places: it used to end the
1772 group early, and the short group was taken as the final one, so
1773 everything after the first line break was dropped. */
1774 while (count < 5 && input_index < length) {
1775 if (isspace((unsigned char)input[input_index])) {
1776 input_index++;
1777 continue;
1778 }
1779
1780 unsigned char ch = (unsigned char)input[input_index++];

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected