c0ldshadow
December 9th, 2006, 10:54 AM
**EDIT**
the code is actual not working properly at all. it crashes quickly after only searching a few files out. it only started generating error reports when i added a few messageboxes to the code to see each file and folder found...
**EDIT**
I am writing a function in a DLL that scans through all files on a drive and decompresses any NTFS files that are compressed.
The filecount variable is defined in the .h and keeps track of how many files have been found on the drive.
The variable char **megabuffer is defined in the .h file and stores the full path names of each file on the hard drive.
The variable int *uarray is defined in my .h file and keeps track of which files in megabuffer were decompressed successfully. For example, uarray[0] might store the number 4, meaning megabuffer[4] contains a file path of a file that was decompressed successfully.
I am planning on writing another function that returns the file path of each of the files that was decompressed successfully.
I have not worked with recursive functions much before, and I am particular uncertain about how the stack will work with the recusion, and if my DLLs code has problems related to this.
If an application calls the dcmp32 function twice, will there be conflict with the uarray and megabuffer variables being accessed twice? Or will a new set of variables be used for each call to the function?
I plan on using a separate function to free() the dynamically allocated memory.
Any advice about problems with the code would be really helpful as I'm not entirely confident about what I have so far, although it appears to be working properly. Not used to working with recusion and DLLs.
Here is the code. Thanks for any tips.
#include "dll.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define ID_LIST 1
#define FILE_DEVICE_FILE_SYSTEM 0x00000009
#define METHOD_BUFFERED 0
#define CTL_CODE2(de,fc,meth,acs)(\
((de)<<16)|((acs)<<14)|((fc)<<2)|(meth)\
)
#define FSCTL_SET_COMPRESSION2 CTL_CODE2(FILE_DEVICE_FILE_SYSTEM,16,METHOD_BUFFERED,FILE_READ_DATA|FILE_WRITE_DATA)
__declspec (dllexport) int dcmp32(const char *drive)
{
int drivelen=strlen(drive);
if(drivelen>512)
{
goto cleanup;
}
char *wc=(char *)malloc(drivelen+2);
memset(wc,0x0,drivelen+2);
strncpy(wc,drive,drivelen);
strncat(wc,"*",1);
HANDLE search=0;
WIN32_FIND_DATA wfd;
BOOL rcode=TRUE;
search=FindFirstFile(wc,&wfd);
if(search==INVALID_HANDLE_VALUE)
{
free(wc);
goto cleanup;
}
free(wc);
unsigned len2=0;
unsigned tot=0;
for(;rcode;rcode=FindNextFile(search,&wfd))
{
len2=0;
tot=0;
if(wfd.cFileName[0]=='.')
{
continue;
}
++filecount;
megabuffer=(char **)realloc(megabuffer,filecount*(sizeof(char *)));
if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
len2=strlen(wfd.cFileName);
tot=drivelen+len2+1+1;
megabuffer[filecount]=(char *)malloc(tot);
memset(megabuffer[filecount],0x0,tot);
strncpy(megabuffer[filecount],drive,drivelen);
strncat(megabuffer[filecount],wfd.cFileName,len2);
strncat(megabuffer[filecount],"\\",1);
MessageBox(0,megabuffer[filecount],"good",0);
DWORD attrib=GetFileAttributes(megabuffer[filecount]);
if(attrib&FILE_ATTRIBUTE_COMPRESSED)
{
HANDLE h=CreateFile(megabuffer[filecount],GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
if(h==INVALID_HANDLE_VALUE)
{
}
USHORT comp=COMPRESSION_FORMAT_NONE;
DWORD lpB;
if(DeviceIoControl(h,FSCTL_SET_COMPRESSION2,&comp,sizeof(comp),0,0,&lpB,0)==0)
{
}
else
{
++dfiles;
uarray=(unsigned *)realloc(uarray,dfiles*sizeof(unsigned));
uarray[dfiles]=filecount;
}
CloseHandle(h);
}
dcmp32(megabuffer[filecount]);
}
else
{
len2=strlen(wfd.cFileName);
tot=drivelen+len2+1;
megabuffer[filecount]=(char *)malloc(tot);
memset(megabuffer[filecount],0x0,tot);
strncpy(megabuffer[filecount],drive,drivelen);
strncat(megabuffer[filecount],wfd.cFileName,len2);
MessageBox(0,megabuffer[filecount],"good",0);
DWORD attrib=GetFileAttributes(megabuffer[filecount]);
if(attrib&FILE_ATTRIBUTE_COMPRESSED)
{
HANDLE h=CreateFile(megabuffer[filecount],GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
if(h==INVALID_HANDLE_VALUE)
{
}
USHORT comp=COMPRESSION_FORMAT_NONE;
DWORD lpB;
if(DeviceIoControl(h,FSCTL_SET_COMPRESSION2,&comp,sizeof(comp),0,0,&lpB,0)==0)
{
}
else
{
++dfiles;
uarray=(unsigned *)realloc(uarray,dfiles*sizeof(unsigned));
uarray[dfiles]=filecount;
}
CloseHandle(h);
}
}
}
FindClose(search);
cleanup:
return 0;
}
BOOL APIENTRY DllMain(HINSTANCE hInst,DWORD reason,LPVOID reserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
the code is actual not working properly at all. it crashes quickly after only searching a few files out. it only started generating error reports when i added a few messageboxes to the code to see each file and folder found...
**EDIT**
I am writing a function in a DLL that scans through all files on a drive and decompresses any NTFS files that are compressed.
The filecount variable is defined in the .h and keeps track of how many files have been found on the drive.
The variable char **megabuffer is defined in the .h file and stores the full path names of each file on the hard drive.
The variable int *uarray is defined in my .h file and keeps track of which files in megabuffer were decompressed successfully. For example, uarray[0] might store the number 4, meaning megabuffer[4] contains a file path of a file that was decompressed successfully.
I am planning on writing another function that returns the file path of each of the files that was decompressed successfully.
I have not worked with recursive functions much before, and I am particular uncertain about how the stack will work with the recusion, and if my DLLs code has problems related to this.
If an application calls the dcmp32 function twice, will there be conflict with the uarray and megabuffer variables being accessed twice? Or will a new set of variables be used for each call to the function?
I plan on using a separate function to free() the dynamically allocated memory.
Any advice about problems with the code would be really helpful as I'm not entirely confident about what I have so far, although it appears to be working properly. Not used to working with recusion and DLLs.
Here is the code. Thanks for any tips.
#include "dll.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define ID_LIST 1
#define FILE_DEVICE_FILE_SYSTEM 0x00000009
#define METHOD_BUFFERED 0
#define CTL_CODE2(de,fc,meth,acs)(\
((de)<<16)|((acs)<<14)|((fc)<<2)|(meth)\
)
#define FSCTL_SET_COMPRESSION2 CTL_CODE2(FILE_DEVICE_FILE_SYSTEM,16,METHOD_BUFFERED,FILE_READ_DATA|FILE_WRITE_DATA)
__declspec (dllexport) int dcmp32(const char *drive)
{
int drivelen=strlen(drive);
if(drivelen>512)
{
goto cleanup;
}
char *wc=(char *)malloc(drivelen+2);
memset(wc,0x0,drivelen+2);
strncpy(wc,drive,drivelen);
strncat(wc,"*",1);
HANDLE search=0;
WIN32_FIND_DATA wfd;
BOOL rcode=TRUE;
search=FindFirstFile(wc,&wfd);
if(search==INVALID_HANDLE_VALUE)
{
free(wc);
goto cleanup;
}
free(wc);
unsigned len2=0;
unsigned tot=0;
for(;rcode;rcode=FindNextFile(search,&wfd))
{
len2=0;
tot=0;
if(wfd.cFileName[0]=='.')
{
continue;
}
++filecount;
megabuffer=(char **)realloc(megabuffer,filecount*(sizeof(char *)));
if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
len2=strlen(wfd.cFileName);
tot=drivelen+len2+1+1;
megabuffer[filecount]=(char *)malloc(tot);
memset(megabuffer[filecount],0x0,tot);
strncpy(megabuffer[filecount],drive,drivelen);
strncat(megabuffer[filecount],wfd.cFileName,len2);
strncat(megabuffer[filecount],"\\",1);
MessageBox(0,megabuffer[filecount],"good",0);
DWORD attrib=GetFileAttributes(megabuffer[filecount]);
if(attrib&FILE_ATTRIBUTE_COMPRESSED)
{
HANDLE h=CreateFile(megabuffer[filecount],GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
if(h==INVALID_HANDLE_VALUE)
{
}
USHORT comp=COMPRESSION_FORMAT_NONE;
DWORD lpB;
if(DeviceIoControl(h,FSCTL_SET_COMPRESSION2,&comp,sizeof(comp),0,0,&lpB,0)==0)
{
}
else
{
++dfiles;
uarray=(unsigned *)realloc(uarray,dfiles*sizeof(unsigned));
uarray[dfiles]=filecount;
}
CloseHandle(h);
}
dcmp32(megabuffer[filecount]);
}
else
{
len2=strlen(wfd.cFileName);
tot=drivelen+len2+1;
megabuffer[filecount]=(char *)malloc(tot);
memset(megabuffer[filecount],0x0,tot);
strncpy(megabuffer[filecount],drive,drivelen);
strncat(megabuffer[filecount],wfd.cFileName,len2);
MessageBox(0,megabuffer[filecount],"good",0);
DWORD attrib=GetFileAttributes(megabuffer[filecount]);
if(attrib&FILE_ATTRIBUTE_COMPRESSED)
{
HANDLE h=CreateFile(megabuffer[filecount],GENERIC_ALL,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
if(h==INVALID_HANDLE_VALUE)
{
}
USHORT comp=COMPRESSION_FORMAT_NONE;
DWORD lpB;
if(DeviceIoControl(h,FSCTL_SET_COMPRESSION2,&comp,sizeof(comp),0,0,&lpB,0)==0)
{
}
else
{
++dfiles;
uarray=(unsigned *)realloc(uarray,dfiles*sizeof(unsigned));
uarray[dfiles]=filecount;
}
CloseHandle(h);
}
}
}
FindClose(search);
cleanup:
return 0;
}
BOOL APIENTRY DllMain(HINSTANCE hInst,DWORD reason,LPVOID reserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}