1 /* $NetBSD: utf8.c,v 1.6 2017/10/07 19:39:19 christos Exp $ */ 2 /* $OpenBSD: utf8.c,v 1.7 2017/05/31 09:15:42 deraadt Exp $ */ 3 /* 4 * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include "includes.h" 20 __RCSID("$NetBSD: utf8.c,v 1.6 2017/10/07 19:39:19 christos Exp $"); 21 /* 22 * Utility functions for multibyte-character handling, 23 * in particular to sanitize untrusted strings for terminal output. 24 */ 25 26 #include <sys/types.h> 27 #include <langinfo.h> 28 #include <limits.h> 29 #include <stdarg.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <vis.h> 34 #include <wchar.h> 35 36 #include "utf8.h" 37 38 static int dangerous_locale(void); 39 static int grow_dst(char **, size_t *, size_t, char **, size_t); 40 static int vasnmprintf(char **, size_t, int *, const char *, va_list) 41 __attribute__((format(printf, 4, 0))); 42 43 44 /* 45 * For US-ASCII and UTF-8 encodings, we can safely recover from 46 * encoding errors and from non-printable characters. For any 47 * other encodings, err to the side of caution and abort parsing: 48 * For state-dependent encodings, recovery is impossible. 49 * For arbitrary encodings, replacement of non-printable 50 * characters would be non-trivial and too fragile. 51 */ 52 53 static int 54 dangerous_locale(void) { 55 char *loc; 56 57 loc = nl_langinfo(CODESET); 58 return strcmp(loc, "US-ASCII") != 0 && strcmp(loc, "UTF-8") != 0 && 59 strcmp(loc, "ANSI_X3.4-1968") != 0 && strcmp(loc, "646") != 0 && 60 strcmp(loc, "") != 0; 61 } 62 63 static int 64 grow_dst(char **dst, size_t *sz, size_t maxsz, char **dp, size_t need) 65 { 66 char *tp; 67 size_t tsz; 68 69 if (*dp + need < *dst + *sz) 70 return 0; 71 tsz = *sz + 128; 72 if (tsz > maxsz) 73 tsz = maxsz; 74 if ((tp = recallocarray(*dst, *sz, tsz, 1)) == NULL) 75 return -1; 76 *dp = tp + (*dp - *dst); 77 *dst = tp; 78 *sz = tsz; 79 return 0; 80 } 81 82 /* 83 * The following two functions limit the number of bytes written, 84 * including the terminating '\0', to sz. Unless wp is NULL, 85 * they limit the number of display columns occupied to *wp. 86 * Whichever is reached first terminates the output string. 87 * To stay close to the standard interfaces, they return the number of 88 * non-NUL bytes that would have been written if both were unlimited. 89 * If wp is NULL, newline, carriage return, and tab are allowed; 90 * otherwise, the actual number of columns occupied by what was 91 * written is returned in *wp. 92 */ 93 94 static int 95 vasnmprintf(char **str, size_t maxsz, int *wp, const char *fmt, va_list ap) 96 { 97 char *src; /* Source string returned from vasprintf. */ 98 char *sp; /* Pointer into src. */ 99 char *dst; /* Destination string to be returned. */ 100 char *dp; /* Pointer into dst. */ 101 char *tp; /* Temporary pointer for dst. */ 102 size_t sz; /* Number of bytes allocated for dst. */ 103 wchar_t wc; /* Wide character at sp. */ 104 int len; /* Number of bytes in the character at sp. */ 105 int ret; /* Number of bytes needed to format src. */ 106 int width; /* Display width of the character wc. */ 107 int total_width, max_width, print; 108 109 src = NULL; 110 if ((ret = vasprintf(&src, fmt, ap)) <= 0) 111 goto fail; 112 113 sz = strlen(src) + 1; 114 if ((dst = malloc(sz)) == NULL) { 115 free(src); 116 ret = -1; 117 goto fail; 118 } 119 120 if (maxsz > INT_MAX) 121 maxsz = INT_MAX; 122 123 sp = src; 124 dp = dst; 125 ret = 0; 126 print = 1; 127 total_width = 0; 128 max_width = wp == NULL ? INT_MAX : *wp; 129 while (*sp != '\0') { 130 if ((len = mbtowc(&wc, sp, MB_CUR_MAX)) == -1) { 131 (void)mbtowc(NULL, NULL, MB_CUR_MAX); 132 if (dangerous_locale()) { 133 ret = -1; 134 break; 135 } 136 len = 1; 137 width = -1; 138 } else if (wp == NULL && 139 (wc == L'\n' || wc == L'\r' || wc == L'\t')) { 140 /* 141 * Don't use width uninitialized; the actual 142 * value doesn't matter because total_width 143 * is only returned for wp != NULL. 144 */ 145 width = 0; 146 } else if ((width = wcwidth(wc)) == -1 && 147 dangerous_locale()) { 148 ret = -1; 149 break; 150 } 151 152 /* Valid, printable character. */ 153 154 if (width >= 0) { 155 if (print && (dp - dst >= (int)maxsz - len || 156 total_width > max_width - width)) 157 print = 0; 158 if (print) { 159 if (grow_dst(&dst, &sz, maxsz, 160 &dp, len) == -1) { 161 ret = -1; 162 break; 163 } 164 total_width += width; 165 memcpy(dp, sp, len); 166 dp += len; 167 } 168 sp += len; 169 if (ret >= 0) 170 ret += len; 171 continue; 172 } 173 174 /* Escaping required. */ 175 176 while (len > 0) { 177 if (print && (dp - dst >= (int)maxsz - 4 || 178 total_width > max_width - 4)) 179 print = 0; 180 if (print) { 181 if (grow_dst(&dst, &sz, maxsz, 182 &dp, 4) == -1) { 183 ret = -1; 184 break; 185 } 186 #ifndef VIS_ALL 187 #define VIS_ALL VIS_WHITE /* XXX */ 188 #endif 189 tp = vis(dp, *sp, VIS_OCTAL | VIS_ALL, 0); 190 width = tp - dp; 191 total_width += width; 192 dp = tp; 193 } else 194 width = 4; 195 len--; 196 sp++; 197 if (ret >= 0) 198 ret += width; 199 } 200 if (len > 0) 201 break; 202 } 203 free(src); 204 *dp = '\0'; 205 *str = dst; 206 if (wp != NULL) 207 *wp = total_width; 208 209 /* 210 * If the string was truncated by the width limit but 211 * would have fit into the size limit, the only sane way 212 * to report the problem is using the return value, such 213 * that the usual idiom "if (ret < 0 || ret >= sz) error" 214 * works as expected. 215 */ 216 217 if (ret < (int)maxsz && !print) 218 ret = -1; 219 return ret; 220 221 fail: 222 if (wp != NULL) 223 *wp = 0; 224 if (ret == 0) { 225 *str = src; 226 return 0; 227 } else { 228 *str = NULL; 229 return -1; 230 } 231 } 232 233 int 234 snmprintf(char *str, size_t sz, int *wp, const char *fmt, ...) 235 { 236 va_list ap; 237 char *cp; 238 int ret; 239 240 va_start(ap, fmt); 241 ret = vasnmprintf(&cp, sz, wp, fmt, ap); 242 va_end(ap); 243 if (cp != NULL) { 244 (void)strlcpy(str, cp, sz); 245 free(cp); 246 } else 247 *str = '\0'; 248 return ret; 249 } 250 251 /* 252 * To stay close to the standard interfaces, the following functions 253 * return the number of non-NUL bytes written. 254 */ 255 256 int 257 vfmprintf(FILE *stream, const char *fmt, va_list ap) 258 { 259 char *str; 260 int ret; 261 262 if ((ret = vasnmprintf(&str, INT_MAX, NULL, fmt, ap)) < 0) 263 return -1; 264 if (fputs(str, stream) == EOF) 265 ret = -1; 266 free(str); 267 return ret; 268 } 269 270 int 271 fmprintf(FILE *stream, const char *fmt, ...) 272 { 273 va_list ap; 274 int ret; 275 276 va_start(ap, fmt); 277 ret = vfmprintf(stream, fmt, ap); 278 va_end(ap); 279 return ret; 280 } 281 282 int 283 mprintf(const char *fmt, ...) 284 { 285 va_list ap; 286 int ret; 287 288 va_start(ap, fmt); 289 ret = vfmprintf(stdout, fmt, ap); 290 va_end(ap); 291 return ret; 292 } 293