32 if (!CF)
return(NULL);
36 CF->
FileHandle = open(Fname,O_RDONLY|O_LARGEFILE);
43 LOG_ERROR(
"Unable to open file (%s)\n",Fname);
49 LOG_ERROR(
"Unable to stat file (%s)\n",Fname);
68 if (CF->
Mmap == MAP_FAILED)
70 LOG_ERROR(
"Unable to mmap file (%s)\n",Fname);
101 for(Val=10; (Val > 0) && (Val < Num); Val = Val * 10)
117 gcry_md_hd_t checksumhandler;
118 gcry_error_t checksumError = 0;
121 unsigned char *tempBuff;
123 uint64_t ReadTotalLen=0;
126 if (!Sum)
return(NULL);
128 checksumError = gcry_md_open(&checksumhandler, GCRY_MD_NONE, 0);
129 if (! checksumhandler)
131 LOG_ERROR(
"Unable to initialize checksum\n");
135 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_MD5);
136 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
138 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
139 gcry_strerror(checksumError));
144 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_SHA1);
145 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
147 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
148 gcry_strerror(checksumError));
155 ReadLen = fread(Buffer,1,64,Fin);
158 gcry_md_write(checksumhandler, Buffer, ReadLen);
159 ReadTotalLen += ReadLen;
165 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_MD5);
168 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_SHA1);
170 gcry_md_close(checksumhandler);
185 gcry_md_hd_t checksumhandler;
186 gcry_error_t checksumError;
187 unsigned char *tempBuff;
189 checksumError = gcry_md_open(&checksumhandler, GCRY_MD_NONE, 0);
190 if (! checksumhandler)
192 LOG_ERROR(
"Unable to initialize checksum\n");
195 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_MD5);
196 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
198 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
199 gcry_strerror(checksumError));
203 checksumError = gcry_md_enable(checksumhandler, GCRY_MD_SHA1);
204 if (gcry_err_code(checksumError) != GPG_ERR_NO_ERROR)
206 LOG_ERROR(
"GCRY Error: %s/%s\n", gcry_strsource(checksumError),
207 gcry_strerror(checksumError));
220 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_MD5);
223 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_SHA1);
226 gcry_md_close(checksumhandler);
243 if (!
Result)
return(NULL);
255 sprintf(
Result + 33 + 41,
"%Lu",(
long long unsigned int)Sum->
DataLen);
259 int calc_sha256sum(
char*filename,
char* dst) {
260 gcry_md_hd_t checksumhandler;
261 unsigned char buf[32];
262 unsigned char *tempBuff;
263 memset(buf,
'\0',
sizeof(buf));
265 if(!(f=fopen(filename,
"rb")))
267 LOG_FATAL(
"Failed to open file '%s'\n", filename);
270 gcry_md_open(&checksumhandler, GCRY_MD_SHA256, 0);
271 if (! checksumhandler ||
272 (! gcry_md_is_enabled(checksumhandler, GCRY_MD_SHA256)))
274 LOG_ERROR(
"Unable to initialize checksum\n");
279 while((i=fread(buf, 1,
sizeof(buf), f)) > 0) {
280 gcry_md_write(checksumhandler, buf, i);
283 memset(buf,
'\0',
sizeof(buf));
284 tempBuff = gcry_md_read(checksumhandler, GCRY_MD_SHA256);
285 memcpy(buf, tempBuff,
sizeof(buf));
287 gcry_md_close(checksumhandler);
291 snprintf(dst+i*2, 3,
"%02X", buf[i]);
char * SumToString(Cksum *Sum)
Return string representing a Cksum. NOTE: The calling function must free() the string!
Cksum * SumComputeBuff(CksumFile *CF)
Compute the checksum, allocate and return a Cksum containing the sum value.
int CountDigits(uint64_t Num)
Count how many digits are in a number.
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
CksumFile * SumOpenFile(char *Fname)
Open and mmap a file.
void SumCloseFile(CksumFile *CF)
Close a file that was opened with SumOpenFile()
Store file handler and mmap of a file.
int FileHandle
File handler for the file.
unsigned char * Mmap
Mmap of the file.
uint64_t MmapSize
Size of mmap.
uint64_t MmapOffset
Index into mmap.
Store check sum of a file.
uint8_t SHA1digest[20]
SHA1 digest of the file.
uint64_t DataLen
Size of the file.
uint8_t MD5digest[16]
MD5 digest of the file.
static int Result
Result of calls.