예제 #1
0
size_t
UnZipBuffer (unsigned char *outbuffer)
{
	PKZIPHEADER pkzip;
	size_t zipoffset = 0;
	size_t zipchunk = 0;
	char out[ZIPCHUNK];
	z_stream zs;
	int res;
	size_t bufferoffset = 0;
	size_t have = 0;
	char readbuffer[ZIPCHUNK];
	size_t sizeread = 0;

	// Read Zip Header
	fseek(file, 0, SEEK_SET);
	sizeread = fread (readbuffer, 1, ZIPCHUNK, file);

	if(sizeread <= 0)
		return 0;

	/*** Copy PKZip header to local, used as info ***/
	memcpy (&pkzip, readbuffer, sizeof (PKZIPHEADER));

	pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);

	ShowProgress ("Loading...", 0, pkzip.uncompressedSize);

	/*** Prepare the zip stream ***/
	memset (&zs, 0, sizeof (z_stream));
	zs.zalloc = Z_NULL;
	zs.zfree = Z_NULL;
	zs.opaque = Z_NULL;
	zs.avail_in = 0;
	zs.next_in = Z_NULL;
	res = inflateInit2 (&zs, -MAX_WBITS);

	if (res != Z_OK)
		goto done;

	/*** Set ZipChunk for first pass ***/
	zipoffset =
	(sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) +
	FLIP16 (pkzip.extraDataLength));
	zipchunk = ZIPCHUNK - zipoffset;

	/*** Now do it! ***/
	do
	{
		zs.avail_in = zipchunk;
		zs.next_in = (Bytef *) & readbuffer[zipoffset];

		/*** Now inflate until input buffer is exhausted ***/
		do
		{
			zs.avail_out = ZIPCHUNK;
			zs.next_out = (Bytef *) & out;

			res = inflate (&zs, Z_NO_FLUSH);

			if (res == Z_MEM_ERROR)
			{
				goto done;
			}

			have = ZIPCHUNK - zs.avail_out;
			if (have)
			{
				/*** Copy to normal block buffer ***/
				memcpy (&outbuffer[bufferoffset], &out, have);
				bufferoffset += have;
			}
		}
		while (zs.avail_out == 0);

		// Readup the next 2k block
		zipoffset = 0;
		zipchunk = ZIPCHUNK;

		sizeread = fread (readbuffer, 1, ZIPCHUNK, file);
		if(sizeread <= 0)
			goto done; // read failure

		ShowProgress ("Loading...", bufferoffset, pkzip.uncompressedSize);
	}
	while (res != Z_STREAM_END);

