Coverage Report

Created: 2025-07-11 06:20

/src/tmux/compat/vis.c
Line
Count
Source (jump to first uncovered line)
1
/*  $OpenBSD: vis.c,v 1.24 2015/07/20 01:52:28 millert Exp $ */
2
/*-
3
 * Copyright (c) 1989, 1993
4
 *  The Regents of the University of California.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. Neither the name of the University nor the names of its contributors
15
 *    may be used to endorse or promote products derived from this software
16
 *    without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include <sys/types.h>
32
#include <errno.h>
33
#include <ctype.h>
34
#include <limits.h>
35
#include <string.h>
36
#include <stdlib.h>
37
38
#include "compat.h"
39
40
0
#define isoctal(c)  (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
41
#define isvisible(c,flag)           \
42
17.0k
  (((c) == '\\' || (flag & VIS_ALL) == 0) &&      \
43
17.0k
  (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) &&   \
44
17.0k
  (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') ||  \
45
12.4k
    (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) ||  \
46
17.0k
  ((flag & VIS_SP) == 0 && (c) == ' ') ||       \
47
17.0k
  ((flag & VIS_TAB) == 0 && (c) == '\t') ||     \
48
17.0k
  ((flag & VIS_NL) == 0 && (c) == '\n') ||     \
49
17.0k
  ((flag & VIS_SAFE) && ((c) == '\b' ||       \
50
0
    (c) == '\007' || (c) == '\r' ||       \
51
0
    isgraph((u_char)(c))))))
52
53
/*
54
 * vis - visually encode characters
55
 */
56
char *
57
vis(char *dst, int c, int flag, int nextc)
58
17.0k
{
59
17.0k
  if (isvisible(c, flag)) {
60
12.3k
    if ((c == '"' && (flag & VIS_DQ) != 0) ||
61
12.3k
        (c == '\\' && (flag & VIS_NOSLASH) == 0))
62
852
      *dst++ = '\\';
63
12.3k
    *dst++ = c;
64
12.3k
    *dst = '\0';
65
12.3k
    return (dst);
66
12.3k
  }
67
68
4.67k
  if (flag & VIS_CSTYLE) {
69
4.67k
    switch(c) {
70
0
    case '\n':
71
0
      *dst++ = '\\';
72
0
      *dst++ = 'n';
73
0
      goto done;
74
0
    case '\r':
75
0
      *dst++ = '\\';
76
0
      *dst++ = 'r';
77
0
      goto done;
78
0
    case '\b':
79
0
      *dst++ = '\\';
80
0
      *dst++ = 'b';
81
0
      goto done;
82
0
    case '\a':
83
0
      *dst++ = '\\';
84
0
      *dst++ = 'a';
85
0
      goto done;
86
0
    case '\v':
87
0
      *dst++ = '\\';
88
0
      *dst++ = 'v';
89
0
      goto done;
90
0
    case '\t':
91
0
      *dst++ = '\\';
92
0
      *dst++ = 't';
93
0
      goto done;
94
0
    case '\f':
95
0
      *dst++ = '\\';
96
0
      *dst++ = 'f';
97
0
      goto done;
98
0
    case ' ':
99
0
      *dst++ = '\\';
100
0
      *dst++ = 's';
101
0
      goto done;
102
0
    case '\0':
103
0
      *dst++ = '\\';
104
0
      *dst++ = '0';
105
0
      if (isoctal(nextc)) {
106
0
        *dst++ = '0';
107
0
        *dst++ = '0';
108
0
      }
109
0
      goto done;
110
4.67k
    }
111
4.67k
  }
112
4.67k
  if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
113
4.67k
      ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
114
4.67k
    *dst++ = '\\';
115
4.67k
    *dst++ = ((u_char)c >> 6 & 07) + '0';
116
4.67k
    *dst++ = ((u_char)c >> 3 & 07) + '0';
117
4.67k
    *dst++ = ((u_char)c & 07) + '0';
118
4.67k
    goto done;
119
4.67k
  }
