コード例 #1
0
    bool Execute()
    {
        int  count;
        int  shifter;
        int  offset    = 0;
        int  newPos    = 0;
        int  ch;
        char oldchars[ 3 ];

        for(count = 0; count < WINSIZE; count ++)
        {
            m_window[ count ] = ' ';
        }

        //
        // Go through input file until we're done.
        //
        m_currPos = DROP_INDEX(m_currPos);
        ch = GetNextChar();
        while(ch != EOF)
        {
            //
            // If less than 3 chars from end, just write out remainder.
            //
            if((m_infileSize - m_infilePos) < 2)
            {
                SaveUncompByte( ch );
                ch = GetNextChar();
                continue;
            } 

            //
            // Find previous occurrence of character in window.
            //
            count   = 1;
            shifter = DROP_INDEX(m_currPos);
            
            while((m_window[ shifter ] != m_window[ m_currPos ]) && (count < WINSIZE))
            {
                count++;
                shifter = DROP_INDEX(shifter);
            }

            //
            // Check if char is unique so far in input file.
            //
            if(count == WINSIZE)
            {
                SaveUncompByte( ch );
                ch = GetNextChar();
                continue;
            }
            else
            {
                //
                // Find out how many characters match.
                //
                int savePos = m_currPos;

                oldchars[ 2 ] = oldchars[ 0 ] = m_window[ ADD_INDEX(m_currPos) ];

                count   = 1;
                offset  = shifter;
                shifter = ADD_INDEX(shifter);

                while((ch = GetNextChar()) != EOF && (m_window[ shifter ] == ch) && (count < MAXLEN))
                {
                    ++count;

                    if(count == 2) oldchars[ 1 ] = m_window[ ADD_INDEX(m_currPos) ];
                    /************/ oldchars[ 2 ] = m_window[ ADD_INDEX(m_currPos) ];

                    shifter = ADD_INDEX(shifter); 
                }

                //
                // Since this is the first match, save it as the best so far.
                //
                int bestcount  = count;
                int bestoffset = offset;

                if(ch != EOF && ((m_window[ shifter ] != ch) || (count == MAXLEN)))
                {
                    UnreadChar( oldchars[ 2 ] );
                }

                //
                // Now find the best match for the string in the window.
                //
                shifter = DROP_INDEX( offset );

                while((shifter != m_currPos) && (bestcount < MAXLEN) && (!InBetween( savePos, m_currPos, shifter )))
                {
                    while((shifter != m_currPos) && (m_window[ shifter ] != m_window[ savePos ]))
                    {
                        shifter = DROP_INDEX(shifter);
                    }

                    if(shifter == m_currPos) break;

                    count  = 0;
                    offset = shifter;
                    newPos = savePos;

                    while(ch != EOF && (m_window[ shifter ] == m_window[ newPos ]) && (count < MAXLEN))
                    {
                        if(count >= (bestcount - 1))
                        {
                            if(count == 1) oldchars[ 1 ] = m_window[ ADD_INDEX(m_currPos) ];
                            /************/ oldchars[ 2 ] = m_window[ ADD_INDEX(m_currPos) ];

                            ch = GetNextChar();
                        }

                        shifter = ADD_INDEX(shifter);
                        newPos  = ADD_INDEX(newPos );
                        count++;
                    }

                    if(ch != EOF && ((count >= MAXLEN) || ((m_window[ shifter ] != m_window[ newPos ]) && (count >= bestcount))))
                    {
                        UnreadChar( oldchars[ 2 ] );
                    }

                    if(count > bestcount)
                    {
                        bestcount  = count;
                        bestoffset = offset;
                    }

                    shifter = DROP_INDEX(offset);
                }

                if(ch != EOF)
                {
                    ch = GetNextChar();
                }

                count  = bestcount;
                offset = bestoffset;

                //
                // If count < 3, then not enough chars to compress.
                //
                if(count < 3)
                {
                    SaveUncompByte( m_window[ savePos ] );

                    m_infilePos -= count;

                    m_window[ savePos = ADD_INDEX(savePos) ] = oldchars[ 0 ];

                    m_currPos = DROP_INDEX(m_currPos);

                    if(count == 2)
                    {
                        m_window[ ADD_INDEX(savePos) ] = oldchars[ 1 ];

                        m_currPos = DROP_INDEX(m_currPos);
                    }
                }
                else
                {
                    int iCompCode = COMP_CODE(count, offset);

                    m_dataBytes[   m_dataCount ] = COMP_CODE_LOBYTE( iCompCode );
                    m_dataBytes[ ++m_dataCount ] = COMP_CODE_HIBYTE( iCompCode );

                    CheckFlagByte();

                    if(ch != EOF)
                    {
                        UnreadChar( oldchars[ 2 ] );
                    }
                }

                if(ch != EOF && (count <= MAXLEN))
                {
                    ch = GetNextChar();
                }
            }
        }

        WriteFlagByte();

        return true;
    }
