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
|
/* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#ifdef Linux
#include <./linux/limits.h>
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
/* general purpose error flag, gets mapped at a higher layer */
#define ERROR_CODE 1
/* Email response types */
/* 0 is success, send back email body and attachment */
/* do not send back any email */
#define RESPONSE_NO_EMAIL 1
/* send back email body, no attachment */
#define RESPONSE_BODY_ONLY 2
#if 0
/* fatal framework error, exit. Don't want to keep looping because it will just fill up the log and
discard email. */
#define ERROR_FATAL 3
#endif
/* send email body to framework admin, no attachment */
#define RESPONSE_BODY_TO_ADMIN 4
/* file handling */
/* 1024 should accomodate lines with a 2048-bit key, 256 bytes, 512 characters */
#define MAX_LINE_SIZE 1024
int File_Copy(const char *destinationFilename,
const char *sourceFilename);
int File_Open(FILE **file,
const char *filename,
const char *mode);
int File_OpenMessageFile(const char *outputBodyFilename,
const char* mode);
int File_CloseMessageFile(void);
int File_ReadLine(int *haveLine,
char *line,
size_t *lineLength,
size_t lineSize,
FILE *file);
int File_ReadBinaryFile(unsigned char **data,
size_t *length,
size_t length_max,
const char *filename);
int File_ReadTextFile(char **text,
size_t *length,
size_t length_max,
const char *filename);
int File_GetSize(size_t *fileLength,
const char *filename);
int File_ValidateOpen(const char *filename,
const char *mode);
int File_WriteBinaryFile(const unsigned char *data,
size_t length,
const char *filename);
int File_WriteBinaryFileVa(const char *filename, ...);
void File_LogTime(FILE *logFile);
void File_Printf(FILE *lFile,
FILE *mFile,
const char *format,
...);
/* configuration file parsing */
int File_GetNameValue(int *haveLine,
char **name,
char **value,
char *lineBuffer,
size_t lineBufferLength,
FILE *file);
int File_MapNameToValue(char **value,
const char *name,
char *lineBuffer,
size_t lineBufferLength,
FILE *file);
int File_MapNameToBool(int *booln,
const char *name,
char *lineBuffer,
size_t lineBufferLength,
FILE *file);
int File_MapNameToUint(unsigned int *value,
const char *name,
char *lineBuffer,
size_t lineBufferLength,
FILE *file);
int File_GetNameValueArray(char ***names,
char ***values,
size_t *length,
char *lineBuffer,
size_t lineBufferLength,
FILE *file);
int File_GetValueArray(char ***values,
size_t *length,
const char *inName,
char *lineBuffer,
size_t lineBufferLength,
FILE *file);
/* argv handling */
/* Number of arguments (not the number of bytes). Since typical signers take ~10 arguments, this
should be sufficient. Note that arguments with a value count as two arguments. */
#define MAX_ARGV_BODY 200
#ifdef Windows
#define ARG_MAX 4000
#endif
typedef struct tdArguments {
char *argv[MAX_ARGV_BODY];
int argc;
size_t argvBytes;
} Arguments;
void Arguments_Init(Arguments *arguments);
void Arguments_Delete(Arguments *arguments);
void Arguments_ClearSecret(const char *flag,
Arguments *arguments);
int Arguments_AddPairTo(Arguments *arguments,
const char *flag,
const char *value);
int Arguments_AddTo(Arguments *arguments,
const char *data,
int zero);
int Arguments_GetFrom(const char **value,
const char *flag,
Arguments *arguments);
/* character array handling */
int Array_GetLine(int *haveLine,
char *outLine,
const char **nextLine,
size_t lineSize,
const char *inLines,
FILE *logFile);
/* memory allocation */
int Malloc_Safe(unsigned char **ptr, size_t len, size_t max_length);
int Malloc_Strcpy(char **out,
const char *in);
int Realloc_Safe(unsigned char **buffer,
size_t size);
/*
Format Conversion
*/
void Format_ToHexascii(char *string,
unsigned char *binary,
size_t length);
int Format_FromHexascii(unsigned char *binary,
const char *string,
size_t length);
int Format_ByteFromHexascii(unsigned char *byte,
const char *string);
#endif
|