/src/sleuthkit/tsk/base/tsk_printf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * The Sleuth Kit |
3 | | * |
4 | | * Brian Carrier [carrier <at> sleuthkit [dot] org] |
5 | | * Copyright (c) 2007-2011 Brian Carrier. All Rights reserved |
6 | | * |
7 | | * This software is distributed under the Common Public License 1.0 |
8 | | */ |
9 | | |
10 | | /** \file tsk_printf.c |
11 | | * These are printf wrappers that are needed so that we can |
12 | | * easily print in both Unix and Windows. For Unix, the |
13 | | * internal UTF-8 representation is kept and a normal printf |
14 | | * is performed. For Windows, the UTF-8 representation is first |
15 | | * converted to UTF-16 and then printed |
16 | | */ |
17 | | |
18 | | #include "tsk_base_i.h" |
19 | | #include <stdarg.h> |
20 | | |
21 | | |
22 | | /** \internal |
23 | | * Convert the UTF-8 printf arguments to UTF-16 and fill in the |
24 | | * printf types (%s, %d, etc.) |
25 | | * |
26 | | * @param wbuf wide char string to write result to |
27 | | * @param wlen number of characters in wbuf |
28 | | * @param msg printf message string |
29 | | * @param args Arguments to use when filling in message string |
30 | | * @returns 1 on error and 0 on success |
31 | | */ |
32 | | #ifdef TSK_WIN32 |
33 | | static int |
34 | | tsk_printf_conv(WCHAR * wbuf, int wlen, const char *msg, va_list * args) |
35 | | { |
36 | | char *cbuf; |
37 | | UTF8 *ptr8; |
38 | | UTF16 *ptr16; |
39 | | int retVal; |
40 | | size_t len, clen; |
41 | | |
42 | | wbuf[0] = '\0'; |
43 | | |
44 | | /* Allocate a UTF-8 buffer and process the printf args */ |
45 | | clen = wlen * 3; |
46 | | if (NULL == (cbuf = (char *) tsk_malloc(clen))) { |
47 | | return 1; |
48 | | } |
49 | | |
50 | | #ifdef _MSC_VER |
51 | | vsnprintf_s(cbuf, clen - 1, _TRUNCATE, msg, *args); |
52 | | #else |
53 | | vsnprintf(cbuf, clen - 1, msg, *args); |
54 | | #endif |
55 | | len = strlen(cbuf); |
56 | | |
57 | | //Convert to UTF-16 |
58 | | ptr8 = (UTF8 *) cbuf; |
59 | | ptr16 = (UTF16 *) wbuf; |
60 | | retVal = |
61 | | tsk_UTF8toUTF16((const UTF8 **) &ptr8, &ptr8[len + 1], &ptr16, |
62 | | &ptr16[wlen], TSKlenientConversion); |
63 | | if (retVal != TSKconversionOK) { |
64 | | *ptr16 = '\0'; |
65 | | if (tsk_verbose) |
66 | | tsk_fprintf(stderr, |
67 | | "tsk_printf_conv: error converting string to UTF-16\n"); |
68 | | } |
69 | | |
70 | | free(cbuf); |
71 | | return 0; |
72 | | } |
73 | | #endif |
74 | | |
75 | | /** |
76 | | * \ingroup baselib |
77 | | * fprintf wrapper function that takes UTF-8 strings as input |
78 | | * (on all platforms) and does what is necessary to output |
79 | | * strings in the correct encoding (UTF-8 on Unix and |
80 | | * UTF-16 on Windows). |
81 | | * |
82 | | * @param fd File to print to |
83 | | * @param msg printf message |
84 | | */ |
85 | | void |
86 | | tsk_fprintf(FILE * fd, const char *msg, ...) |
87 | 0 | { |
88 | 0 | va_list args; |
89 | 0 | va_start(args, msg); |
90 | |
|
91 | | #ifdef TSK_WIN32 |
92 | | { |
93 | | WCHAR wbuf[2048]; |
94 | | tsk_printf_conv(wbuf, 2048, msg, &args); |
95 | | fwprintf(fd, _TSK_T("%ls"), wbuf); |
96 | | } |
97 | | #else |
98 | 0 | vfprintf(fd, msg, args); |
99 | 0 | #endif |
100 | 0 | va_end(args); |
101 | 0 | } |
102 | | |
103 | | /** |
104 | | * \ingroup baselib |
105 | | * printf wrapper function that takes UTF-8 strings as input |
106 | | * (on all platforms) and does what is necessary to output |
107 | | * strings in the correct encoding (UTF-8 on Unix and |
108 | | * UTF-16 on Windows). |
109 | | * |
110 | | * @param msg printf message |
111 | | */ |
112 | | void |
113 | | tsk_printf(const char *msg, ...) |
114 | 0 | { |
115 | 0 | va_list args; |
116 | 0 | va_start(args, msg); |
117 | |
|
118 | | #ifdef TSK_WIN32 |
119 | | { |
120 | | WCHAR wbuf[2048]; |
121 | | tsk_printf_conv(wbuf, 2048, msg, &args); |
122 | | wprintf(_TSK_T("%ls"), wbuf); |
123 | | } |
124 | | #else |
125 | 0 | vprintf(msg, args); |
126 | 0 | #endif |
127 | 0 | va_end(args); |
128 | 0 | } |
129 | | |
130 | | /** |
131 | | * \ingroup baselib |
132 | | * Prints the string removing control characters. |
133 | | * |
134 | | * @param fd File to print to |
135 | | * @param str string to be printed |
136 | | * |
137 | | * @returns 0 on success and 1 on error |
138 | | */ |
139 | | int |
140 | | tsk_print_sanitized(FILE * fd, const char *str) |
141 | 0 | { |
142 | 0 | size_t index = 0; |
143 | 0 | char *buf = NULL; |
144 | |
|
145 | 0 | buf = tsk_malloc(strlen(str) + 1); |
146 | 0 | if (buf == NULL) |
147 | 0 | return 1; |
148 | | |
149 | 0 | strcpy(buf, str); |
150 | |
|
151 | 0 | for (index = 0; index < strlen(buf); index++) |
152 | 0 | if (TSK_IS_CNTRL(buf[index])) |
153 | 0 | buf[index] = '^'; |
154 | |
|
155 | 0 | tsk_fprintf(fd, "%s", buf); |
156 | |
|
157 | 0 | free(buf); |
158 | |
|
159 | 0 | return 0; |
160 | 0 | } |