HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size) { if ((hsd == NULL) || (out_buf == NULL) || (output_size == NULL)) { return HSDR_POLL_ERROR_NULL; } *output_size = 0; output_info oi; oi.buf = out_buf; oi.buf_size = out_buf_size; oi.output_size = output_size; while (1) { LOG("-- poll, state is %d (%s), input_size %d\n", hsd->state, state_names[hsd->state], hsd->input_size); uint8_t in_state = hsd->state; switch (in_state) { case HSDS_EMPTY: return HSDR_POLL_EMPTY; case HSDS_INPUT_AVAILABLE: hsd->state = st_input_available(hsd); break; case HSDS_YIELD_LITERAL: hsd->state = st_yield_literal(hsd, &oi); break; case HSDS_BACKREF_INDEX_MSB: hsd->state = st_backref_index_msb(hsd); break; case HSDS_BACKREF_INDEX_LSB: hsd->state = st_backref_index_lsb(hsd); break; case HSDS_BACKREF_COUNT_MSB: hsd->state = st_backref_count_msb(hsd); break; case HSDS_BACKREF_COUNT_LSB: hsd->state = st_backref_count_lsb(hsd); break; case HSDS_YIELD_BACKREF: hsd->state = st_yield_backref(hsd, &oi); break; case HSDS_CHECK_FOR_MORE_INPUT: hsd->state = st_check_for_input(hsd); break; default: return HSDR_POLL_ERROR_UNKNOWN; } /* If the current state cannot advance, check if input or output * buffer are exhausted. */ if (hsd->state == in_state) { if (*output_size == out_buf_size) { return HSDR_POLL_MORE; } return HSDR_POLL_EMPTY; } } }
HSE_poll_res heatshrink_encoder_poll(heatshrink_encoder *hse, uint8_t *out_buf, size_t out_buf_size, size_t *output_size) { if ((hse == NULL) || (out_buf == NULL) || (output_size == NULL)) { return HSER_POLL_ERROR_NULL; } if (out_buf_size == 0) { LOG("-- MISUSE: output buffer size is 0\n"); return HSER_POLL_ERROR_MISUSE; } *output_size = 0; output_info oi; oi.buf = out_buf; oi.buf_size = out_buf_size; oi.output_size = output_size; while (1) { LOG("-- polling, state %u (%s), flags 0x%02x\n", hse->state, state_names[hse->state], hse->flags); uint8_t in_state = hse->state; switch (in_state) { case HSES_NOT_FULL: return HSER_POLL_EMPTY; case HSES_FILLED: do_indexing(hse); hse->state = HSES_SEARCH; break; case HSES_SEARCH: hse->state = st_step_search(hse); break; case HSES_YIELD_TAG_BIT: hse->state = st_yield_tag_bit(hse, &oi); break; case HSES_YIELD_LITERAL: hse->state = st_yield_literal(hse, &oi); break; case HSES_YIELD_BR_INDEX: hse->state = st_yield_br_index(hse, &oi); break; case HSES_YIELD_BR_LENGTH: hse->state = st_yield_br_length(hse, &oi); break; case HSES_SAVE_BACKLOG: hse->state = st_save_backlog(hse); break; case HSES_FLUSH_BITS: hse->state = st_flush_bit_buffer(hse, &oi); case HSES_DONE: return HSER_POLL_EMPTY; default: LOG("-- bad state %s\n", state_names[hse->state]); return HSER_POLL_ERROR_MISUSE; } if (hse->state == in_state) { /* Check if output buffer is exhausted. */ if (*output_size == out_buf_size) return HSER_POLL_MORE; } } }