third_party/cobs-c-0.5.0/cobs.c
/* Decode a COBS byte string.
 *
 * dst_buf_ptr:    The buffer into which the result will be written
 * dst_buf_len:    Length of the buffer into which the result will be written
 * src_ptr:        The byte string to be decoded
 * src_len         Length of the byte string to be decoded
 *
 * returns:        A struct containing the success status of the decoding
 *                 operation and the length of the result (that was written to
 *                 dst_buf_ptr)
 */
Decode a COBS byte string.
dst_buf_ptr:    The buffer into which the result will be written
dst_buf_len:    Length of the buffer into which the result will be written
src_ptr:        The byte string to be decoded
src_len         Length of the byte string to be decoded

A struct containing the success status of the decoding
operation and the length of the result (that was written to
dst_buf_ptr)
Decode a COBS byte string . dst_buf_ptr : The buffer into which the result will be written dst_buf_len : Length of the buffer into which the result will be written src_ptr : The byte string to be decoded src_len Length of the byte string to be decoded A struct containing the success status of the decoding operation and the length of the result ( that was written to dst_buf_ptr )
cobs_decode_result cobs_decode(void * dst_buf_ptr, size_t dst_buf_len,
                               const void * src_ptr, size_t src_len)
{
    cobs_decode_result  result              = { 0, COBS_DECODE_OK };
    const uint8_t *     src_read_ptr        = src_ptr;
    const uint8_t *     src_end_ptr         = src_ptr + src_len;
    uint8_t *           dst_buf_start_ptr   = dst_buf_ptr;
    uint8_t *           dst_buf_end_ptr     = dst_buf_ptr + dst_buf_len;
    uint8_t *           dst_write_ptr       = dst_buf_ptr;
    size_t              remaining_bytes;
    uint8_t             src_byte;
    uint8_t             i;
    uint8_t             len_code;
    if ((dst_buf_ptr == NULL) || (src_ptr == NULL))
    {
        result.status = COBS_DECODE_NULL_POINTER;
        return result;
    }
    if (src_len != 0)
    {
        for (;;)
        {
            len_code = *src_read_ptr++;
            if (len_code == 0)
            {
                result.status |= COBS_DECODE_ZERO_BYTE_IN_INPUT;
                break;
            }
            len_code--;
            remaining_bytes = src_end_ptr - src_read_ptr;
            if (len_code > remaining_bytes)
            {
                result.status |= COBS_DECODE_INPUT_TOO_SHORT;
                len_code = remaining_bytes;
            }
            remaining_bytes = dst_buf_end_ptr - dst_write_ptr;
            if (len_code > remaining_bytes)
            {
                result.status |= COBS_DECODE_OUT_BUFFER_OVERFLOW;
                len_code = remaining_bytes;
            }
            for (i = len_code; i != 0; i--)
            {
                src_byte = *src_read_ptr++;
                if (src_byte == 0)
                {
                    result.status |= COBS_DECODE_ZERO_BYTE_IN_INPUT;
                }
                *dst_write_ptr++ = src_byte;
            }
            if (src_read_ptr >= src_end_ptr)
            {
                break;
            }
            if (len_code != 0xFE)
            {
                if (dst_write_ptr >= dst_buf_end_ptr)
                {
                    result.status |= COBS_DECODE_OUT_BUFFER_OVERFLOW;
                    break;
                }
                *dst_write_ptr++ = 0;
            }
        }
    }
    result.out_len = dst_write_ptr - dst_buf_start_ptr;
    return result;
}
cobs_decode_result cobs_decode ( void * dst_buf_ptr , size_t dst_buf_len , const void * src_ptr , size_t src_len ) { cobs_decode_result result = { 0 , COBS_DECODE_OK } ; const uint8_t * src_read_ptr = src_ptr ; const uint8_t * src_end_ptr = src_ptr + src_len ; uint8_t * dst_buf_start_ptr = dst_buf_ptr ; uint8_t * dst_buf_end_ptr = dst_buf_ptr + dst_buf_len ; uint8_t * dst_write_ptr = dst_buf_ptr ; size_t remaining_bytes ; uint8_t src_byte ; uint8_t i ; uint8_t len_code ; if ( ( dst_buf_ptr == NULL ) || ( src_ptr == NULL ) ) { result . status = COBS_DECODE_NULL_POINTER ; return result ; } if ( src_len != 0 ) { for ( ; ; ) { len_code = * src_read_ptr ++ ; if ( len_code == 0 ) { result . status |= COBS_DECODE_ZERO_BYTE_IN_INPUT ; break ; } len_code -- ; remaining_bytes = src_end_ptr - src_read_ptr ; if ( len_code > remaining_bytes ) { result . status |= COBS_DECODE_INPUT_TOO_SHORT ; len_code = remaining_bytes ; } remaining_bytes = dst_buf_end_ptr - dst_write_ptr ; if ( len_code > remaining_bytes ) { result . status |= COBS_DECODE_OUT_BUFFER_OVERFLOW ; len_code = remaining_bytes ; } for ( i = len_code ; i != 0 ; i -- ) { src_byte = * src_read_ptr ++ ; if ( src_byte == 0 ) { result . status |= COBS_DECODE_ZERO_BYTE_IN_INPUT ; } * dst_write_ptr ++ = src_byte ; } if ( src_read_ptr >= src_end_ptr ) { break ; } if ( len_code != 0xFE ) { if ( dst_write_ptr >= dst_buf_end_ptr ) { result . status |= COBS_DECODE_OUT_BUFFER_OVERFLOW ; break ; } * dst_write_ptr ++ = 0 ; } } } result . out_len = dst_write_ptr - dst_buf_start_ptr ; return result ; }
Decode a COBS byte string.
Decode a COBS byte string .
/* First, do a NULL pointer check and return immediately if it fails. */ /* Check length code against remaining input bytes */ /* Check length code against remaining output buffer space */ /* Add a zero to the end */