-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decoder.cpp
144 lines (135 loc) · 4.27 KB
/
Decoder.cpp
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
/*------------------------------------------------------
*
* Decoder.cpp
*
* Project : Base64_Decoder
* Name : Chong Guo
* Student ID : 301295753
* Language : C++
* SFU username : armourg
*
* Created by Armour on 25/10/2015
* Copyright (c) 2015 Armour. All rights reserved.
*
*------------------------------------------------------
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
FILE *fin, *fout;
char *file_in, *file_out; /* The file name of input and output file */
/*
* Function: Base64_table
* -------------------
* This function is used to map character to integer
* using base64 encode table.
*
* Parameters:
* c: the character that need to map
*
* Returns:
* the integer that mapped from c
*/
int base64_table(char c) {
if (c >= 'A' && c <= 'Z') /* 'A'-'Z' */
return (int)(c - 'A');
else if (c >= 'a' && c <= 'z') { /* 'a'-'z' */
return (int)(c - 'a' + 26);
} else if (c >= '0' && c <= '9') { /* '0'-'9' */
return (char)(c - '0' + 52);
} else if (c == '+') /* '+' */
return 62;
else /* '/' */
return 63;
}
/*
* Function: Get_output_file
* -------------------
* This function is used to get the name of the output file
*
* Parameters:
* input: the name of the input file
*
* Returns:
* the name of the output file
*/
char *get_output_file(char *input) {
char *output;
output = (char *)malloc(sizeof(char) * 1024);
strcpy(output, input);
char *end = output + strlen(output) - 1;
while (end > output) {
if (*end == '.') { /* If found extension name */
*(end + 1) = 'o';
*(end + 2) = 'u';
*(end + 3) = 't';
*(end + 4) = '\0';
return output; /* Change extension name to .out and return it */
}
end--;
}
end = output + strlen(output) - 1; /* If not found extension name */
*(end + 1) = '.';
*(end + 2) = 'o';
*(end + 3) = 'u';
*(end + 4) = 't';
*(end + 5) = '\0'; /* Just add .out extension at the end of file name */
return output;
}
/*
* Function: Main
* -------------------
* The main function
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
int main() {
int count = 0; /* Used to decide when to decode */
int bin = 0; /* Used to store the binary number */
char c;
file_in = (char *)malloc(sizeof(char) * 1024);
cout << "Please input a file name:" << endl;
scanf("%s", file_in); /* Get input file name */
fin = fopen(file_in, "r+");
if (!fin) { /* If input file is not exist or can't open */
cout << "Can't open input file " << file_in << "! " << endl;
return 1;
}
file_out = get_output_file(file_in);
fout = fopen(file_out, "w");
while (fscanf(fin, "%c", &c) != EOF) {
if (c == '\n') continue; /* New line character should be ignore */
if (c == '=') { /* If read until the end of file */
if (count == 3) { /* If just 3 characters in the end */
bin >>= 2;
fprintf(fout, "%c", bin >> 8);
bin = bin & 0x0000ff;
fprintf(fout, "%c", bin);
} else if (count == 2) { /* If just 2 characters in the end */
bin >>= 4;
fprintf(fout, "%c", bin);
}
cout << "Decode done!" << endl;
return 0;
}
count++;
bin = (bin << 6) + base64_table(c); /* Update binary number using base64 table */
if (count == 4) { /* If already have 4 characters */
fprintf(fout, "%c", bin >> 16);
bin = bin & 0x00ffff;
fprintf(fout, "%c", bin >> 8);
bin = bin & 0x0000ff;
fprintf(fout, "%c", bin);
count = 0; /* Update count and bin to initial value */
bin = 0;
}
}
cout << "Decode done!" << endl;
}