done:
	inflateEnd (&zs);
	CancelAction();

	if (res == Z_STREAM_END)
		return pkzip.uncompressedSize;
	else
		return 0;
}
예제 #2
0
파일: net.cpp 프로젝트: dangordon1212/sdk
// handle WinHTTP callbacks (which can be in a worker thread context)
VOID CALLBACK WinHttpIO::asynccallback(HINTERNET hInternet, DWORD_PTR dwContext,
                                       DWORD dwInternetStatus,
                                       LPVOID lpvStatusInformation,
                                       DWORD dwStatusInformationLength)
{
    WinHttpContext* httpctx = (WinHttpContext*)dwContext;
    WinHttpIO* httpio = (WinHttpIO*)httpctx->httpio;

    if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING)
    {
        LOG_verbose << "Closing request";
        assert(!httpctx->req);

        if (httpctx->gzip)
        {
            inflateEnd(&httpctx->z);
        }
        
        delete httpctx;
        return;
    }

    httpio->lock();

    HttpReq* req = httpctx->req;

    // request cancellations that occured after asynccallback() was entered are caught here
    if (!req)
    {
        LOG_verbose << "Request cancelled";
        httpio->unlock();
        return;
    }

    switch (dwInternetStatus)
    {
        case WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE:
        {
            DWORD size = *(DWORD*)lpvStatusInformation;

            if (!size)
            {
                if (req->binary)
                {
                    LOG_debug << "[received " << (req->buf ? req->buflen : req->in.size()) << " bytes of raw data]";
                }
                else
                {
                    if(req->in.size() < 2048)
                    {
                        LOG_debug << "Received: " << req->in.c_str();
                    }
                    else
                    {
                        LOG_debug << "Received: " << req->in.substr(0,2048).c_str();
                    }
                }

                LOG_debug << "Request finished with HTTP status: " << req->httpstatus;

                req->status = req->httpstatus == 200 ? REQ_SUCCESS : REQ_FAILURE;
                httpio->success = true;
            }
            else
            {
                LOG_verbose << "Data available. Remaining: " << size << " bytes";

                char* ptr;

                if (httpctx->gzip)
                {                    
                    m_off_t zprevsize = httpctx->zin.size();
                    httpctx->zin.resize(zprevsize + size);
                    ptr = (char*)httpctx->zin.data() + zprevsize;
                }
                else
                {                    
                    ptr = (char*)req->reserveput((unsigned*)&size);
                    req->bufpos += size;
                }

                if (!WinHttpReadData(hInternet, ptr, size, NULL))
                {
                    LOG_err << "Unable to get more data. Code: " << GetLastError();
                    httpio->cancel(req);
                }
            }

            httpio->httpevent();
            break;
        }

        case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
            LOG_verbose << "Read complete";

            if (dwStatusInformationLength)
            {
                LOG_verbose << dwStatusInformationLength << " bytes received";
                if (req->httpio)
                {
                    req->httpio->lastdata = Waiter::ds;
                }
            
                if (httpctx->gzip)
                {                    
                    httpctx->z.next_in = (Bytef*)lpvStatusInformation;
                    httpctx->z.avail_in = dwStatusInformationLength;

                    req->bufpos += httpctx->z.avail_out;
                    int t = inflate(&httpctx->z, Z_SYNC_FLUSH);
                    req->bufpos -= httpctx->z.avail_out;

                    if ((char*)lpvStatusInformation + dwStatusInformationLength ==
                             httpctx->zin.data() + httpctx->zin.size())
                    {
                        httpctx->zin.clear();
                    }

                    if (t != Z_OK && (t != Z_STREAM_END || httpctx->z.avail_out))
                    {
                        LOG_err << "GZIP error";
                        httpio->cancel(req);
                    }
                }

                if (!WinHttpQueryDataAvailable(httpctx->hRequest, NULL))
                {
                    LOG_err << "Error on WinHttpQueryDataAvailable. Code: " << GetLastError();
                    httpio->cancel(req);
                    httpio->httpevent();
                }
            }
            break;

        case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
        {
            DWORD statusCode;
            DWORD statusCodeSize = sizeof(statusCode);

            if (!WinHttpQueryHeaders(httpctx->hRequest,
                                     WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
                                     WINHTTP_HEADER_NAME_BY_INDEX,
                                     &statusCode,
                                     &statusCodeSize,
                                     WINHTTP_NO_HEADER_INDEX))
            {
                LOG_err << "Error getting headers. Code: " << GetLastError();
                httpio->cancel(req);
                httpio->httpevent();
            }
            else
            {
                LOG_verbose << "Headers available";

                req->httpstatus = statusCode;

                if (req->httpio)
                {
                    req->httpio->lastdata = Waiter::ds;
                }

                if (!req->buf)
                {
                    // obtain original content length - always present if gzip is in use
                    DWORD contentLength;
                    DWORD contentLengthSize = sizeof(contentLength);

                    if (WinHttpQueryHeaders(httpctx->hRequest,
                                            WINHTTP_QUERY_CUSTOM | WINHTTP_QUERY_FLAG_NUMBER,
                                            L"Original-Content-Length",
                                            &contentLength,
                                            &contentLengthSize,
                                            WINHTTP_NO_HEADER_INDEX))
                    {
                        LOG_verbose << "Content-Length: " << contentLength;
                        req->setcontentlength(contentLength);

                        // check for gzip content encoding
                        WCHAR contentEncoding[16];
                        DWORD contentEncodingSize = sizeof(contentEncoding);

                        httpctx->gzip = WinHttpQueryHeaders(httpctx->hRequest,
                                                WINHTTP_QUERY_CONTENT_ENCODING,
                                                WINHTTP_HEADER_NAME_BY_INDEX,
                                                &contentEncoding,
                                                &contentEncodingSize,
                                                WINHTTP_NO_HEADER_INDEX)
                                    && !wcscmp(contentEncoding, L"gzip");

                        if (httpctx->gzip)
                        {
                            LOG_verbose << "Using GZIP";

                            httpctx->z.zalloc = Z_NULL;
                            httpctx->z.zfree = Z_NULL;
                            httpctx->z.opaque = Z_NULL;
                            httpctx->z.avail_in = 0;
                            httpctx->z.next_in = Z_NULL;

                            inflateInit2(&httpctx->z, MAX_WBITS+16);

                            req->in.resize(contentLength);
                            httpctx->z.avail_out = contentLength;
                            httpctx->z.next_out = (unsigned char*)req->in.data();
                        }
                        else
                        {
                            LOG_verbose << "Not using GZIP";
                        }
                    }
                    else
                    {
                        LOG_verbose << "Content-Length not available";
                    }
                }

                if (!WinHttpQueryDataAvailable(httpctx->hRequest, NULL))
                {
                    LOG_err << "Unable to query data. Code: " << GetLastError();

                    httpio->cancel(req);
                    httpio->httpevent();
                }
                else if (httpio->waiter && httpio->noinetds)
                {
                    httpio->inetstatus(true);
                }
            }

            break;
        }

        case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
        {
            DWORD e = GetLastError();

            LOG_err << "Request error. Code: " << e;

            if (httpio->waiter && e != ERROR_WINHTTP_TIMEOUT)
            {
                httpio->inetstatus(false);
            }
        }
        // fall through
        case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE:
            if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_SECURE_FAILURE)
            {
                LOG_err << "Security check failed. Code: " << (*(DWORD*)lpvStatusInformation);
            }

            httpio->cancel(req);
            httpio->httpevent();
            break;

        case WINHTTP_CALLBACK_STATUS_SENDING_REQUEST:
        {
            if(MegaClient::disablepkp)
            {
                break;
            }

            PCCERT_CONTEXT cert;
            DWORD len = sizeof cert;

            if (WinHttpQueryOption(httpctx->hRequest, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert, &len))
            {
                CRYPT_BIT_BLOB* pkey = &cert->pCertInfo->SubjectPublicKeyInfo.PublicKey;

                // this is an SSL connection: verify public key to prevent MITM attacks
                if (pkey->cbData != 270
                 || (memcmp(pkey->pbData, "\x30\x82\x01\x0a\x02\x82\x01\x01\x00" APISSLMODULUS1
                                          "\x02" APISSLEXPONENTSIZE APISSLEXPONENT, 270)
                  && memcmp(pkey->pbData, "\x30\x82\x01\x0a\x02\x82\x01\x01\x00" APISSLMODULUS2
                                          "\x02" APISSLEXPONENTSIZE APISSLEXPONENT, 270)))
                {
                    LOG_err << "Certificate error. Possible MITM attack!!";
                    CertFreeCertificateContext(cert);
                    httpio->cancel(req);
                    httpio->httpevent();
                    break;
                }

                CertFreeCertificateContext(cert);
            }
            else
            {
                LOG_verbose << "Unable to get server certificate. Code: " << GetLastError();
            }

            break;
        }

        case WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE:
        case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
            if (httpctx->postpos < httpctx->postlen)
            {
                LOG_verbose << "Chunk written";
                unsigned pos = httpctx->postpos;
                unsigned t = httpctx->postlen - pos;

                if (t > HTTP_POST_CHUNK_SIZE)
                {
                    t = HTTP_POST_CHUNK_SIZE;
                }

                httpctx->postpos += t;

                if (!WinHttpWriteData(httpctx->hRequest, (LPVOID)(httpctx->postdata + pos), t, NULL))
                {
                    LOG_err << "Error writting data. Code: " << GetLastError();
                    req->httpio->cancel(req);
                }

                httpio->httpevent();
            }
            else
            {
                LOG_verbose << "Request written";
                if (!WinHttpReceiveResponse(httpctx->hRequest, NULL))
                {
                    LOG_err << "Error receiving response. Code: " << GetLastError();
                    httpio->cancel(req);
                    httpio->httpevent();
                }

                httpctx->postdata = NULL;
            }
    }

    httpio->unlock();
}
예제 #3
0
파일: csw_cas.c 프로젝트: cdenix/psmame
static int csw_cas_to_wav_size( const UINT8 *casdata, int caslen ) {
UINT32 SampleRate;
UINT32 NumberOfPulses;
UINT8  MajorRevision;
UINT8  MinorRevision;
UINT8  CompressionType;
UINT8  Flags;
UINT8  HeaderExtensionLength;
UINT8 *gz_ptr = NULL;

int			total_size;
z_stream	d_stream;
int 		err;
UINT8		*in_ptr;
int 		bsize=0;

	if ( memcmp( casdata, CSW_HEADER, sizeof(CSW_HEADER) ) ) {
		logerror( "csw_cas_to_wav_size: cassette image has incompatible header\n" );
		goto cleanup;
	}

	if (casdata[0x16]!=0x1a) {
		logerror( "csw_cas_to_wav_size: Terminator Code Not Found\n" );
		goto cleanup;
	}

	MajorRevision=casdata[0x17];
	MinorRevision=casdata[0x18];

	logerror("Version %d : %d\n",MajorRevision,MinorRevision);

	if (casdata[0x17]!=2){
		logerror( "csw_cas_to_wav_size: Unsuported Major Version\n" );
		goto cleanup;
	}

	SampleRate=get_leuint32(casdata+0x19);
	logerror("Sample rate %d\n",SampleRate);

	NumberOfPulses=get_leuint32(casdata+0x1d);
	logerror("Number Of Pulses %d\n",NumberOfPulses);


	CompressionType=casdata[0x21];
	Flags=casdata[0x22];
	HeaderExtensionLength=casdata[0x23];

	logerror("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);

	mycaslen=caslen;
	//from here on down for now I am assuming it is compressed csw file.
	in_ptr = (UINT8*) casdata+0x34+HeaderExtensionLength;

	gz_ptr = (UINT8*)malloc( 8 );


	d_stream.next_in = (unsigned char *)in_ptr;
	d_stream.avail_in = caslen - ( in_ptr - casdata );
	d_stream.total_in=0;

	d_stream.next_out = gz_ptr;
	d_stream.avail_out = 1;
	d_stream.total_out=0;

	d_stream.zalloc = 0;
	d_stream.zfree = 0;
	d_stream.opaque = 0;
	d_stream.data_type=0;

	err = inflateInit( &d_stream );
	if ( err != Z_OK ) {
		logerror( "inflateInit2 error: %d\n", err );
		goto cleanup;
	}


	total_size=1;
	do
	{
		d_stream.next_out = gz_ptr;
		d_stream.avail_out=1;
		err=inflate( &d_stream, Z_SYNC_FLUSH );
		if (err==Z_OK) {
			bsize=gz_ptr[0];
			if (bsize==0) {
				d_stream.avail_out=4;
				d_stream.next_out = gz_ptr;
				err=inflate( &d_stream, Z_SYNC_FLUSH );
				bsize=get_leuint32(gz_ptr);
			}
			total_size=total_size+bsize;
		}
	}
	while (err==Z_OK);

	if ( err != Z_STREAM_END ) {
		logerror( "inflate error: %d\n", err );
		goto cleanup;
	}

	err = inflateEnd( &d_stream );
	if ( err != Z_OK ) {
		logerror( "inflateEnd error: %d\n", err );
		goto cleanup;
	}






	if ( gz_ptr ) {
		free( gz_ptr );
		gz_ptr = NULL;
	}



	return total_size;

cleanup:
	if ( gz_ptr ) {
		free( gz_ptr );
		gz_ptr = NULL;
	}
	return -1;

}
예제 #4
0
//---------------------------------------------------------------------------
FileInformation::FileInformation (MainWindow* Main_, const QString &FileName_, activefilters ActiveFilters_, activealltracks ActiveAllTracks_, BlackmagicDeckLink_Glue* blackmagicDeckLink_Glue_, int FrameCount, const string &Encoding_FileName, const std::string &Encoding_Format) :
    FileName(FileName_),
    ActiveFilters(ActiveFilters_),
    ActiveAllTracks(ActiveAllTracks_),
    Main(Main_),
    blackmagicDeckLink_Glue(blackmagicDeckLink_Glue_)
{
    QString StatsFromExternalData_FileName;
    bool StatsFromExternalData_FileName_IsCompressed=false;

    // Finding the right file names (both media file and stats file)
    if (FileName.indexOf(".qctools.xml.gz")==FileName.length()-15)
    {
        StatsFromExternalData_FileName=FileName;
        FileName.resize(FileName.length()-15);
        StatsFromExternalData_FileName_IsCompressed=true;
    }
    else if (FileName.indexOf(".qctools.xml")==FileName.length()-12)
    {
        StatsFromExternalData_FileName=FileName;
        FileName.resize(FileName.length()-12);
    }
    else if (FileName.indexOf(".xml.gz")==FileName.length()-7)
    {
        StatsFromExternalData_FileName=FileName;
        FileName.resize(FileName.length()-7);
        StatsFromExternalData_FileName_IsCompressed=true;
    }
    else if (FileName.indexOf(".xml")==FileName.length()-4)
    {
        StatsFromExternalData_FileName=FileName;
        FileName.resize(FileName.length()-4);
    }
    if (StatsFromExternalData_FileName.size()==0)
    {
        if (QFile::exists(FileName+".qctools.xml.gz"))
        {
            StatsFromExternalData_FileName=FileName+".qctools.xml.gz";
            StatsFromExternalData_FileName_IsCompressed=true;
        }
        else if (QFile::exists(FileName+".qctools.xml"))
        {
            StatsFromExternalData_FileName=FileName+".qctools.xml";
        }
        else if (QFile::exists(FileName+".xml.gz"))
        {
            StatsFromExternalData_FileName=FileName+".xml.gz";
            StatsFromExternalData_FileName_IsCompressed=true;
        }
        else if (QFile::exists(FileName+".xml"))
        {
            StatsFromExternalData_FileName=FileName+".xml";
        }
    }

    // External data optional input
    string StatsFromExternalData;
    if (!StatsFromExternalData_FileName.size()==0)
    {
        if (StatsFromExternalData_FileName_IsCompressed)
        {
            QFile F(StatsFromExternalData_FileName);
            if (F.open(QIODevice::ReadOnly))
            {
                QByteArray Compressed=F.readAll();
                uLongf Buffer_Size=0;
                Buffer_Size|=((unsigned char)Compressed[Compressed.size()-1])<<24;
                Buffer_Size|=((unsigned char)Compressed[Compressed.size()-2])<<16;
                Buffer_Size|=((unsigned char)Compressed[Compressed.size()-3])<<8;
                Buffer_Size|=((unsigned char)Compressed[Compressed.size()-4]);
                char* Buffer=new char[Buffer_Size];
                z_stream strm;
                strm.next_in = (Bytef *) Compressed.data();
                strm.avail_in = Compressed.size() ;
                strm.next_out = (unsigned char*) Buffer;
                strm.avail_out = Buffer_Size;
                strm.total_out = 0;
                strm.zalloc = Z_NULL;
                strm.zfree = Z_NULL;
                if (inflateInit2(&strm, 15 + 16)>=0) // 15 + 16 are magic values for gzip
                {
                    if (inflate(&strm, Z_SYNC_FLUSH)>=0)
                    {
                        inflateEnd (&strm);
                        StatsFromExternalData.assign(Buffer, Buffer_Size);
                    }
                }
                delete[] Buffer;
            }
        }
        else
        {
            QFile F(StatsFromExternalData_FileName);
            if (F.open(QIODevice::ReadOnly))
            {
                QByteArray Data=F.readAll();

                StatsFromExternalData.assign(Data.data(), Data.size());
            }
        }
    }

    // Running FFmpeg
    string FileName_string=FileName.toUtf8().data();
    #ifdef _WIN32
        replace(FileName_string.begin(), FileName_string.end(), '/', '\\' );
    #endif
    string Filters[Type_Max];
    if (StatsFromExternalData.empty())
    {
        if (ActiveFilters[ActiveFilter_Video_signalstats])
            Filters[0]+=",signalstats=stat=tout+vrep+brng";
        if (ActiveFilters[ActiveFilter_Video_cropdetect])
            Filters[0]+=",cropdetect=reset=1:round=1";
        if (ActiveFilters[ActiveFilter_Video_Psnr])
            Filters[0]+=",split[a][b];[a]field=top[a1];[b]field=bottom[b1],[a1][b1]psnr";
        Filters[0].erase(0, 1); // remove first comma
        if (ActiveFilters[ActiveFilter_Audio_EbuR128])
            Filters[1]+=",ebur128=metadata=1";
        if (ActiveFilters[ActiveFilter_Audio_astats])
            Filters[1]+=",astats=metadata=1:reset=1:length=0.4";
        Filters[1].erase(0, 1); // remove first comma
    }
    else
    {
        VideoStats* Video=new VideoStats();
        Stats.push_back(Video);
        Video->StatsFromExternalData(StatsFromExternalData);

        AudioStats* Audio=new AudioStats();
        Stats.push_back(Audio);
        Audio->StatsFromExternalData(StatsFromExternalData);
    }
    Glue=new FFmpeg_Glue(FileName_string.c_str(), ActiveAllTracks, &Stats, Stats.empty());
    if (FileName_string.empty())
    {
        Glue->AddInput_Video(FrameCount, 1001, 30000, 720, 486, blackmagicDeckLink_Glue->Config_In.VideoBitDepth, blackmagicDeckLink_Glue->Config_In.VideoCompression, blackmagicDeckLink_Glue->Config_In.TC_in);
        Glue->AddInput_Audio(FrameCount, 1001, 30000, 48000, blackmagicDeckLink_Glue->Config_In.AudioBitDepth, blackmagicDeckLink_Glue->Config_In.AudioTargetBitDepth, blackmagicDeckLink_Glue->Config_In.ChannelsCount);
    }
    else if (Glue->ContainerFormat_Get().empty())
    {
        delete Glue;
        Glue=NULL;
        for (size_t Pos=0; Pos<Stats.size(); Pos++)
            Stats[Pos]->StatsFinish();
    }

    if (Glue)
    {
        Glue->AddOutput(0, 72, 72, FFmpeg_Glue::Output_Jpeg);
        if (!Encoding_FileName.empty())
        {
            Glue->AddOutput(Encoding_FileName, Encoding_Format);
            FileName=QString::fromUtf8(Encoding_FileName.c_str());
        }
        Glue->AddOutput(1, 0, 0, FFmpeg_Glue::Output_Stats, 0, Filters[0]);
        Glue->AddOutput(0, 0, 0, FFmpeg_Glue::Output_Stats, 1, Filters[1]);
    }

    // Looking for the first video stream
    ReferenceStream_Pos=0;
    for (; ReferenceStream_Pos<Stats.size(); ReferenceStream_Pos++)
        if (Stats[ReferenceStream_Pos] && Stats[ReferenceStream_Pos]->Type_Get()==0)
            break;
    if (ReferenceStream_Pos>=Stats.size())
        ReferenceStream_Pos=0;

    Frames_Pos=0;
    WantToStop=false;
    Parse();
}
예제 #5
0
File* ZipArchive::inflateToDisk(const std::string& outFileName, const size_t compressedSize,
        const size_t uncompressedSize, size_t archiveOffset)
{
    if (checkFileHandleValid() == false)
        return 0;

    // Prepare z_stream structure for inflating
    //
    z_stream strm;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.avail_in = 0;
    strm.next_in = Z_NULL;
    strm.avail_out = 0;
    strm.next_out = Z_NULL;

    // Initialize inflate:
    //
    // NOTE: inflateInit2() is a badly documented macro. The second parameter ("windowBits")
    // suppresses header and CRC32 check by the inflate() function if it is negative!
    //
    if (inflateInit2(&strm, -15) != Z_OK) {
        LERROR("inflateToDisk(): call to zlib function inflateInit() failed!");
        return 0;
    }

    std::ofstream ofs(outFileName.c_str(), std::ios_base::binary | std::ios_base::out);
    if (ofs.fail()) {
        LERROR("failed to open file '" << outFileName << "' for writing decompressed data!");
        ofs.close();
        return 0;
    }

    // Allocate buffers for in- and output
    //
    size_t inBufferSize = (compressedSize < MAX_BUFFER_SIZE) ? compressedSize : MAX_BUFFER_SIZE;
    size_t outBufferSize = (uncompressedSize < MAX_BUFFER_SIZE) ? uncompressedSize : MAX_BUFFER_SIZE;
    char* inBuffer = 0;
    char* outBuffer = 0;

    try {
        inBuffer = new char[inBufferSize];
    } catch (std::bad_alloc&) {
        LERROR("inflateToMemory(): failed to allocate " << inBufferSize << " Bytes for input buffer!");
        return 0;
    }

    try {
        outBuffer = new char[outBufferSize];
    } catch (std::bad_alloc&) {
        LERROR("inflateToMemory(): failed to allocate " << outBufferSize << " Bytes for output buffer!");
        delete [] inBuffer;
        return 0;
    }

    size_t readTotal = 0;
    size_t writtenTotal = 0;
    archive_->seek(archiveOffset, File::BEGIN);
    do {
        // If the number of bytes left to read becomes smaller than the buffer's size
        // reduce the number of bytes to be read.
        //
        size_t left = compressedSize - readTotal;
        if (left < inBufferSize)
            inBufferSize = left;

        size_t read = archive_->read(inBuffer, inBufferSize);
        readTotal += read;
        strm.avail_in = static_cast<uInt>(read);
        strm.next_in = reinterpret_cast<Bytef*>(inBuffer);

        do {
            strm.avail_out = static_cast<uInt>(outBufferSize);
            strm.next_out = reinterpret_cast<Bytef*>(outBuffer);
            int res = inflate(&strm, Z_NO_FLUSH);
            if ((res == Z_OK) || (res == Z_STREAM_END)) {
                size_t written = outBufferSize - strm.avail_out;
                ofs.write(outBuffer, written);
                if (ofs.fail() == true) {
                    LERROR("Failed to write to output file '" << outFileName <<"'!");
                    break;
                }
                writtenTotal += written;
            } else
                break;
        } while ((strm.avail_out == 0) && (writtenTotal < uncompressedSize));
    } while ((archive_->eof() == false) && (readTotal < compressedSize));

    inflateEnd(&strm);
    delete [] inBuffer;
    delete [] outBuffer;
    ofs.close();

    return new RegularFile(outFileName);
}
예제 #6
0
NS_CC_BEGIN
    
// --------------------- ZipUtils ---------------------
// memory in iPhone is precious
// Should buffer factor be 1.5 instead of 2 ?
#define BUFFER_INC_FACTOR (2)

KDint ZipUtils::ccInflateMemoryWithHint ( KDubyte* pSrc, KDuint uLenSrc, KDubyte** ppDst, KDuint* pLenDst, KDuint uLenHintDst )
{
	// ret value 
	KDint  nErr = Z_OK;

	KDint  nBufferSize = uLenHintDst;
	*ppDst = new KDubyte [ nBufferSize ];

	z_stream d_stream; // decompression stream
	d_stream.zalloc = (alloc_func) 0;
	d_stream.zfree  = (free_func) 0;
	d_stream.opaque = (voidpf) 0;

	d_stream.next_in   = pSrc;
	d_stream.avail_in  = uLenSrc;
	d_stream.next_out  = *ppDst;
	d_stream.avail_out = nBufferSize;

	// window size to hold 256k
	if ( ( nErr = inflateInit2 ( &d_stream, 15 + 32 ) ) != Z_OK )
	{
		return nErr;
	}

	for ( ; ; ) 
	{
		nErr = inflate ( &d_stream, Z_NO_FLUSH );

		if ( nErr == Z_STREAM_END )
		{
			break;
		}

		switch ( nErr ) 
		{
			case Z_NEED_DICT :
				nErr = Z_DATA_ERROR;
			case Z_DATA_ERROR :
			case Z_MEM_ERROR :
				inflateEnd ( &d_stream );
				return nErr;
	    }

		// not enough memory ?
		if ( nErr != Z_STREAM_END ) 
		{
            delete[] *ppDst;
            *ppDst = new KDubyte [ nBufferSize * BUFFER_INC_FACTOR ];

			// not enough memory, ouch
			if ( !*ppDst ) 
			{
				CCLOG ( "XMCocos2D : ZipUtils - realloc failed" );
				inflateEnd ( &d_stream );
				return Z_MEM_ERROR;
			}

			d_stream.next_out = *ppDst + nBufferSize;
			d_stream.avail_out = nBufferSize;
			nBufferSize *= BUFFER_INC_FACTOR;
		}
	}

	*pLenDst = nBufferSize - d_stream.avail_out;
	nErr = inflateEnd ( &d_stream );
	return nErr;
}
예제 #7
0
static php_stream_filter_status_t php_zlib_inflate_filter(
	php_stream *stream,
	php_stream_filter *thisfilter,
	php_stream_bucket_brigade *buckets_in,
	php_stream_bucket_brigade *buckets_out,
	size_t *bytes_consumed,
	int flags
	TSRMLS_DC)
{
	php_zlib_filter_data *data;
	php_stream_bucket *bucket;
	size_t consumed = 0, original_out, original_in;
	int status;
	php_stream_filter_status_t exit_status = PSFS_FEED_ME;
	z_stream *streamp;

	if (!thisfilter || !thisfilter->abstract) {
		/* Should never happen */
		return PSFS_ERR_FATAL;
	}

	data = (php_zlib_filter_data *)(thisfilter->abstract);
	streamp = &(data->strm);
	original_in = data->strm.total_in;
	original_out = data->strm.total_out;

	while (buckets_in->head) {
		size_t bin = 0, desired;

		bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
		while (bin < bucket->buflen) {

			if (data->finished) {
				consumed += bucket->buflen;
				break;
			}

			desired = bucket->buflen - bin;
			if (desired > data->inbuf_len) {
				desired = data->inbuf_len;
			}
			memcpy(data->strm.next_in, bucket->buf + bin, desired);
			data->strm.avail_in = desired;

			status = inflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FINISH : Z_SYNC_FLUSH);
			if (status == Z_STREAM_END) {
				inflateEnd(&(data->strm));
				data->finished = '\1';
			} else if (status != Z_OK) {
				/* Something bad happened */
				php_stream_bucket_delref(bucket TSRMLS_CC);
				return PSFS_ERR_FATAL;
			}
			desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
			data->strm.next_in = data->inbuf;
			data->strm.avail_in = 0;
			consumed += desired;
			bin += desired;

			if (data->strm.avail_out < data->outbuf_len) {
				php_stream_bucket *out_bucket;
				size_t bucketlen = data->outbuf_len - data->strm.avail_out;
				out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
				php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
				data->strm.avail_out = data->outbuf_len;
				data->strm.next_out = data->outbuf;
				exit_status = PSFS_PASS_ON;
			} else if (status == Z_STREAM_END && data->strm.avail_out >= data->outbuf_len) {
				/* no more data to decompress, and nothing was spat out */
				php_stream_bucket_delref(bucket TSRMLS_CC);
				return PSFS_PASS_ON;
			}
		}
		php_stream_bucket_delref(bucket TSRMLS_CC);
	}

	if (!data->finished && flags & PSFS_FLAG_FLUSH_CLOSE) {
		/* Spit it out! */
		status = Z_OK;
		while (status == Z_OK) {
			status = inflate(&(data->strm), Z_FINISH);
			if (data->strm.avail_out < data->outbuf_len) {
				size_t bucketlen = data->outbuf_len - data->strm.avail_out;

				bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
				php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
				data->strm.avail_out = data->outbuf_len;
				data->strm.next_out = data->outbuf;
				exit_status = PSFS_PASS_ON;
			}
		}
	}

	if (bytes_consumed) {
		*bytes_consumed = consumed;
	}

	return exit_status;
}
예제 #8
0
파일: corefile.c 프로젝트: andysarcade/mame
file_error core_fcompress(core_file *file, int level)
{
	file_error result = FILERR_NONE;

	/* can only do this for read-only and write-only cases */
	if ((file->openflags & OPEN_FLAG_WRITE) != 0 && (file->openflags & OPEN_FLAG_READ) != 0)
		return FILERR_INVALID_ACCESS;

	/* if we have been compressing, flush and free the data */
	if (file->zdata != NULL && level == FCOMPRESS_NONE)
	{
		int zerr = Z_OK;

		/* flush any remaining data if we are writing */
		while ((file->openflags & OPEN_FLAG_WRITE) != 0 && zerr != Z_STREAM_END)
		{
			UINT32 actualdata;
			file_error filerr;

			/* deflate some more */
			zerr = deflate(&file->zdata->stream, Z_FINISH);
			if (zerr != Z_STREAM_END && zerr != Z_OK)
			{
				result = FILERR_INVALID_DATA;
				break;
			}

			/* write the data */
			if (file->zdata->stream.avail_out != sizeof(file->zdata->buffer))
			{
				filerr = osd_write(file->file, file->zdata->buffer, file->zdata->realoffset, sizeof(file->zdata->buffer) - file->zdata->stream.avail_out, &actualdata);
				if (filerr != FILERR_NONE)
					break;
				file->zdata->realoffset += actualdata;
				file->zdata->stream.next_out = file->zdata->buffer;
				file->zdata->stream.avail_out = sizeof(file->zdata->buffer);
			}
		}

		/* end the appropriate operation */
		if ((file->openflags & OPEN_FLAG_WRITE) != 0)
			deflateEnd(&file->zdata->stream);
		else
			inflateEnd(&file->zdata->stream);

		/* free memory */
		free(file->zdata);
		file->zdata = NULL;
	}

	/* if we are just starting to compress, allocate a new buffer */
	if (file->zdata == NULL && level > FCOMPRESS_NONE)
	{
		int zerr;

		/* allocate memory */
		file->zdata = (zlib_data *)malloc(sizeof(*file->zdata));
		if (file->zdata == NULL)
			return FILERR_OUT_OF_MEMORY;
		memset(file->zdata, 0, sizeof(*file->zdata));

		/* initialize the stream and compressor */
		if ((file->openflags & OPEN_FLAG_WRITE) != 0)
		{
			file->zdata->stream.next_out = file->zdata->buffer;
			file->zdata->stream.avail_out = sizeof(file->zdata->buffer);
			zerr = deflateInit(&file->zdata->stream, level);
		}
		else
			zerr = inflateInit(&file->zdata->stream);

		/* on error, return an error */
		if (zerr != Z_OK)
		{
			free(file->zdata);
			file->zdata = NULL;
			return FILERR_OUT_OF_MEMORY;
		}

		/* flush buffers */
		file->bufferbytes = 0;

		/* set the initial offset */
		file->zdata->realoffset = file->offset;
		file->zdata->nextoffset = file->offset;
	}

	return result;
}
예제 #9
0
int gzip68_buffer(void * dst, int dsize, const void * src, int csize)
{
  const struct {
    byte magic[2]; /* gz header                */
    byte method;   /* method (Z_DEFLATED)      */
    byte flags;    /* Xtra,name,comment...     */
    byte info[6];  /* time, xflags and OS code */
  } * header = src;
  int err = -(!dst || !src || csize < sizeof(*header) || dsize <= 0);
  byte crc[2];
  z_stream c_stream;

  for (;;) {
    int len = sizeof(*header), flags;
    byte * d = (byte *)dst;
    const byte * s = (const byte *) src;

    /* check gzip header */
    if (0
        || gz_magic[0] != header->magic[0]
        || gz_magic[1] != header->magic[1]
        || header->method != Z_DEFLATED
        || (header->flags & RESERVED) != 0) {
      err = -1;
      break;
    }

    /* Skip the extra field. */
    if ((header->flags & EXTRA_FIELD) != 0) {
      if (len + 2 < csize)
        len += s[len] + (s[len+1]<<8);
      len += 2;
    }

    /* Skip the original file name & .gz file comment. */
    flags = header->flags & (ORIG_NAME|COMMENT);
    while (len < csize && flags) {
      if (flags & ORIG_NAME) {
        flags &= ~ORIG_NAME;
      } else {
        flags &= ~COMMENT;
      }
      while (len < csize && !!s[len++])
        ;
    }

    /* Skip the header crc */
    if (header->flags & HEAD_CRC) {
      if (len + 2 < csize) {
        crc[0] = s[len+0];
        crc[1] = s[len+1];
      } else {
        crc[0] = crc[1] = 0;
      }
      len += 2;
    }

    /* Deflate now  */
    if (len < csize) {
      const int z_flush_mode = Z_FINISH;

      memset(&c_stream,0,sizeof(c_stream));
      err = -(Z_OK != inflateInit2(&c_stream, -MAX_WBITS));
      if (err)
        break;

      c_stream.next_in   = (byte *)(s + len);
      c_stream.avail_in  = csize - len;
      c_stream.next_out  = d;
      c_stream.avail_out = dsize;
      err = inflate(&c_stream, z_flush_mode);
      inflateEnd(&c_stream);
      if (err != Z_STREAM_END) {
        err = -1;
      } else {
        err = c_stream.total_out;
      }
    } else {
      err = -1;
    }
    break;
  }

  return err;
}
예제 #10
0
QByteArray qt_inflateGZipDataFrom(QIODevice *device)
{
    if (!device)
        return QByteArray();

    if (!device->isOpen())
        device->open(QIODevice::ReadOnly);

    Q_ASSERT(device->isOpen() && device->isReadable());

    static const int CHUNK_SIZE = 4096;
    int zlibResult = Z_OK;

    QByteArray source;
    QByteArray destination;

    // Initialize zlib stream struct
    z_stream zlibStream;
    zlibStream.next_in = Z_NULL;
    zlibStream.avail_in = 0;
    zlibStream.avail_out = 0;
    zlibStream.zalloc = Z_NULL;
    zlibStream.zfree = Z_NULL;
    zlibStream.opaque = Z_NULL;

    // Adding 16 to the window size gives us gzip decoding
    if (inflateInit2(&zlibStream, MAX_WBITS + 16) != Z_OK) {
        qWarning("Cannot initialize zlib, because: %s",
                (zlibStream.msg != NULL ? zlibStream.msg : "Unknown error"));
        return QByteArray();
    }

    bool stillMoreWorkToDo = true;
    while (stillMoreWorkToDo) {

        if (!zlibStream.avail_in) {
            source = device->read(CHUNK_SIZE);

            if (source.isEmpty())
                break;

            zlibStream.avail_in = source.size();
            zlibStream.next_in = reinterpret_cast<Bytef*>(source.data());
        }

        do {
            // Prepare the destination buffer
            int oldSize = destination.size();
            destination.resize(oldSize + CHUNK_SIZE);
            zlibStream.next_out = reinterpret_cast<Bytef*>(
                    destination.data() + oldSize - zlibStream.avail_out);
            zlibStream.avail_out += CHUNK_SIZE;

            zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
            switch (zlibResult) {
                case Z_NEED_DICT:
                case Z_DATA_ERROR:
                case Z_STREAM_ERROR:
                case Z_MEM_ERROR: {
                    inflateEnd(&zlibStream);
                    qWarning("Error while inflating gzip file: %s",
                            (zlibStream.msg != NULL ? zlibStream.msg : "Unknown error"));
                    destination.chop(zlibStream.avail_out);
                    return destination;
                }
            }

        // If the output buffer still has more room after calling inflate
        // it means we have to provide more data, so exit the loop here
        } while (!zlibStream.avail_out);

        if (zlibResult == Z_STREAM_END) {
            // Make sure there are no more members to process before exiting
            if (!(zlibStream.avail_in && inflateReset(&zlibStream) == Z_OK))
                stillMoreWorkToDo = false;
        }
    }

    // Chop off trailing space in the buffer
    destination.chop(zlibStream.avail_out);

    inflateEnd(&zlibStream);
    return destination;
}
static void Inflater_endImpl(JNIEnv*, jobject, jlong handle) {
    NativeZipStream* stream = toNativeZipStream(handle);
    inflateEnd(&stream->stream);
    delete stream;
}
예제 #12
0
void zlib_stream_free(void *data)
{
   z_stream *ret = (z_stream*)data;
   if (ret)
      inflateEnd(ret);
}
예제 #13
0
파일: inf.c 프로젝트: 4179e1/misc
/* decompress */
int inf(int source, int dest, int chunk)
{
	int ret;
	unsigned have;
	z_stream strm;
	unsigned char in[chunk];
	unsigned char out[chunk];
	int flush;

	/* allocate inflate state */
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.avail_in = 0;
	strm.next_in = Z_NULL;
	ret = inflateInit (&strm);
	if (ret != Z_OK)
	{
		return ret;
	}ssize_t n;

	/* decompress until deflate stream ends or end of file */
	do
	{

		flush = Z_SYNC_FLUSH;
		n = readn (source, in, chunk);
		if (n == -1)
		{
			(void)inflateEnd (&strm);
			return Z_ERRNO;
		}
		else if (n == 0)
		{
			flush = Z_FINISH;
		}
		strm.avail_in = n;
		strm.next_in = in;

		/* run inflate() on input until output buffer not full */
		do
		{
			strm.avail_out = chunk;
			strm.next_out = out;
			ret = inflate (&strm, flush);
			assert (ret != Z_STREAM_ERROR);
			switch (ret)
			{
				case Z_NEED_DICT:
					ret = Z_DATA_ERROR; /* and fall through */
				case Z_DATA_ERROR:
				case Z_MEM_ERROR:
					(void) inflateEnd (&strm);
					return ret;
			}

			have = chunk - strm.avail_out;
			if (write (dest, out, have) != have)
			{
				fprintf (stderr, "write() error");
				(void)inflateEnd (&strm);
				return Z_ERRNO;
			}
		} while (strm.avail_out == 0);


	}while (flush != Z_FINISH);

	(void)inflateEnd (&strm);

	/* clean up and return */
	return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
예제 #14
0
파일: ZLib.cpp 프로젝트: cobrajs/hxcpp
static void free_stream_inf( value v ) {
	z_stream *s = val_stream(v);
	inflateEnd(s); // no error
	free(s);
	val_gc(v,NULL);
}
예제 #15
0
/* free all memory used by the read (old method) */
void /* PRIVATE */
png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
{
#ifdef PNG_SETJMP_SUPPORTED
   jmp_buf tmp_jmp;
#endif
   png_error_ptr error_fn;
   png_error_ptr warning_fn;
   png_voidp error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif

   png_debug(1, "in png_read_destroy\n");
   if (info_ptr != NULL)
      png_info_destroy(png_ptr, info_ptr);

   if (end_info_ptr != NULL)
      png_info_destroy(png_ptr, end_info_ptr);

   png_free(png_ptr, png_ptr->zbuf);
   png_free(png_ptr, png_ptr->big_row_buf);
   png_free(png_ptr, png_ptr->prev_row);
#if defined(PNG_READ_DITHER_SUPPORTED)
   png_free(png_ptr, png_ptr->palette_lookup);
   png_free(png_ptr, png_ptr->dither_index);
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
   png_free(png_ptr, png_ptr->gamma_table);
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   png_free(png_ptr, png_ptr->gamma_from_1);
   png_free(png_ptr, png_ptr->gamma_to_1);
#endif
#ifdef PNG_FREE_ME_SUPPORTED
   if (png_ptr->free_me & PNG_FREE_PLTE)
      png_zfree(png_ptr, png_ptr->palette);
   png_ptr->free_me &= ~PNG_FREE_PLTE;
#else
   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
      png_zfree(png_ptr, png_ptr->palette);
   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
#endif
#if defined(PNG_tRNS_SUPPORTED) || \
    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
   if (png_ptr->free_me & PNG_FREE_TRNS)
      png_free(png_ptr, png_ptr->trans);
   png_ptr->free_me &= ~PNG_FREE_TRNS;
#else
   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
      png_free(png_ptr, png_ptr->trans);
   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
#endif
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
#ifdef PNG_FREE_ME_SUPPORTED
   if (png_ptr->free_me & PNG_FREE_HIST)
      png_free(png_ptr, png_ptr->hist);
   png_ptr->free_me &= ~PNG_FREE_HIST;
#else
   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
      png_free(png_ptr, png_ptr->hist);
   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
#endif
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
   if (png_ptr->gamma_16_table != NULL)
   {
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
      for (i = 0; i < istop; i++)
      {
         png_free(png_ptr, png_ptr->gamma_16_table[i]);
      }
   png_free(png_ptr, png_ptr->gamma_16_table);
   }
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if (png_ptr->gamma_16_from_1 != NULL)
   {
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
      for (i = 0; i < istop; i++)
      {
         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
      }
   png_free(png_ptr, png_ptr->gamma_16_from_1);
   }
   if (png_ptr->gamma_16_to_1 != NULL)
   {
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
      for (i = 0; i < istop; i++)
      {
         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
      }
   png_free(png_ptr, png_ptr->gamma_16_to_1);
   }
#endif
#endif
#if defined(PNG_TIME_RFC1123_SUPPORTED)
   png_free(png_ptr, png_ptr->time_buffer);
#endif

   inflateEnd(&png_ptr->zstream);
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   png_free(png_ptr, png_ptr->save_buffer);
#endif

#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
#ifdef PNG_TEXT_SUPPORTED
   png_free(png_ptr, png_ptr->current_text);
#endif /* PNG_TEXT_SUPPORTED */
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */

   /* Save the important info out of the png_struct, in case it is
    * being used again.
    */
#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
#endif

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif

   png_memset(png_ptr, 0, sizeof (png_struct));

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif

#ifdef PNG_SETJMP_SUPPORTED
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
#endif

}
예제 #16
0
    char *Util::DecompressGZIP(const char *data, int dataSize, int expectedSize) 
    {
        int bufferSize = expectedSize;
        int ret;
        z_stream strm;
        char *out = (char*)malloc(bufferSize);

        strm.zalloc = Z_NULL;
        strm.zfree = Z_NULL;
        strm.opaque = Z_NULL;
        strm.next_in = (Bytef*)data;
        strm.avail_in = dataSize;
        strm.next_out = (Bytef*)out;
        strm.avail_out = bufferSize;

        ret = inflateInit2(&strm, 15 + 32);

        if (ret != Z_OK) 
        {
            free(out);
            return NULL;
        }

        do 
        {
            ret = inflate(&strm, Z_SYNC_FLUSH);

            switch (ret) 
            {
                case Z_NEED_DICT:
                case Z_STREAM_ERROR:
                    ret = Z_DATA_ERROR;
                case Z_DATA_ERROR:
                case Z_MEM_ERROR:
                    inflateEnd(&strm);
                    free(out);
                    return NULL;
            }

            if (ret != Z_STREAM_END) 
            {
                out = (char *) realloc(out, bufferSize * 2);

                if (!out) 
                {
                    inflateEnd(&strm);
                    free(out);
                    return NULL;
                }

                strm.next_out = (Bytef *)(out + bufferSize);
                strm.avail_out = bufferSize;
                bufferSize *= 2;
            }
        }
        while (ret != Z_STREAM_END);

        if (strm.avail_in != 0) 
        {
            free(out);
            return NULL;
        }

        inflateEnd(&strm);

        return out;
    }
예제 #17
0
파일: torgzip.c 프로젝트: Lab414/30May
/** Given zero or more zlib-compressed or gzip-compressed strings of
 * total length
 * <b>in_len</b> bytes at <b>in</b>, uncompress them into a newly allocated
 * buffer, using the method described in <b>method</b>.  Store the uncompressed
 * string in *<b>out</b>, and its length in *<b>out_len</b>.  Return 0 on
 * success, -1 on failure.
 *
 * If <b>complete_only</b> is true, we consider a truncated input as a
 * failure; otherwise we decompress as much as we can.  Warn about truncated
 * or corrupt inputs at <b>protocol_warn_level</b>.
 */
int
tor_gzip_uncompress(char **out, size_t *out_len,
                    const char *in, size_t in_len,
                    compress_method_t method,
                    int complete_only,
                    int protocol_warn_level)
{
  struct z_stream_s *stream = NULL;
  size_t out_size, old_size;
  off_t offset;
  int r;

  tor_assert(out);
  tor_assert(out_len);
  tor_assert(in);
  tor_assert(in_len < UINT_MAX);

  if (method == GZIP_METHOD && !is_gzip_supported()) {
    /* Old zlib version don't support gzip in inflateInit2 */
    log_warn(LD_BUG, "Gzip not supported with zlib %s", ZLIB_VERSION);
    return -1;
  }

  *out = NULL;

  stream = tor_malloc_zero(sizeof(struct z_stream_s));
  stream->zalloc = Z_NULL;
  stream->zfree = Z_NULL;
  stream->opaque = NULL;
  stream->next_in = (unsigned char*) in;
  stream->avail_in = (unsigned int)in_len;

  if (inflateInit2(stream,
                   method_bits(method)) != Z_OK) {
    log_warn(LD_GENERAL, "Error from inflateInit2: %s",
             stream->msg?stream->msg:"<no message>");
    goto err;
  }

  out_size = in_len * 2;  /* guess 50% compression. */
  if (out_size < 1024) out_size = 1024;
  if (out_size >= SIZE_T_CEILING || out_size > UINT_MAX)
    goto err;

  *out = tor_malloc(out_size);
  stream->next_out = (unsigned char*)*out;
  stream->avail_out = (unsigned int)out_size;

  while (1) {
    switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH))
      {
      case Z_STREAM_END:
        if (stream->avail_in == 0)
          goto done;
        /* There may be more compressed data here. */
        if ((r = inflateEnd(stream)) != Z_OK) {
          log_warn(LD_BUG, "Error freeing gzip structures");
          goto err;
        }
        if (inflateInit2(stream, method_bits(method)) != Z_OK) {
          log_warn(LD_GENERAL, "Error from second inflateInit2: %s",
                   stream->msg?stream->msg:"<no message>");
          goto err;
        }
        break;
      case Z_OK:
        if (!complete_only && stream->avail_in == 0)
          goto done;
        /* In case zlib doesn't work as I think.... */
        if (stream->avail_out >= stream->avail_in+16)
          break;
      case Z_BUF_ERROR:
        if (stream->avail_out > 0) {
          log_fn(protocol_warn_level, LD_PROTOCOL,
                 "possible truncated or corrupt zlib data");
          goto err;
        }
        offset = stream->next_out - (unsigned char*)*out;
        old_size = out_size;
        out_size *= 2;
        if (out_size < old_size) {
          log_warn(LD_GENERAL, "Size overflow in uncompression.");
          goto err;
        }
        if (is_compression_bomb(in_len, out_size)) {
          log_warn(LD_GENERAL, "Input looks like a possible zlib bomb; "
                   "not proceeding.");
          goto err;
        }
        if (out_size >= SIZE_T_CEILING) {
          log_warn(LD_BUG, "Hit SIZE_T_CEILING limit while uncompressing.");
          goto err;
        }
        *out = tor_realloc(*out, out_size);
        stream->next_out = (unsigned char*)(*out + offset);
        if (out_size - offset > UINT_MAX) {
          log_warn(LD_BUG,  "Ran over unsigned int limit of zlib while "
                   "uncompressing.");
          goto err;
        }
        stream->avail_out = (unsigned int)(out_size - offset);
        break;
      default:
        log_warn(LD_GENERAL, "Gzip decompression returned an error: %s",
                 stream->msg ? stream->msg : "<no message>");
        goto err;
      }
  }
 done:
  *out_len = stream->next_out - (unsigned char*)*out;
  r = inflateEnd(stream);
  tor_free(stream);
  if (r != Z_OK) {
    log_warn(LD_BUG, "Error freeing gzip structures");
    goto err;
  }

  /* NUL-terminate output. */
  if (out_size == *out_len)
    *out = tor_realloc(*out, out_size + 1);
  (*out)[*out_len] = '\0';

  return 0;
 err:
  if (stream) {
    inflateEnd(stream);
    tor_free(stream);
  }
  if (*out) {
    tor_free(*out);
  }
  return -1;
}
예제 #18
0
파일: NptZip.cpp 프로젝트: sandalsoft/mrmc
/*----------------------------------------------------------------------
|   NPT_ZipInflateState::~NPT_ZipInflateState
+---------------------------------------------------------------------*/
NPT_ZipInflateState::~NPT_ZipInflateState()
{
    inflateEnd(&m_Stream);
}
예제 #19
0
CAMLprim value zlib_inflate_end(value zv) {
	if( inflateEnd(zval(zv)) != 0 )
		failwith("zlib_inflate_end");
	return Val_unit;
}
예제 #20
0
파일: dmg.c 프로젝트: sikopet/clamav-devel
/* Stripe handling: deflate block (type 0x80000005) */
static int dmg_stripe_inflate(cli_ctx *ctx, int fd, uint32_t index, struct dmg_mish_with_stripes *mish_set)
{
    int ret = CL_CLEAN, zstat;
    z_stream strm;
    size_t off = mish_set->stripes[index].dataOffset;
    size_t len = mish_set->stripes[index].dataLength;
    uint64_t size_so_far = 0;
    uint64_t expected_len = mish_set->stripes[index].sectorCount * DMG_SECTOR_SIZE;
    uint8_t obuf[BUFSIZ];

    cli_dbgmsg("dmg_stripe_inflate: stripe " STDu32 "\n", index);
    if (len == 0)
        return CL_CLEAN;

    memset(&strm, 0, sizeof(strm));
    strm.next_in = (void*)fmap_need_off_once(*ctx->fmap, off, len);
    if (!strm.next_in) {
        cli_warnmsg("dmg_stripe_inflate: fmap need failed on stripe " STDu32 "\n", index);
        return CL_EMAP;
    }
    strm.avail_in = len;
    strm.next_out = obuf;
    strm.avail_out = sizeof(obuf);

    zstat = inflateInit(&strm);
    if(zstat != Z_OK) {
        cli_warnmsg("dmg_stripe_inflate: inflateInit failed\n");
        return CL_EMEM;
    }

    while(strm.avail_in) {
        int written;
        if (size_so_far > expected_len) {
            cli_warnmsg("dmg_stripe_inflate: expected size exceeded!\n");
            inflateEnd(&strm);
            return CL_EFORMAT;
        }
        zstat = inflate(&strm, Z_NO_FLUSH);   /* zlib */
        switch(zstat) {
        case Z_OK:
            if(strm.avail_out == 0) {
                if ((written=cli_writen(fd, obuf, sizeof(obuf)))!=sizeof(obuf)) {
                    cli_errmsg("dmg_stripe_inflate: failed write to output file\n");
                    inflateEnd(&strm);
                    return CL_EWRITE;
                }
                size_so_far += written;
                strm.next_out = (Bytef *)obuf;
                strm.avail_out = sizeof(obuf);
            }
            continue;
        case Z_STREAM_END:
        default:
            written = sizeof(obuf) - strm.avail_out;
            if (written) {
                if ((cli_writen(fd, obuf, written))!=written) {
                    cli_errmsg("dmg_stripe_inflate: failed write to output file\n");
                    inflateEnd(&strm);
                    return CL_EWRITE;
                }
                size_so_far += written;
                strm.next_out = (Bytef *)obuf;
                strm.avail_out = sizeof(obuf);
                if (zstat == Z_STREAM_END)
                    break;
            }
            if(strm.msg)
                cli_dbgmsg("dmg_stripe_inflate: after writing " STDu64 " bytes, "
                           "got error \"%s\" inflating stripe " STDu32 "\n",
                           size_so_far, strm.msg, index);
            else
                cli_dbgmsg("dmg_stripe_inflate: after writing " STDu64 " bytes, "
                           "got error %d inflating stripe " STDu32 "\n",
                           size_so_far, zstat, index);
            inflateEnd(&strm);
            return CL_EFORMAT;
        }
        break;
    }

    if(strm.avail_out != sizeof(obuf)) {
        if(cli_writen(fd, obuf, sizeof(obuf) - strm.avail_out) < 0) {
            cli_errmsg("dmg_stripe_inflate: failed write to output file\n");
            inflateEnd(&strm);
            return CL_EWRITE;
        }
    }

    inflateEnd(&strm);
    return CL_CLEAN;
}
예제 #21
0
파일: s_bsd.c 프로젝트: mojadita/ircd
/** Attempt to send a sequence of bytes to the connection.
 * As a side effect, updates \a cptr's FLAG_BLOCKED setting
 * and sendB/sendK fields.
 * @param cptr Client that should receive data.
 * @param buf Message buffer to send to client.
 * @return Negative on connection-fatal error; otherwise
 *  number of bytes sent.
 */
