X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/d5b80e59458182b2d557a929a18cb8c70cd56b68..a85c067ba6c6940512cf57ec213277a370d87e70:/src/src/rfc2047.c diff --git a/src/src/rfc2047.c b/src/src/rfc2047.c index 041a18858..af8993695 100644 --- a/src/src/rfc2047.c +++ b/src/src/rfc2047.c @@ -2,8 +2,10 @@ * Exim - an Internet mail transport agent * *************************************************/ -/* Copyright (c) University of Cambridge 1995 - 2015 */ +/* Copyright (c) The Exim Maintainers 2020 - 2022 */ +/* Copyright (c) University of Cambridge 1995 - 2018 */ /* See the file NOTICE for conditions of use and distribution. */ +/* SPDX-License-Identifier: GPL-2.0-only */ /* This file contains a function for decoding message header lines that may contain encoded "words" according to the rules described in @@ -46,7 +48,7 @@ rfc2047_qpdecode(uschar *string, uschar **ptrptr) int len = 0; uschar *ptr; -ptr = *ptrptr = store_get(Ustrlen(string) + 1); /* No longer than this */ +ptr = *ptrptr = store_get(Ustrlen(string) + 1, string); /* No longer than this */ while (*string != 0) { @@ -185,13 +187,13 @@ Returns: the decoded, converted string, or NULL on error; if there are */ uschar * -rfc2047_decode2(uschar *string, BOOL lencheck, uschar *target, int zeroval, - int *lenptr, int *sizeptr, uschar **error) +rfc2047_decode2(uschar *string, BOOL lencheck, const uschar *target, + int zeroval, int *lenptr, int *sizeptr, uschar **error) { -int ptr = 0; int size = Ustrlen(string); size_t dlen; -uschar *dptr, *yield; +uschar *dptr; +gstring *yield; uschar *mimeword, *q1, *q2, *endword; *error = NULL; @@ -208,7 +210,10 @@ building the result as we go. The result may be longer than the input if it is translated into a multibyte code such as UTF-8. That's why we use the dynamic string building code. */ -yield = store_get(++size); +yield = store_get(sizeof(gstring) + ++size, string); +yield->size = size; +yield->ptr = 0; +yield->s = US(yield + 1); while (mimeword) { @@ -218,7 +223,8 @@ while (mimeword) #endif if (mimeword != string) - yield = string_catn(yield, &size, &ptr, string, mimeword - string); + yield = string_catn(yield, string, mimeword - string); +/*XXX that might have to convert an untainted string to a tainted one */ /* Do a charset translation if required. This is supported only on hosts that have the iconv() function. Translation errors set error, but carry on, @@ -229,17 +235,11 @@ while (mimeword) #if HAVE_ICONV *q1 = 0; - if (target != NULL && strcmpic(target, mimeword+2) != 0) - { - icd = iconv_open(CS target, CS(mimeword+2)); - - if (icd == (iconv_t)(-1)) - { + if (target && strcmpic(target, mimeword+2) != 0) + if ((icd = iconv_open(CS target, CS(mimeword+2))) == (iconv_t)-1) *error = string_sprintf("iconv_open(\"%s\", \"%s\") failed: %s%s", target, mimeword+2, strerror(errno), (errno == EINVAL)? " (maybe unsupported conversion)" : ""); - } - } *q1 = '?'; #endif @@ -297,15 +297,12 @@ while (mimeword) /* Deal with zero values; convert them if requested. */ if (zeroval != 0) - { - int i; - for (i = 0; i < tlen; i++) + for (int i = 0; i < tlen; i++) if (tptr[i] == 0) tptr[i] = zeroval; - } /* Add the new string onto the result */ - yield = string_catn(yield, &size, &ptr, tptr, tlen); + yield = string_catn(yield, tptr, tlen); } #if HAVE_ICONV @@ -328,11 +325,11 @@ while (mimeword) /* Copy the remaining characters of the string, zero-terminate it, and return the length as well if requested. */ -yield = string_cat(yield, &size, &ptr, string); -yield[ptr] = 0; -if (lenptr) *lenptr = ptr; -if (sizeptr) *sizeptr = size; -return yield; +yield = string_cat(yield, string); + +if (lenptr) *lenptr = yield->ptr; +if (sizeptr) *sizeptr = yield->size; +return string_from_gstring(yield); } @@ -340,7 +337,7 @@ return yield; argument. */ uschar * -rfc2047_decode(uschar *string, BOOL lencheck, uschar *target, int zeroval, +rfc2047_decode(uschar *string, BOOL lencheck, const uschar *target, int zeroval, int *lenptr, uschar **error) { return rfc2047_decode2(string, lencheck, target, zeroval, lenptr, NULL, error);