diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-06-30 11:57:07 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-06-30 11:57:28 +0200 |
commit | bc458ee7949e01e0b5a1f7f15773c02be94cb8fa (patch) | |
tree | 3491aafa2f3dc5265145b5d27a2b468b140c50ee /src/util/strings.c | |
parent | f9b0e00a193e735db74f0abe907caf97c7da054b (diff) | |
download | gnunet-bc458ee7949e01e0b5a1f7f15773c02be94cb8fa.tar.gz gnunet-bc458ee7949e01e0b5a1f7f15773c02be94cb8fa.zip |
simplify memory allocation in plugin_namestore_flat, use cleaner signatures for base64 encoding/decoding functions
Diffstat (limited to 'src/util/strings.c')
-rw-r--r-- | src/util/strings.c | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index 5ed195933..ea3c8cfb9 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ | 17 | */ |
@@ -1947,27 +1947,27 @@ static char *cvt = | |||
1947 | /** | 1947 | /** |
1948 | * Encode into Base64. | 1948 | * Encode into Base64. |
1949 | * | 1949 | * |
1950 | * @param data the data to encode | 1950 | * @param in the data to encode |
1951 | * @param len the length of the input | 1951 | * @param len the length of the input |
1952 | * @param output where to write the output (*output should be NULL, | 1952 | * @param output where to write the output (*output should be NULL, |
1953 | * is allocated) | 1953 | * is allocated) |
1954 | * @return the size of the output | 1954 | * @return the size of the output |
1955 | */ | 1955 | */ |
1956 | size_t | 1956 | size_t |
1957 | GNUNET_STRINGS_base64_encode (const char *data, | 1957 | GNUNET_STRINGS_base64_encode (const void *in, |
1958 | size_t len, | 1958 | size_t len, |
1959 | char **output) | 1959 | char **output) |
1960 | { | 1960 | { |
1961 | size_t i; | 1961 | const char *data = in; |
1962 | char c; | ||
1963 | size_t ret; | 1962 | size_t ret; |
1964 | char *opt; | 1963 | char *opt; |
1965 | 1964 | ||
1966 | ret = 0; | 1965 | ret = 0; |
1967 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); | 1966 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); |
1968 | *output = opt; | 1967 | for (size_t i = 0; i < len; ++i) |
1969 | for (i = 0; i < len; ++i) | ||
1970 | { | 1968 | { |
1969 | char c; | ||
1970 | |||
1971 | c = (data[i] >> 2) & 0x3f; | 1971 | c = (data[i] >> 2) & 0x3f; |
1972 | opt[ret++] = cvt[(int) c]; | 1972 | opt[ret++] = cvt[(int) c]; |
1973 | c = (data[i] << 4) & 0x3f; | 1973 | c = (data[i] << 4) & 0x3f; |
@@ -1997,6 +1997,7 @@ GNUNET_STRINGS_base64_encode (const char *data, | |||
1997 | } | 1997 | } |
1998 | } | 1998 | } |
1999 | opt[ret++] = FILLCHAR; | 1999 | opt[ret++] = FILLCHAR; |
2000 | *output = opt; | ||
2000 | return ret; | 2001 | return ret; |
2001 | } | 2002 | } |
2002 | 2003 | ||
@@ -2018,11 +2019,10 @@ GNUNET_STRINGS_base64_encode (const char *data, | |||
2018 | */ | 2019 | */ |
2019 | size_t | 2020 | size_t |
2020 | GNUNET_STRINGS_base64_decode (const char *data, | 2021 | GNUNET_STRINGS_base64_decode (const char *data, |
2021 | size_t len, char **output) | 2022 | size_t len, |
2023 | void **out) | ||
2022 | { | 2024 | { |
2023 | size_t i; | 2025 | char *output; |
2024 | char c; | ||
2025 | char c1; | ||
2026 | size_t ret = 0; | 2026 | size_t ret = 0; |
2027 | 2027 | ||
2028 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ | 2028 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ |
@@ -2031,12 +2031,15 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2031 | if (i >= len) goto END; \ | 2031 | if (i >= len) goto END; \ |
2032 | } | 2032 | } |
2033 | 2033 | ||
2034 | *output = GNUNET_malloc ((len * 3 / 4) + 8); | 2034 | output = GNUNET_malloc ((len * 3 / 4) + 8); |
2035 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2035 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2036 | "base64_decode decoding len=%d\n", | 2036 | "base64_decode decoding len=%d\n", |
2037 | (int) len); | 2037 | (int) len); |
2038 | for (i = 0; i < len; ++i) | 2038 | for (size_t i = 0; i < len; ++i) |
2039 | { | 2039 | { |
2040 | char c; | ||
2041 | char c1; | ||
2042 | |||
2040 | CHECK_CRLF; | 2043 | CHECK_CRLF; |
2041 | if (FILLCHAR == data[i]) | 2044 | if (FILLCHAR == data[i]) |
2042 | break; | 2045 | break; |
@@ -2045,7 +2048,7 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2045 | CHECK_CRLF; | 2048 | CHECK_CRLF; |
2046 | c1 = (char) cvtfind (data[i]); | 2049 | c1 = (char) cvtfind (data[i]); |
2047 | c = (c << 2) | ((c1 >> 4) & 0x3); | 2050 | c = (c << 2) | ((c1 >> 4) & 0x3); |
2048 | (*output)[ret++] = c; | 2051 | output[ret++] = c; |
2049 | if (++i < len) | 2052 | if (++i < len) |
2050 | { | 2053 | { |
2051 | CHECK_CRLF; | 2054 | CHECK_CRLF; |
@@ -2054,7 +2057,7 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2054 | break; | 2057 | break; |
2055 | c = (char) cvtfind (c); | 2058 | c = (char) cvtfind (c); |
2056 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); | 2059 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); |
2057 | (*output)[ret++] = c1; | 2060 | output[ret++] = c1; |
2058 | } | 2061 | } |
2059 | if (++i < len) | 2062 | if (++i < len) |
2060 | { | 2063 | { |
@@ -2065,15 +2068,13 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2065 | 2068 | ||
2066 | c1 = (char) cvtfind (c1); | 2069 | c1 = (char) cvtfind (c1); |
2067 | c = ((c << 6) & 0xc0) | c1; | 2070 | c = ((c << 6) & 0xc0) | c1; |
2068 | (*output)[ret++] = c; | 2071 | output[ret++] = c; |
2069 | } | 2072 | } |
2070 | } | 2073 | } |
2071 | END: | 2074 | END: |
2075 | *out = output; | ||
2072 | return ret; | 2076 | return ret; |
2073 | } | 2077 | } |
2074 | 2078 | ||
2075 | 2079 | ||
2076 | |||
2077 | |||
2078 | |||
2079 | /* end of strings.c */ | 2080 | /* end of strings.c */ |