third_party/cobs-c-0.5.0/cobs.c
/* COBS-encode a string of input bytes.
 *
 * 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 encoded
 * src_len         Length of the byte string to be encoded
 *
 * returns:        A struct containing the success status of the encoding
 *                 operation and the length of the result (that was written to
 *                 dst_buf_ptr)
 */
COBS-encode a string of input bytes.
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 encoded
src_len         Length of the byte string to be encoded

A struct containing the success status of the encoding
operation and the length of the result (that was written to
dst_buf_ptr)
COBS - encode a string of input bytes . 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 encoded src_len Length of the byte string to be encoded A struct containing the success status of the encoding operation and the length of the result ( that was written to dst_buf_ptr )
cobs_encode_result cobs_encode(void * dst_buf_ptr, size_t dst_buf_len,
                               const void * src_ptr, size_t src_len)
{
    cobs_encode_result  result              = { 0, COBS_ENCODE_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_code_write_ptr  = dst_buf_ptr;
    uint8_t *           dst_write_ptr       = dst_code_write_ptr + 1;
    uint8_t             src_byte            = 0;
    uint8_t             search_len          = 1;
    if ((dst_buf_ptr == NULL) || (src_ptr == NULL))
    {
        result.status = COBS_ENCODE_NULL_POINTER;
        return result;
    }
    if (src_len != 0)
    {
        for (;;)
        {
            if (dst_write_ptr >= dst_buf_end_ptr)
            {
                result.status |= COBS_ENCODE_OUT_BUFFER_OVERFLOW;
                break;
            }
            src_byte = *src_read_ptr++;
            if (src_byte == 0)
            {
                *dst_code_write_ptr = search_len;
                dst_code_write_ptr = dst_write_ptr++;
                search_len = 1;
                if (src_read_ptr >= src_end_ptr)
                {
                    break;
                }
            }
            else
            {
                *dst_write_ptr++ = src_byte;
                search_len++;
                if (src_read_ptr >= src_end_ptr)
                {
                    break;
                }
                if (search_len == 0xFF)
                {
                    *dst_code_write_ptr = search_len;
                    dst_code_write_ptr = dst_write_ptr++;
                    search_len = 1;
                }
            }
        }
    }
    if (dst_code_write_ptr >= dst_buf_end_ptr)
    {
        result.status |= COBS_ENCODE_OUT_BUFFER_OVERFLOW;
        dst_write_ptr = dst_buf_end_ptr;
    }
    else
    {
        *dst_code_write_ptr = search_len;
    }
    result.out_len = dst_write_ptr - dst_buf_start_ptr;
    return result;
}
cobs_encode_result cobs_encode ( void * dst_buf_ptr , size_t dst_buf_len , const void * src_ptr , size_t src_len ) { cobs_encode_result result = { 0 , COBS_ENCODE_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_code_write_ptr = dst_buf_ptr ; uint8_t * dst_write_ptr = dst_code_write_ptr + 1 ; uint8_t src_byte = 0 ; uint8_t search_len = 1 ; if ( ( dst_buf_ptr == NULL ) || ( src_ptr == NULL ) ) { result . status = COBS_ENCODE_NULL_POINTER ; return result ; } if ( src_len != 0 ) { for ( ; ; ) { if ( dst_write_ptr >= dst_buf_end_ptr ) { result . status |= COBS_ENCODE_OUT_BUFFER_OVERFLOW ; break ; } src_byte = * src_read_ptr ++ ; if ( src_byte == 0 ) { * dst_code_write_ptr = search_len ; dst_code_write_ptr = dst_write_ptr ++ ; search_len = 1 ; if ( src_read_ptr >= src_end_ptr ) { break ; } } else { * dst_write_ptr ++ = src_byte ; search_len ++ ; if ( src_read_ptr >= src_end_ptr ) { break ; } if ( search_len == 0xFF ) { * dst_code_write_ptr = search_len ; dst_code_write_ptr = dst_write_ptr ++ ; search_len = 1 ; } } } } if ( dst_code_write_ptr >= dst_buf_end_ptr ) { result . status |= COBS_ENCODE_OUT_BUFFER_OVERFLOW ; dst_write_ptr = dst_buf_end_ptr ; } else { * dst_code_write_ptr = search_len ; } result . out_len = dst_write_ptr - dst_buf_start_ptr ; return result ; }
COBS-encode a string of input bytes.
COBS - encode a string of input bytes .
/* First, do a NULL pointer check and return immediately if it fails. */ /* Iterate over the source bytes */ /* Check for running out of output buffer space */ /* We found a zero byte */ /* Copy the non-zero byte to the destination buffer */ /* We have a long string of non-zero bytes, so we need
                     * to write out a length code of 0xFF. */ /* We've reached the end of the source data (or possibly run out of output buffer)
     * Finalise the remaining output. In particular, write the code (length) byte.
     * Update the pointer to calculate the final output length.
     */ /* We've run out of output buffer to write the code byte. */ /* Write the last code (length) byte. */ /* Calculate the output length, from the value of dst_code_write_ptr */