cvs: /php3/functions imap.c
| From: | Andrew Skalski | Date: | Tue, 18 Jan 2000 19:49:18 +0000 |
| Subject: | cvs: /php3/functions imap.c | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-14745@lists.php.net to get a copy of this message | ||
askalski Tue Jan 18 11:49:18 2000 EDT
Modified files:
/php3/functions imap.c
Log:
bugfix and code cleanup for IMAP utf7 routines
Index: php3/functions/imap.c
diff -u php3/functions/imap.c:1.79 php3/functions/imap.c:1.80
--- php3/functions/imap.c:1.79 Mon Jan 17 11:37:25 2000
+++ php3/functions/imap.c Tue Jan 18 11:48:47 2000
@@ -33,7 +33,7 @@
| Andrew Skalski <askalski@chek.com> |
+----------------------------------------------------------------------+
*/
-/* $Id: imap.c,v 1.79 2000/01/17 19:37:25 eschmid Exp $ */
+/* $Id: imap.c,v 1.80 2000/01/18 19:48:47 askalski Exp $ */
#define IMAP41
@@ -2525,8 +2525,8 @@
/* validate a modified-base64 character */
#define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == ',')
/* map the low 64 bits of `n' to the modified-base64 characters */
-#define B64(n) "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
- "abcdefghijklmnopqrstuvwxyz0123456789+,"[(n) & 0x3f];
+#define B64(n) ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+ "abcdefghijklmnopqrstuvwxyz0123456789+,"[(n) & 0x3f])
/* map the modified-base64 character `c' to its 64 bit value */
#define UNB64(c) ((c) == '+' ? 62 : (c) == ',' ? 63 : (c) >=
'a' ? \
(c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4)
@@ -2563,14 +2563,23 @@
for (endp = (inp = in) + inlen; inp < endp; inp++) {
if (state == ST_NORMAL) {
/* process printable character */
- if (SPECIAL(*inp))
- {RETURN_FALSE;}
- else if (*inp != '&')
+ if (SPECIAL(*inp)) {
+ php3_error(E_WARNING, "imap_utf7_decode: "
+ "Invalid modified UTF-7 character: "
+ "`%c'", *inp);
+ RETURN_FALSE;
+ }
+ else if (*inp != '&') {
outlen++;
- else if (inp == endp)
- {RETURN_FALSE;}
- else if (inp[1] != '-')
+ }
+ else if (inp + 1 == endp) {
+ php3_error(E_WARNING, "imap_utf7_decode: "
+ "Unexpected end of string");
+ RETURN_FALSE;
+ }
+ else if (inp[1] != '-') {
state = ST_DECODE0;
+ }
else {
outlen++;
inp++;
@@ -2578,12 +2587,20 @@
}
else if (*inp == '-') {
/* return to NORMAL mode */
- if (state == ST_DECODE1)
+ if (state == ST_DECODE1) {
+ php3_error(E_WARNING, "imap_utf7_decode: "
+ "Stray modified base64 character: "
+ "`%c'", *--inp);
RETURN_FALSE;
+ }
state = ST_NORMAL;
}
- else if (!B64CHAR(*inp))
- {RETURN_FALSE;}
+ else if (!B64CHAR(*inp)) {
+ php3_error(E_WARNING, "imap_utf7_decode: "
+ "Invalid modified base64 character: "
+ "`%c'", *inp);
+ RETURN_FALSE;
+ }
else {
switch (state) {
case ST_DECODE3:
@@ -2601,25 +2618,34 @@
}
/* enforce end state */
- if (state != ST_NORMAL)
+ if (state != ST_NORMAL) {
+ php3_error(E_WARNING, "imap_utf7_decode: "
+ "Unexpected end of string");
RETURN_FALSE;
+ }
/* allocate output buffer */
- if ((out = emalloc(outlen)) == NULL)
+ if ((out = emalloc(outlen)) == NULL) {
+ php3_error(E_WARNING, "imap_utf7_decode: "
+ "Unable to allocate result string");
RETURN_FALSE;
+ }
/* decode input string */
outp = out;
state = ST_NORMAL;
for (endp = (inp = in) + inlen; inp < endp; inp++) {
if (state == ST_NORMAL) {
- if (*inp == '&' && inp[1] != '-')
+ if (*inp == '&' && inp[1] != '-') {
state = ST_DECODE0;
- else if ((*outp++ = *inp) == '&')
+ }
+ else if ((*outp++ = *inp) == '&') {
inp++;
+ }
}
- else if (*inp == '-')
+ else if (*inp == '-') {
state = ST_NORMAL;
+ }
else {
/* decode input character */
switch (state) {
@@ -2647,6 +2673,15 @@
}
}
+#if DEBUG
+ /* warn if we computed outlen incorrectly */
+ if (outp - out != outlen) {
+ php3_error(E_WARNING,
+ "imap_utf7_decode: outp - out [%d] != outlen [%d]",
+ outp - out, outlen);
+ }
+#endif
+
RETURN_STRINGL(out, outlen, 0);
}
/* }}} */
@@ -2686,29 +2721,36 @@
state = ST_ENCODE0;
outlen++;
}
- else if (*inp++ == '&')
+ else if (*inp++ == '&') {
outlen++;
+ }
outlen++;
}
- else if (!SPECIAL(*inp))
+ else if (!SPECIAL(*inp)) {
state = ST_NORMAL;
+ }
else {
/* ST_ENCODE0 -> ST_ENCODE1 - two chars
* ST_ENCODE1 -> ST_ENCODE2 - one char
* ST_ENCODE2 -> ST_ENCODE0 - one char
*/
- if (state == ST_ENCODE2)
+ if (state == ST_ENCODE2) {
state = ST_ENCODE0;
- else if (state++ == ST_ENCODE0)
+ }
+ else if (state++ == ST_ENCODE0) {
outlen++;
+ }
outlen++;
inp++;
}
}
/* allocate output buffer */
- if ((out = emalloc(outlen)) == NULL)
+ if ((out = emalloc(outlen)) == NULL) {
+ php3_error(E_WARNING, "imap_utf7_encode: "
+ "Unable to allocate result string");
RETURN_FALSE;
+ }
/* encode input string */
outp = out;
@@ -2721,13 +2763,15 @@
*outp++ = '&';
state = ST_ENCODE0;
}
- else if ((*outp++ = *inp++) == '&')
+ else if ((*outp++ = *inp++) == '&') {
*outp++ = '-';
+ }
}
else if (inp == endp || !SPECIAL(*inp)) {
/* flush overflow and terminate region */
- if (state != ST_ENCODE0)
+ if (state != ST_ENCODE0) {
*outp++ = B64(*outp);
+ }
*outp++ = '-';
state = ST_NORMAL;
}
@@ -2752,6 +2796,15 @@
}
}
}
+
+#if DEBUG
+ /* warn if we computed outlen incorrectly */
+ if (outp - out != outlen) {
+ php3_error(E_WARNING,
+ "imap_utf7_encode: outp - out [%d] != outlen [%d]",
+ outp - out, outlen);
+ }
+#endif
RETURN_STRINGL(out, outlen, 0);
}