-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRead File.c
More file actions
245 lines (223 loc) · 4.9 KB
/
Copy pathRead File.c
File metadata and controls
245 lines (223 loc) · 4.9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#define length 3
#define SX 16
#define SXO 17
typedef unsigned long long int GF2n[length];
typedef unsigned long long u64_int;
typedef struct{
GF2n*f;
int num;
}FUNCTION;
int getFlagPos(const char*str,char flag)
{
int len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]==flag)
return i;
return -1;
}
char *trimwhitespace(char *str)
{
char *end;
while(isspace(*str)) str++;
if(*str == 0)
return str;
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
*(end+1) = 0;
return str;
}
char** str_split(char* a_str,int &count)
{
char** result = 0;
char* tmp = a_str;
char* last_comma = 0;
while (*tmp)
{
if (',' == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
count += last_comma < (a_str + strlen(a_str) - 1);
count++;
result = (char**)malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, ",");
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, ",");
}
assert(idx == count - 1);
*(result + idx) = 0;
}
count--;
return result;
}
u64_int convertHexToDecLen8(const char*str)
{
int len=strlen(str);
u64_int a1=0,a2=0;
if(len<=4)
a1=strtol(str,NULL,16);
else
{
char tmp1[1000];memset(tmp1,0,1000);
strncpy(tmp1,str,len-4);
a2=strtol(tmp1,NULL,16);
a1=strtol(str+len-4,NULL,16);
}
u64_int tmp2=1;tmp2<<=16;
a1=a2*tmp2+a1;
return a1;
}
u64_int convertHexToDecLen16(const char*str)
{
int len=strlen(str);
u64_int a1=0,a2=0;
if(len<=8)
a1=atoll(str);
else
{
char tmp1[1000];memset(tmp1,0,1000);
strncpy(tmp1,str,len-8);
a2=convertHexToDecLen8(tmp1);
a1=convertHexToDecLen8(str+len-8);
}
u64_int tmp2=1;tmp2<<=32;
a1=a2*tmp2+a1;
return a1;
}
void getFromStrConvertHexToDec(const char*str,GF2n&value)
{
memset(value,0,sizeof(value));
int len=strlen(str);char strTmp[SXO];
if(len<=SX)
{
memset(strTmp,0,SXO);
strncpy(strTmp,str,len);
value[0]=convertHexToDecLen16(strTmp);
}
else if(len<=SX*2)
{
memset(strTmp,0,SXO);
strncpy(strTmp,str,len-SX);
value[1]=convertHexToDecLen16(strTmp);
memset(strTmp,0,SXO);
strncpy(strTmp,str+len-SX,SX);
value[0]=convertHexToDecLen16(strTmp);
}
else
{
memset(strTmp,0,SXO);
strncpy(strTmp,str,len-SX*2);
value[2]=convertHexToDecLen16(strTmp);
memset(strTmp,0,SXO);
strncpy(strTmp,str+len-SX*2,SX);
value[1]=convertHexToDecLen16(strTmp);
memset(strTmp,0,SXO);
strncpy(strTmp,str+len-SX,SX);
value[0]=convertHexToDecLen16(strTmp);
}
}
void getFromStr(const char*str,GF2n&value)
{
memset(value,0,sizeof(value));
int len=strlen(str);char strTmp[SXO];
value[0]=atoll(str);
}
void GFread(FILE *file,GF2n&m, FUNCTION&ff, GF2n&a,GF2n&b,GF2n&Px,GF2n&Py,GF2n&n,GF2n&Qx,GF2n&Qy)
{
if(!file)
{
printf("error file status\n");
return;
}
int i=0;
char*input=new char[1000];
char*left=new char[1000];
char*right=new char[1000];char*tmp=new char[1000];
size_t len = 1000;
while(fgets(input,1000,file))
{
input=trimwhitespace(input);
int pos=getFlagPos(input,'=');
memset(left,0,1000);
memset(right,0,1000);
strncpy(left,input,pos);
strncpy(right,input+pos+1,strlen(input)-pos-1);
left=trimwhitespace(left);
right=trimwhitespace(right);
if(!strcmp(left,"m"))
getFromStr(right,m);
else if(!strcmp(left,"f"))
{
memset(tmp,0,1000);
strncpy(tmp,right+1,strlen(right)-2);
int len=0;
char**result=str_split(tmp,len);
ff.f=(GF2n*)malloc(sizeof(GF2n)*len);ff.num=len;
for(int i=0;i<len;i++)
getFromStr(trimwhitespace(result[i]),ff.f[i]);
}
else if(!strcmp(left,"a"))
getFromStrConvertHexToDec(right,a);
else if(!strcmp(left,"b"))
getFromStrConvertHexToDec(right,b);
else if(!strcmp(left,"P_x"))
getFromStrConvertHexToDec(right,Px);
else if(!strcmp(left,"P_y"))
getFromStrConvertHexToDec(right,Py);
else if(!strcmp(left,"n"))
getFromStrConvertHexToDec(right,n);
else if(!strcmp(left,"Q_x"))
getFromStrConvertHexToDec(right,Qx);
else if(!strcmp(left,"Q_y"))
getFromStrConvertHexToDec(right,Qy);
}
}
void print(GF2n value)
{
printf("%llu,%llu,%llu\n",value[2],value[1],value[0]);
}
void printFunction(FUNCTION ff)
{
printf("Function:\n");
for(int i=0;i<ff.num;i++)
{
printf("\t");
print(ff.f[i]);
}
}
void printValue(GF2n value,const char*str)
{
printf("%s\n\t",str);
print(value);
}
int main()
{
FILE*file=fopen("input.txt","r");
if(!file)
{
printf("can't open file\n");
return 1;
}
GF2n m, a, b, Px, Py, n, Qx, Qy;
FUNCTION ff;
GFread(file,m,ff,a,b,Px,Py,n,Qx,Qy);
printValue(m,"m");printValue(a,"a");
printValue(b,"b");printValue(Px,"Px");printValue(Py,"Py");
printValue(n,"n");printValue(Qx,"Qx");printValue(Qy,"Qy");
printFunction(ff);
return 0;
}