120
0
  if ((flag & VIS_NOSLASH) == 0)
121
0
    *dst++ = '\\';
122
0
  if (c & 0200) {
123
0
    c &= 0177;
124
0
    *dst++ = 'M';
125
0
  }
126
0
  if (iscntrl((u_char)c)) {
127
0
    *dst++ = '^';
128
0
    if (c == 0177)
129
0
      *dst++ = '?';
130
0
    else
131
0
      *dst++ = c + '@';
132
0
  } else {
133
0
    *dst++ = '-';
134
0
    *dst++ = c;
135
0
  }
136
4.67k
done:
137
4.67k
  *dst = '\0';
138
4.67k
  return (dst);
139
0
}
140
141
/*
142
 * strvis, strnvis, strvisx - visually encode characters from src into dst
143
 *  
144
 *  Dst must be 4 times the size of src to account for possible
145
 *  expansion.  The length of dst, not including the trailing NULL,
146
 *  is returned. 
147
 *
148
 *  Strnvis will write no more than siz-1 bytes (and will NULL terminate).
149
 *  The number of bytes needed to fully encode the string is returned.
150
 *
151
 *  Strvisx encodes exactly len bytes from src into dst.
152
 *  This is useful for encoding a block of data.
153
 */
154
int
155
strvis(char *dst, const char *src, int flag)
156
0
{
157
0
  char c;
158
0
  char *start;
159
160
0
  for (start = dst; (c = *src);)
161
0
    dst = vis(dst, c, flag, *++src);
162
0
  *dst = '\0';
163
0
  return (dst - start);
164
0
}
165
166
int
167
strnvis(char *dst, const char *src, size_t siz, int flag)
168
0
{
169
0
  char *start, *end;
170
0
  char tbuf[5];
171
0
  int c, i;
172
173
0
  i = 0;
174
0
  for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
175
0
    if (isvisible(c, flag)) {
176
0
      if ((c == '"' && (flag & VIS_DQ) != 0) ||
177
0
          (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
178
        /* need space for the extra '\\' */
179
0
        if (dst + 1 >= end) {
180
0
          i = 2;
181
0
          break;
182
0
        }
183
0
        *dst++ = '\\';
184
0
      }
185
0
      i = 1;
186
0
      *dst++ = c;
187
0
      src++;
188
0
    } else {
189
0
      i = vis(tbuf, c, flag, *++src) - tbuf;
190
0
      if (dst + i <= end) {
191
0
        memcpy(dst, tbuf, i);
192
0
        dst += i;
193
0
      } else {
194
0
        src--;
195
0
        break;
196
0
      }
197
0
    }
198
0
  }
199
0
  if (siz > 0)
200
0
    *dst = '\0';
201
0
  if (dst + i > end) {
202
    /* adjust return value for truncation */
203
0
    while ((c = *src))
204
0
      dst += vis(tbuf, c, flag, *++src) - tbuf;
205
0
  }
206
0
  return (dst - start);
207
0
}
208
209
int
210
stravis(char **outp, const char *src, int flag)
211
0
{
212
0
  char *buf;
213
0
  int len, serrno;
214
215
0
  buf = calloc(4, strlen(src) + 1);
216
0
  if (buf == NULL)
217
0
    return -1;
218
0
  len = strvis(buf, src, flag);
219
0
  serrno = errno;
220
0
  *outp = realloc(buf, len + 1);
221
0
  if (*outp == NULL) {
222
0
    *outp = buf;
223
0
    errno = serrno;
224
0
  }
225
0
  return (len);
226
0
}
227
228
int
229
strvisx(char *dst, const char *src, size_t len, int flag)
230
0
{
231
0
  char c;
232
0
  char *start;
233
234
0
  for (start = dst; len > 1; len--) {
235
0
    c = *src;
236
0
    dst = vis(dst, c, flag, *++src);
237
0
  }
238
0
  if (len)
239
0
    dst = vis(dst, *src, flag, '\0');
240
0
  *dst = '\0';
241
0
  return (dst - start);
242
0
}