-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
219 lines (187 loc) · 5.46 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <inttypes.h> // for PRId64
#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#include "std_testcase.h"
#ifndef _WIN32
#include <wchar.h>
#endif
void printLine (const char * line)
{
printf("entry13\n");
if(line != NULL)
{
printf("%s\n", line);
}
printf("entry14\n");
}
void printSinkLine (const char * line)
{
if(line != NULL)
{
printf("%s\n", line);
}
}
void printWLine (const wchar_t * line)
{
if(line != NULL)
{
wprintf(L"%ls\n", line);
}
}
void printIntLine (int intNumber)
{
printf("%d\n", intNumber);
}
void printShortLine (short shortNumber)
{
printf("%hd\n", shortNumber);
}
void printFloatLine (float floatNumber)
{
printf("%f\n", floatNumber);
}
void printLongLine (long longNumber)
{
printf("%ld\n", longNumber);
}
void printLongLongLine (int64_t longLongIntNumber)
{
printf("%" PRId64 "\n", longLongIntNumber);
}
void printSizeTLine (size_t sizeTNumber)
{
printf("%zu\n", sizeTNumber);
}
void printHexCharLine (char charHex)
{
printf("%02x\n", charHex);
}
void printWcharLine(wchar_t wideChar)
{
/* ISO standard dictates wchar_t can be ref'd only with %ls, so we must make a
* string to print a wchar */
wchar_t s[2];
s[0] = wideChar;
s[1] = L'\0';
printf("%ls\n", s);
}
void printUnsignedLine(unsigned unsignedNumber)
{
printf("%u\n", unsignedNumber);
}
void printHexUnsignedCharLine(unsigned char unsignedCharacter)
{
printf("%02x\n", unsignedCharacter);
}
void printDoubleLine(double doubleNumber)
{
printf("%g\n", doubleNumber);
}
void printStructLine (const twoIntsStruct * structTwoIntsStruct)
{
printf("%d -- %d\n", structTwoIntsStruct->intOne, structTwoIntsStruct->intTwo);
}
void printBytesLine(const unsigned char * bytes, size_t numBytes)
{
size_t i;
for (i = 0; i < numBytes; ++i)
{
printf("%02x", bytes[i]);
}
puts(""); /* output newline */
}
/* Decode a string of hex characters into the bytes they represent. The second
* parameter specifies the length of the output buffer. The number of bytes
* actually written to the output buffer is returned. */
size_t decodeHexChars(unsigned char * bytes, size_t numBytes, const char * hex)
{
size_t numWritten = 0;
/* We can't sscanf directly into the byte array since %02x expects a pointer to int,
* not a pointer to unsigned char. Also, since we expect an unbroken string of hex
* characters, we check for that before calling sscanf; otherwise we would get a
* framing error if there's whitespace in the input string. */
while (numWritten < numBytes && isxdigit(hex[2 * numWritten]) && isxdigit(hex[2 * numWritten + 1]))
{
int byte;
sscanf(&hex[2 * numWritten], "%02x", &byte);
bytes[numWritten] = (unsigned char) byte;
++numWritten;
}
return numWritten;
}
/* Decode a string of hex characters into the bytes they represent. The second
* parameter specifies the length of the output buffer. The number of bytes
* actually written to the output buffer is returned. */
size_t decodeHexWChars(unsigned char * bytes, size_t numBytes, const wchar_t * hex)
{
size_t numWritten = 0;
/* We can't swscanf directly into the byte array since %02x expects a pointer to int,
* not a pointer to unsigned char. Also, since we expect an unbroken string of hex
* characters, we check for that before calling swscanf; otherwise we would get a
* framing error if there's whitespace in the input string. */
while (numWritten < numBytes && iswxdigit(hex[2 * numWritten]) && iswxdigit(hex[2 * numWritten + 1]))
{
int byte;
swscanf(&hex[2 * numWritten], L"%02x", &byte);
bytes[numWritten] = (unsigned char) byte;
++numWritten;
}
return numWritten;
}
/* The two functions always return 1 or 0, so a tool should be able to
identify that uses of these functions will always return these values */
int globalReturnsTrue()
{
return 1;
}
int globalReturnsFalse()
{
return 0;
}
int globalReturnsTrueOrFalse()
{
return (rand() % 2);
}
/* The variables below are declared "const", so a tool should
be able to identify that reads of these will always return their
initialized values. */
const int GLOBAL_CONST_TRUE = 1; /* true */
const int GLOBAL_CONST_FALSE = 0; /* false */
const int GLOBAL_CONST_FIVE = 5;
/* The variables below are not defined as "const", but are never
assigned any other value, so a tool should be able to identify that
reads of these will always return their initialized values. */
int globalTrue = 1; /* true */
int globalFalse = 0; /* false */
int globalFive = 5;
/* define a bunch of these as empty functions so that if a test case forgets
to make their's statically scoped, we'll get a linker error */
void good1() { }
void good2() { }
void good3() { }
void good4() { }
void good5() { }
void good6() { }
void good7() { }
void good8() { }
void good9() { }
/* shouldn't be used, but just in case */
void bad1() { }
void bad2() { }
void bad3() { }
void bad4() { }
void bad5() { }
void bad6() { }
void bad7() { }
void bad8() { }
void bad9() { }
/* define global argc and argv */
#ifdef __cplusplus
extern "C" {
#endif
int globalArgc = 0;
char** globalArgv = NULL;
#ifdef __cplusplus
}
#endif