File: FileUtil.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (202 lines) | stat: -rw-r--r-- 4,713 bytes parent folder | download
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
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "PrintBasic.h"
#include "PrintStruct.h"

#include "FileUtil.h"

///////////////////////////// File functions //////////////////////////
FILE *FileOpen(const char *name	, const char *folder, int bVerify, int ocsp, int crl)
{
	FILE *f=NULL;
	char *buff=NULL;
	char *suffix=NULL;
	size_t len=0;
	errno_t err=0;

	if(bVerify)
		suffix=SUFFIX_NEW;
	else
		suffix=SUFFIX_OLD;

	len=strlen(folder)+strlen(name)+strlen(suffix)+5;
	buff=(char *)malloc(len);

	sprintf_s(buff,len,"%s\\%s%d%d_%s",folder,name,ocsp,crl,suffix);

	if(0 != (err=fopen_s(&f,buff,"w")))
		printf("ERROR : could not open file : %s\n",buff);

	free(buff);

	return f;
}

void FileClose(FILE *f)
{
	fclose(f);
}

int FileVerify(const char *name, const char *folder, int bVerify, int ocsp, int crl)
{
	FILE *fOld=NULL;
	FILE *fNew=NULL;
	char *buffOld=NULL;
	char *buffNew=NULL;
	char *buffResult=NULL;
	size_t lenOld=0;
	size_t lenNew=0;
	unsigned char *contentOld=NULL;
	unsigned char *contentNew=NULL;
	int lRet = 0;
	errno_t err=0;

#ifdef WIN32
    struct _stat sizeOld = {0};
    struct _stat sizeNew = {0};
#else
	struct stat sizeOld = {0};
	struct stat sizeNew = {0};
#endif
 
	if(!bVerify)
		return 0;

	//Open the reference file
	lenOld=strlen(folder)+strlen(name)+strlen(SUFFIX_OLD)+5;
	buffOld=(char *)malloc(lenOld);

	sprintf_s(buffOld,lenOld,"%s\\%s%d%d_%s",folder,name,ocsp,crl,SUFFIX_OLD);

	if(0 != (err=fopen_s(&fOld,buffOld,"rb")))
	{
		printf("ERROR : could not open file : %s\n",buffOld);
		free(buffOld);
		return -1;
	}

	free(buffOld);

	//Open the result file
	lenNew=strlen(folder)+strlen(name)+strlen(SUFFIX_NEW)+5;
	buffNew=(char *)malloc(lenNew);

	sprintf_s(buffNew,lenNew,"%s\\%s%d%d_%s",folder,name,ocsp,crl,SUFFIX_NEW);

	if(0 != (err=fopen_s(&fNew,buffNew,"rb")))
	{
		printf("ERROR : could not open file : %s\n",buffNew);
		fclose(fOld);
		free(buffNew);
		return -1;
	}

#ifdef WIN32
    if(0 != _fstat(_fileno(fOld), &sizeOld))
#else
    if(0 != fstat(fileno(fOld), &sizeOld))
#endif
    {
		printf("ERROR : file %s_%s could not be read\n",name,SUFFIX_OLD);
		free(buffNew);
		fclose(fOld);
		fclose(fNew);
		return -1;
	}

    contentOld = (unsigned char *)malloc(sizeOld.st_size);
	if( sizeOld.st_size != fread(contentOld, sizeof(unsigned char), sizeOld.st_size, fOld))
	{
		printf("ERROR : file %s_%s could not be read\n",name,SUFFIX_OLD);
		free(buffNew);
		free(contentOld);
		fclose(fOld);
		fclose(fNew);
		return -1;
	}

#ifdef WIN32
    if(0 != _fstat(_fileno(fNew), &sizeNew))
#else
    if(0 != fstat(fileno(fNew), &sizeNew))
#endif
    {
		printf("ERROR : file %s_%s could not be read\n",name,SUFFIX_NEW);
		free(buffNew);
		free(contentOld);
		fclose(fOld);
		fclose(fNew);
		return -1;
	}

    contentNew = (unsigned char *)malloc(sizeNew.st_size);
	if( sizeNew.st_size != fread(contentNew, sizeof(unsigned char), sizeNew.st_size, fNew))
	{
		printf("ERROR : file %s_%s could not be read\n",name,SUFFIX_NEW);
		free(buffNew);
		free(contentOld);
		free(contentNew);
		fclose(fOld);
		fclose(fNew);
		return -1;
	}

	if(sizeOld.st_size!=sizeNew.st_size)
	{
		PrintERROR("The result files have NOT the same SIZE");
		lRet = -1;
	}
	else
	{
		if(memcmp(contentOld,contentNew,sizeNew.st_size)!=0)
		{
			PrintERROR("The result files have NOT the same CONTENT");
			lRet = -1;
		}
	}

	free(contentOld);
	free(contentNew);

	fclose(fOld);
	fclose(fNew);

	if(lRet==0)
	{
		PrintSUCCESS("Test runs successfully");
	}
	else
	{
		buffResult=(char *)malloc(lenNew+6);
		sprintf_s(buffResult,lenNew+6,"%s_error",buffNew);

		rename(buffNew,buffResult);

		free(buffResult);
	}

	free(buffNew);

	return lRet;
}