• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "tests.h"
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 /*
8  * Based on string_quote() from util.c.
9  * Assumes instr is NUL-terminated.
10  */
11 
12 void
print_quoted_string_ex(const char * instr,bool quote,const char * escape_chars)13 print_quoted_string_ex(const char *instr, bool quote, const char *escape_chars)
14 {
15 	print_quoted_memory_ex(instr, strlen(instr), quote, escape_chars);
16 }
17 
18 void
print_quoted_string(const char * instr)19 print_quoted_string(const char *instr)
20 {
21 	print_quoted_memory(instr, strlen(instr));
22 }
23 
24 void
print_quoted_cstring(const char * instr,const size_t size)25 print_quoted_cstring(const char *instr, const size_t size)
26 {
27 	const size_t len = strnlen(instr, size);
28 	if (len < size) {
29 		print_quoted_memory(instr, len);
30 	} else {
31 		print_quoted_memory(instr, size - 1);
32 		printf("...");
33 	}
34 }
35 
36 void
print_quoted_stringn(const char * instr,const size_t size)37 print_quoted_stringn(const char *instr, const size_t size)
38 {
39 	const size_t len = strnlen(instr, size);
40 	if (len < size) {
41 		print_quoted_memory(instr, len);
42 	} else {
43 		print_quoted_memory(instr, size);
44 		printf("...");
45 	}
46 }
47 
48 static void
print_octal(unsigned char c,char next)49 print_octal(unsigned char c, char next)
50 {
51 	putchar('\\');
52 
53 	char c1 = '0' + (c & 0x7);
54 	char c2 = '0' + ((c >> 3) & 0x7);
55 	char c3 = '0' + (c >> 6);
56 
57 	if (next >= '0' && next <= '7') {
58 		/* Print \octal */
59 		putchar(c3);
60 		putchar(c2);
61 	} else {
62 		/* Print \[[o]o]o */
63 		if (c3 != '0')
64 			putchar(c3);
65 		if (c3 != '0' || c2 != '0')
66 			putchar(c2);
67 	}
68 	putchar(c1);
69 }
70 
71 void
print_quoted_memory_ex(const void * const instr,const size_t len,bool quote,const char * escape_chars)72 print_quoted_memory_ex(const void *const instr, const size_t len,
73 		       bool quote, const char *escape_chars)
74 {
75 	const unsigned char *str = (const unsigned char *) instr;
76 	size_t i;
77 
78 	if (quote)
79 		putchar('"');
80 
81 	for (i = 0; i < len; ++i) {
82 		const int c = str[i];
83 		switch (c) {
84 			case '\"':
85 				printf("\\\"");
86 				break;
87 			case '\\':
88 				printf("\\\\");
89 				break;
90 			case '\f':
91 				printf("\\f");
92 				break;
93 			case '\n':
94 				printf("\\n");
95 				break;
96 			case '\r':
97 				printf("\\r");
98 				break;
99 			case '\t':
100 				printf("\\t");
101 				break;
102 			case '\v':
103 				printf("\\v");
104 				break;
105 			default:
106 				if (c >= ' ' && c <= 0x7e &&
107 				    !(escape_chars && strchr(escape_chars, c))) {
108 					putchar(c);
109 				} else {
110 					print_octal(c,
111 						i < (len - 1) ? str[i + 1] : 0);
112 				}
113 
114 				break;
115 		}
116 	}
117 
118 	if (quote)
119 		putchar('"');
120 }
121 
122 void
print_quoted_memory(const void * const instr,const size_t len)123 print_quoted_memory(const void *const instr, const size_t len)
124 {
125 	print_quoted_memory_ex(instr, len, true, NULL);
126 }
127 
128 void
print_quoted_hex(const void * const instr,const size_t len)129 print_quoted_hex(const void *const instr, const size_t len)
130 {
131 	const unsigned char *str = instr;
132 	size_t i;
133 
134 	printf("\"");
135 	for (i = 0; i < len; i++)
136 		printf("\\x%02x", str[i]);
137 	printf("\"");
138 }
139