comparison src/lib-imap/imap-quote.c @ 898:0d5be52d7131 HEAD

Use unsigned char* when accessing non-NUL terminating strings. Compiler warnings would then notify about accidentally passing them to functions which require them NUL-terminated. Changed a few functions to use void* to avoid unneeded casting.
author Timo Sirainen <tss@iki.fi>
date Sat, 04 Jan 2003 19:26:29 +0200
parents 41ec8cadd238
children fd8888f6f037
comparison
equal deleted inserted replaced
897:e27267f227e6 898:0d5be52d7131
11 (c) == '[' || (c) == ']' || (c) == '=') 11 (c) == '[' || (c) == ']' || (c) == '=')
12 12
13 #define IS_BREAK_OR_CRLF_CHAR(c) \ 13 #define IS_BREAK_OR_CRLF_CHAR(c) \
14 (IS_BREAK_CHAR(c) || (c) == '\r' || (c) == '\n') 14 (IS_BREAK_CHAR(c) || (c) == '\r' || (c) == '\n')
15 15
16 static size_t next_token_quoted(const char *value, size_t len, 16 static size_t next_token_quoted(const unsigned char *value, size_t len,
17 int *need_qp, int *quoted) 17 int *need_qp, int *quoted)
18 { 18 {
19 size_t i; 19 size_t i;
20 20
21 *need_qp = FALSE; 21 *need_qp = FALSE;
22 *quoted = TRUE; 22 *quoted = TRUE;
23 23
24 for (i = *quoted ? 0 : 1; i < len; i++) { 24 for (i = *quoted ? 0 : 1; i < len; i++) {
25 if ((unsigned char)value[i] & 0x80) 25 if (value[i] & 0x80)
26 *need_qp = TRUE; 26 *need_qp = TRUE;
27 27
28 if (value[i] == '"' || value[i] == '\r' || value[i] == '\n') { 28 if (value[i] == '"' || value[i] == '\r' || value[i] == '\n') {
29 i++; 29 i++;
30 *quoted = value[i] == '"'; 30 *quoted = value[i] == '"';
33 } 33 }
34 34
35 return i; 35 return i;
36 } 36 }
37 37
38 static size_t next_token(const char *value, size_t len, 38 static size_t next_token(const unsigned char *value, size_t len,
39 int *need_qp, int *quoted, int qp_on) 39 int *need_qp, int *quoted, int qp_on)
40 { 40 {
41 size_t i = 0; 41 size_t i = 0;
42 42
43 if (value[0] == '"' || *quoted) 43 if (value[0] == '"' || *quoted)
66 return i; 66 return i;
67 } 67 }
68 68
69 /* then stop at break-char */ 69 /* then stop at break-char */
70 for (; i < len; i++) { 70 for (; i < len; i++) {
71 if ((unsigned char)value[i] & 0x80) 71 if (value[i] & 0x80)
72 *need_qp = TRUE; 72 *need_qp = TRUE;
73 73
74 if (IS_BREAK_OR_CRLF_CHAR(value[i])) 74 if (IS_BREAK_OR_CRLF_CHAR(value[i]))
75 break; 75 break;
76 } 76 }
77 77
78 return i; 78 return i;
79 } 79 }
80 80
81 static void append_quoted_qp(String *str, const char *value, size_t len) 81 static void append_quoted_qp(String *str, const unsigned char *value,
82 size_t len)
82 { 83 {
83 size_t i; 84 size_t i;
84 unsigned char c; 85 unsigned char c;
85 86
86 /* do this the easy way, it's already broken behaviour to leave the 87 /* do this the easy way, it's already broken behaviour to leave the
94 (value[i] >= 'a' && value[i] <= 'z') || 95 (value[i] >= 'a' && value[i] <= 'z') ||
95 (value[i] >= '0' && value[i] <= '9')) { 96 (value[i] >= '0' && value[i] <= '9')) {
96 str_append_c(str, value[i]); 97 str_append_c(str, value[i]);
97 } else { 98 } else {
98 str_append_c(str, '='); 99 str_append_c(str, '=');
99 c = (unsigned char)value[i] >> 4; 100 c = value[i] >> 4;
100 str_append_c(str, c < 10 ? (c+'0') : (c-10+'A')); 101 str_append_c(str, c < 10 ? (c+'0') : (c-10+'A'));
101 c = (unsigned char)value[i] & 0x0f; 102 c = value[i] & 0x0f;
102 str_append_c(str, c < 10 ? (c+'0') : (c-10+'A')); 103 str_append_c(str, c < 10 ? (c+'0') : (c-10+'A'));
103 } 104 }
104 } 105 }
105 } 106 }
106 107
107 static void append_quoted(String *str, const char *value, size_t len) 108 static void append_quoted(String *str, const unsigned char *value, size_t len)
108 { 109 {
109 size_t i; 110 size_t i;
110 111
111 for (i = 0; i < len; i++) { 112 for (i = 0; i < len; i++) {
112 if (value[i] == '\\' || value[i] == '"') 113 if (value[i] == '\\' || value[i] == '"')
114 str_append_c(str, value[i]); 115 str_append_c(str, value[i]);
115 } 116 }
116 } 117 }
117 118
118 /* does two things: 1) escape '\' and '"' characters, 2) 8bit text -> QP */ 119 /* does two things: 1) escape '\' and '"' characters, 2) 8bit text -> QP */
119 static String *get_quoted_str(const char *value, size_t value_len) 120 static String *get_quoted_str(const unsigned char *value, size_t value_len)
120 { 121 {
121 String *str; 122 String *str;
122 size_t token_len; 123 size_t token_len;
123 int qp, need_qp, quoted; 124 int qp, need_qp, quoted;
124 125
163 } 164 }
164 165
165 const char *imap_quote_str_nil(const char *value) 166 const char *imap_quote_str_nil(const char *value)
166 { 167 {
167 return value == NULL ? "NIL" : 168 return value == NULL ? "NIL" :
168 str_c(get_quoted_str(value, strlen(value))); 169 str_c(get_quoted_str((const unsigned char *) value,
170 strlen(value)));
169 } 171 }
170 172
171 char *imap_quote_value(Pool pool, const char *value, size_t value_len) 173 char *imap_quote_value(Pool pool, const unsigned char *value, size_t value_len)
172 { 174 {
173 String *str; 175 String *str;
174 176
175 str = get_quoted_str(value, value_len); 177 str = get_quoted_str(value, value_len);
176 return p_strndup(pool, str_c(str), str_len(str)); 178 return p_strndup(pool, str_data(str), str_len(str));
177 } 179 }