-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_file.c
151 lines (142 loc) · 4.33 KB
/
split_file.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
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
#include <bits/stdc++.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <openssl/sha.h>
#define BUFFS 2048*16
using namespace std;
unsigned char* encrypt(FILE *fp){
SHA_CTX c;
unsigned char *md=new unsigned char[SHA_DIGEST_LENGTH];
unsigned char *buffer=new unsigned char[BUFFS];
int i,fd=fileno(fp);
SHA1_Init(&c);
while(1){
i=read(fd,buffer,BUFFS);
if(i<=0) break;
SHA1_Update(&c,buffer,(unsigned long)i);
}
SHA1_Final(&(md[0]),&c);
return md;
/*for(int i=0;i<SHA_DIGEST_LENGTH;i++)
printf("%02x",md[i]);
printf("\n");*/
}
FILE *openforwrite(int filecounter){
char fileout[20];
sprintf(fileout,"file_part%d",filecounter);
return fopen(fileout,"w");
}
int split_file(char *filename){
FILE *ptr_readfile,*ptr_writefile;
char line[128];
//char *filename=(char*)"/home/aklesh/OS_DIR_P2P/p2p_client.cpp";
int filecounter=1,linecounter=1,temp;
if((ptr_readfile = fopen(filename,"r"))==NULL){
perror("Fileopen error\n");
exit(1);
}
ptr_writefile=openforwrite(filecounter);
while(fgets(line,sizeof line,ptr_readfile)!=NULL){
if(linecounter==5){
fclose(ptr_writefile);
linecounter=1;
temp=filecounter;
filecounter++;
if((ptr_writefile=openforwrite(filecounter))==NULL){
perror("No write file\n");
exit(1);
}
//printf("SHA1(file_part%d)= ",temp);
//encrypt(ptr_writefile);
}
fprintf(ptr_writefile,"%s\n",line);
linecounter++;
}
for(int i=1;i<=filecounter;i++){
char fname[]="/home/aklesh/OS_DIR_P2P/file_part";
int x=strlen(fname);
if(i>=10){
string str;
int n=i;
while(n>0){
str.push_back((char)(n%10+'0'));
n/=10;
}
for(int i=str.size()-1;i>=0;i--)
fname[x++]=str[i];
}
else fname[x++]=(char)(i+'0');
fname[x]='\0';
FILE *fp;
if((fp=fopen(fname,"r"))==NULL){
perror("Unable to open");
exit(EXIT_FAILURE);
}
unsigned char *md=new unsigned char[SHA_DIGEST_LENGTH];
md=encrypt(fp);
//for(int i=0;i<SHA_DIGEST_LENGTH;i++)
// printf("%02x",md[i]);
//cout<<std::endl<<std::endl;
}
return filecounter;
}
int main(){
FILE *ptr_readfile,*ptr_writefile;
char line[128],*filename=(char*)"/home/aklesh/OS_DIR_P2P/p2p_client.cpp";
int filecounter=1,linecounter=1,temp;
if((ptr_readfile = fopen(filename,"r"))==NULL){
perror("Fileopen error\n");
exit(1);
}
ptr_writefile=openforwrite(filecounter);
while(fgets(line,sizeof line,ptr_readfile)!=NULL){
if(linecounter==5){
fclose(ptr_writefile);
linecounter=1;
temp=filecounter;
filecounter++;
if((ptr_writefile=openforwrite(filecounter))==NULL){
perror("No write file\n");
exit(1);
}
//printf("SHA1(file_part%d)= ",temp);
//encrypt(ptr_writefile);
}
fprintf(ptr_writefile,"%s\n",line);
linecounter++;
}
for(int i=1;i<=filecounter;i++){
char fname[]="/home/aklesh/OS_DIR_P2P/file_part";
int x=strlen(fname);
if(i>=10){
string str;
int n=i;
while(n>0){
str.push_back((char)(n%10+'0'));
n/=10;
}
for(int i=str.size()-1;i>=0;i--)
fname[x++]=str[i];
}
else fname[x++]=(char)(i+'0');
fname[x]='\0';
FILE *fp;
if((fp=fopen(fname,"r"))==NULL){
perror("Unable to open");
exit(EXIT_FAILURE);
}
unsigned char *md=new unsigned char[SHA_DIGEST_LENGTH];
md=encrypt(fp);
for(int i=0;i<SHA_DIGEST_LENGTH;i++)
printf("%02x",md[i]);
//cout<<std::endl<<std::endl;
}
fclose(ptr_readfile);
return 0;
}