unsigned int deliver_it(struct Client *cptr, struct MsgQ *buf)
{
  unsigned int bytes_written = 0;
  unsigned int bytes_count = 0;
  assert(0 != cptr);

#if defined(USE_SSL)
  switch (client_sendv(cptr, buf, &bytes_count, &bytes_written)) {
#else
  switch (os_sendv_nonb(cli_fd(cptr), buf, &bytes_count, &bytes_written)) {
#endif
  case IO_SUCCESS:
    ClrFlag(cptr, FLAG_BLOCKED);

    cli_sendB(cptr) += bytes_written;
    cli_sendB(&me)  += bytes_written;
    /* A partial write implies that future writes will block. */
    if (bytes_written < bytes_count)
      SetFlag(cptr, FLAG_BLOCKED);
    break;
  case IO_BLOCKED:
    SetFlag(cptr, FLAG_BLOCKED);
    break;
  case IO_FAILURE:
    cli_error(cptr) = errno;
    SetFlag(cptr, FLAG_DEADSOCKET);
    break;
  }
  return bytes_written;
}

/** Complete non-blocking connect()-sequence. Check access and
 * terminate connection, if trouble detected.
 * @param cptr Client to which we have connected, with all ConfItem structs attached.
 * @return Zero on failure (caller should exit_client()), non-zero on success.
 */
static int completed_connection(struct Client* cptr)
{
  struct ConfItem *aconf;
  time_t newts;
  struct Client *acptr;
  int i;
#if defined(USE_SSL)
  char *sslfp;
  int r;
#endif

  assert(0 != cptr);

  /*
   * get the socket status from the fd first to check if
   * connection actually succeeded
   */
  if ((cli_error(cptr) = os_get_sockerr(cli_fd(cptr)))) {
    const char* msg = strerror(cli_error(cptr));
    if (!msg)
      msg = "Unknown error";
    sendto_opmask(0, SNO_OLDSNO, "Connection failed to %s: %s",
                  cli_name(cptr), msg);
    return 0;
  }
  if (!(aconf = find_conf_byname(cli_confs(cptr), cli_name(cptr), CONF_SERVER))) {
    sendto_opmask(0, SNO_OLDSNO, "Lost Server Line for %s", cli_name(cptr));
    return 0;
  }

#if defined(USE_SSL)
  if (aconf->flags & CONF_SSL) {
    r = ssl_connect(&(cli_socket(cptr)));
    if (r == -1) {
      sendto_opmask(0, SNO_OLDSNO, "Connection failed to %s: SSL error",
                    cli_name(cptr));
      return 0;
    } else if (r == 0)
      return 1;
    sslfp = ssl_get_fingerprint(cli_socket(cptr).s_ssl);
    if (sslfp)
      ircd_strncpy(cli_sslclifp(cptr), sslfp, BUFSIZE+1);
    SetSSL(cptr);
  }
#endif

  if (s_state(&(cli_socket(cptr))) == SS_CONNECTING)
    socket_state(&(cli_socket(cptr)), SS_CONNECTED);

  if (!EmptyString(aconf->passwd))
    sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd);

  /*
   * Create a unique timestamp
   */
  newts = TStime();
  for (i = HighestFd; i > -1; --i) {
    if ((acptr = LocalClientArray[i]) &&
        (IsServer(acptr) || IsHandshake(acptr))) {
      if (cli_serv(acptr)->timestamp >= newts)
        newts = cli_serv(acptr)->timestamp + 1;
    }
  }
  assert(0 != cli_serv(cptr));

  cli_serv(cptr)->timestamp = newts;
  SetHandshake(cptr);
  /*
   * Make us timeout after twice the timeout for DNS look ups
   */
  cli_lasttime(cptr) = CurrentTime;
  ClearPingSent(cptr);

/* TODO: NEGOCIACION
  envia_config_req(cptr);
*/

  sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6 :%s",
                cli_name(&me), cli_serv(&me)->timestamp, newts,
		MAJOR_PROTOCOL, NumServCap(&me),
		feature_bool(FEAT_HUB) ? "h" : "", cli_info(&me));

