Changeset View
Changeset View
Standalone View
Standalone View
dirmngr/cdblib.c
Show First 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | /* Initializes structure given by CDBP pointer and associates it with | ||||
application. File FD should be opened at least read-only, and | application. File FD should be opened at least read-only, and | ||||
should be seekable. Routine returns 0 on success or negative value | should be seekable. Routine returns 0 on success or negative value | ||||
on error. */ | on error. */ | ||||
int | int | ||||
cdb_init(struct cdb *cdbp, int fd) | cdb_init(struct cdb *cdbp, int fd) | ||||
{ | { | ||||
struct stat st; | struct stat st; | ||||
unsigned char *mem; | unsigned char *mem; | ||||
#ifdef _WIN32 | #ifndef _WIN32 | ||||
HANDLE hFile, hMapping; | |||||
#else | |||||
unsigned int fsize; | unsigned int fsize; | ||||
#endif | #endif | ||||
/* get file size */ | /* get file size */ | ||||
if (fstat(fd, &st) < 0) | if (fstat(fd, &st) < 0) | ||||
return -1; | return -1; | ||||
/* trivial sanity check: at least toc should be here */ | /* trivial sanity check: at least toc should be here */ | ||||
if (st.st_size < 2048) { | if (st.st_size < 2048) { | ||||
gpg_err_set_errno (EPROTO); | gpg_err_set_errno (EPROTO); | ||||
return -1; | return -1; | ||||
} | } | ||||
/* memory-map file */ | |||||
#ifdef _WIN32 | #ifdef _WIN32 | ||||
# ifdef __MINGW32CE__ | /* Read the full file */ | ||||
hFile = fd; | mem = malloc (st.st_size); | ||||
# else | if (!mem) { | ||||
hFile = (HANDLE) _get_osfhandle(fd); | gpg_err_set_errno (ENOMEM); | ||||
# endif | |||||
if (hFile == (HANDLE) -1) | |||||
return -1; | |||||
hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); | |||||
if (!hMapping) | |||||
return -1; | return -1; | ||||
mem = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); | } | ||||
if (!mem) | |||||
/* error out if the file was somehow modified */ | |||||
if (read (fd, mem, st.st_size) != st.st_size) { | |||||
free (mem); | |||||
gpg_err_set_errno (EIO); | |||||
return -1; | return -1; | ||||
cdbp->cdb_mapping = hMapping; | } | ||||
close (fd); | |||||
fd = -1; | |||||
#else /*!_WIN32*/ | #else /*!_WIN32*/ | ||||
/* memory-map file */ | |||||
fsize = (unsigned int)(st.st_size & 0xffffffffu); | fsize = (unsigned int)(st.st_size & 0xffffffffu); | ||||
mem = (unsigned char*)mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); | mem = (unsigned char*)mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); | ||||
if (mem == MAP_FAILED) | if (mem == MAP_FAILED) | ||||
return -1; | return -1; | ||||
#endif /*!_WIN32*/ | #endif /*!_WIN32*/ | ||||
cdbp->cdb_fd = fd; | cdbp->cdb_fd = fd; | ||||
cdbp->cdb_fsize = st.st_size; | cdbp->cdb_fsize = st.st_size; | ||||
Show All 20 Lines | |||||
/* Frees the internal resources held by structure. Note that this | /* Frees the internal resources held by structure. Note that this | ||||
routine does not close the file. */ | routine does not close the file. */ | ||||
void | void | ||||
cdb_free(struct cdb *cdbp) | cdb_free(struct cdb *cdbp) | ||||
{ | { | ||||
if (cdbp->cdb_mem) { | if (cdbp->cdb_mem) { | ||||
#ifdef _WIN32 | #ifdef _WIN32 | ||||
UnmapViewOfFile ((void*) cdbp->cdb_mem); | free ((void*)cdbp->cdb_mem); | ||||
CloseHandle (cdbp->cdb_mapping); | |||||
cdbp->cdb_mapping = NULL; | |||||
#else | #else | ||||
munmap((void*)cdbp->cdb_mem, cdbp->cdb_fsize); | munmap((void*)cdbp->cdb_mem, cdbp->cdb_fsize); | ||||
#endif /* _WIN32 */ | #endif /* _WIN32 */ | ||||
cdbp->cdb_mem = NULL; | cdbp->cdb_mem = NULL; | ||||
} | } | ||||
cdbp->cdb_fsize = 0; | cdbp->cdb_fsize = 0; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 733 Lines • Show Last 20 Lines |