-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
291 lines (247 loc) · 5.5 KB
/
main.cpp
File metadata and controls
291 lines (247 loc) · 5.5 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
//
// file : main.cpp
// desc. : command line parser for anfweight
//
// author : Cagdas Calik, ccalik@metu.edu.tr
// date : 14.03.2012
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "anfweight.h"
bool IsDigit(char ch)
{
if(ch >= '0' && ch <= '9')
return true;
else
return false;
}
const char* SkipWhitespace(const char *str)
{
const char *ret = str;
while(*ret == ' ' || *ret == '\t' || *ret == '\r' || *ret == '\n')
ret++;
if(*ret == '\0')
ret = NULL;
return ret;
}
int Str2ANFTermList(const char *szANF, ANFTERMLIST &monomials, int &N)
{
ANFTERM term;
char szIndex[16];
const char *str, *end;
bool bInTerm = false;
int i, index;
N = 0;
ANFTerm_Clear(term);
str = SkipWhitespace(szANF);
while(str != NULL)
{
if(*str == '+' || *str == '^')
{
if(bInTerm)
{
monomials.push_back(term);
ANFTerm_Clear(term);
bInTerm = false;
}
str++;
}
else if(*str == '1')
{
// constant term
monomials.push_back(term);
str++;
}
else if(*str == 'x')
{
if(!bInTerm)
{
bInTerm = true;
ANFTerm_Clear(term);
}
end = str + 1;
i = 0;
while(IsDigit(*end))
szIndex[i++] = *end++;
str = end;
szIndex[i] = 0;
index = atoi(szIndex);
if(index > N)
N = index;
ANFSETBIT(term, index - 1);
}
else
{
// skip '.'
str++;
}
str = SkipWhitespace(str);
}
if(bInTerm)
monomials.push_back(term);
return 0;
}
void PrintUsage()
{
printf("anf2weight v1.1 (March 2012) - Cagdas Calik\n");
printf("\n");
printf("\n");
printf("Usage: anf2weight [-n N] [-f filename] | [anf] [-p] ");
printf("\n");
printf(" -n N : declare that the boolean function is of N variables\n");
printf(" -f filename : read ANF from the file\n");
printf(" -p : show progress\n");
printf("\n");
printf("Example1: anf2weight x1.x2+x3.x4\n");
printf("Example2: anf2weight -n 8 x1.x2+x3.x4\n");
printf("Example3: anf2weight -f anf.txt\n");
printf("Example4: anf2weight -n 64 -f anf.txt -p\n");
printf("\n");
}
int ParseCmdLine(int argc, char* argv[])
{
int i;
int N = -1; // number of variables declared by user
int n; // number of variabes appearing in the anf
bool bParseError = false;// indicates a parsing error
bool bProgress = false; // show progress
bool bFromFile = false; // read function anf from file
char szFileName[260]; // input file
char *pFileBuffer = NULL;// file contents
long lFileLength; // length of the input file
int nANFArgIndex = -1; // Argument index of anf if it is given from cmdline
ANFTERMLIST monomials; // function's and
FILE *fp = NULL;
LINT weight; // Result
for(i = 1; i < argc; i++)
{
// number of variables
if(strcmp(argv[i], "-n") == 0)
{
if((i + 1) < argc)
{
N = atoi(argv[i + 1]);
if(N > 128)
{
bParseError = true;
printf("error: declared number of input variables cannot exceed 128.\n");
}
i++;
}
else
{
bParseError = true;
printf("missing parameter after %s\n", argv[i]);
}
}
else if(strcmp(argv[i], "-p") == 0)
{
bProgress = true;
}
else if(strcmp(argv[i], "-f") == 0)
{
if((i + 1) < argc)
{
strcpy(szFileName, argv[i + 1]);
bFromFile = true;
i++;
}
else
{
bParseError = true;
printf("missing parameter after %s\n", argv[i]);
}
}
else
{
if(nANFArgIndex == -1)
nANFArgIndex = i;
else
{
// multiple arguments for anf
printf("multiple anf definition in arg. %d, remove blank spaces from the anf.\n", i + 1);
bParseError = true;
}
}
}
if(bFromFile)
{
fp = fopen(szFileName, "rb");
if(fp == NULL)
{
printf("cannot open file: %s\n", szFileName);
bParseError = true;
}
else
{
// compute file length
fseek(fp, 0, SEEK_END);
lFileLength = ftell(fp);
fseek(fp, 0, SEEK_SET);
pFileBuffer = new char[lFileLength];
if(pFileBuffer == NULL)
{
printf("cannot allocate memory for file : %s\n", szFileName);
bParseError = true;
}
else
{
// v1.1
fread(pFileBuffer, 1, lFileLength, fp);
// parse anf from the file
if(Str2ANFTermList(pFileBuffer, monomials, n) != 0)
bParseError = true;
}
}
}
else
{
// parse anf from command line
if(Str2ANFTermList(argv[nANFArgIndex], monomials, n) != 0)
bParseError = true;
}
if(fp)
fclose(fp);
if(pFileBuffer)
delete [] pFileBuffer;
if(!bParseError)
{
// Check maximum number of variables supported
if(n > 128)
{
printf("error: maximum supported number of variables is 128.\n");
}
else if((N != -1) && (N < n))
{
// Check if number of variables in the anf complies with users declared number of variables
printf("error: declared number of variables is less than the one in the ANF.\n");
}
else
{
if(N != -1)
n = N;
// Compute the weight
weight = ANFWeight_Compute(monomials, bProgress ? 1 : 2, NULL);
// Normalize the weight if number of variables is less than 128
// since the weight is computed as if the function contains 128 variables
if(n < ANFWEIGHT_N)
{
LINT_Shr(&weight, ANFWEIGHT_N - n);
}
LINT_Print(&weight);
}
}
return 0;
}
int main(int argc, char* argv[])
{
if(argc == 1)
{
PrintUsage();
return -1;
}
ParseCmdLine(argc, argv);
return 0;
}