* @brief Encodes binary data into a Base58 encoded string. * * Base58 is commonly used in cryptocurrencies such as Bitcoin. It is designed to avoid * visually similar characters and is not case-sensitive. * * @param data Pointer to the binary data to be encoded. * @param binsz The size of the binary data in bytes. * * @return A dynamically allocated string containing the Base58 encoded r
| 1889 | free(buf); |
| 1890 | return NULL; |
| 1891 | } |
| 1892 | j = size - 1 - k; |
| 1893 | carry += 256 * buf[j]; |
| 1894 | buf[j] = carry % 58; |
| 1895 | carry /= 58; |
| 1896 | } |
| 1897 | used = k; |
| 1898 | } |
| 1899 | |
| 1900 | for (j = 0; j < size && !buf[j]; ++j) { |
| 1901 | // Skip leading zeros in binary |
| 1902 | } |
| 1903 | |
| 1904 | size_t b58sz = zcount + size - j + 1; |
| 1905 | char *b58 = (char*) malloc(b58sz); |
| 1906 | if (!b58) { |
| 1907 | ENCODING_LOG("[encoding_base58_encode] Error: Memory allocation failed for Base58 encoded result"); |
| 1908 | free(buf); |
| 1909 | return NULL; |
| 1910 | } |
| 1911 | |
| 1912 | // Handle leading zeros in the input |
| 1913 | if (zcount) { |
| 1914 | memset(b58, '1', zcount); |
| 1915 | } |
| 1916 | |
| 1917 | for (i = zcount; j < size; ++i, ++j) { |
| 1918 | b58[i] = b58digits_ordered[buf[j]]; |
| 1919 | } |
| 1920 | b58[i] = '\0'; |
| 1921 | |
| 1922 | ENCODING_LOG("[encoding_base58_encode] Encoding completed successfully"); |
| 1923 | |
| 1924 | free(buf); |
| 1925 | return b58; |
| 1926 | } |
| 1927 | |
| 1928 | |
| 1929 | /** |
| 1930 | * @brief Decodes a Base58 encoded string back into its original binary form. |
| 1931 | * |
| 1932 | * @param b58 Pointer to the Base58 encoded string. |
| 1933 | * @param binszp Pointer to a size_t variable where the length of the decoded data will be stored. |
| 1934 | * |
| 1935 | * @return A dynamically allocated array containing the decoded binary data. |
| 1936 | * Returns NULL if an error occurs (e.g., invalid input, memory allocation failure). |
| 1937 | */ |
| 1938 | char *encoding_base58_decode(const char *b58, size_t *binszp) { |
| 1939 | if (b58 == NULL || binszp == NULL) { |
| 1940 | ENCODING_LOG("[encoding_base58_decode] Error: Invalid input or binszp pointer"); |
| 1941 | return NULL; |
| 1942 | } |
| 1943 | |
| 1944 | ENCODING_LOG("[encoding_base58_decode] Starting Base58 decoding"); |
| 1945 | |
| 1946 | size_t b58sz = strlen(b58); |
| 1947 | |
| 1948 | /* Count leading '1' characters — each represents a leading zero byte. */ |
nothing calls this directly
no outgoing calls
no test coverage detected