コード例 #2
0
ファイル: Uploader.cpp プロジェクト: examyes/prtscrpp
/// <summary>
/// Uploads a temporarily stored image to imgur.
/// </summary>
/// <returns>Direct link to the image if succeess, empty string otherwise</returns>
std::string Uploader::imgur() {
    FILE *fp; // File pointer
    unsigned char *buffer; // our buffer, a buffer of BYTE[]
    long fileLen; // Just the file length in bytes.

    // Convert the CStringW to a char* for fopen
    CString absPath = this->getTempFile();
    const size_t __path = (absPath.GetLength() + 1) * 2;
    char *absPathChar = new char[__path];
    size_t convertedCharsw = 0;
    wcstombs_s(&convertedCharsw, absPathChar, __path, absPath, _TRUNCATE);

    // Open the file in binary mode
    fp = fopen(absPathChar, "rb");
    if(!fp) {
        AfxMessageBox(_T("Failed to open the screenshot file."));
        return _EMPTY;
    }

    fseek(fp, 0, SEEK_END); // Jump to the end of the file
    fileLen = ftell(fp); // Get the size
    rewind(fp); // Jump back to the beginning of the file

    // Allocate memory
    buffer = (unsigned char *)malloc((fileLen)*sizeof(unsigned char));
    if(!buffer) {
        AfxMessageBox(_T("Failed to allocate memory."));
        return _EMPTY;
    }

    fread(buffer, fileLen, 1, fp); // Read in the entire file
    fclose(fp); // Close

    CStringA Dest = this->ToBase64(buffer, fileLen); // Base64 encode

    // Now construct an HTTP request.
    CString requestHeaders = _T("Content-Type: application/x-www-form-urlencoded\r\nAuthorization: Client-ID 3c1a6553ba0bbe4");

    // Cast it to a CStringA which can then be used to convert to a char *
    CStringA postData("image=" + Dest);

    // Convert to char* otherwise the server drops requests, time_wasted = ~10 hours.
    const size_t newsizea = (postData.GetLength() + 1);
    char *postDataChar    = new char[newsizea];
    strcpy_s(postDataChar, newsizea, postData);

    // Spawn a session here
    CInternetSession session(NULL, 1, PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, 0);
    if(!session) return _EMPTY;

    // Look up for 10 seconds
    session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10000);
    session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
    INTERNET_PORT port = 443; // We are demanded to use HTTPS so

    // Open up the connection
    CHttpConnection *pConnection = session.GetHttpConnection(_T("api.imgur.com"), port);

    // Some self-explanatory flags.
    DWORD dwRequestFlags = INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_SECURE | INTERNET_DEFAULT_HTTPS_PORT;

    // Open up a connection
    CHttpFile *pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, _T("/3/image.xml"), NULL, 1, NULL, NULL, dwRequestFlags);
    pFile->AddRequestHeaders(requestHeaders);

    // Send the request
    BOOL result = pFile->SendRequest(requestHeaders, (LPVOID)postDataChar, strlen(postDataChar));
    DWORD retCode;
    pFile->QueryInfoStatusCode(retCode);
    if(retCode != HTTP_STATUS_OK) { // API Failed
        AfxMessageBox(_T("API did not return 200 OK. Request failed."));

        delete[] postDataChar;free(buffer);
        return _EMPTY;
    }

    // Grab the request now
    char *szBuff = new char[1024];
    pFile->Read(szBuff, 1024);

    // Check out if it's empty or not
    if(!szBuff) {
        AfxMessageBox(_T("Failed to retrieve a response."));

        delete []postDataChar;delete []szBuff;free(buffer);
        return _EMPTY;
    }

    // Construct this into a std string
    std::string response(szBuff);
    response = InBetween(response, "<link>", "</link>"); // Grab the direct link.

    // Cleanup
    delete[] szBuff;
    delete[] postDataChar;

    pFile->Close();
    delete pFile;
    delete pConnection;

    free(buffer);
    session.Close();

    return response;
}