/** gets data from array, writes to callback if nonzero. Returns total length. */ uint32_t heatshrink_encode(unsigned char *data, size_t dataLen, void (*callback)(unsigned char ch, uint32_t *cbdata), uint32_t *cbdata) { heatshrink_encoder hse; uint8_t outBuf[BUFFERSIZE]; heatshrink_encoder_reset(&hse); size_t i; size_t count = 0; size_t sunk = 0; size_t polled = 0; while (sunk < dataLen) { bool ok = heatshrink_encoder_sink(&hse, &data[sunk], dataLen - sunk, &count) >= 0; assert(ok);NOT_USED(ok); sunk += count; if (sunk == dataLen) { heatshrink_encoder_finish(&hse); } HSE_poll_res pres; do { pres = heatshrink_encoder_poll(&hse, outBuf, sizeof(outBuf), &count); assert(pres >= 0); if (callback) for (i=0;i<count;i++) callback(outBuf[i], cbdata); polled += count; } while (pres == HSER_POLL_MORE); assert(pres == HSER_POLL_EMPTY); if (sunk == dataLen) { heatshrink_encoder_finish(&hse); } } return (uint32_t)polled; }
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 int compress_and_expand_and_check(uint8_t *input, uint32_t input_size) { // clear encoder&decoder state machines heatshrink_encoder_reset(&hse); heatshrink_decoder_reset(&hsd); // allocate memory for before and after size_t comp_sz = input_size + (input_size/2) + 4; uint8_t *comp = malloc(comp_sz); if (comp == NULL) printf("malloc fail\r\n"); memset(comp, 0, comp_sz); size_t count = 0; uint32_t sunk = 0; uint32_t polled = 0; while (sunk < input_size) { ASSERT(heatshrink_encoder_sink(&hse, &input[sunk], input_size - sunk, &count) >= 0); sunk += count; if (sunk == input_size) { ASSERT_EQ(HSER_FINISH_MORE, heatshrink_encoder_finish(&hse)); } HSE_poll_res pres; do { /* "turn the crank" */ pres = heatshrink_encoder_poll(&hse, &comp[polled], comp_sz - polled, &count); ASSERT(pres >= 0); polled += count; } while (pres == HSER_POLL_MORE); ASSERT_EQ(HSER_POLL_EMPTY, pres); if (polled >= comp_sz) { printf("compression should never expand that muchr\r\n"); show_error(); return-1;} if (sunk == input_size) { ASSERT_EQ(HSER_FINISH_DONE, heatshrink_encoder_finish(&hse)); } } for (int i = 0; i < polled; i++) { while(app_uart_put(comp[i]) != NRF_SUCCESS); // for(int time_ctr = 0; time_ctr <100000; time_ctr ++); } // while(app_uart_put(13) != NRF_SUCCESS); // printf("%d", polled); free(comp); }
void compress(uint32_t buf, uint32_t size) { size_t count = 0; uint32_t sunk = 0; uint32_t polled = 0; // Compression in progress. //compression_in_progress = true; //printf("Buf %d compression start...\r\n", buf); // Clear encoder state machine heatshrink_encoder_reset(&hse); while (sunk < size) { ASSERTC(heatshrink_encoder_sink(&hse, &(data_buffers[buf][sunk]), size - sunk, &count) >= 0); sunk += count; if (sunk == size) { ASSERT_EQ(HSER_FINISH_MORE, heatshrink_encoder_finish(&hse)); } HSE_poll_res pres; do { /* "turn the crank" */ pres = heatshrink_encoder_poll(&hse, &transmission_buffer[polled], TRANSMISSION_BUF_SIZE - polled, &count); ASSERTC(pres >= 0); polled += count; } while (pres == HSER_POLL_MORE); ASSERT_EQ(HSER_POLL_EMPTY, pres); if (polled >= TRANSMISSION_BUF_SIZE) { printf("compression should never expand that muchr\r\n"); } if (sunk == size) { ASSERT_EQ(HSER_FINISH_DONE, heatshrink_encoder_finish(&hse)); } } // Finished compression. buffers_ready[buf] = false; printf("Buf %d compression done\r\n", buf); send_data(transmission_buffer, polled); }
static int compress_and_expand_and_check(uint8_t *input, uint32_t input_size, int log_lvl) { heatshrink_encoder_reset(&hse); heatshrink_decoder_reset(&hsd); size_t comp_sz = input_size + (input_size/2) + 4; size_t decomp_sz = input_size + (input_size/2) + 4; uint8_t *comp = malloc(comp_sz); uint8_t *decomp = malloc(decomp_sz); if (comp == NULL) FAILm("malloc fail"); if (decomp == NULL) FAILm("malloc fail"); memset(comp, 0, comp_sz); memset(decomp, 0, decomp_sz); size_t count = 0; if (log_lvl > 1) { printf("\n^^ COMPRESSING\n"); dump_buf("input", input, input_size); } uint32_t sunk = 0; uint32_t polled = 0; while (sunk < input_size) { ASSERT(heatshrink_encoder_sink(&hse, &input[sunk], input_size - sunk, &count) >= 0); sunk += count; if (log_lvl > 1) printf("^^ sunk %zd\n", count); if (sunk == input_size) { ASSERT_EQ(HSER_FINISH_MORE, heatshrink_encoder_finish(&hse)); } HSE_poll_res pres; do { /* "turn the crank" */ pres = heatshrink_encoder_poll(&hse, &comp[polled], comp_sz - polled, &count); ASSERT(pres >= 0); polled += count; if (log_lvl > 1) printf("^^ polled %zd\n", count); } while (pres == HSER_POLL_MORE); ASSERT_EQ(HSER_POLL_EMPTY, pres); if (polled >= comp_sz) FAILm("compression should never expand that much"); if (sunk == input_size) { ASSERT_EQ(HSER_FINISH_DONE, heatshrink_encoder_finish(&hse)); } } if (log_lvl > 0) printf("in: %u compressed: %u ", input_size, polled); uint32_t compressed_size = polled; sunk = 0; polled = 0; if (log_lvl > 1) { printf("\n^^ DECOMPRESSING\n"); dump_buf("comp", comp, compressed_size); } while (sunk < compressed_size) { ASSERT(heatshrink_decoder_sink(&hsd, &comp[sunk], compressed_size - sunk, &count) >= 0); sunk += count; if (log_lvl > 1) printf("^^ sunk %zd\n", count); if (sunk == compressed_size) { ASSERT_EQ(HSDR_FINISH_MORE, heatshrink_decoder_finish(&hsd)); } HSD_poll_res pres; do { pres = heatshrink_decoder_poll(&hsd, &decomp[polled], decomp_sz - polled, &count); ASSERT(pres >= 0); polled += count; if (log_lvl > 1) printf("^^ polled %zd\n", count); } while (pres == HSDR_POLL_MORE); ASSERT_EQ(HSDR_POLL_EMPTY, pres); if (sunk == compressed_size) { HSD_finish_res fres = heatshrink_decoder_finish(&hsd); ASSERT_EQ(HSDR_FINISH_DONE, fres); } if (polled > input_size) { FAILm("Decompressed data is larger than original input"); } } if (log_lvl > 0) printf("decompressed: %u\n", polled); if (polled != input_size) { FAILm("Decompressed length does not match original input length"); } if (log_lvl > 1) dump_buf("decomp", decomp, polled); for (size_t i=0; i<input_size; i++) { if (input[i] != decomp[i]) { printf("*** mismatch at %zd\n", i); if (0) { for (size_t j=0; j<=/*i*/ input_size; j++) { printf("in[%zd] == 0x%02x ('%c') => out[%zd] == 0x%02x ('%c')\n", j, input[j], isprint(input[j]) ? input[j] : '.', j, decomp[j], isprint(decomp[j]) ? decomp[j] : '.'); } } } ASSERT_EQ(input[i], decomp[i]); } free(comp); free(decomp); PASS(); }