void tape_event_record_sample( libspectrum_dword last_tstates, int type, void *user_data ) { if( rec_state.last_level != (ula_tape_level()) ) { /* put a sample into the recording buffer */ rec_state.tape_buffer_used = write_rec_buffer( rec_state.tape_buffer, rec_state.tape_buffer_used, rec_state.last_level_count ); rec_state.last_level_count = 0; rec_state.last_level = ula_tape_level(); /* make sure we can still fit a dword and a flag byte in the buffer */ if( rec_state.tape_buffer_used+5 >= rec_state.tape_buffer_size ) { rec_state.tape_buffer_size = rec_state.tape_buffer_size*2; rec_state.tape_buffer = libspectrum_renew( libspectrum_byte, rec_state.tape_buffer, rec_state.tape_buffer_size ); } } rec_state.last_level_count++; /* schedule next timer */ event_add( last_tstates + rec_state.tstates_per_sample, record_event ); }
int rzx_store_byte( libspectrum_byte value ) { /* Get more space if we need it; allocate twice as much as we currently have, with a minimum of 50 */ if( rzx_in_count == rzx_in_allocated ) { libspectrum_byte *ptr; size_t new_allocated; new_allocated = rzx_in_allocated >= 25 ? 2 * rzx_in_allocated : 50; ptr = libspectrum_renew( libspectrum_byte, rzx_in_bytes, new_allocated ); rzx_in_bytes = ptr; rzx_in_allocated = new_allocated; } rzx_in_bytes[ rzx_in_count++ ] = value; return 0; }
static libspectrum_error input_block_resize( input_block_t *input, size_t new_count ) { libspectrum_rzx_frame_t *ptr; size_t new_allocated; /* Get more space if we need it; allocate twice as much as we currently have, with a minimum of 50 */ if( new_count > input->allocated ) { new_allocated = input->allocated >= 25 ? 2 * input->allocated : 50; if( new_allocated < new_count ) new_allocated = new_count; ptr = libspectrum_renew( libspectrum_rzx_frame_t, input->frames, new_allocated ); if( !ptr ) return LIBSPECTRUM_ERROR_MEMORY; input->frames = ptr; input->allocated = new_allocated; } return LIBSPECTRUM_ERROR_NONE; }
libspectrum_error libspectrum_bzip2_inflate( const libspectrum_byte *bzptr, size_t bzlength, libspectrum_byte **outptr, size_t *outlength ) { int error; unsigned int length2; /* Known length, so we can use the easy method */ if( *outlength ) { *outptr = libspectrum_new( libspectrum_byte, *outlength ); length2 = *outlength; error = BZ2_bzBuffToBuffDecompress( (char*)*outptr, &length2, (char*)bzptr, bzlength, 0, 0 ); if( error != BZ_OK ) { libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "error decompressing bzip data" ); return LIBSPECTRUM_ERROR_LOGIC; } *outlength = length2; return LIBSPECTRUM_ERROR_NONE; } else { /* Unknown length, have to stream */ bz_stream stream; libspectrum_byte *ptr; size_t length; length = bzlength; *outptr = libspectrum_new( libspectrum_byte, length ); /* Use standard memory allocation/free routines */ stream.bzalloc = NULL; stream.bzfree = NULL; stream.opaque = NULL; error = BZ2_bzDecompressInit( &stream, 0, 0 ); if( error != BZ_OK ) { switch( error ) { case BZ_MEM_ERROR: libspectrum_print_error( LIBSPECTRUM_ERROR_MEMORY, "out of memory at %s:%d", __FILE__, __LINE__ ); libspectrum_free( *outptr ); return LIBSPECTRUM_ERROR_MEMORY; default: libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "bzip2_inflate: serious error from BZ2_bzDecompressInit: %d", error ); libspectrum_free( *outptr ); return LIBSPECTRUM_ERROR_LOGIC; } } stream.next_in = (char*)bzptr; stream.avail_in = bzlength; stream.next_out = (char*)*outptr; stream.avail_out = bzlength; while( 1 ) { error = BZ2_bzDecompress( &stream ); switch( error ) { case BZ_STREAM_END: /* Finished decompression */ error = BZ2_bzDecompressEnd( &stream ); if( error ) { libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "bzip2_inflate: error from BZ2_bzDecompressEnd: %d", error ); libspectrum_free( *outptr ); return LIBSPECTRUM_ERROR_LOGIC; } *outlength = stream.total_out_lo32; *outptr = libspectrum_renew( libspectrum_byte, *outptr, *outlength ); return LIBSPECTRUM_ERROR_NONE; case BZ_OK: /* More output space required */ length += bzlength; ptr = libspectrum_renew( libspectrum_byte, *outptr, length ); *outptr = ptr; stream.next_out = (char*)*outptr + stream.total_out_lo32; stream.avail_out += bzlength; break; default: libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "bzip2_inflate: serious error from BZ2_bzDecompress: %d", error ); BZ2_bzDecompressEnd( &stream ); libspectrum_free( *outptr ); return LIBSPECTRUM_ERROR_LOGIC; } } /* Matches while( 1 ) { ... } */ } /* Matches if( *outlength ) { ... } */ }