bool do_copy() // Copy the data. Return true on success, false on failure. { if (m_done_copy) { assert(0); log_error("gameswf::tools::copy_helper() already done copy\n"); return false; } m_done_copy = true; int current_in_pos = m_in->get_position(); int bytes_to_copy = current_in_pos - m_initial_in_pos; if (bytes_to_copy > 0) { m_in->set_position(m_initial_in_pos); int bytes_copied = m_out->copy_bytes(m_in, bytes_to_copy); if (bytes_copied != bytes_to_copy) { m_in->set_position(current_in_pos); // fixup return false; } assert(m_in->get_position() == current_in_pos); return true; } else { log_error("gameswf::tools::copy_helper asked to copy %d bytes\n", bytes_to_copy); return false; } }
copy_helper(tu_file* in, tu_file* out) : m_in(in), m_out(out), m_initial_in_pos(in->get_position()), m_done_copy(false) { assert(m_in && m_in->get_error() == TU_FILE_NO_ERROR); assert(m_out && m_out->get_error() == TU_FILE_NO_ERROR); }
void rewind_unused_bytes() // If we have unused bytes in our input buffer, rewind // to before they started. { if (m_zstream.avail_in > 0) { int pos = m_in->get_position(); int rewound_pos = pos - m_zstream.avail_in; assert(pos >= 0); assert(pos >= m_initial_stream_pos); assert(rewound_pos >= 0); assert(rewound_pos >= m_initial_stream_pos); m_in->set_position(rewound_pos); } }
int inflate_from_stream(void* dst, int bytes) { if (m_error) { return 0; } m_zstream.next_out = (unsigned char*) dst; m_zstream.avail_out = bytes; for (;;) { if (m_zstream.avail_in == 0) { // Get more raw data. int new_bytes = m_in->read_bytes(m_rawdata, ZBUF_SIZE); if (new_bytes == 0) { // The cupboard is bare! We have nothing to feed to inflate(). break; } else { m_zstream.next_in = m_rawdata; m_zstream.avail_in = new_bytes; } } int err = inflate(&m_zstream, Z_SYNC_FLUSH); if (err == Z_STREAM_END) { m_at_eof = true; break; } if (err != Z_OK) { // something's wrong. m_error = 1; break; } if (m_zstream.avail_out == 0) { break; } } int bytes_read = bytes - m_zstream.avail_out; m_logical_stream_pos += bytes_read; return bytes_read; }
void reset() // Discard current results and rewind to the beginning. // Necessary in order to seek backwards. { m_error = 0; m_at_eof = 0; int err = inflateReset(&m_zstream); if (err != Z_OK) { m_error = 1; return; } m_zstream.next_in = 0; m_zstream.avail_in = 0; m_zstream.next_out = 0; m_zstream.avail_out = 0; // Rewind the underlying stream. m_in->set_position(m_initial_stream_pos); m_logical_stream_pos = 0; }