44 if (!CF)
return(NULL);
48 CF->FileHandle = open(Fname,O_RDONLY|O_LARGEFILE);
51 CF->FileHandle = open(Fname,O_RDONLY);
53 if (CF->FileHandle == -1)
55 LOG_ERROR(
"Unable to open file (%s)\n",Fname);
59 if (fstat(CF->FileHandle,&Stat) == -1)
61 LOG_ERROR(
"Unable to stat file (%s)\n",Fname);
62 close(CF->FileHandle);
66 CF->MmapSize = Stat.st_size;
70 if (CF->MmapSize >= (uint32_t)(-1))
72 close(CF->FileHandle);
79 CF->Mmap = mmap(0,CF->MmapSize,PROT_READ,MAP_PRIVATE,CF->FileHandle,0);
80 if (CF->Mmap == MAP_FAILED)
82 LOG_ERROR(
"Unable to mmap file (%s)\n",Fname);
83 close(CF->FileHandle);
113 for(Val=10; (Val > 0) && (Val < Num); Val = Val * 10)
129 gcry_md_hd_t checksumhandler;
130 gcry_error_t checksumError = 0;
133 unsigned char *tempBuff;
135 uint64_t ReadTotalLen=0;
138 if (!Sum)
return(NULL);
140 checksumError = gcry_md_open(&checksumhandler, GCRY_MD_NONE, 0);
141 if (! checksumhandler)
143 LOG_ERROR(
"Unable to initialize checksum\n");
147 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_MD5);
148 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
150 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
151 gcry_strerror(checksumError));
156 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_SHA1);
157 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
159 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
160 gcry_strerror(checksumError));
167 ReadLen = fread(Buffer,1,64,Fin);
170 gcry_md_write(checksumhandler, Buffer, ReadLen);
171 ReadTotalLen += ReadLen;
177 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_MD5);
180 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_SHA1);
182 gcry_md_close(checksumhandler);
197 gcry_md_hd_t checksumhandler;
198 gcry_error_t checksumError;
199 unsigned char *tempBuff;
201 checksumError = gcry_md_open(&checksumhandler, GCRY_MD_NONE, 0);
202 if (! checksumhandler)
204 LOG_ERROR(
"Unable to initialize checksum\n");
207 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_MD5);
208 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
210 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
211 gcry_strerror(checksumError));
215 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_SHA1);
216 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
218 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
219 gcry_strerror(checksumError));
232 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_MD5);
235 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_SHA1);
238 gcry_md_close(checksumhandler);
255 if (!Result)
return(NULL);
259 sprintf(Result + (i*2),
"%02X",Sum->
SHA1digest[i]);
264 sprintf(Result + 41 + (i*2),
"%02X",Sum->
MD5digest[i]);
267 sprintf(Result + 33 + 41,
"%Lu",(
long long unsigned int)Sum->
DataLen);
271 int calc_sha256sum(
char*filename,
char* dst) {
272 gcry_md_hd_t checksumhandler;
273 unsigned char buf[32];
274 unsigned char *tempBuff;
275 memset(buf,
'\0',
sizeof(buf));
277 if(!(f=fopen(filename,
"rb")))
279 LOG_FATAL(
"Failed to open file '%s'\n", filename);
282 gcry_md_open(&checksumhandler, GCRY_MD_SHA256, 0);
283 if (! checksumhandler ||
284 (! gcry_md_is_enabled(checksumhandler, GCRY_MD_SHA256)))
286 LOG_ERROR(
"Unable to initialize checksum\n");
291 while((i=fread(buf, 1,
sizeof(buf), f)) > 0) {
292 gcry_md_write(checksumhandler, buf, i);
295 memset(buf,
'\0',
sizeof(buf));
296 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_SHA256);
297 memcpy(buf, tempBuff,
sizeof(buf));
299 gcry_md_close(checksumhandler);
303 snprintf(dst+i*2, 3,
"%02X", buf[i]);
static int Result
Result of calls.
CksumFile * SumOpenFile(char *Fname)
Open and mmap a file.
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
uint8_t SHA1digest[20]
SHA1 digest of the file.
int CountDigits(uint64_t Num)
Count how many digits are in a number.
uint8_t MD5digest[16]
MD5 digest of the file.
unsigned char * Mmap
Mmap of the file.
Store check sum of a file.
uint64_t DataLen
Size of the file.
char * SumToString(Cksum *Sum)
Return string representing a Cksum. NOTE: The calling function must free() the string! ...
uint64_t MmapSize
Size of mmap.
void SumCloseFile(CksumFile *CF)
Close a file that was opened with SumOpenFile()
Cksum * SumComputeBuff(CksumFile *CF)
Compute the checksum, allocate and return a Cksum containing the sum value.
int FileHandle
File handler for the file.
Store file handler and mmap of a file.