/* * coreMQTT * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * SPDX-License-Identifier: MIT * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * @file core_mqtt_utils.c * @brief Implements the utility functions in core_mqtt_utils.h. */ #include #include #include /* Include config defaults header to get default values of configs. */ #include "core_mqtt_config_defaults.h" #include "core_mqtt_utils.h" uint8_t * encodeVariableLength( uint8_t * pDestination, size_t length ) { uint8_t lengthByte; uint8_t * pLengthEnd = NULL; size_t remainingLength = length; assert( pDestination != NULL ); pLengthEnd = pDestination; /* This algorithm is copied from the MQTT 5.0 spec. */ do { lengthByte = ( uint8_t ) ( remainingLength % 128U ); remainingLength = remainingLength / 128U; /* Set the high bit of this byte, indicating that there's more data. */ if( remainingLength > 0U ) { UINT8_SET_BIT( lengthByte, 7 ); } /* Output a single encoded byte. */ *pLengthEnd = lengthByte; pLengthEnd++; } while( remainingLength > 0U ); return pLengthEnd; }