#if defined(DDB)
  ddb_burst(cptr);
#endif

  return (IsDead(cptr)) ? 0 : 1;
}

/** Close the physical connection.  Side effects: MyConnect(cptr)
 * becomes false and cptr->from becomes NULL.
 * @param cptr Client to disconnect.
 */
void close_connection(struct Client *cptr)
{
  struct ConfItem* aconf;

  if (IsServer(cptr)) {
    ServerStats->is_sv++;
    ServerStats->is_sbs += cli_sendB(cptr);
    ServerStats->is_sbr += cli_receiveB(cptr);
    ServerStats->is_sti += CurrentTime - cli_firsttime(cptr);
    /*
     * If the connection has been up for a long amount of time, schedule
     * a 'quick' reconnect, else reset the next-connect cycle.
     */
    if ((aconf = find_conf_exact(cli_name(cptr), cptr, CONF_SERVER))) {
      /*
       * Reschedule a faster reconnect, if this was a automatically
       * connected configuration entry. (Note that if we have had
       * a rehash in between, the status has been changed to
       * CONF_ILLEGAL). But only do this if it was a "good" link.
       */
      aconf->hold = CurrentTime;
      aconf->hold += ((aconf->hold - cli_since(cptr) >
		       feature_int(FEAT_HANGONGOODLINK)) ?
		      feature_int(FEAT_HANGONRETRYDELAY) : ConfConFreq(aconf));
/*        if (nextconnect > aconf->hold) */
/*          nextconnect = aconf->hold; */
    }
  }
  else if (IsUser(cptr)) {
    ServerStats->is_cl++;
    ServerStats->is_cbs += cli_sendB(cptr);
    ServerStats->is_cbr += cli_receiveB(cptr);
    ServerStats->is_cti += CurrentTime - cli_firsttime(cptr);
  }
  else
    ServerStats->is_ni++;

#if defined(USE_ZLIB)
  /*
   * Siempre es una conexion nuestra
   */
  if (cli_connect(cptr)->zlib_negociation & ZLIB_IN) {
    inflateEnd(cli_connect(cptr)->comp_in);
    MyFree(cli_connect(cptr)->comp_in);
  }
  if (cli_connect(cptr)->zlib_negociation & ZLIB_OUT) {
    deflateEnd(cli_connect(cptr)->comp_out);
    MyFree(cli_connect(cptr)->comp_out);
  }
#endif

  if (-1 < cli_fd(cptr)) {
    flush_connections(cptr);
    LocalClientArray[cli_fd(cptr)] = 0;
    close(cli_fd(cptr));
    socket_del(&(cli_socket(cptr))); /* queue a socket delete */
    cli_fd(cptr) = -1;
    cli_freeflag(cptr) &= ~FREEFLAG_SOCKET;
  }
  SetFlag(cptr, FLAG_DEADSOCKET);

  MsgQClear(&(cli_sendQ(cptr)));
  client_drop_sendq(cli_connect(cptr));
  DBufClear(&(cli_recvQ(cptr)));
  memset(cli_passwd(cptr), 0, sizeof(cli_passwd(cptr)));
  set_snomask(cptr, 0, SNO_SET);

  det_confs_butmask(cptr, 0);

  if (cli_listener(cptr)) {
    release_listener(cli_listener(cptr));
    cli_listener(cptr) = 0;
  }

  for ( ; HighestFd > 0; --HighestFd) {
    if (LocalClientArray[HighestFd])
      break;
  }
}

