123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #include <kopano/platform.h>
- #include <kopano/stringutil.h>
- #include <kopano/charset/convert.h>
- #include <memory>
- #include <string>
- #include <cerrno>
- #include <cstring>
- #include <kopano/ECIConv.h>
- #include <kopano/ECLogger.h>
- #include <mapicode.h>
- #include <mapidefs.h>
- #include <sys/mman.h>
- #include <sys/socket.h>
- #include <sys/stat.h>
- #include <arpa/inet.h>
- #include "fileutil.h"
- #define BLOCKSIZE 65536
- namespace KC {
- HRESULT HrFileLFtoCRLF(FILE *fin, FILE** fout)
- {
- char bufferin[BLOCKSIZE / 2];
- char bufferout[BLOCKSIZE+1];
- size_t sizebufferout;
- FILE* fTmp = NULL;
- if(fin == NULL || fout == NULL)
- return MAPI_E_INVALID_PARAMETER;
- fTmp = tmpfile();
- if(fTmp == NULL) {
- perror("Unable to create tmp file");
- return MAPI_E_CALL_FAILED;
- }
- while (!feof(fin)) {
- size_t readsize = fread(bufferin, 1, BLOCKSIZE / 2, fin);
- if (ferror(fin)) {
- perror("Read error");
- fclose(fTmp);
- return MAPI_E_CORRUPT_DATA;
- }
- BufferLFtoCRLF(readsize, bufferin, bufferout, &sizebufferout);
- if (fwrite(bufferout, 1, sizebufferout, fTmp) != sizebufferout) {
- perror("Write error");
- fclose(fTmp);
- return MAPI_E_CORRUPT_DATA;
- }
- }
- *fout = fTmp;
- return hrSuccess;
- }
- static inline int mmapsize(unsigned int size)
- {
- return (((size + 1) >> 12) + 1) << 12;
- }
- static HRESULT HrMapFileToBuffer(FILE *f, char **lppBuffer, int *lpSize,
- bool *lpImmap)
- {
- char *lpBuffer = NULL;
- int offset = 0;
- long ulBufferSize = BLOCKSIZE;
- struct stat stat;
- int fd = fileno(f);
- *lpImmap = false;
-
- if (fstat(fd, &stat) != 0) {
- perror("Stat failed");
- return MAPI_E_CALL_FAILED;
- }
-
- lpBuffer = (char *)mmap(0, mmapsize(stat.st_size), PROT_READ, MAP_PRIVATE, fd, 0);
- if (lpBuffer != MAP_FAILED) {
- *lpImmap = true;
- *lppBuffer = lpBuffer;
- *lpSize = stat.st_size;
- return hrSuccess;
- }
-
- lpBuffer = (char*)malloc(BLOCKSIZE);
- while (!feof(f)) {
- long ulReadsize = fread(lpBuffer+offset, 1, BLOCKSIZE, f);
- if (ferror(f)) {
- perror("Read error");
- break;
- }
- offset += ulReadsize;
- if (offset + BLOCKSIZE > ulBufferSize) {
- auto lpRealloc = static_cast<char *>(realloc(lpBuffer, offset + BLOCKSIZE));
- if (lpRealloc == NULL) {
- free(lpBuffer);
- return MAPI_E_NOT_ENOUGH_MEMORY;
- }
- lpBuffer = lpRealloc;
- ulBufferSize += BLOCKSIZE;
- }
- }
-
- if (offset == 0) {
- free(lpBuffer);
- *lppBuffer = NULL;
- *lpSize = 0;
- return hrSuccess;
- }
-
- lpBuffer[offset] = 0;
- *lppBuffer = lpBuffer;
- *lpSize = offset;
- return hrSuccess;
- }
- static HRESULT HrUnmapFileBuffer(char *lpBuffer, int ulSize, bool bImmap)
- {
- if (bImmap)
- munmap(lpBuffer, mmapsize(ulSize));
- else
- free(lpBuffer);
- return hrSuccess;
- }
- HRESULT HrMapFileToString(FILE *f, std::string *lpstrBuffer, int *lpSize)
- {
- HRESULT hr = hrSuccess;
- char *lpBuffer = NULL;
- int ulBufferSize = 0;
- bool immap = false;
- hr = HrMapFileToBuffer(f, &lpBuffer, &ulBufferSize, &immap);
- if (hr != hrSuccess || !lpBuffer)
- goto exit;
- if (lpstrBuffer)
- *lpstrBuffer = std::string(lpBuffer, ulBufferSize);
- if (lpSize)
- *lpSize = ulBufferSize;
- exit:
- if (lpBuffer)
- HrUnmapFileBuffer(lpBuffer, ulBufferSize, immap);
- return hr;
- }
- bool DuplicateFile(FILE *lpFile, std::string &strFileName)
- {
- bool bResult = true;
- size_t ulReadsize = 0;
- FILE *pfNew = NULL;
- std::unique_ptr<char[]> lpBuffer;
-
- pfNew = fopen(strFileName.c_str(), "wb");
- if(pfNew == NULL) {
- ec_log_err("Unable to create file %s: %s", strFileName.c_str(), strerror(errno));
- bResult = false;
- goto exit;
- }
-
- rewind(lpFile);
- lpBuffer.reset(new(std::nothrow) char[BLOCKSIZE]);
- if (!lpBuffer) {
- ec_log_crit("DuplicateFile is out of memory");
- bResult = false;
- goto exit;
- }
-
- while (!feof(lpFile)) {
- ulReadsize = fread(lpBuffer.get(), 1, BLOCKSIZE, lpFile);
- if (ferror(lpFile)) {
- ec_log_crit("DuplicateFile: fread: %s", strerror(errno));
- bResult = false;
- goto exit;
- }
- if (fwrite(lpBuffer.get(), 1, ulReadsize , pfNew) != ulReadsize) {
- ec_log_crit("Error during write to \"%s\": %s", strFileName.c_str(), strerror(errno));
- bResult = false;
- goto exit;
- }
- }
- exit:
- if (pfNew)
- fclose(pfNew);
- return bResult;
- }
- }
|