From 5f893991aab62b5ec23d555ddb57382857e8746e Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Thu, 14 Jul 2011 16:24:51 -0700 Subject: [PATCH] mime encoding: encode control chars Signed-off-by: Colin McCabe --- src/common/mime.c | 2 +- src/common/utf8.c | 8 ++++++-- src/common/utf8.h | 6 +++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/common/mime.c b/src/common/mime.c index 1c5f3aca1a21a..90c19a7684e6f 100644 --- a/src/common/mime.c +++ b/src/common/mime.c @@ -27,7 +27,7 @@ int mime_encode_as_qp(const char *input, char *output, int outlen) if (c == '\0') { break; } - else if ((c & 0x80) || (c == '=')) { + else if ((c & 0x80) || (c == '=') || (is_control_character(c))) { if (outlen >= 3) { snprintf(o, outlen, "=%02X", c); outlen -= 3; diff --git a/src/common/utf8.c b/src/common/utf8.c index f6a1b07b5956f..d8e085391981a 100644 --- a/src/common/utf8.c +++ b/src/common/utf8.c @@ -165,12 +165,16 @@ int check_utf8_cstr(const char *buf) return check_utf8(buf, strlen(buf)); } +int is_control_character(int c) +{ + return (((c != 0) && (c < 0x20)) || (c == 0x7f)); +} + int check_for_control_characters(const char *buf, int len) { int i; for (i = 0; i < len; ++i) { - unsigned char c = (unsigned char)buf[i]; - if (((c != 0U) && (c < 0x20U)) || (c == 0x7fU)) { + if (is_control_character((int)(unsigned char)buf[i])) { return i + 1; } } diff --git a/src/common/utf8.h b/src/common/utf8.h index e2b25b94c33d3..a25fe21b5c166 100644 --- a/src/common/utf8.h +++ b/src/common/utf8.h @@ -31,9 +31,13 @@ int check_utf8(const char *buf, int len); */ int check_utf8_cstr(const char *buf); -/* Checks if a buffer contains control characters. +/* Returns true if 'ch' is a control character. * We do count newline as a control character, but not NULL. */ +int is_control_character(int ch); + +/* Checks if a buffer contains control characters. + */ int check_for_control_characters(const char *buf, int len); /* Checks if a null-terminated string contains control characters. -- 2.39.5