/** Close all unregistered connections.
 * @param source Oper who requested the close.
 * @return Number of closed connections.
 */
int net_close_unregistered_connections(struct Client* source)
{
  int            i;
  struct Client* cptr;
  int            count = 0;
  assert(0 != source);

  for (i = HighestFd; i > 0; --i) {
    if ((cptr = LocalClientArray[i]) && !IsRegistered(cptr)) {
      send_reply(source, RPL_CLOSING, get_client_name(source, HIDE_IP));
      exit_client(source, cptr, &me, "Oper Closing");
      ++count;
    }
  }
  return count;
}
예제 #22
0
int wcache_close(struct wcache_stream *wcs)
{
	// parameter checking
	if ( (wcs == 0) || (wcs->entry == 0) )
	{
		return(0);
	}

#if (WEBC_SUPPORT_CACHE_COMPRESSION)

	if (wcs->entry->flags & WEBC_CACHE_ENTRY_COMPRESSED)
	{
		z_streamp pz = (z_streamp) wcs->pZlibStream;

		if (wcs->mode != WEBC_CO_READ)
		{
			long bytes_written = 0;
			int result, val;

			do
			{
				result = deflate(pz, Z_FINISH);
				if ((result == Z_STREAM_ERROR) || (result == Z_BUF_ERROR))
				{
					break;
				}
				if ((wcs->outBufferSize - pz->avail_out) > (wcs->cc->bytesMax - wcs->cc->bytesUsed))
				{
					// won't fit; try to free up some space
					if (_wcache_free_bytes(wcs->cc, (wcs->outBufferSize - pz->avail_out)) == 0)
					{
						break;
					}
				}

				val = webc_fwrite (wcs->fp, (char *) wcs->pOutBuffer, (wcs->outBufferSize - pz->avail_out));
				if (val < 0)
				{
					break;
				}
				bytes_written += val;
				wcs->cc->bytesUsed += val;

				pz->avail_out = wcs->outBufferSize;
				pz->next_out = wcs->pOutBuffer;

			} while (result != Z_STREAM_END);

			wcs->entry->size += bytes_written;

			deflateEnd(pz);
		}
		else
		{
			inflateEnd(pz);
		}
		WEBC_FREE(wcs->pInBuffer);
		WEBC_FREE(wcs->pOutBuffer);
		WEBC_FREE(pz);
	}

#endif // WEBC_SUPPORT_CACHE_COMPRESSION

	_wcache_unlock_entry(wcs->entry);
	wcs->cc->spec.sys_close(wcs->cc->sysPtr, wcs->fp);
	return (0);
}
예제 #23
0
BgzfReader::~BgzfReader() {
    inflateEnd(&stream);
}
예제 #24
0
/*
    Source stringstream is in Base64, decoded from Base64 to get compressed bytes
    which is uncompressed and then put into the destination stringstream
*/
int InflateSStream( std::stringstream &ssSrc, std::stringstream &ssDst )
{
    int ret = Z_OK;

    int flush = true;

    //  Allocate inflate state
    z_stream d_stream; //decompression stream
    d_stream.zalloc     = Z_NULL;
    d_stream.zfree      = Z_NULL;
    d_stream.opaque     = Z_NULL;
    d_stream.avail_in   = Z_NULL;
    d_stream.next_in    = Z_NULL;

    //  Initialize inflate
    ret = inflateInit( &d_stream );
    if ( ret != Z_OK )
        return ret;

    std::string strBase64( ssSrc.str() );

    //  Free up the source stringstream
    ssSrc.str("");

    //  Decode from Base64
    std::string asciiStr;
    Base64ToString( strBase64.c_str(), strBase64.size(), asciiStr );

    //  Free up the temporary Base64 data
    strBase64 = "";

    uLong nInputSize  = asciiStr.size();
    int nRemaining = 0;

#ifndef OBSOLETE
    while ( d_stream.total_in < nInputSize )
    {
        char out[ s_nChunkSize ];

        d_stream.avail_in   = nInputSize - d_stream.total_in;
        d_stream.avail_out  = s_nChunkSize;
        d_stream.next_in    = ( Bytef* ) asciiStr.c_str() + d_stream.total_in;
        d_stream.next_out   = ( Bytef* ) out;

        //  Uncompress
        ret = inflate( &d_stream, Z_NO_FLUSH );

        //  Check for possible error conditions with ret
        switch ( ret )
        {
            case Z_NEED_DICT:
                ret = Z_DATA_ERROR;     /* and fall through */
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                inflateEnd( &d_stream );
                return ret;
        }

        int nHave = s_nChunkSize - d_stream.avail_out;

        //  Copy the uncompressed data into the stringstream
        ssDst.write ( out, nHave );

        if ( ret == Z_STREAM_END ) break;
    }
#else
    do {
        d_stream.avail_in   = nInputSize;
        d_stream.next_in    = ( Bytef* ) asciiStr.c_str() + nRemaining;

        do {
            d_stream.avail_out  = s_nChunkSize;
            d_stream.next_out   = ( Bytef* ) out;
            ret = inflate( &d_stream, Z_NO_FLUSH );
            if ( ret != Z_OK )
                return ret;

            //  How big is the inflate result
            int nHave = s_nChunkSize - d_stream.avail_out;

            ssDst.write ( out, nHave );

        } while ( d_stream.avail_out == 0 );
        ;//  Update nRemaining here
    } while ( ret != Z_STREAM_END );
#endif // OBSOLETE

    //  Free all allocated state information referenced by stream
    ret = inflateEnd( &d_stream );

return ret;
}
예제 #25
0
File* ZipArchive::inflateToMemory(const std::string& outFileName, const size_t compressedSize,
        const size_t uncompressedSize, size_t archiveOffset)
{
    if (checkFileHandleValid() == false)
        return 0;

    // Prepare z_stream structure for inflating
    //
    z_stream strm;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.avail_in = 0;
    strm.next_in = Z_NULL;
    strm.avail_out = 0;
    strm.next_out = Z_NULL;

    // Initialize inflate:
    //
    // NOTE: inflateInit2() is an undocumented macro. The second parameter ("windowBits")
    // suppresses header and CRC32 check by the inflate() function if it is negative!
    //
    if (inflateInit2(&strm, -15) != Z_OK) {
        LERROR("inflateToDisk(): call to zlib function inflateInit() failed!");
        return 0;
    }

    // Allocate buffers for in- and output
    //
    size_t inBufferSize = (compressedSize < MAX_BUFFER_SIZE) ? compressedSize : MAX_BUFFER_SIZE;
    size_t outBufferSize = uncompressedSize;
    char* inBuffer = 0;
    char* outBuffer = 0;

    try {
        inBuffer = new char[inBufferSize];
    } catch (std::bad_alloc&) {
        LERROR("inflateToMemory(): failed to allocate " << inBufferSize << " Bytes for input buffer!");
        return 0;
    }

    try {
        outBuffer = new char[outBufferSize];
    } catch (std::bad_alloc&) {
        LERROR("inflateToMemory(): failed to allocate " << outBufferSize << " Bytes for memory file!");
        delete [] inBuffer;
        return 0;
    }

    size_t readTotal = 0;
    size_t writtenTotal = 0;
    strm.avail_out = static_cast<uInt>(outBufferSize);
    strm.next_out = reinterpret_cast<Bytef*>(outBuffer);

    archive_->seek(archiveOffset, File::BEGIN);
    do {
        // If the number of bytes left to read becomes smaller than the buffer's size
        // reduce the number of bytes to be read.
        //
        size_t left = compressedSize - readTotal;
        if (left < inBufferSize)
            inBufferSize = left;

        size_t read = archive_->read(inBuffer, inBufferSize);
        readTotal += read;
        strm.avail_in = static_cast<uInt>(read);
        strm.next_in = reinterpret_cast<Bytef*>(inBuffer);

        // As the entire uncompressed file size is known, the buffer has
        // been allocated to be large enough to hold all inflated data and
        // should therefore not need to be resized.
        //
        int res = inflate(&strm, Z_NO_FLUSH);
        if ((res == Z_OK) || (res == Z_STREAM_END))
            writtenTotal += outBufferSize - strm.avail_out;
        if ((res == Z_STREAM_END) || (strm.avail_out > 0) || (writtenTotal >= uncompressedSize))
            break;

    } while ((archive_->eof() == false) && (readTotal < compressedSize));

    inflateEnd(&strm);
    delete [] inBuffer;

    return new MemoryFile(outBuffer, outBufferSize, outFileName, true);
}
예제 #26
0
CMUSHclientDoc::~CMUSHclientDoc()
{
int i;

  // stop sounds playing, release sound buffers
  for (i = 0; i < MAX_SOUND_BUFFERS; i++)
    if (m_pDirectSoundSecondaryBuffer [i])
      {
      DWORD iStatus;
      if (SUCCEEDED (m_pDirectSoundSecondaryBuffer [i]->GetStatus (&iStatus)) &&
          (iStatus & DSBSTATUS_PLAYING))
        m_pDirectSoundSecondaryBuffer [i]->Stop ();

      m_pDirectSoundSecondaryBuffer [i]->Release ();
      }

  if (m_pTimerWnd)
    {
	  m_pTimerWnd->DestroyWindow();
	  delete m_pTimerWnd;
    }

  for (i = 0; i < 8; i++)  
    delete m_font [i];
  delete m_input_font;

  if (m_hNameLookup)
    WSACancelAsyncRequest (m_hNameLookup);  // cancel host name lookup in progress

  delete [] m_pGetHostStruct;   // delete buffer used by host name lookup

  delete m_MapFailureRegexp;    // delete regexp structure for mapping failures

	if (m_pSocket)
	{

    ShutDownSocket (*m_pSocket);

    delete m_pSocket;
    m_pSocket = NULL;

    }

	if (m_pChatListenSocket)
	{

    ShutDownSocket (*m_pChatListenSocket);

    delete m_pChatListenSocket;
    m_pChatListenSocket = NULL;

    }

  // UDP listening sockets
  for (map<int, UDPsocket *>::iterator udpSocketIterator = m_UDPsocketMap.begin ();
       udpSocketIterator != m_UDPsocketMap.end ();
       udpSocketIterator++)
      delete udpSocketIterator->second;

// delete chat sessions

  DELETE_LIST (m_ChatList);

// delete plugins

  DELETE_LIST (m_PluginList);

  CloseLog ();    // this writes out the log file postamble as well

// delete triggers
       
  DELETE_MAP (m_TriggerMap, CTrigger); 

// delete aliass

  DELETE_MAP (m_AliasMap, CAlias); 

// delete lines list

  DELETE_LIST (m_LineList);

// delete timer map

  DELETE_MAP (m_TimerMap, CTimer); 
  
// delete variables map

  DELETE_MAP (m_VariableMap, CVariable); 

// delete Element map

  DELETE_MAP (m_CustomElementMap, CElement); 

// delete active tags list

  DELETE_LIST (m_ActiveTagList);

// delete actions list

  DELETE_LIST (m_ActionList);


// get rid of our positions array

  delete [] m_pLinePositions;

// one less document

  gdoccount--;

// update activity window

  App.m_bUpdateActivity = TRUE;

// ****************** release scripting stuff

  DisableScripting ();

  if (!bWine)
  	AfxOleUnlockApp();        // not needed?

  // free compression stuff

  if (m_CompressOutput)
    free (m_CompressOutput);
  if (m_CompressInput)
    free (m_CompressInput);

  // don't wrap up if not initialised
  if (m_bCompressInitOK)
    inflateEnd (&m_zCompress);

  // don't need to know what the configuration was any more
  DeleteConfigurationArrays ();

  // delete our arrays
  for (tStringMapOfMaps::iterator it = m_Arrays.begin (); 
       it != m_Arrays.end ();
       it++)
         {
         tStringToStringMap * m = it->second;
         m->clear ();
         delete m;
         }

  // destroy accelerator table, if we had one
  if (m_accelerator)
    DestroyAcceleratorTable (m_accelerator);

  // if they loaded a special font, get rid of it
  RemoveSpecialFont ();

#ifdef PANE

  // get rid of owned panes

  safe_for_each (m_PaneMap.begin (), m_PaneMap.end (), closepane);
#endif // PANE

  // delete MiniWindow map

  for (MiniWindowMapIterator mwit = m_MiniWindows.begin (); 
       mwit != m_MiniWindows.end ();
       mwit++)
         delete mwit->second;


  // delete databases
  for (tDatabaseMapIterator dbit = m_Databases.begin (); 
       dbit != m_Databases.end ();
       dbit++)
         {
         if (dbit->second->pStmt)        // finalize any outstanding statement
           sqlite3_finalize(dbit->second->pStmt);
         if (dbit->second->db)           // and close the database
           sqlite3_close(dbit->second->db);
         delete dbit->second;      // now delete memory used by it
         }

}
예제 #27
0
파일: csw_cas.c 프로젝트: cdenix/psmame
static int csw_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) {
UINT32 SampleRate;
UINT32 NumberOfPulses;
UINT8  CompressionType;
UINT8  Flags;
UINT8  HeaderExtensionLength;
INT8   Bit;

UINT8 *gz_ptr = NULL;
int			total_size;
z_stream	d_stream;
int 		err;
UINT8		*in_ptr;
int 		bsize=0;
int		i;


	logerror("Length %d\n",length);

	SampleRate=get_leuint32(bytes+0x19);
	logerror("Sample rate %d\n",SampleRate);

	NumberOfPulses=get_leuint32(bytes+0x1d);
	logerror("Number Of Pulses %d\n",NumberOfPulses);

	CompressionType=bytes[0x21];
	Flags=bytes[0x22];
	HeaderExtensionLength=bytes[0x23];

	if ((Flags&0)==0)
	{
		Bit=-100;
	} else {
		Bit=100;
	}

	logerror("CompressionType %d   Flast %d   HeaderExtensionLength %d\n",CompressionType,Flags,HeaderExtensionLength);


	//from here on down for now I am assuming it is compressed csw file.
	in_ptr = (UINT8*) bytes+0x34+HeaderExtensionLength;

	gz_ptr = (UINT8*)malloc( 8 );


	d_stream.next_in = (unsigned char *)in_ptr;
	d_stream.avail_in = mycaslen - ( in_ptr - bytes );
	d_stream.total_in=0;

	d_stream.next_out = gz_ptr;
	d_stream.avail_out = 1;
	d_stream.total_out=0;

	d_stream.zalloc = 0;
	d_stream.zfree = 0;
	d_stream.opaque = 0;
	d_stream.data_type=0;

	err = inflateInit( &d_stream );
	if ( err != Z_OK ) {
		logerror( "inflateInit2 error: %d\n", err );
		goto cleanup;
	}

	total_size=0;

	do
	{
		d_stream.next_out = gz_ptr;
		d_stream.avail_out=1;
		err=inflate( &d_stream, Z_SYNC_FLUSH );
		if (err==Z_OK) {
			bsize=gz_ptr[0];
			if (bsize==0) {
				d_stream.avail_out=4;
				d_stream.next_out = gz_ptr;
				err=inflate( &d_stream, Z_SYNC_FLUSH );
				bsize=get_leuint32(gz_ptr);
			}
			for (i=0;i<bsize;i++)
			{
				buffer[total_size++]=Bit;
			}
			Bit=-Bit;
		}
	}
	while (err==Z_OK);

	if ( err != Z_STREAM_END ) {
		logerror( "inflate error: %d\n", err );
		goto cleanup;
	}

	err = inflateEnd( &d_stream );
	if ( err != Z_OK ) {
		logerror( "inflateEnd error: %d\n", err );
		goto cleanup;
	}






	if ( gz_ptr ) {
		free( gz_ptr );
		gz_ptr = NULL;
	}




	return length;



	cleanup:
		if ( gz_ptr ) {
			free( gz_ptr );
			gz_ptr = NULL;
		}
	return -1;
}
예제 #28
0
void *rd_gz_decompress (const void *compressed, int compressed_len,
			uint64_t *decompressed_lenp) {
	int pass = 1;
	char *decompressed = NULL;

	/* First pass (1): calculate decompressed size.
	 *                 (pass-1 is skipped if *decompressed_lenp is
	 *                  non-zero).
	 * Second pass (2): perform actual decompression.
	 */

	if (*decompressed_lenp != 0LLU)
		pass++;

	for (; pass <= 2 ; pass++) {
		z_stream strm = RD_ZERO_INIT;
		gz_header hdr;
		char buf[512];
		char *p;
		int len;
		int r;
		
		if ((r = inflateInit2(&strm, 15+32)) != Z_OK)
			goto fail;

		strm.next_in = (void *)compressed;
		strm.avail_in = compressed_len;

		if ((r = inflateGetHeader(&strm, &hdr)) != Z_OK) {
			inflateEnd(&strm);
			goto fail;
		}

		if (pass == 1) {
			/* Use dummy output buffer */
			p = buf;
			len = sizeof(buf);
		} else {
			/* Use real output buffer */
			p = decompressed;
			len = (int)*decompressed_lenp;
		}

		do {
			strm.next_out = (unsigned char *)p;
			strm.avail_out = len;

			r = inflate(&strm, Z_NO_FLUSH);
			switch  (r) {
			case Z_STREAM_ERROR:
			case Z_NEED_DICT:
			case Z_DATA_ERROR:
			case Z_MEM_ERROR:
				inflateEnd(&strm);
				goto fail;
			}

			if (pass == 2) {
				/* Advance output pointer (in pass 2). */
				p += len - strm.avail_out;
				len -= len - strm.avail_out;
			}

		} while (strm.avail_out == 0 && r != Z_STREAM_END);


		if (pass == 1) {
			*decompressed_lenp = strm.total_out;
			if (!(decompressed = malloc((size_t)(*decompressed_lenp)+1))) {
				inflateEnd(&strm);
				return NULL;
			}
			/* For convenience of the caller we nul-terminate
			 * the buffer. If it happens to be a string there
			 * is no need for extra copies. */
			decompressed[*decompressed_lenp] = '\0';
		}

		inflateEnd(&strm);
	}

	return decompressed;

fail:
	if (decompressed)
		free(decompressed);
	return NULL;
}
예제 #29
0
파일: filesubr.cpp 프로젝트: todace/G-CVSNT
/*
 * Copies "from" to "to", decompressing "from" on the fly
 */
