summaryrefslogtreecommitdiffstats
path: root/Utilities/cmcurl/lib/imap.c
diff options
context:
space:
mode:
Diffstat (limited to 'Utilities/cmcurl/lib/imap.c')
-rw-r--r--Utilities/cmcurl/lib/imap.c103
1 files changed, 35 insertions, 68 deletions
diff --git a/Utilities/cmcurl/lib/imap.c b/Utilities/cmcurl/lib/imap.c
index 045fe24..de64c2a 100644
--- a/Utilities/cmcurl/lib/imap.c
+++ b/Utilities/cmcurl/lib/imap.c
@@ -45,9 +45,6 @@
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
-#ifdef HAVE_UTSNAME_H
-#include <sys/utsname.h>
-#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
@@ -1094,10 +1091,19 @@ static CURLcode imap_state_select_resp(struct Curl_easy *data, int imapcode,
if(imapcode == '*') {
/* See if this is an UIDVALIDITY response */
- char tmp[20];
- if(sscanf(line + 2, "OK [UIDVALIDITY %19[0123456789]]", tmp) == 1) {
- Curl_safefree(imapc->mailbox_uidvalidity);
- imapc->mailbox_uidvalidity = strdup(tmp);
+ if(checkprefix("OK [UIDVALIDITY ", line + 2)) {
+ size_t len = 0;
+ const char *p = &line[2] + strlen("OK [UIDVALIDITY ");
+ while((len < 20) && p[len] && ISDIGIT(p[len]))
+ len++;
+ if(len && (p[len] == ']')) {
+ struct dynbuf uid;
+ Curl_dyn_init(&uid, 20);
+ if(Curl_dyn_addn(&uid, p, len))
+ return CURLE_OUT_OF_MEMORY;
+ Curl_safefree(imapc->mailbox_uidvalidity);
+ imapc->mailbox_uidvalidity = Curl_dyn_ptr(&uid);
+ }
}
}
else if(imapcode == IMAP_RESP_OK) {
@@ -1109,7 +1115,10 @@ static CURLcode imap_state_select_resp(struct Curl_easy *data, int imapcode,
}
else {
/* Note the currently opened mailbox on this connection */
+ DEBUGASSERT(!imapc->mailbox);
imapc->mailbox = strdup(imap->mailbox);
+ if(!imapc->mailbox)
+ return CURLE_OUT_OF_MEMORY;
if(imap->custom)
result = imap_perform_list(data);
@@ -1806,79 +1815,37 @@ static CURLcode imap_sendf(struct Curl_easy *data, const char *fmt, ...)
*/
static char *imap_atom(const char *str, bool escape_only)
{
- /* !checksrc! disable PARENBRACE 1 */
- const char atom_specials[] = "(){ %*]";
- const char *p1;
- char *p2;
- size_t backsp_count = 0;
- size_t quote_count = 0;
- bool others_exists = FALSE;
- size_t newlen = 0;
- char *newstr = NULL;
+ struct dynbuf line;
+ size_t nclean;
+ size_t len;
if(!str)
return NULL;
- /* Look for "atom-specials", counting the backslash and quote characters as
- these will need escaping */
- p1 = str;
- while(*p1) {
- if(*p1 == '\\')
- backsp_count++;
- else if(*p1 == '"')
- quote_count++;
- else if(!escape_only) {
- const char *p3 = atom_specials;
-
- while(*p3 && !others_exists) {
- if(*p1 == *p3)
- others_exists = TRUE;
-
- p3++;
- }
- }
-
- p1++;
- }
-
- /* Does the input contain any "atom-special" characters? */
- if(!backsp_count && !quote_count && !others_exists)
+ len = strlen(str);
+ nclean = strcspn(str, "() {%*]\\\"");
+ if(len == nclean)
+ /* nothing to escape, return a strdup */
return strdup(str);
- /* Calculate the new string length */
- newlen = strlen(str) + backsp_count + quote_count + (escape_only ? 0 : 2);
+ Curl_dyn_init(&line, 2000);
- /* Allocate the new string */
- newstr = (char *) malloc((newlen + 1) * sizeof(char));
- if(!newstr)
+ if(!escape_only && Curl_dyn_addn(&line, "\"", 1))
return NULL;
- /* Surround the string in quotes if necessary */
- p2 = newstr;
- if(!escape_only) {
- newstr[0] = '"';
- newstr[newlen - 1] = '"';
- p2++;
+ while(*str) {
+ if((*str == '\\' || *str == '"') &&
+ Curl_dyn_addn(&line, "\\", 1))
+ return NULL;
+ if(Curl_dyn_addn(&line, str, 1))
+ return NULL;
+ str++;
}
- /* Copy the string, escaping backslash and quote characters along the way */
- p1 = str;
- while(*p1) {
- if(*p1 == '\\' || *p1 == '"') {
- *p2 = '\\';
- p2++;
- }
-
- *p2 = *p1;
-
- p1++;
- p2++;
- }
-
- /* Terminate the string */
- newstr[newlen] = '\0';
+ if(!escape_only && Curl_dyn_addn(&line, "\"", 1))
+ return NULL;
- return newstr;
+ return Curl_dyn_ptr(&line);
}
/***********************************************************************