static size_t escape_string_hide_passwords()

in plugin/server_audit/server_audit.cc [1342:1437]


static size_t escape_string_hide_passwords(const char *str, unsigned int len,
    char *result, size_t result_len,
    const char *word1, size_t word1_len,
    const char *word2, size_t word2_len,
    int next_text_string)
{
  const char *res_start= result;
  const char *res_end= result + result_len - 2;
  size_t d_len;

  while (len)
  {
    if (len > word1_len + 1 && strncasecmp(str, word1, word1_len) == 0)
    {
      const char *next_s= str + word1_len;
      size_t c;

      if (next_text_string)
      {
        while (*next_s && *next_s != '\'' && *next_s != '"')
          ++next_s;
      }
      else
      {
        if (word2)
        {
          SKIP_SPACES(next_s);
          if (len < (next_s - str) + word2_len + 1 ||
              strncasecmp(next_s, word2, word2_len) != 0)
            goto no_password;
          next_s+= word2_len;
        }

        while (*next_s && *next_s != '\'' && *next_s != '"')
          ++next_s;
      }

      d_len= next_s - str;
      if (result + d_len + 5 > res_end)
        break;

      for (c=0; c<d_len; c++)
        result[c]= is_space(str[c]) ? ' ' : str[c];

      if (*next_s)
      {
        const char b_char= *next_s++;
        memset(result + d_len, '*', 5);
        result+= d_len + 5;

        while (*next_s)
        {
          if (*next_s == b_char)
          {
            ++next_s;
            break;
          }
          if (*next_s == '\\')
          {
            if (next_s[1])
              next_s++;
          }
          next_s++;
        }
      }
      else
        result+= d_len;

      len-= (uint)(next_s - str);
      str= next_s;
      continue;
    }
no_password:
    if (result >= res_end)
      break;
    else
    {
      const char b_char= escaped_char(*str);
      if (b_char)
      {
        if (result+1 >= res_end)
          break;
        *(result++)= '\\';
        *(result++)= b_char;
      }
      else if (is_space(*str))
        *(result++)= ' ';
      else
        *(result++)= *str;
      str++;
      len--;
    }
  }
  *result= 0;
  return result - res_start;
}