tor  0.4.0.1-alpha
escape.c
Go to the documentation of this file.
1 /* Copyright (c) 2003, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #include "lib/log/escape.h"
12 #include "lib/log/util_bug.h"
14 #include "lib/string/printf.h"
15 #include "lib/malloc/malloc.h"
16 
29 char *
30 esc_for_log(const char *s)
31 {
32  const char *cp;
33  char *result, *outp;
34  size_t len = 3;
35  if (!s) {
36  return tor_strdup("(null)");
37  }
38 
39  for (cp = s; *cp; ++cp) {
40  switch (*cp) {
41  case '\\':
42  case '\"':
43  case '\'':
44  case '\r':
45  case '\n':
46  case '\t':
47  len += 2;
48  break;
49  default:
50  if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127)
51  ++len;
52  else
53  len += 4;
54  break;
55  }
56  }
57 
58  tor_assert(len <= SSIZE_MAX);
59 
60  result = outp = tor_malloc(len);
61  *outp++ = '\"';
62  for (cp = s; *cp; ++cp) {
63  /* This assertion should always succeed, since we will write at least
64  * one char here, and two chars for closing quote and nul later */
65  tor_assert((outp-result) < (ssize_t)len-2);
66  switch (*cp) {
67  case '\\':
68  case '\"':
69  case '\'':
70  *outp++ = '\\';
71  *outp++ = *cp;
72  break;
73  case '\n':
74  *outp++ = '\\';
75  *outp++ = 'n';
76  break;
77  case '\t':
78  *outp++ = '\\';
79  *outp++ = 't';
80  break;
81  case '\r':
82  *outp++ = '\\';
83  *outp++ = 'r';
84  break;
85  default:
86  if (TOR_ISPRINT(*cp) && ((uint8_t)*cp)<127) {
87  *outp++ = *cp;
88  } else {
89  tor_assert((outp-result) < (ssize_t)len-4);
90  tor_snprintf(outp, 5, "\\%03o", (int)(uint8_t) *cp);
91  outp += 4;
92  }
93  break;
94  }
95  }
96 
97  tor_assert((outp-result) <= (ssize_t)len-2);
98  *outp++ = '\"';
99  *outp++ = 0;
100 
101  return result;
102 }
103 
109 char *
110 esc_for_log_len(const char *chars, size_t n)
111 {
112  char *string = tor_strndup(chars, n);
113  char *string_escaped = esc_for_log(string);
114  tor_free(string);
115  return string_escaped;
116 }
117 
125 const char *
126 escaped(const char *s)
127 {
128  static char *escaped_val_ = NULL;
129  tor_free(escaped_val_);
130 
131  if (s)
132  escaped_val_ = esc_for_log(s);
133  else
134  escaped_val_ = NULL;
135 
136  return escaped_val_;
137 }
Header for printf.c.
char * esc_for_log_len(const char *chars, size_t n)
Definition: escape.c:110
#define tor_free(p)
Definition: malloc.h:52
Headers for util_malloc.c.
tor_assert(buffer)
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
const char * escaped(const char *s)
Definition: escape.c:126
Locale-independent character-type inspection (header)
Header for escape.c.
char * esc_for_log(const char *s)
Definition: escape.c:30
Macros to manage assertions, fatal and non-fatal.