bool inflate_gzip(char const* in, int size, std::vector<char>& buffer, int maximum_size, std::string& error) { LIBED2K_ASSERT(maximum_size > 0); int header_len = gzip_header(in, size); if (header_len < 0) { error = "invalid gzip header"; return true; } // start off with one kilobyte and grow // if needed buffer.resize(maximum_size); boost::uint32_t destlen = buffer.size(); boost::uint32_t srclen = size - header_len; in += header_len; int ret = puff((unsigned char*)&buffer[0], &destlen, (unsigned char*)in, &srclen); if (ret == -1) { error = "inflated data too big"; return true; } buffer.resize(destlen); if (ret != 0) { error = "error while inflating data"; return true; } return false; }
TORRENT_EXTRA_EXPORT void inflate_gzip( char const* in , int size , std::vector<char>& buffer , int maximum_size , error_code& ec) { ec.clear(); TORRENT_ASSERT(maximum_size > 0); int header_len = gzip_header(in, size); if (header_len < 0) { ec = gzip_errors::invalid_gzip_header; return; } // start off with 4 kilobytes and grow // if needed boost::uint32_t destlen = 4096; int ret = 0; boost::uint32_t srclen = size - header_len; in += header_len; do { TORRENT_TRY { buffer.resize(destlen); } TORRENT_CATCH(std::exception&) { ec = errors::no_memory; return; } ret = puff(reinterpret_cast<unsigned char*>(&buffer[0]), &destlen , reinterpret_cast<const unsigned char*>(in), &srclen); // if the destination buffer wasn't large enough, double its // size and try again. Unless it's already at its max, in which // case we fail if (ret == 1) // 1: output space exhausted before completing inflate { if (destlen == boost::uint32_t(maximum_size)) { ec = gzip_errors::inflated_data_too_large; return; } destlen *= 2; if (destlen > boost::uint32_t(maximum_size)) destlen = maximum_size; } } while (ret == 1);
// TODO: 2 it would be nice to use proper error handling here TORRENT_EXTRA_EXPORT bool inflate_gzip( char const* in , int size , std::vector<char>& buffer , int maximum_size , std::string& error) { TORRENT_ASSERT(maximum_size > 0); int header_len = gzip_header(in, size); if (header_len < 0) { error = "invalid gzip header"; return true; } // start off with 4 kilobytes and grow // if needed boost::uint32_t destlen = 4096; int ret = 0; boost::uint32_t srclen = size - header_len; in += header_len; do { TORRENT_TRY { buffer.resize(destlen); } TORRENT_CATCH(std::exception& e) { error = "out of memory: "; error += e.what(); return true; } ret = puff((unsigned char*)&buffer[0], &destlen, (unsigned char*)in, &srclen); // if the destination buffer wasn't large enough, double its // size and try again. Unless it's already at its max, in which // case we fail if (ret == 1) // 1: output space exhausted before completing inflate { if (destlen == boost::uint32_t(maximum_size)) { error = "inflated data too big"; return true; } destlen *= 2; if (destlen > (unsigned int)maximum_size) destlen = maximum_size; } } while (ret == 1);
int main() { const int num_steps=1000; const real_t x=1., y=1., z=1., sigma=10., rho=28, beta=8./3., dt=0.02; air sky(x, sigma); cloud puff(y, rho); ground earth(z, beta); ptr_t boundary_layer = ptr_t(new atmosphere(sky, puff, earth)); real_t t = 0.; std::cout << fmt(t,5,2) << " " << fmt(boundary_layer->state_vector()) << "\n"; for(int step = 1; step <= num_steps; ++step) { integrate(boundary_layer, dt); t += dt; std::cout << fmt(t,5,2) << " " << fmt(boundary_layer->state_vector()) << "\n"; } }
void PNG_DecompressBGRX(PNGChunkIHDR *ihdr, // IN uint32 *framebuffer, // OUT uint32 pitch) // OUT { uint32 width = bswap32(ihdr->width); uint32 height = bswap32(ihdr->height); /* * Size of raw decompressed image: 3 bytes per pixel, plus one byte * (filter type) per scanline. */ uint32 rawPitch = (width * 3) + 1; unsigned long rawSize = height * rawPitch; /* * Size of final decompressed image */ uint32 finalSize = height * pitch; /* * Use the bottom of the framebuffer for temporary memory. The * final raw-to-final conversion must read from higher addresses * and write to lower addresses, so we don't overwrite our * temporary buffer prematurely. To do this with all filter types, * we need to use one extra line of temporary space. */ uint8 *rawBuffer = (uint8*)framebuffer + finalSize - rawSize + pitch; /* * Decompress all IDAT data into our raw buffer. We need to join * all IDAT chunks to get a raw zlib data stream, then strip off * the 2-byte ZLIB header and 4-byte checksum to get a raw DEFLATE * stream. */ PNGChunk *idat = PNGJoinIDAT(&ihdr->hdr); unsigned long compressedSize = bswap32(idat->length) - 6; puff(rawBuffer, &rawSize, idat->data + 2, &compressedSize); /* * Line-by-line, expand the decompressed filtered data into BGRX. */ uint32 lines = height; Bool notFirstRow = FALSE; while (lines--) { uint8 *rawLine = rawBuffer; uint32 *fbLine = framebuffer; uint32 pixels = width; framebuffer = (uint32*) (pitch + (uint8*)framebuffer); rawBuffer += rawPitch; uint8 filterType = *(rawLine++); Bool notFirstColumn = FALSE; while (pixels--) { /* * Undo the per-scanline filtering. */ uint8 *up = rawLine - rawPitch; uint8 *left = rawLine - 3; uint8 *upLeft = rawLine - 3 - rawPitch; uint32 i; for (i = 0; i < 3; i++) { switch (filterType) { case 0: // None break; case 1: // Sub if (notFirstColumn) { rawLine[i] += left[i]; } break; case 2: // Up if (notFirstRow) { rawLine[i] += up[i]; } break; case 3: // Average rawLine[i] += ((notFirstColumn ? left[i] : 0) + (notFirstRow ? up[i] : 0)) >> 1; break; case 4: // Paeth rawLine[i] += PaethPredictor(notFirstColumn ? left[i] : 0, notFirstRow ? up[i] : 0, (notFirstRow && notFirstColumn) ? upLeft[i] : 0); break; } } /* * Decode RGB to BGRX. */ uint8 r = rawLine[0]; uint8 g = rawLine[1]; uint8 b = rawLine[2]; *(fbLine++) = (r << 16) | (g << 8) | b; rawLine += 3; notFirstColumn = TRUE; } notFirstRow = TRUE; } }