SEARCH THE RFC INDEX
BSTR to LPSTR conversion TOGGLE (show/hide this piece of code)
BYTE *ConvertBSTRToLPSTRSbr(BSTR bstrIn){
static BYTE RetVal[65535];
DWORD CbtlRetVal;
memset(RetVal, 0, sizeof(RetVal));
CbtlRetVal = ConvertBSTRToLPSTR(bstrIn, RetVal, sizeof(RetVal)-1);
if(CbtlRetVal){
return RetVal;
}
return NULL;
}
DWORD ConvertBSTRToLPSTR(BSTR bstrIn, char *pszOut, int maxlen){
int nOutputStrLen;
int nInputStrLen;
DWORD nRetVal;
nRetVal = 0;
if(bstrIn != NULL){
nInputStrLen = -1;
if(maxlen <= nInputStrLen)
return 0;
nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, bstrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
if (pszOut){
memset (pszOut, 0x00, sizeof (char)*nOutputStrLen);
WideCharToMultiByte (CP_ACP, 0, bstrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
nRetVal = 1;
}
}
return nRetVal;
}
Create entire directory tree (mkdir -p) TOGGLE (show/hide this piece of code)
void CreateDirTree(char *path){
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
DWORD x;
DWORD flag;
flag = 0;
hFind = FindFirstFile(path, &FindFileData);
if(hFind == INVALID_HANDLE_VALUE){
for(x = 0 ; x <= strlen(path) && x < MAX_PATH ; x++){
if(path[x] == '\\' || path[x] == '\0'){
if(path[x] == '\\'){
flag = 1;
path[x] = '\0';
}
hFind = FindFirstFile(path, &FindFileData);
if(hFind == INVALID_HANDLE_VALUE){
CreateDirectory(path, NULL);
}else{
FindClose(hFind);
}
if(flag){
path[x] = '\\';
flag = 0;
}
}
}
}else{
FindClose(hFind);
}
}
Debug log helper TOGGLE (show/hide this piece of code)
void DebugLog(char *fname, char *data, DWORD datalen){
HANDLE fda;
DWORD fwl;
fda = (HANDLE)CreateFile(fname, FILE_APPEND_DATA, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(fda){
WriteFile(fda, data, datalen, &fwl, NULL);
CloseHandle(fda);
}
}
#define StringDebugLog(x, y) DebugLog(x, y, strlen(y))
Customizable hex dump with file descriptor output (log file, stdout, etc) TOGGLE (show/hide this piece of code)
void fprint_hex_dump(FILE *outfd, unsigned int len, unsigned char *buf, unsigned int bufl){
unsigned int x, y, z;
char bfs[255];
char bfc[255];
char bfx[255];
y = z = x = 1;
bfs[sizeof(bfs)-1] = 0x00;
memset(bfs, 0x20, sizeof(bfs)-1);
memset(bfx, 0x00, sizeof(bfx));
memset(bfc, 0x00, sizeof(bfc));
for(x = 0 ; x < bufl ; x++){
sprintf(bfc, "%s%c" , bfc, (buf[x] >= 0x00 &&
buf[x] <= 0x20 )? '.' : buf[x]);
sprintf(bfx, "%s%02x ", bfx, buf[x]);
if(y == len || x >= bufl-1){
fprintf(outfd, "%s%.*s\t%s\n", bfx, ((len*3) - strlen(bfx)), bfs, bfc);
memset(bfx, 0, sizeof(bfx));
memset(bfc, 0, sizeof(bfc));
y = 0;
}
y++;
}
}
Windows mmap TOGGLE (show/hide this piece of code)
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset){
char *szRval;
HANDLE hFileMapping;
SECURITY_ATTRIBUTES sAttribs;
int dwProt;
memset(&sAttribs, 0, sizeof(SECURITY_ATTRIBUTES));
SetLastError(0);
dwProt = 0;
dwProt = (prot == PROT_READ) ? PAGE_READONLY : dwProt;
dwProt = (prot == PROT_WRITE) ? PAGE_READWRITE : dwProt;
dwProt = (prot == PROT_EXEC) ? PAGE_EXECUTE_READWRITE : dwProt;
hFileMapping = (HANDLE)_get_osfhandle(fd);
hFileMapping = CreateFileMapping(hFileMapping, &sAttribs, dwProt, 0, (long)offset, NULL);
if(GetLastError() != 0){
return MAP_FAILED;
}
dwProt = 0;
dwProt = (prot == PROT_READ) ? FILE_MAP_READ : dwProt;
dwProt = (prot == PROT_WRITE) ? FILE_MAP_WRITE : dwProt;
dwProt = (prot == PROT_EXEC) ? FILE_MAP_ALL_ACCESS : dwProt; //FILE_MAP_EXECUTE falta
szRval = MapViewOfFile(hFileMapping, dwProt, 0, (long)offset, length);
if(GetLastError() != 0){
CloseHandle(hFileMapping);
errno = EINVAL;
return MAP_FAILED;
}
return szRval;
}
int munmap(void *start, size_t length){
return UnmapViewOfFile(start) ? 0 : 1;
}