Wall posté le Mercredi 16 janvier 2008


...


  1. <?php
  2. /*
  3. void decode_lwsp (const char **in)
  4. {
  5.    const char *inptr = *in;
  6.  
  7.    while (*inptr && (*inptr == '(' || is_lwsp (*inptr))) {
  8.  
  9.       while (*inptr && is_lwsp (*inptr))
  10.       inptr++;
  11.  
  12.       if (*inptr == '(') {
  13.          int depth = 1;
  14.  
  15.          inptr++;
  16.          while (*inptr && depth) {
  17.             if (*inptr == '\\' && *(inptr + 1))
  18.                inptr++;
  19.             else if (*inptr == '(')
  20.                depth++;
  21.             else if (*inptr == ')')
  22.                depth--;
  23.  
  24.             inptr++;
  25.          }
  26.       }
  27.    }
  28.  
  29.    *in = inptr;
  30. }
  31. */  
  32.  
  33. function decode_lwsp (&$in, $return_decalage = false) { 
  34.    $inptr = $in; 
  35.  
  36.    $i = 0; 
  37.    while (strlen($inptr) > 0 && ($inptr{0} == '(' || is_lwsp($inptr{0}))) { 
  38.       while (strlen($inptr) > 0 && is_lwsp($inptr{0})) { 
  39.          $inptr = substr($inptr, 1); 
  40.          $i++; 
  41.       } 
  42.  
  43.       if (strlen($inptr) > 0 && $inptr{0} == '(') { 
  44.          $depth = 1; 
  45.  
  46.          $inptr = substr($inptr, 1); 
  47.          $i++; 
  48.  
  49.          while (strlen($inptr) > 0 && $depth != 0) { 
  50.          if ($inptr{0} == '\\' && strlen($inptr) > 1) { 
  51.             $inptr = substr($inptr, 1); 
  52.             $i++; 
  53.          } 
  54.          else if ($inptr{0} == '(') 
  55.             $depth++; 
  56.          else if ($inptr{0} == ')') 
  57.             $depth--; 
  58.  
  59.          $inptr = substr($inptr, 1); 
  60.          $i++; 
  61.          } 
  62.       } 
  63.    } 
  64.  
  65.    $in = $inptr; 
  66.  
  67.    if ($return_decalage) return $i;  
  68. }  
  69.  
  70. /*
  71. function is_lwsp($char) {
  72.    $string = " \t\n\r";
  73.  
  74.    $return = false;
  75.    if (strstr($string, $char)) {
  76.       $return = true;
  77.    }
  78.  
  79.    return $return;
  80. }
  81. */  
  82.  
  83. function is_lwsp($char) { 
  84.    $string = " \t\n\r"; 
  85.    if (strstr($string, $char) != false) { 
  86.       return true; 
  87.    } 
  88.    return false;  
  89. }  
  90.  
  91. /*
  92. static char *
  93. decode_quoted_string (const char **in)
  94. {
  95.    const char *inptr = *in;
  96.    char *out = NULL;
  97.  
  98.    decode_lwsp (&inptr);
  99.    if (*inptr == '"') {
  100.       out = (char *) inptr;
  101.  
  102.       inptr++;
  103.       while (*inptr && *inptr != '"') {
  104.       if (*inptr == '\\')
  105.          inptr++;
  106.  
  107.       if (*inptr)
  108.          inptr++;
  109.       }
  110.  
  111.       if (*inptr == '"')
  112.       inptr++;
  113.  
  114.       out = g_strndup (out, inptr - out);
  115.    }
  116.  
  117.    *in = inptr;
  118.  
  119.    return out;
  120. }
  121. */  
  122.  
  123. function decode_quoted_string(&$in) { 
  124.    $inptr = $in; 
  125.    $out = false; 
  126.  
  127.    decode_lwsp ($inptr); 
  128.  
  129.    if (strlen($inptr)>0 && $inptr{0} == '"') { 
  130.       $out = $inptr; 
  131.  
  132.       $inptr = substr($inptr, 1); 
  133.  
  134.       $i = 1; 
  135.       while (strlen($inptr)>0 && $inptr{0} != '"') { 
  136.          if (strlen($inptr)>0 && $inptr{0} == '\\') { 
  137.             $inptr = substr($inptr, 1); 
  138.             $i++; 
  139.          } 
  140.  
  141.          if (strlen($inptr)>0) { 
  142.             $inptr = substr($inptr, 1); 
  143.             $i++; 
  144.          } 
  145.       } 
  146.  
  147.       if (strlen($inptr)>0 && $inptr{0} == '"') { 
  148.          $inptr = substr($inptr, 1); 
  149.       $i++; 
  150.       } 
  151.  
  152.       $out = substr($out, 0, $i); 
  153.    } 
  154.  
  155.    $in = $inptr; 
  156.  
  157.    return $out;  
  158. }  
  159.  
  160. /*
  161. static char *
  162. decode_atom (const char **in)
  163. {
  164.    const char *inptr = *in, *start;
  165.  
  166.    decode_lwsp (&inptr);
  167.    start = inptr;
  168.    while (is_atom (*inptr))
  169.       inptr++;
  170.    *in = inptr;
  171.    if (inptr > start)
  172.       return g_strndup (start, inptr - start);
  173.    else
  174.       return NULL;
  175. }
  176. */  
  177.  
  178. function decode_atom(&$in) { 
  179.    $inptr = $in; 
  180.  
  181.    decode_lwsp($inptr); 
  182.  
  183.    $start = $inptr; 
  184.  
  185.    $i = 0; 
  186.    while (strlen($inptr) > 0 && is_atom($inptr{0})) { 
  187.       $inptr = substr($inptr, 1); 
  188.       $i++; 
  189.    } 
  190.    $in = $inptr; 
  191.    if ($i > 0) { 
  192.       return substr($start, 0, $i); 
  193.    } 
  194.    else { 
  195.       return false; 
  196.    }  
  197. }  
  198.  
  199. function is_atom ($char) { 
  200.    $string = "()<>@,;:\\\".[] "; 
  201.  
  202.    if (strstr($string, $char) != false) { 
  203.       return false; 
  204.    } 
  205.  
  206.    if (ord($char) < 32 || ord($char) == 127) { 
  207.       return false; 
  208.    } 
  209.  
  210.    return true;  
  211. }  
  212.  
  213. /*
  214. char *
  215. decode_word (const char **in)
  216. {
  217.    const char *inptr = *in;
  218.  
  219.    decode_lwsp (&inptr);
  220.    if (*inptr == '"') {
  221.       *in = inptr;
  222.       return decode_quoted_string (in);
  223.    } else {
  224.       *in = inptr;
  225.       return decode_atom (in);
  226.    }
  227. }
  228. */  
  229.  
  230. function decode_word(&$in) { 
  231.    $inptr = $in; 
  232.  
  233.    decode_lwsp($inptr); 
  234.  
  235.    if (strlen($inptr) > 0 && $inptr{0} == '"') { 
  236.       $in = $inptr; 
  237.       return decode_quoted_string($in); 
  238.    } 
  239.    else { 
  240.       $in = $inptr; 
  241.       return decode_atom($in); 
  242.    }  
  243. }  
  244.  
  245. /*
  246. static gboolean
  247. decode_subliteral (const char **in, GString *domain)
  248. {
  249.    const char *inptr = *in;
  250.    gboolean got = FALSE;
  251.  
  252.    while (*inptr && *inptr != '.' && *inptr != ']') {
  253.       if (is_dtext (*inptr)) {
  254.          g_string_append_c (domain, *inptr);
  255.          inptr++;
  256.          got = TRUE;
  257.       } else if (is_lwsp (*inptr))
  258.          decode_lwsp (&inptr);
  259.       else
  260.          break;
  261.    }
  262.  
  263.    *in = inptr;
  264.  
  265.    return got;
  266. }
  267. */  
  268.  
  269. function decode_subliteral (&$in, &$domain) { 
  270.    $inptr = $in; 
  271.    $got = false; 
  272.  
  273.    while (strlen($inptr)>0 && $inptr{0} != '.' && $inptr{0} != ']') { 
  274.       if (is_dtext($inptr{0})) { 
  275.          $domain .= $inptr{0}; 
  276.          $inptr = substr($inptr, 1); 
  277.          $got = true; 
  278.       } 
  279.       elseif (is_lwsp ($inptr{0})) { 
  280.          decode_lwsp ($inptr); 
  281.       } 
  282.       else break; 
  283.    } 
  284.  
  285.    $in = $inptr; 
  286.  
  287.    return $got;  
  288. }  
  289.  
  290. /*
  291. gmime/gmime-table-private.h:#define is_dtext(x) ((gmime_special_table[(unsigned char)(x)] & IS_DSPECIAL) == 0)
  292. CHARS_DSPECIAL "[]\\\r \t"
  293. */  
  294.  
  295. function is_dtext($char) { 
  296.    $string = "[]\\\r \t"; 
  297.  
  298.    $return = false; 
  299.    if (strstr($string, $char)) { 
  300.       $return = true; 
  301.    } 
  302.  
  303.    return $return;  
  304. }  
  305.  
  306. /*
  307. static void
  308. decode_domain_literal (const char **in, GString *domain)
  309. {
  310.    const char *inptr = *in;
  311.  
  312.    decode_lwsp (&inptr);
  313.    while (*inptr && *inptr != ']') {
  314.       if (decode_subliteral (&inptr, domain) && *inptr == '.') {
  315.          g_string_append_c (domain, *inptr);
  316.          inptr++;
  317.       } else if (*inptr != ']') {
  318.          w(g_warning ("Malformed domain-literal, unexpected char '%c': %s",
  319.              *inptr, *in));
  320.          inptr++;
  321.       }
  322.    }
  323.  
  324.    *in = inptr;
  325. }
  326. */  
  327.  
  328. function decode_domain_literal (&$in, &$domain) { 
  329.    $inptr = $in; 
  330.  
  331.    decode_lwsp ($inptr); 
  332.    while (strlen($inptr)>0 && $inptr{0} != ']') { 
  333.       if (decode_subliteral ($inptr, $domain) && $inptr{0} == '.') { 
  334.          $domain .= $inptr{0}; 
  335.          $inptr = substr($inptr, 1); 
  336.       } 
  337.       elseif (strlen($inptr)>0 && $inptr{0} != ']') { 
  338.          $inptr = substr($inptr, 1); 
  339.       } 
  340.    } 
  341.  
  342.    $in = $inptr;  
  343. }  
  344.  
  345. /*
  346. static char *
  347. decode_domain (const char **in)
  348. {
  349.    const char *inptr, *save;
  350.    GString *domain;
  351.    char *dom, *atom;
  352.  
  353.    domain = g_string_new ("");
  354.  
  355.    inptr = *in;
  356.    while (inptr && *inptr) {
  357.       decode_lwsp (&inptr);
  358.       if (*inptr == '[') {
  359.          g_string_append_c (domain, '[');
  360.          inptr++;
  361.  
  362.          decode_domain_literal (&inptr, domain);
  363.  
  364.          if (*inptr == ']') {
  365.             g_string_append_c (domain, ']');
  366.             inptr++;
  367.          } else
  368.             w(g_warning ("Missing ']' in domain-literal: %s", *in));
  369.       } else {
  370.          if (!(atom = decode_atom (&inptr))) {
  371.             w(g_warning ("Unexpected char '%c' in domain: %s", *inptr, *in));
  372.             if (domain->len && domain->str[domain->len - 1] == '.')
  373.                g_string_truncate (domain, domain->len - 1);
  374.             break;
  375.          }
  376.  
  377.          g_string_append (domain, atom);
  378.          g_free (atom);
  379.       }
  380.  
  381.       save = inptr;
  382.       decode_lwsp (&inptr);
  383.       if (*inptr != '.') {
  384.          inptr = save;
  385.          break;
  386.       }
  387.  
  388.       g_string_append_c (domain, '.');
  389.       inptr++;
  390.    }
  391.  
  392.    if (domain->len)
  393.       dom = domain->str;
  394.    else
  395.       dom = NULL;
  396.  
  397.    g_string_free (domain, dom ? FALSE : TRUE);
  398.  
  399.    *in = inptr;
  400.  
  401.    return dom;
  402. }
  403. */  
  404.  
  405. function decode_domain (&$in) { 
  406.  
  407.    $domain = ""; 
  408.    $atom = ""; 
  409.    $inptr = $in; 
  410.  
  411.    while ($inptr != false && strlen($inptr)>0) { 
  412.       decode_lwsp ($inptr); 
  413.       if ($inptr{0} == '[') { 
  414.          $domain .= '['; 
  415.          $inptr = substr($inptr, 1); 
  416.  
  417.          decode_domain_literal ($inptr, $domain); 
  418.  
  419.          if ($inptr{0} == ']') { 
  420.             $domain .= ']'; 
  421.             $inptr = substr($inptr, 1); 
  422.          } 
  423.          else { 
  424.             // warning !!! 
  425.          } 
  426.       } 
  427.       else { 
  428.          if (!($atom = decode_atom ($inptr))) { 
  429.             // warning !!! 
  430.             if ($domain{strlen($domain)-1} == '.') 
  431.                $domain = substr($domain, 0, -1); 
  432.             break; 
  433.          } 
  434.  
  435.          $domain .= $atom; 
  436.          $atom = false; 
  437.       } 
  438.  
  439.       $save = $inptr; 
  440.       decode_lwsp ($inptr); 
  441.       if ($inptr{0} != '.') { 
  442.          $inptr = $save; 
  443.          break; 
  444.       } 
  445.  
  446.       $domain .= '.'; 
  447.  
  448.       $inptr = substr($inptr, 1); 
  449.    } 
  450.  
  451.    if (strlen($domain)>0) 
  452.       $dom = $domain; 
  453.    else 
  454.       $dom = false; 
  455.  
  456.    //g_string_free (domain, dom ? FALSE : TRUE); 
  457.  
  458.    $in = $inptr; 
  459.  
  460.    return $dom;  
  461. }  
  462.  
  463. /*
  464. char *
  465. decode_addrspec (const char **in)
  466. {
  467.    char *domain, *word, *str = NULL;
  468.    const char *inptr;
  469.    GString *addrspec;
  470.  
  471.    decode_lwsp (in);
  472.    inptr = *in;
  473.  
  474.    if (!(word = decode_word (&inptr))) {
  475.       w(g_warning ("No local-part in addr-spec: %s", *in));
  476.       return NULL;
  477.    }
  478.  
  479.    addrspec = g_string_new (word);
  480.    g_free (word);
  481.  
  482.    decode_lwsp (&inptr);
  483.    while (*inptr == '.') {
  484.       g_string_append_c (addrspec, *inptr++);
  485.       word = decode_word (&inptr);
  486.       if (word) {
  487.