1
0
mirror of https://github.com/FreeRTOS/FreeRTOS-Plus-TCP synced 2025-10-20 13:23:44 +08:00
Files
FreeRTOS-Plus-TCP/source/FreeRTOS_BitConfig.c
Tony Josi 0e9628796c Fixing doxygen comments (#728)
* updating doxygen config

* fixing doxygen comments

* adding IPv6 files and fixing comments

* fix doxygen cfg and file names in comments

* wip doxygen v6 docs

* adding doxygen comments

* include RA src file to doxgendocs generation

* fix spell check issues

* Uncrustify: triggered by comment.

* fix minor build issue

* fix spell check issues

* Uncrustify: triggered by comment

* fix trailing white space

* Dev integration hein.v8 (#738)

* Updating tcp utilities

* Some more change in dev_integration_hein.v8

* In FreeRTOS_DNS_Parser.c : use 'ipUDP_PAYLOAD_OFFSET_IPv4' in stead of 'ipIP_PAYLOAD_OFFSET'

* And a few more corrections

* Changes to WinPCap network interface, removed debugging code

* After applying uncrustify

* Oops, I forgot the push changes in include files.

* Now removing it, hopefully

---------

Co-authored-by: Nikhil Kamath <110539926+amazonKamath@users.noreply.github.com>
Co-authored-by: Monika Singh <108652024+moninom1@users.noreply.github.com>

* Fix CBMC proofs for DNS (#718)

* Use CBMC XML output to enable VSCode debugger (#673)

Prior to this commit, CBMC would emit logging information in plain text
format, which does not contain information required for the CBMC VSCode
debugger. This commit makes CBMC use XML instead of plain text.

Co-authored-by: Mark Tuttle <tuttle@acm.org>

* wip

* wip DNSgetHostByName

* wip DNSgetHostByName

* fixed cbmc proof for DNS_ReadNameField

* wip DNSgetHostByName_a_harness

* Fix CBMC prooff for DNSgetHostByName

* wip fix DNSgetHostByName_a CBMC proof

* fixed cbmc target func not called issue in DNSclear

* fixed cbmc target func not called issue in DNSlookup

* fix DNSgetHostByName_a CBMC proof

* update comments

* more asserts

* fixing formatting

* updating as per review comments

* fix dns after review comments

* adding more asserts

* adds more asserts

* minor fix

* fixing comments

* fixing comments

* fixing minor issue

* fixing DNS_ReadReply() signature

* making code more consistant

* adding more  asserts

* making code more consistent

---------

Co-authored-by: Kareem Khazem <karkhaz@amazon.com>
Co-authored-by: Mark Tuttle <tuttle@acm.org>

* Uncrustify: triggered by comment

* fixing formatting

---------

Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: Hein Tibosch <hein_tibosch@yahoo.es>
Co-authored-by: Nikhil Kamath <110539926+amazonKamath@users.noreply.github.com>
Co-authored-by: Monika Singh <108652024+moninom1@users.noreply.github.com>
Co-authored-by: Kareem Khazem <karkhaz@amazon.com>
Co-authored-by: Mark Tuttle <tuttle@acm.org>
2023-02-24 13:58:53 +05:30

314 lines
9.6 KiB
C

/*
* FreeRTOS+TCP V2.3.1
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* 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.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file FreeRTOS_BitConfig.c
* @brief Some functions that help when analysing a binary stream of information.
* It offers an alternative to using packet structs with unaligned data members.
*/
/* Standard includes. */
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_BitConfig.h"
/*-----------------------------------------------------------*/
/**
* @brief Initialise a bit-config struct.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
* @param[in] uxSize: The length of the binary data stream.
* @param[in] pucData: Not NULL if a bit-stream must be analysed, otherwise NULL.
*
* @return pdTRUE if the malloc was OK, otherwise pdFALSE.
*/
BaseType_t xBitConfig_init( BitConfig_t * pxConfig,
const uint8_t * pucData,
size_t uxSize )
{
BaseType_t xResult = pdFALSE;
( void ) memset( ( void * ) pxConfig, 0, sizeof( *pxConfig ) );
pxConfig->ucContents = ( uint8_t * ) pvPortMalloc( uxSize );
if( pxConfig->ucContents != NULL )
{
pxConfig->uxSize = uxSize;
if( pucData != NULL )
{
( void ) memcpy( pxConfig->ucContents, pucData, uxSize );
}
else
{
( void ) memset( pxConfig->ucContents, 0, uxSize );
}
xResult = pdTRUE;
}
else
{
pxConfig->xHasError = pdTRUE;
}
return xResult;
}
/*-----------------------------------------------------------*/
/**
* @brief Initialise a bit-config struct.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
* @param[in] uxSize: The length of the binary data stream.
* @param[in] pucData: Not NULL if a bit-stream must be analysed, otherwise NULL.
*
* @return pdTRUE if the malloc was OK, otherwise pdFALSE.
*/
BaseType_t xBitConfig_read_uc( BitConfig_t * pxConfig,
uint8_t * pucData,
size_t uxSize )
{
BaseType_t xResult = pdFALSE;
const size_t uxNeeded = uxSize;
if( pxConfig->xHasError == pdFALSE )
{
if( pxConfig->uxIndex <= ( pxConfig->uxSize - uxNeeded ) )
{
if( pucData != NULL )
{
( void ) memcpy( pucData, &( pxConfig->ucContents[ pxConfig->uxIndex ] ), uxNeeded );
}
else
{
/* Caller just wants to skip some bytes. */
}
pxConfig->uxIndex += uxNeeded;
xResult = pdTRUE;
}
else
{
pxConfig->xHasError = pdTRUE;
}
}
return xResult;
}
/*-----------------------------------------------------------*/
/**
* @brief Read a byte from the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
*
* @return A byte value. When there was not enough data, xHasError will be set.
*/
uint8_t ucBitConfig_read_8( BitConfig_t * pxConfig )
{
uint8_t ucResult = 0xffU;
const size_t uxNeeded = sizeof ucResult;
uint8_t pucData[ sizeof ucResult ];
if( xBitConfig_read_uc( pxConfig, pucData, uxNeeded ) != pdFALSE )
{
ucResult = pucData[ 0 ];
}
return ucResult;
}
/*-----------------------------------------------------------*/
/**
* @brief Read 2 bytes from the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
*
* @return A 16-bit value. When there was not enough data, xHasError will be set.
*/
uint16_t usBitConfig_read_16( BitConfig_t * pxConfig )
{
uint16_t usResult = 0xffffU;
const size_t uxNeeded = sizeof usResult;
uint8_t pucData[ sizeof usResult ];
if( xBitConfig_read_uc( pxConfig, pucData, uxNeeded ) != pdFALSE )
{
usResult = ( ( ( uint16_t ) pucData[ 0 ] ) << 8 ) |
( ( ( uint16_t ) pucData[ 1 ] ) );
}
return usResult;
}
/*-----------------------------------------------------------*/
/**
* @brief Read 4 bytes from the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
*
* @return A 32-bit value. When there was not enough data, xHasError will be set.
*/
uint32_t ulBitConfig_read_32( BitConfig_t * pxConfig )
{
uint32_t ulResult = 0xffffffffU;
const size_t uxNeeded = sizeof ulResult;
uint8_t pucData[ sizeof ulResult ];
if( xBitConfig_read_uc( pxConfig, pucData, uxNeeded ) != pdFALSE )
{
ulResult = ( ( ( uint32_t ) pucData[ 0 ] ) << 24 ) |
( ( ( uint32_t ) pucData[ 1 ] ) << 16 ) |
( ( ( uint32_t ) pucData[ 2 ] ) << 8 ) |
( ( ( uint32_t ) pucData[ 3 ] ) );
}
return ulResult;
}
/*-----------------------------------------------------------*/
/**
* @brief Read any number bytes from the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bit stream.
* @param[in] pucData: The binary data to be written.
* @param[in] uxSize: The number of bytes to be written.
*
* There is no return value. If the operation has failed,
* the field xHasError will be set.
*/
void vBitConfig_write_uc( BitConfig_t * pxConfig,
const uint8_t * pucData,
size_t uxSize )
{
const size_t uxNeeded = uxSize;
if( pxConfig->xHasError == pdFALSE )
{
if( pxConfig->uxIndex <= ( pxConfig->uxSize - uxNeeded ) )
{
uint8_t * pucDestination = &( pxConfig->ucContents[ pxConfig->uxIndex ] );
( void ) memcpy( pucDestination, pucData, uxNeeded );
pxConfig->uxIndex += uxNeeded;
}
else
{
pxConfig->xHasError = pdTRUE;
}
}
}
/*-----------------------------------------------------------*/
/**
* @brief Write a byte to the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
* @param[in] ucValue: The byte to be written.
*
* There is no return value. If the operation has failed,
* the field xHasError will be set.
*/
void vBitConfig_write_8( BitConfig_t * pxConfig,
uint8_t ucValue )
{
const size_t uxNeeded = sizeof ucValue;
vBitConfig_write_uc( pxConfig, &( ucValue ), uxNeeded );
}
/*-----------------------------------------------------------*/
/**
* @brief Write a short word to the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
* @param[in] usValue: The 16-bit value to be written.
*
* There is no return value. If the operation has failed,
* the field xHasError will be set.
*/
void vBitConfig_write_16( BitConfig_t * pxConfig,
uint16_t usValue )
{
const size_t uxNeeded = sizeof usValue;
uint8_t pucData[ sizeof usValue ];
pucData[ 0 ] = ( uint8_t ) ( ( usValue >> 8 ) & 0xFFU );
pucData[ 1 ] = ( uint8_t ) ( usValue & 0xFFU );
vBitConfig_write_uc( pxConfig, pucData, uxNeeded );
}
/*-----------------------------------------------------------*/
/**
* @brief Write a 32-bit word to the bit stream.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
* @param[in] ulValue: The 32-bit value to be written.
*
* There is no return value. If the operation has failed,
* the field xHasError will be set.
*/
void vBitConfig_write_32( BitConfig_t * pxConfig,
uint32_t ulValue )
{
const size_t uxNeeded = sizeof ulValue;
uint8_t pucData[ sizeof ulValue ];
pucData[ 0 ] = ( uint8_t ) ( ( ulValue >> 24 ) & 0xFFU );
pucData[ 1 ] = ( uint8_t ) ( ( ulValue >> 16 ) & 0xFFU );
pucData[ 2 ] = ( uint8_t ) ( ( ulValue >> 8 ) & 0xFFU );
pucData[ 3 ] = ( uint8_t ) ( ulValue & 0xFFU );
vBitConfig_write_uc( pxConfig, pucData, uxNeeded );
}
/*-----------------------------------------------------------*/
/**
* @brief Deallocate ( release ) the buffer, and clear the bit stream structure.
* Note that the struct must have be initialised before calling this function.
*
* @param[in] pxConfig: The structure containing a copy of the bits.
*
*/
void vBitConfig_release( BitConfig_t * pxConfig )
{
if( pxConfig->ucContents != NULL )
{
vPortFree( pxConfig->ucContents );
}
( void ) memset( pxConfig, 0, sizeof( BitConfig_t ) );
}
/*-----------------------------------------------------------*/