Wall posté le Mercredi 16 janvier 2008
...
- <?php
- /*
- void decode_lwsp (const char **in)
- {
- const char *inptr = *in;
- while (*inptr && (*inptr == '(' || is_lwsp (*inptr))) {
- while (*inptr && is_lwsp (*inptr))
- inptr++;
- if (*inptr == '(') {
- int depth = 1;
- inptr++;
- while (*inptr && depth) {
- if (*inptr == '\\' && *(inptr + 1))
- inptr++;
- else if (*inptr == '(')
- depth++;
- else if (*inptr == ')')
- depth--;
- inptr++;
- }
- }
- }
- *in = inptr;
- }
- */
- function decode_lwsp (&$in, $return_decalage = false) {
- $inptr = $in;
- $i = 0;
- while (strlen($inptr) > 0 && ($inptr{0} == '(' || is_lwsp($inptr{0}))) {
- while (strlen($inptr) > 0 && is_lwsp($inptr{0})) {
- $inptr = substr($inptr, 1);
- $i++;
- }
- if (strlen($inptr) > 0 && $inptr{0} == '(') {
- $depth = 1;
- $inptr = substr($inptr, 1);
- $i++;
- while (strlen($inptr) > 0 && $depth != 0) {
- if ($inptr{0} == '\\' && strlen($inptr) > 1) {
- $inptr = substr($inptr, 1);
- $i++;
- }
- else if ($inptr{0} == '(')
- $depth++;
- else if ($inptr{0} == ')')
- $depth--;
- $inptr = substr($inptr, 1);
- $i++;
- }
- }
- }
- $in = $inptr;
- if ($return_decalage) return $i;
- }
- /*
- function is_lwsp($char) {
- $string = " \t\n\r";
- $return = false;
- if (strstr($string, $char)) {
- $return = true;
- }
- return $return;
- }
- */
- function is_lwsp($char) {
- $string = " \t\n\r";
- if (strstr($string, $char) != false) {
- return true;
- }
- return false;
- }
- /*
- static char *
- decode_quoted_string (const char **in)
- {
- const char *inptr = *in;
- char *out = NULL;
- decode_lwsp (&inptr);
- if (*inptr == '"') {
- out = (char *) inptr;
- inptr++;
- while (*inptr && *inptr != '"') {
- if (*inptr == '\\')
- inptr++;
- if (*inptr)
- inptr++;
- }
- if (*inptr == '"')
- inptr++;
- out = g_strndup (out, inptr - out);
- }
- *in = inptr;
- return out;
- }
- */
- function decode_quoted_string(&$in) {
- $inptr = $in;
- $out = false;
- decode_lwsp ($inptr);
- if (strlen($inptr)>0 && $inptr{0} == '"') {
- $out = $inptr;
- $inptr = substr($inptr, 1);
- $i = 1;
- while (strlen($inptr)>0 && $inptr{0} != '"') {
- if (strlen($inptr)>0 && $inptr{0} == '\\') {
- $inptr = substr($inptr, 1);
- $i++;
- }
- if (strlen($inptr)>0) {
- $inptr = substr($inptr, 1);
- $i++;
- }
- }
- if (strlen($inptr)>0 && $inptr{0} == '"') {
- $inptr = substr($inptr, 1);
- $i++;
- }
- $out = substr($out, 0, $i);
- }
- $in = $inptr;
- return $out;
- }
- /*
- static char *
- decode_atom (const char **in)
- {
- const char *inptr = *in, *start;
- decode_lwsp (&inptr);
- start = inptr;
- while (is_atom (*inptr))
- inptr++;
- *in = inptr;
- if (inptr > start)
- return g_strndup (start, inptr - start);
- else
- return NULL;
- }
- */
- function decode_atom(&$in) {
- $inptr = $in;
- decode_lwsp($inptr);
- $start = $inptr;
- $i = 0;
- while (strlen($inptr) > 0 && is_atom($inptr{0})) {
- $inptr = substr($inptr, 1);
- $i++;
- }
- $in = $inptr;
- if ($i > 0) {
- return substr($start, 0, $i);
- }
- else {
- return false;
- }
- }
- function is_atom ($char) {
- $string = "()<>@,;:\\\".[] ";
- if (strstr($string, $char) != false) {
- return false;
- }
- if (ord($char) < 32 || ord($char) == 127) {
- return false;
- }
- return true;
- }
- /*
- char *
- decode_word (const char **in)
- {
- const char *inptr = *in;
- decode_lwsp (&inptr);
- if (*inptr == '"') {
- *in = inptr;
- return decode_quoted_string (in);
- } else {
- *in = inptr;
- return decode_atom (in);
- }
- }
- */
- function decode_word(&$in) {
- $inptr = $in;
- decode_lwsp($inptr);
- if (strlen($inptr) > 0 && $inptr{0} == '"') {
- $in = $inptr;
- return decode_quoted_string($in);
- }
- else {
- $in = $inptr;
- return decode_atom($in);
- }
- }
- /*
- static gboolean
- decode_subliteral (const char **in, GString *domain)
- {
- const char *inptr = *in;
- gboolean got = FALSE;
- while (*inptr && *inptr != '.' && *inptr != ']') {
- if (is_dtext (*inptr)) {
- g_string_append_c (domain, *inptr);
- inptr++;
- got = TRUE;
- } else if (is_lwsp (*inptr))
- decode_lwsp (&inptr);
- else
- break;
- }
- *in = inptr;
- return got;
- }
- */
- function decode_subliteral (&$in, &$domain) {
- $inptr = $in;
- $got = false;
- while (strlen($inptr)>0 && $inptr{0} != '.' && $inptr{0} != ']') {
- if (is_dtext($inptr{0})) {
- $domain .= $inptr{0};
- $inptr = substr($inptr, 1);
- $got = true;
- }
- elseif (is_lwsp ($inptr{0})) {
- decode_lwsp ($inptr);
- }
- else break;
- }
- $in = $inptr;
- return $got;
- }
- /*
- gmime/gmime-table-private.h:#define is_dtext(x) ((gmime_special_table[(unsigned char)(x)] & IS_DSPECIAL) == 0)
- CHARS_DSPECIAL "[]\\\r \t"
- */
- function is_dtext($char) {
- $string = "[]\\\r \t";
- $return = false;
- if (strstr($string, $char)) {
- $return = true;
- }
- return $return;
- }
- /*
- static void
- decode_domain_literal (const char **in, GString *domain)
- {
- const char *inptr = *in;
- decode_lwsp (&inptr);
- while (*inptr && *inptr != ']') {
- if (decode_subliteral (&inptr, domain) && *inptr == '.') {
- g_string_append_c (domain, *inptr);
- inptr++;
- } else if (*inptr != ']') {
- w(g_warning ("Malformed domain-literal, unexpected char '%c': %s",
- *inptr, *in));
- inptr++;
- }
- }
- *in = inptr;
- }
- */
- function decode_domain_literal (&$in, &$domain) {
- $inptr = $in;
- decode_lwsp ($inptr);
- while (strlen($inptr)>0 && $inptr{0} != ']') {
- if (decode_subliteral ($inptr, $domain) && $inptr{0} == '.') {
- $domain .= $inptr{0};
- $inptr = substr($inptr, 1);
- }
- elseif (strlen($inptr)>0 && $inptr{0} != ']') {
- $inptr = substr($inptr, 1);
- }
- }
- $in = $inptr;
- }
- /*
- static char *
- decode_domain (const char **in)
- {
- const char *inptr, *save;
- GString *domain;
- char *dom, *atom;
- domain = g_string_new ("");
- inptr = *in;
- while (inptr && *inptr) {
- decode_lwsp (&inptr);
- if (*inptr == '[') {
- g_string_append_c (domain, '[');
- inptr++;
- decode_domain_literal (&inptr, domain);
- if (*inptr == ']') {
- g_string_append_c (domain, ']');
- inptr++;
- } else
- w(g_warning ("Missing ']' in domain-literal: %s", *in));
- } else {
- if (!(atom = decode_atom (&inptr))) {
- w(g_warning ("Unexpected char '%c' in domain: %s", *inptr, *in));
- if (domain->len && domain->str[domain->len - 1] == '.')
- g_string_truncate (domain, domain->len - 1);
- break;
- }
- g_string_append (domain, atom);
- g_free (atom);
- }
- save = inptr;
- decode_lwsp (&inptr);
- if (*inptr != '.') {
- inptr = save;
- break;
- }
- g_string_append_c (domain, '.');
- inptr++;
- }
- if (domain->len)
- dom = domain->str;
- else
- dom = NULL;
- g_string_free (domain, dom ? FALSE : TRUE);
- *in = inptr;
- return dom;
- }
- */
- function decode_domain (&$in) {
- $domain = "";
- $atom = "";
- $inptr = $in;
- while ($inptr != false && strlen($inptr)>0) {
- decode_lwsp ($inptr);
- if ($inptr{0} == '[') {
- $domain .= '[';
- $inptr = substr($inptr, 1);
- decode_domain_literal ($inptr, $domain);
- if ($inptr{0} == ']') {
- $domain .= ']';
- $inptr = substr($inptr, 1);
- }
- else {
- // warning !!!
- }
- }
- else {
- if (!($atom = decode_atom ($inptr))) {
- // warning !!!
- if ($domain{strlen($domain)-1} == '.')
- $domain = substr($domain, 0, -1);
- break;
- }
- $domain .= $atom;
- $atom = false;
- }
- $save = $inptr;
- decode_lwsp ($inptr);
- if ($inptr{0} != '.') {
- $inptr = $save;
- break;
- }
- $domain .= '.';
- $inptr = substr($inptr, 1);
- }
- if (strlen($domain)>0)
- $dom = $domain;
- else
- $dom = false;
- //g_string_free (domain, dom ? FALSE : TRUE);
- $in = $inptr;
- return $dom;
- }
- /*
- char *
- decode_addrspec (const char **in)
- {
- char *domain, *word, *str = NULL;
- const char *inptr;
- GString *addrspec;
- decode_lwsp (in);
- inptr = *in;
- if (!(word = decode_word (&inptr))) {
- w(g_warning ("No local-part in addr-spec: %s", *in));
- return NULL;
- }
- addrspec = g_string_new (word);
- g_free (word);
- decode_lwsp (&inptr);
- while (*inptr == '.') {
- g_string_append_c (addrspec, *inptr++);
- word = decode_word (&inptr);
- if (word) {
-  

