-
Notifications
You must be signed in to change notification settings - Fork 0
/
textread.c
101 lines (81 loc) · 2.3 KB
/
textread.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
#include <stdio.h>
#include <string.h>
#include <gmp.h>
// Function reading plaintext
//
// Parameter:
// pt: plaintext
//
// Return:
// 0: success to read
// -1: fail to read
int readPlainText(mpz_t pt) {
char str[1024];
FILE *plaintext_file;
// Open plaintext file
plaintext_file = fopen("./text/plaintext", "rt");
if (plaintext_file == NULL) {
printf("There is no plaintext file.\n");
printf("The plaintext must be stored in ./text/plaintext.\n");
return -1;
}
fseek(plaintext_file, 0, SEEK_END);
if (ftell(plaintext_file) >= 1024) {
printf("The plaintext is too long.\n");
printf("The length of plaintext must be at most 1023.\n");
return -1;
}
fseek(plaintext_file, 0, SEEK_SET);
// Read plaintext
fgets(str, 1024, plaintext_file);
if (mpz_init_set_str(pt, str, 10) == -1) {
printf("The plaintext is invalid.\n");
return -1;
}
if (mpz_sgn(pt) <= 0) {
printf("The plaintext must be positive.\n");
return -1;
}
// Close plaintext file
fclose(plaintext_file);
return 0;
}
// Function reading ciphertext
//
// Parameter:
// ct: ciphertext
//
// Return:
// 0: success to read
// -1: fail to read
int readCipherText(mpz_t ct) {
char str[1024];
FILE *ciphertext_file;
// Open ciphertext file
ciphertext_file = fopen("./text/ciphertext", "rt");
if (ciphertext_file == NULL) {
printf("There is no ciphertext file.\n");
printf("The ciphertext must be stored in ./text/ciphertext.\n");
return -1;
}
fseek(ciphertext_file, 0, SEEK_END);
if (ftell(ciphertext_file) >= 1024) {
printf("The ciphertext is too long.\n");
printf("The length of ciphertext must be at most 1023.\n");
return -1;
}
fseek(ciphertext_file, 0, SEEK_SET);
// Read ciphertext
fgets(str, 1024, ciphertext_file);
if (mpz_init_set_str(ct, str, 10) == -1) {
printf("The ciphertext is invalid.\n");
return -1;
}
if (mpz_sgn(ct) <= 0) {
printf("The ciphertext must be positive.\n");
return -1;
}
// Close ciphertext file
fclose(ciphertext_file);
return 0;
}