in source/huffman.c [131:187]
int aws_huffman_encode(
struct aws_huffman_encoder *encoder,
struct aws_byte_cursor *to_encode,
struct aws_byte_buf *output) {
AWS_ASSERT(encoder);
AWS_ASSERT(encoder->coder);
AWS_ASSERT(to_encode);
AWS_ASSERT(output);
struct encoder_state state = {
.working = 0,
.bit_pos = 8,
};
state.encoder = encoder;
state.output_buf = output;
/* Write any bits leftover from previous invocation */
if (encoder->overflow_bits.num_bits) {
if (output->len == output->capacity) {
return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
}
if (encode_write_bit_pattern(&state, encoder->overflow_bits)) {
return AWS_OP_ERR;
}
encoder->overflow_bits.num_bits = 0;
}
while (to_encode->len) {
if (output->len == output->capacity) {
return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
}
uint8_t new_byte = 0;
aws_byte_cursor_read_u8(to_encode, &new_byte);
struct aws_huffman_code code_point = encoder->coder->encode(new_byte, encoder->coder->userdata);
if (encode_write_bit_pattern(&state, code_point)) {
return AWS_OP_ERR;
}
}
/* The following code only runs when the buffer has written successfully */
/* If whole buffer processed, write EOS */
if (state.bit_pos != 8) {
struct aws_huffman_code eos_cp;
eos_cp.pattern = encoder->eos_padding;
eos_cp.num_bits = state.bit_pos;
encode_write_bit_pattern(&state, eos_cp);
AWS_ASSERT(state.bit_pos == 8);
}
return AWS_OP_SUCCESS;
}