heatshrink_encoder *heatshrink_encoder_alloc(uint8_t window_sz2, uint8_t lookahead_sz2) { if ((window_sz2 < HEATSHRINK_MIN_WINDOW_BITS) || (window_sz2 > HEATSHRINK_MAX_WINDOW_BITS) || (lookahead_sz2 < HEATSHRINK_MIN_LOOKAHEAD_BITS) || (lookahead_sz2 >= window_sz2)) { return NULL; } /* Note: 2 * the window size is used because the buffer needs to fit * (1 << window_sz2) bytes for the current input, and an additional * (1 << window_sz2) bytes for the previous buffer of input, which * will be scanned for useful backreferences. */ size_t buf_sz = (2 << window_sz2); heatshrink_encoder *hse = HEATSHRINK_MALLOC(sizeof(*hse) + buf_sz); if (hse == NULL) { return NULL; } hse->window_sz2 = window_sz2; hse->lookahead_sz2 = lookahead_sz2; heatshrink_encoder_reset(hse); #if HEATSHRINK_USE_INDEX size_t index_sz = buf_sz*sizeof(uint16_t); hse->search_index = HEATSHRINK_MALLOC(index_sz + sizeof(struct hs_index)); if (hse->search_index == NULL) { HEATSHRINK_FREE(hse, sizeof(*hse) + buf_sz); return NULL; } hse->search_index->size = index_sz; #endif LOG("-- allocated encoder with buffer size of %zu (%u byte input size)\n", buf_sz, get_input_buffer_size(hse)); return hse; }
static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, const struct audio_config *config) { int channel_count = popcount(config->channel_mask); return get_input_buffer_size(config->sample_rate, config->format, channel_count); }
HSE_sink_res heatshrink_encoder_sink(heatshrink_encoder *hse, uint8_t *in_buf, size_t size, size_t *input_size) { if ((hse == NULL) || (in_buf == NULL) || (input_size == NULL)) { return HSER_SINK_ERROR_NULL; } /* Sinking more content after saying the content is done, tsk tsk */ if (is_finishing(hse)) { return HSER_SINK_ERROR_MISUSE; } /* Sinking more content before processing is done */ if (hse->state != HSES_NOT_FULL) { return HSER_SINK_ERROR_MISUSE; } uint16_t write_offset = get_input_offset(hse) + hse->input_size; uint16_t ibs = get_input_buffer_size(hse); uint16_t rem = ibs - hse->input_size; uint16_t cp_sz = rem < size ? rem : size; memcpy(&hse->buffer[write_offset], in_buf, cp_sz); *input_size = cp_sz; hse->input_size += cp_sz; LOG("-- sunk %u bytes (of %zu) into encoder at %d, input buffer now has %u\n", cp_sz, size, write_offset, hse->input_size); if (cp_sz == rem) { LOG("-- internal buffer is now full\n"); hse->state = HSES_FILLED; } return HSER_SINK_OK; }
static HSE_state ICACHE_FLASH_ATTR st_step_search(heatshrink_encoder *hse) { uint16_t window_length = get_input_buffer_size(hse); uint16_t lookahead_sz = get_lookahead_size(hse); uint16_t msi = hse->match_scan_index; LOG("## step_search, scan @ +%d (%d/%d), input size %d\n", msi, hse->input_size + msi, 2*window_length, hse->input_size); bool fin = is_finishing(hse); if (msi >= hse->input_size - (fin ? 0 : lookahead_sz)) { /* Current search buffer is exhausted, copy it into the * backlog and await more input. */ LOG("-- end of search @ %d, saving backlog\n", msi); return HSES_SAVE_BACKLOG; } uint16_t input_offset = get_input_offset(hse); uint16_t end = input_offset + msi; uint16_t start = 0; if (backlog_is_filled(hse)) { /* last WINDOW_LENGTH bytes */ start = end - window_length + 1; } else if (backlog_is_partial(hse)) { /* clamp to available data */ start = end - window_length + 1; if (start < lookahead_sz) { start = lookahead_sz; } } else { /* only scan available input */ start = input_offset; } uint16_t max_possible = lookahead_sz; if (hse->input_size - msi < lookahead_sz) { max_possible = hse->input_size - msi; } uint16_t match_length = 0; uint16_t match_pos = find_longest_match(hse, start, end, max_possible, &match_length); if (match_pos == MATCH_NOT_FOUND) { LOG("ss Match not found\n"); hse->match_scan_index++; hse->flags |= FLAG_HAS_LITERAL; hse->match_length = 0; return HSES_YIELD_TAG_BIT; } else { LOG("ss Found match of %d bytes at %d\n", match_length, match_pos); hse->match_pos = match_pos; hse->match_length = match_length; ASSERT(match_pos < 1 << hse->window_sz2 /*window_length*/); return HSES_YIELD_TAG_BIT; } }
static int adev_open_input_stream(struct audio_hw_device *dev, audio_io_handle_t handle, audio_devices_t devices, struct audio_config *config, struct audio_stream_in **stream_in) { struct audio_device *adev = (struct audio_device *)dev; struct stream_in *in; int ret, buffer_size, frame_size; int channel_count = popcount(config->channel_mask); ALOGV("%s: enter", __func__); *stream_in = NULL; if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) return -EINVAL; in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); if (!in) return -ENOMEM; in->stream.common.get_sample_rate = in_get_sample_rate; in->stream.common.set_sample_rate = in_set_sample_rate; in->stream.common.get_buffer_size = in_get_buffer_size; in->stream.common.get_channels = in_get_channels; in->stream.common.get_format = in_get_format; in->stream.common.standby = in_standby; in->stream.common.set_parameters = in_set_parameters; in->stream.common.get_parameters = in_get_parameters; in->stream.read = in_read; in->stream.get_input_frames_lost = in_get_input_frames_lost; in->device = devices; in->source = AUDIO_SOURCE_DEFAULT; in->dev = adev; in->standby = true; in->channel_mask = config->channel_mask; /* Update config params with the requested sample rate and channels */ in->pcm_config = pcm_config_audio_capture; in->pcm_config.channels = channel_count; in->pcm_config.rate = config->sample_rate; frame_size = audio_stream_frame_size((struct audio_stream *)in); buffer_size = get_input_buffer_size(config->sample_rate, config->format, channel_count); in->pcm_config.period_size = buffer_size / frame_size; *stream_in = &in->stream; ALOGV("%s: exit", __func__); return 0; }
int main(int argc, char* argv[]) { char* buff = (char*)malloc(get_input_buffer_size(1024)); size_t bytes_read; int fd = fileno(stdin); print_prompt(); while((bytes_read = read(fd, buff, 1024)) > 0) { parse_command(buff); memset(buff, 0, 1024); print_prompt(); } memset(buff, 0, 1024); free(buff); return(0); }
static HSE_state st_step_search(heatshrink_encoder *hse) { uint16_t window_length = get_input_buffer_size(hse); uint16_t lookahead_sz = get_lookahead_size(hse); uint16_t msi = hse->match_scan_index; LOG("## step_search, scan @ +%d (%d/%d), input size %d\n", msi, hse->input_size + msi, 2*window_length, hse->input_size); bool fin = is_finishing(hse); if (msi > hse->input_size - (fin ? 1 : lookahead_sz)) { /* Current search buffer is exhausted, copy it into the * backlog and await more input. */ LOG("-- end of search @ %d\n", msi); return fin ? HSES_FLUSH_BITS : HSES_SAVE_BACKLOG; } uint16_t input_offset = get_input_offset(hse); uint16_t end = input_offset + msi; uint16_t start = end - window_length; uint16_t max_possible = lookahead_sz; if (hse->input_size - msi < lookahead_sz) { max_possible = hse->input_size - msi; } uint16_t match_length = 0; uint16_t match_pos = find_longest_match(hse, start, end, max_possible, &match_length); if (match_pos == MATCH_NOT_FOUND) { LOG("ss Match not found\n"); hse->match_scan_index++; hse->match_length = 0; return HSES_YIELD_TAG_BIT; } else { LOG("ss Found match of %d bytes at %d\n", match_length, match_pos); hse->match_pos = match_pos; hse->match_length = match_length; ASSERT(match_pos < 1 << HEATSHRINK_ENCODER_WINDOW_BITS(hse) /*window_length*/); return HSES_YIELD_TAG_BIT; } }
void parse_command(char* buff) { char* tmp = (char*)malloc(get_input_buffer_size(1024)); memcpy(tmp, buff, 1024); char* cmd = strtok(tmp, " \0"); char* t; node* root = (node*)malloc(sizeof(node)); root->next = NULL; root->data = cmd; while((t = strtok(NULL, " \0")) != NULL) { add_node_to_list(root, t); } node* ptr = root; do { printf("%s\n", ptr->data); ptr = ptr->next; } while(ptr != NULL); }
static uint16_t get_input_offset(heatshrink_encoder *hse) { return get_input_buffer_size(hse); }
static uint16_t ICACHE_FLASH_ATTR get_input_offset(heatshrink_encoder *hse) { return get_input_buffer_size(hse); }