int copy_and_unzip_file (const char *from, const char *to, int force_overwrite, int must_exist)
{
    struct stat sb;
    struct utimbuf t;
    int fdin, fdout;
    z_stream zstr = {0};
    int zstatus;
    char buf[BUFSIZ*10];
    char zbuf[BUFSIZ*20];
    int n;
#ifdef UTIME_EXPECTS_WRITABLE
    int change_it_back = 0;
#endif

    TRACE(1,"copy_and_unzip(%s,%s)",PATCH_NULL(from),PATCH_NULL(to));
    if (noexec)
        return 0;

    /* If the file to be copied is a link or a device, then just create
       the new link or device appropriately. */
    if (islink (from))
    {
        char *source = xreadlink (from);
        symlink (source, to);
        xfree (source);
        return 0;
    }

    if (isdevice (from))
    {
#if defined(HAVE_MKNOD) && defined(HAVE_STRUCT_STAT_ST_RDEV)
        if (stat (from, &sb) < 0)
            error (1, errno, "cannot stat %s", fn_root(from));
        mknod (to, sb.st_mode, sb.st_rdev);
#else
        error (1, 0, "cannot copy device files on this system (%s)", fn_root(from));
#endif
    }
    else
    {
        /* Not a link or a device... probably a regular file. */
        if ((fdin = CVS_OPEN (from, O_RDONLY)) < 0)
        {
            if(must_exist)
                error (1, errno, "cannot open %s for copying", fn_root(from));
            else
                return -1;
        }
        if (fstat (fdin, &sb) < 0)
        {
            if(must_exist)
                error (1, errno, "cannot fstat %s", fn_root(from));
            else
            {
                close(fdin);
                return -1;
            }
        }
        if (force_overwrite && unlink_file (to) && !existence_error (errno))
            error (1, errno, "unable to remove %s", to);

        if ((fdout = creat (to, (int) sb.st_mode & 07777)) < 0)
            error (1, errno, "cannot create %s for copying", to);

        zstatus = inflateInit2 (&zstr, 47);
        if(zstatus != Z_OK)
            error(1, 0, "expansion error (INIT): (%d)%s", zstatus,zstr.msg);

        if (sb.st_size > 0)
        {
            for (;;)
            {
                n = read (fdin, buf, sizeof(buf));
                if (n == -1)
                {
#ifdef EINTR
                    if (errno == EINTR)
                        continue;
#endif
                    error (1, errno, "cannot read file %s for copying", fn_root(from));
                }
                else if (n == 0)
                    break;

                zstr.next_in = (Bytef*)buf;
                zstr.avail_in = n;
                while(zstr.avail_in)
                {
                    zstr.next_out = (Bytef*)zbuf;
                    zstr.avail_out = sizeof(zbuf);
                    zstatus = inflate (&zstr, 0);
                    if(zstatus != Z_OK && zstatus != Z_STREAM_END)
                        error(1,0, "expansion error (inflate): (%d)%s", zstatus,zstr.msg);

                    n = sizeof(zbuf)-zstr.avail_out;
                    if (n && write(fdout, zbuf, n) != n) {
                        error (1, errno, "cannot write file %s for copying", to);
                    }
                }
                if(zstatus == Z_STREAM_END)
                    break;
            }

#ifdef HAVE_FSYNC
            if (fsync (fdout))
                error (1, errno, "cannot fsync file %s after copying", fn_root(to));
#endif

        }

        zstr.next_out = (Bytef*)zbuf;
        zstr.avail_out = sizeof(zbuf);
        zstatus = inflate (&zstr, Z_FINISH);
        if(zstatus != Z_OK && zstatus != Z_STREAM_END)
            error(1,0, "expansion error (Z_FINISH): (%d)%s", zstatus,zstr.msg);
        n = sizeof(zbuf)-zstr.avail_out;
        if (n && write(fdout, zbuf, n) != n) {
            error (1, errno, "cannot write file %s for copying", fn_root(to));
        }

        zstr.next_in = (Bytef*)buf;
        zstr.avail_in = 0;
        zstr.next_out = (Bytef*)zbuf;
        zstr.avail_out = sizeof(zbuf);
        zstatus = inflateEnd(&zstr);
        if(zstatus != Z_OK)
            error(1,0, "expansion error: %s", zstr.msg);

        if (close (fdin) < 0)
            error (0, errno, "cannot close %s", fn_root(from));
        if (close (fdout) < 0)
            error (1, errno, "cannot close %s", fn_root(to));
    }

#ifdef UTIME_EXPECTS_WRITABLE
    if (!iswritable (to))
    {
        xchmod (to, 1);
        change_it_back = 1;
    }
#endif  /* UTIME_EXPECTS_WRITABLE  */
    /* now, set the times for the copied file to match those of the original */
    memset ((char *) &t, 0, sizeof (t));
    t.actime = sb.st_atime;
    t.modtime = sb.st_mtime;
    (void) utime (to, &t);
#ifdef UTIME_EXPECTS_WRITABLE
    if (change_it_back)
        xchmod (to, 0);
#endif  /*  UTIME_EXPECTS_WRITABLE  */
    return 0;
}
예제 #30
0
tvbuff_t *
tvb_uncompress(tvbuff_t *tvb, const int offset, int comprlen)
{
    gint       err;
    guint      bytes_out      = 0;
    guint8    *compr;
    guint8    *uncompr        = NULL;
    tvbuff_t  *uncompr_tvb    = NULL;
    z_streamp  strm;
    Bytef     *strmbuf;
    guint      inits_done     = 0;
    gint       wbits          = MAX_WBITS;
    guint8    *next;
    guint      bufsiz;
#ifdef TVB_Z_DEBUG
    guint      inflate_passes = 0;
    guint      bytes_in       = tvb_captured_length_remaining(tvb, offset);
#endif

    if (tvb == NULL) {
        return NULL;
    }

    compr = (guint8 *)tvb_memdup(NULL, tvb, offset, comprlen);

    if (!compr)
        return NULL;

    /*
     * Assume that the uncompressed data is at least twice as big as
     * the compressed size.
     */
    bufsiz = tvb_captured_length_remaining(tvb, offset) * 2;
    bufsiz = CLAMP(bufsiz, TVB_Z_MIN_BUFSIZ, TVB_Z_MAX_BUFSIZ);

#ifdef TVB_Z_DEBUG
    printf("bufsiz: %u bytes\n", bufsiz);
#endif

    next = compr;

    strm            = g_new0(z_stream, 1);
    strm->next_in   = next;
    strm->avail_in  = comprlen;

    strmbuf         = (Bytef *)g_malloc0(bufsiz);
    strm->next_out  = strmbuf;
    strm->avail_out = bufsiz;

    err = inflateInit2(strm, wbits);
    inits_done = 1;
    if (err != Z_OK) {
        inflateEnd(strm);
        g_free(strm);
        g_free(compr);
        g_free(strmbuf);
        return NULL;
    }

    while (1) {
        memset(strmbuf, '\0', bufsiz);
        strm->next_out  = strmbuf;
        strm->avail_out = bufsiz;

        err = inflate(strm, Z_SYNC_FLUSH);

        if (err == Z_OK || err == Z_STREAM_END) {
            guint bytes_pass = bufsiz - strm->avail_out;

#ifdef TVB_Z_DEBUG
            ++inflate_passes;
#endif

            if (uncompr == NULL) {
                /*
                 * This is ugly workaround for bug #6480
                 * (https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6480)
                 *
                 * g_memdup(..., 0) returns NULL (g_malloc(0) also)
                 * when uncompr is NULL logic below doesn't create tvb
                 * which is later interpreted as decompression failed.
                 */
                uncompr = (guint8 *)((bytes_pass || err != Z_STREAM_END) ?
                                     g_memdup(strmbuf, bytes_pass) :
                                     g_strdup(""));
            } else {
                guint8 *new_data = (guint8 *)g_malloc0(bytes_out + bytes_pass);

                memcpy(new_data, uncompr, bytes_out);
                memcpy(new_data + bytes_out, strmbuf, bytes_pass);

                g_free(uncompr);
                uncompr = new_data;
            }

            bytes_out += bytes_pass;

            if (err == Z_STREAM_END) {
                inflateEnd(strm);
                g_free(strm);
                g_free(strmbuf);
                break;
            }
        } else if (err == Z_BUF_ERROR) {
            /*
             * It's possible that not enough frames were captured
             * to decompress this fully, so return what we've done
             * so far, if any.
             */
            inflateEnd(strm);
            g_free(strm);
            g_free(strmbuf);

            if (uncompr != NULL) {
                break;
            } else {
                g_free(compr);
                return NULL;
            }

        } else if (err == Z_DATA_ERROR && inits_done == 1
                   && uncompr == NULL && comprlen >= 2 &&
                   (*compr  == 0x1f) && (*(compr + 1) == 0x8b)) {
            /*
             * inflate() is supposed to handle both gzip and deflate
             * streams automatically, but in reality it doesn't
             * seem to handle either (at least not within the
             * context of an HTTP response.)  We have to try
             * several tweaks, depending on the type of data and
             * version of the library installed.
             */

            /*
             * Gzip file format.  Skip past the header, since the
             * fix to make it work (setting windowBits to 31)
             * doesn't work with all versions of the library.
             */
            Bytef *c = compr + 2;
            Bytef  flags = 0;

            /* we read two bytes already (0x1f, 0x8b) and
               need at least Z_DEFLATED, 1 byte flags, 4
               bytes MTIME, 1 byte XFL, 1 byte OS */
            if (comprlen < 10 || *c != Z_DEFLATED) {
                inflateEnd(strm);
                g_free(strm);
                g_free(compr);
                g_free(strmbuf);
                return NULL;
            }

            c++;
            flags = *c;
            c++;

            /* Skip past the MTIME (4 bytes),
               XFL, and OS fields (1 byte each). */
            c += 6;

            if (flags & (1 << 2)) {
                /* An Extra field is present. It
                   consists of 2 bytes xsize and xsize
                   bytes of data.
                   Read byte-by-byte (least significant
                   byte first) to make sure we abort
                   cleanly when the xsize is truncated
                   after the first byte. */
                guint16 xsize = 0;

                if (c-compr < comprlen) {
                    xsize += *c;
                    c++;
                }
                if (c-compr < comprlen) {
                    xsize += *c << 8;
                    c++;
                }

                c += xsize;
            }

            if (flags & (1 << 3)) {
                /* A null terminated filename */

                while ((c - compr) < comprlen && *c != '\0') {
                    c++;
                }

                c++;
            }

            if (flags & (1 << 4)) {
                /* A null terminated comment */

                while ((c - compr) < comprlen && *c != '\0') {
                    c++;
                }

                c++;
            }


            if (c - compr > comprlen) {
                inflateEnd(strm);
                g_free(strm);
                g_free(compr);
                g_free(strmbuf);
                return NULL;
            }
            /* Drop gzip header */
            comprlen -= (int) (c - compr);
            next = c;

            inflateReset(strm);
            strm->next_in   = next;
            strm->avail_in  = comprlen;

            inflateEnd(strm);
            inflateInit2(strm, wbits);
            inits_done++;
        } else if (err == Z_DATA_ERROR && uncompr == NULL &&
                   inits_done <= 3) {

            /*
             * Re-init the stream with a negative
             * MAX_WBITS. This is necessary due to
             * some servers (Apache) not sending
             * the deflate header with the
             * content-encoded response.
             */
            wbits = -MAX_WBITS;

            inflateReset(strm);

            strm->next_in   = next;
            strm->avail_in  = comprlen;

            inflateEnd(strm);
            memset(strmbuf, '\0', bufsiz);
            strm->next_out  = strmbuf;
            strm->avail_out = bufsiz;

            err = inflateInit2(strm, wbits);

            inits_done++;

            if (err != Z_OK) {
                g_free(strm);
                g_free(strmbuf);
                g_free(compr);
                g_free(uncompr);

                return NULL;
            }
        } else {
            inflateEnd(strm);
            g_free(strm);
            g_free(strmbuf);

            if (uncompr == NULL) {
                g_free(compr);
                return NULL;
            }

            break;
        }
    }

#ifdef TVB_Z_DEBUG
    printf("inflate() total passes: %u\n", inflate_passes);
    printf("bytes  in: %u\nbytes out: %u\n\n", bytes_in, bytes_out);
#endif

    if (uncompr != NULL) {
        uncompr_tvb =  tvb_new_real_data((guint8*) uncompr, bytes_out, bytes_out);
        tvb_set_free_cb(uncompr_tvb, g_free);
    }
    g_free(compr);
    return uncompr_tvb;
}