int CStreamCtrl::Read(void* buffer, unsigned int bytes_to_read, unsigned int* bytes_read) { int ret=0; if (m_type==1) { *bytes_read=fread(buffer,1,bytes_to_read,m_openFile); } else if (m_type==2) { _pbyte_t pHeaderBuffer = NULL; _uint32_t ui32HeaderBufferSize = 0; m_sSequence->getHeaderBuffer(&pHeaderBuffer,&ui32HeaderBufferSize); if (m_curPos<ui32HeaderBufferSize) { unsigned int tmp_bytes=ui32HeaderBufferSize-static_cast<unsigned int>(m_curPos); *bytes_read= (bytes_to_read>tmp_bytes) ? tmp_bytes : bytes_to_read; pHeaderBuffer+=m_curPos; memcpy(buffer,pHeaderBuffer,*bytes_read); m_curPos+=*bytes_read; } else { _uint32_t pos=static_cast<unsigned int>(m_curPos); _uint32_t startclipnum = 0; _uint32_t endclipnum = 0; if (pos + bytes_to_read <= m_fileSize) m_sSequence->TranslateSectionRange(pos, pos+bytes_to_read, &startclipnum, &endclipnum); else m_sSequence->TranslateSectionRange(pos, m_fileSize, &startclipnum, &endclipnum); unsigned int index(0); for (_uint32_t j = startclipnum; j <= endclipnum; j++) { ClipOfRangeInfo sClipInfo; m_sSequence->getSectionInfoByIndex(j, &sClipInfo); char query[10]={}; NPT_String path="/"; path+=m_openFileName; NPT_HttpUrl url=NPT_HttpUrl(P2PIPAddress, 8082, path); _itoa_s(j,query,10); url.SetQuery(query); NPT_DataBuffer mediadata; RecvMediaData(url,sClipInfo.StartOffset,sClipInfo.EndOffset,mediadata); memcpy((unsigned char*)buffer+index,mediadata.GetData(),mediadata.GetBufferSize()); index+=mediadata.GetBufferSize(); m_curPos+=mediadata.GetBufferSize(); } *bytes_read=index; } } return ret; }
/*---------------------------------------------------------------------- | NPT_Zip::Inflate +---------------------------------------------------------------------*/ NPT_Result NPT_Zip::Inflate(const NPT_DataBuffer& in, NPT_DataBuffer& out, bool raw) { // assume an output buffer twice the size of the input plus a bit NPT_CHECK_WARNING(out.Reserve(32+2*in.GetDataSize())); // setup the stream z_stream stream; stream.next_in = (Bytef*)in.GetData(); stream.avail_in = (uInt)in.GetDataSize(); stream.next_out = out.UseData(); stream.avail_out = (uInt)out.GetBufferSize(); // setup the memory functions stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; stream.opaque = (voidpf)0; // initialize the decompressor int err = inflateInit2(&stream, raw?-15:15+32); // 15 = default window bits, +32 = automatic header if (err != Z_OK) return MapError(err); // decompress until the end do { err = inflate(&stream, Z_SYNC_FLUSH); if (err == Z_STREAM_END || err == Z_OK || err == Z_BUF_ERROR) { out.SetDataSize((NPT_Size)stream.total_out); if ((err == Z_OK && stream.avail_out == 0) || err == Z_BUF_ERROR) { // grow the output buffer out.Reserve(out.GetBufferSize()*2); stream.next_out = out.UseData()+stream.total_out; stream.avail_out = out.GetBufferSize()-(NPT_Size)stream.total_out; } } } while (err == Z_OK); // check for errors if (err != Z_STREAM_END) { inflateEnd(&stream); return MapError(err); } // cleanup err = inflateEnd(&stream); return MapError(err); }
/*---------------------------------------------------------------------- | NPT_Zip::Deflate +---------------------------------------------------------------------*/ NPT_Result NPT_Zip::Deflate(const NPT_DataBuffer& in, NPT_DataBuffer& out, int compression_level, Format format /* = ZLIB */) { // default return state out.SetDataSize(0); // check parameters if (compression_level < NPT_ZIP_COMPRESSION_LEVEL_DEFAULT || compression_level > NPT_ZIP_COMPRESSION_LEVEL_MAX) { return NPT_ERROR_INVALID_PARAMETERS; } // setup the stream z_stream stream; NPT_SetMemory(&stream, 0, sizeof(stream)); stream.next_in = (Bytef*)in.GetData(); stream.avail_in = (uInt)in.GetDataSize(); // setup the memory functions stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; stream.opaque = (voidpf)0; // initialize the compressor int err = deflateInit2(&stream, compression_level, Z_DEFLATED, 15 + (format == GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY); if (err != Z_OK) return MapError(err); // reserve an output buffer known to be large enough out.Reserve(deflateBound(&stream, stream.avail_in) + (format==GZIP?10:0)); stream.next_out = out.UseData(); stream.avail_out = out.GetBufferSize(); // decompress err = deflate(&stream, Z_FINISH); if (err != Z_STREAM_END) { deflateEnd(&stream); return MapError(err); } // update the output size out.SetDataSize(stream.total_out); // cleanup err = deflateEnd(&stream); return MapError(err); }