Find out the real file type

后端 未结 7 771
遇见更好的自我
遇见更好的自我 2020-12-29 15:02

I am working on an ASP web page that handles file uploads. Only certain types of files are allowed to be uploaded, like .XLS, .XML, .CSV, .TXT, .PDF, .PPT, etc.

I

7条回答
  •  伪装坚强ぢ
    2020-12-29 15:45

    The following C++ code could help you:

    //-1 : File Does not Exist or no access
    //0 : not an office document
    //1 : (General) MS office 2007
    //2 : (General) MS office older than 2007
    //3 : MS office 2003 PowerPoint presentation
    //4 : MS office 2003 Excel spreadsheet
    //5 : MS office applications or others 
    int IsOffice2007OrOlder(wchar_t * fileName)
    {
        int iRet = 0;
        byte msgFormatChk2007[8]    = {0x50, 0x4B, 0x03, 0x04, 0x14, 0x00, 0x06, 0x00};     //offset 0 for office 2007 documents
        byte possibleMSOldOffice[8] = {0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1};     //offset 0 for possible office 2003 documents
    
        byte msgFormatChkXLSPPT[4]  = {0xFD, 0xFF, 0xFF, 0xFF};     // offset 512: xls, ppt: FD FF FF FF 
        byte msgFormatChkOnlyPPT[4] = {0x00, 0x6E, 0x1E, 0xF0};     // offset 512: another ppt offset PPT   
        byte msgFormatChkOnlyDOC[4] = {0xEC, 0xA5, 0xC1, 0x00};     //offset 512: EC A5 C1 00 
        byte msgFormatChkOnlyXLS[8] = {0x09, 0x08, 0x10, 0x00, 0x00, 0x06, 0x05, 0x00};     //offset 512: XLS
    
        int iMsgChk = 0;
        HANDLE fileHandle = CreateFile(fileName, GENERIC_READ,
            FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL  );
        if(INVALID_HANDLE_VALUE == fileHandle) 
        { 
            return -1; 
        }
    
        byte buff[20];
        DWORD bytesRead;
        iMsgChk = 1;
        if(0 == ReadFile(fileHandle, buff, 8, &bytesRead, NULL )) 
        { 
            return -1; 
        }
    
        if(buff[0] == msgFormatChk2007[0]) 
        {
            while(buff[iMsgChk] == msgFormatChk2007[iMsgChk] && iMsgChk < 9)
                iMsgChk++;
    
            if(iMsgChk >= 8) {  
                iRet = 1; //office 2007 file format
            }
        } 
        else if(buff[0] == possibleMSOldOffice[0])
        {
            while(buff[iMsgChk] == possibleMSOldOffice[iMsgChk] && iMsgChk < 9)
                iMsgChk++;
    
            if(iMsgChk >= 8)
            {   
                //old office file format, check 512 offset further in order to filter out real office format
                iMsgChk = 1;
                SetFilePointer(fileHandle, 512, NULL, FILE_BEGIN);
                if(ReadFile(fileHandle, buff, 8, &bytesRead, NULL ) == 0) { return 0; }
    
                if(buff[0] == msgFormatChkXLSPPT[0])
                {
                    while(buff[iMsgChk] == msgFormatChkXLSPPT[iMsgChk] && iMsgChk < 5)
                        iMsgChk++;
    
                    if(iMsgChk == 4)
                        iRet = 2;
                }
                else if(buff[iMsgChk] == msgFormatChkOnlyDOC[iMsgChk])
                {
                    while(buff[iMsgChk] == msgFormatChkOnlyDOC[iMsgChk] && iMsgChk < 5)
                        iMsgChk++;
                    if(iMsgChk == 4)
                        iRet = 2;
    
                }
                else if(buff[0] == msgFormatChkOnlyPPT[0])
                {
                    while(buff[iMsgChk] == msgFormatChkOnlyPPT[iMsgChk] && iMsgChk < 5)
                        iMsgChk++;
    
                    if(iMsgChk == 4)
                        iRet = 3;
                }
                else if(buff[0] == msgFormatChkOnlyXLS[0])
                {
    
                    while(buff[iMsgChk] == msgFormatChkOnlyXLS[iMsgChk] && iMsgChk < 9)
                        iMsgChk++;
    
                    if(iMsgChk == 9)
                        iRet = 4;
                } 
    
                if(0 == iRet){
                    iRet = 5;
                }
            }
        }
    
    
        CloseHandle(fileHandle);
    
        return iRet;
    }
    

提交回复
热议问题