diff options
Diffstat (limited to 'src/util/strings.c')
-rw-r--r-- | src/util/strings.c | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index 4cfcd63b3..ea3c8cfb9 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -2,20 +2,18 @@ | |||
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2005-2017 GNUnet e.V. | 3 | Copyright (C) 2005-2017 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | it under the terms of the GNU General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
7 | by the Free Software Foundation; either version 3, or (at your | 7 | by the Free Software Foundation, either version 3 of the License, |
8 | option) any later version. | 8 | or (at your option) any later version. |
9 | 9 | ||
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | GNUnet is distributed in the hope that it will be useful, but |
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 | 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 General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with GNUnet; see the file COPYING. If not, write to the | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 17 | */ |
20 | /** | 18 | /** |
21 | * @file util/strings.c | 19 | * @file util/strings.c |
@@ -1949,27 +1947,27 @@ static char *cvt = | |||
1949 | /** | 1947 | /** |
1950 | * Encode into Base64. | 1948 | * Encode into Base64. |
1951 | * | 1949 | * |
1952 | * @param data the data to encode | 1950 | * @param in the data to encode |
1953 | * @param len the length of the input | 1951 | * @param len the length of the input |
1954 | * @param output where to write the output (*output should be NULL, | 1952 | * @param output where to write the output (*output should be NULL, |
1955 | * is allocated) | 1953 | * is allocated) |
1956 | * @return the size of the output | 1954 | * @return the size of the output |
1957 | */ | 1955 | */ |
1958 | size_t | 1956 | size_t |
1959 | GNUNET_STRINGS_base64_encode (const char *data, | 1957 | GNUNET_STRINGS_base64_encode (const void *in, |
1960 | size_t len, | 1958 | size_t len, |
1961 | char **output) | 1959 | char **output) |
1962 | { | 1960 | { |
1963 | size_t i; | 1961 | const char *data = in; |
1964 | char c; | ||
1965 | size_t ret; | 1962 | size_t ret; |
1966 | char *opt; | 1963 | char *opt; |
1967 | 1964 | ||
1968 | ret = 0; | 1965 | ret = 0; |
1969 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); | 1966 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); |
1970 | *output = opt; | 1967 | for (size_t i = 0; i < len; ++i) |
1971 | for (i = 0; i < len; ++i) | ||
1972 | { | 1968 | { |
1969 | char c; | ||
1970 | |||
1973 | c = (data[i] >> 2) & 0x3f; | 1971 | c = (data[i] >> 2) & 0x3f; |
1974 | opt[ret++] = cvt[(int) c]; | 1972 | opt[ret++] = cvt[(int) c]; |
1975 | c = (data[i] << 4) & 0x3f; | 1973 | c = (data[i] << 4) & 0x3f; |
@@ -1999,6 +1997,7 @@ GNUNET_STRINGS_base64_encode (const char *data, | |||
1999 | } | 1997 | } |
2000 | } | 1998 | } |
2001 | opt[ret++] = FILLCHAR; | 1999 | opt[ret++] = FILLCHAR; |
2000 | *output = opt; | ||
2002 | return ret; | 2001 | return ret; |
2003 | } | 2002 | } |
2004 | 2003 | ||
@@ -2020,11 +2019,10 @@ GNUNET_STRINGS_base64_encode (const char *data, | |||
2020 | */ | 2019 | */ |
2021 | size_t | 2020 | size_t |
2022 | GNUNET_STRINGS_base64_decode (const char *data, | 2021 | GNUNET_STRINGS_base64_decode (const char *data, |
2023 | size_t len, char **output) | 2022 | size_t len, |
2023 | void **out) | ||
2024 | { | 2024 | { |
2025 | size_t i; | 2025 | char *output; |
2026 | char c; | ||
2027 | char c1; | ||
2028 | size_t ret = 0; | 2026 | size_t ret = 0; |
2029 | 2027 | ||
2030 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ | 2028 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ |
@@ -2033,12 +2031,15 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2033 | if (i >= len) goto END; \ | 2031 | if (i >= len) goto END; \ |
2034 | } | 2032 | } |
2035 | 2033 | ||
2036 | *output = GNUNET_malloc ((len * 3 / 4) + 8); | 2034 | output = GNUNET_malloc ((len * 3 / 4) + 8); |
2037 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2035 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2038 | "base64_decode decoding len=%d\n", | 2036 | "base64_decode decoding len=%d\n", |
2039 | (int) len); | 2037 | (int) len); |
2040 | for (i = 0; i < len; ++i) | 2038 | for (size_t i = 0; i < len; ++i) |
2041 | { | 2039 | { |
2040 | char c; | ||
2041 | char c1; | ||
2042 | |||
2042 | CHECK_CRLF; | 2043 | CHECK_CRLF; |
2043 | if (FILLCHAR == data[i]) | 2044 | if (FILLCHAR == data[i]) |
2044 | break; | 2045 | break; |
@@ -2047,7 +2048,7 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2047 | CHECK_CRLF; | 2048 | CHECK_CRLF; |
2048 | c1 = (char) cvtfind (data[i]); | 2049 | c1 = (char) cvtfind (data[i]); |
2049 | c = (c << 2) | ((c1 >> 4) & 0x3); | 2050 | c = (c << 2) | ((c1 >> 4) & 0x3); |
2050 | (*output)[ret++] = c; | 2051 | output[ret++] = c; |
2051 | if (++i < len) | 2052 | if (++i < len) |
2052 | { | 2053 | { |
2053 | CHECK_CRLF; | 2054 | CHECK_CRLF; |
@@ -2056,7 +2057,7 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2056 | break; | 2057 | break; |
2057 | c = (char) cvtfind (c); | 2058 | c = (char) cvtfind (c); |
2058 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); | 2059 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); |
2059 | (*output)[ret++] = c1; | 2060 | output[ret++] = c1; |
2060 | } | 2061 | } |
2061 | if (++i < len) | 2062 | if (++i < len) |
2062 | { | 2063 | { |
@@ -2067,15 +2068,13 @@ GNUNET_STRINGS_base64_decode (const char *data, | |||
2067 | 2068 | ||
2068 | c1 = (char) cvtfind (c1); | 2069 | c1 = (char) cvtfind (c1); |
2069 | c = ((c << 6) & 0xc0) | c1; | 2070 | c = ((c << 6) & 0xc0) | c1; |
2070 | (*output)[ret++] = c; | 2071 | output[ret++] = c; |
2071 | } | 2072 | } |
2072 | } | 2073 | } |
2073 | END: | 2074 | END: |
2075 | *out = output; | ||
2074 | return ret; | 2076 | return ret; |
2075 | } | 2077 | } |
2076 | 2078 | ||
2077 | 2079 | ||
2078 | |||
2079 | |||
2080 | |||
2081 | /* end of strings.c */ | 2080 | /* end of strings.c */ |