예제 #1
0
파일: evtstr.c 프로젝트: mochrul/eventlog
int
evt_str_append_escape_bs(EVTSTR *es, 
                        char *unescaped, size_t unescaped_len, 
                        char escape_char)
{
  /* a single character is escaped to at most 4 characters: \xXX */

  /* FIXME: this is a gcc extension, alternative would be to use alloca(),
   * which is not portable */

  char *buf = (char *)alloca(4*unescaped_len + 1);
                             
  int i, dst;
  
  for (i = 0, dst = 0; i < unescaped_len; i++)
    {
      
      if ((unsigned) unescaped[i] < 32 || (unsigned) unescaped[i] > 127)
        {
          sprintf(&buf[dst], "\\x%02x", (unsigned char) unescaped[i]);
          dst += 4;
        }
      else if (unescaped[i] == escape_char)
        {
          buf[dst++] = '\\';
          buf[dst++] = escape_char;
        }
      else
        {
          buf[dst++] = unescaped[i];
        }
      assert(dst <= 4*unescaped_len);
    }
  return evt_str_append_len(es, buf, dst);
}
예제 #2
0
파일: evtstr.c 프로젝트: mochrul/eventlog
int
evt_str_append_escape_xml_attr(EVTSTR *es, 
                              char *unescaped, size_t unescaped_len)
{
  /* a single character is escaped to at most 6 characters: '&#xXX;' or '&quot;' */

  /* FIXME: this is a gcc extension, alternative would be to use alloca(),
   * which is not portable */

  char *buf = (char *)alloca(6*unescaped_len + 1);
                             
  int i, dst;
  
  for (i = 0, dst = 0; i < unescaped_len; i++)
    {
      if ((unsigned) unescaped[i] < 32)
        {
          sprintf(&buf[dst], "&#x%02x;", (unsigned char) unescaped[i]);
          dst += 6;
        }
      else if (unescaped[i] == '"')
        {
          strcpy(&buf[dst], "&quot;");
          dst += 6;
        }
      else
        {
          buf[dst++] = unescaped[i];
        }
      assert(dst <= 6*unescaped_len);
    }
  return evt_str_append_len(es, buf, dst);
}
예제 #3
0
int
evt_str_append_escape_bs(EVTSTR *es,
                         char *unescaped, size_t unescaped_len,
                         char escape_char)
{
  /* a single character is escaped to at most 4 characters: \xXX */
  size_t escaped_char_max_len = 4;

  char escaped_buffer[128];
  size_t escaped_buffer_capacity = sizeof(escaped_buffer) / sizeof(escaped_buffer[0]);
  size_t escaped_buffer_length = 0;

  int i;

  for (i = 0; i < unescaped_len; i++)
    {
      unsigned c = (unsigned) unescaped[i];

      if (c < 32 && c != '\t')
        {
          sprintf(escaped_buffer + escaped_buffer_length, "\\x%02x", (unsigned char) unescaped[i]);
          escaped_buffer_length += 4;
        }
      else if (unescaped[i] == escape_char)
        {
          escaped_buffer[escaped_buffer_length++] = '\\';
          escaped_buffer[escaped_buffer_length++] = escape_char;
        }
      else
        {
          escaped_buffer[escaped_buffer_length++] = unescaped[i];
        }

      if (escaped_buffer_capacity < escaped_buffer_length + escaped_char_max_len)
        {
          if (!evt_str_append_len(es, escaped_buffer, escaped_buffer_length))
            return 0;

          escaped_buffer_length = 0;
        }
    }

  return evt_str_append_len(es, escaped_buffer, escaped_buffer_length);
}
예제 #4
0
파일: evtstr.c 프로젝트: mochrul/eventlog
int
evt_str_append(EVTSTR *es, char *str)
{
  return evt_str_append_len(es, str, strlen(str));
}