LCOV - code coverage report
Current view: top level - lib/core - CHIPSafeCasts.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 10 10 100.0 %
Date: 2024-02-15 08:20:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2020 Project CHIP Authors
       4             :  *
       5             :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6             :  *    you may not use this file except in compliance with the License.
       7             :  *    You may obtain a copy of the License at
       8             :  *
       9             :  *        http://www.apache.org/licenses/LICENSE-2.0
      10             :  *
      11             :  *    Unless required by applicable law or agreed to in writing, software
      12             :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13             :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14             :  *    See the License for the specific language governing permissions and
      15             :  *    limitations under the License.
      16             :  */
      17             : 
      18             : /**
      19             :  *    @file
      20             :  *      This file provides functions for safely type casting some basic
      21             :  *      datatypes (e.g. uint8_t * <-> unsigned char *)
      22             :  *
      23             :  *
      24             :  */
      25             : 
      26             : #pragma once
      27             : 
      28             : #include <limits.h>
      29             : #include <nlassert.h>
      30             : #include <stdint.h>
      31             : 
      32             : namespace chip {
      33             : 
      34             : namespace Uint8 {
      35             : 
      36             : /**
      37             :  * Safely typecast a pointer to unsigned char to uint8_t
      38             :  *
      39             :  * @param[in]  in  pointer to unsigned char
      40             :  *
      41             :  * @return The typecast value
      42             :  */
      43             : inline uint8_t * from_uchar(unsigned char * in)
      44             : {
      45             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast unsigned char array as uint8_t array");
      46             : #ifdef __cplusplus
      47             :     return reinterpret_cast<uint8_t *>(in);
      48             : #else
      49             :     return (uint8_t *) in;
      50             : #endif
      51             : }
      52             : 
      53             : /**
      54             :  * Safely typecast a pointer to const unsigned char to const uint8_t
      55             :  *
      56             :  * @param[in]  in  pointer to const unsigned char
      57             :  *
      58             :  * @return The typecast value
      59             :  */
      60             : inline const uint8_t * from_const_uchar(const unsigned char * in)
      61             : {
      62             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast unsigned char array as uint8_t array");
      63             : #ifdef __cplusplus
      64             :     return reinterpret_cast<const uint8_t *>(in);
      65             : #else
      66             :     return (const uint8_t *) in;
      67             : #endif
      68             : }
      69             : 
      70             : /**
      71             :  * Safely typecast a pointer to char to uint8_t
      72             :  *
      73             :  * @param[in]  in  pointer to char
      74             :  *
      75             :  * @return The typecast value
      76             :  */
      77           2 : inline uint8_t * from_char(char * in)
      78             : {
      79             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast char array as uint8_t array");
      80             : #ifdef __cplusplus
      81           2 :     return reinterpret_cast<uint8_t *>(in);
      82             : #else
      83             :     return (uint8_t *) in;
      84             : #endif
      85             : }
      86             : 
      87             : /**
      88             :  * Safely typecast a pointer to const char to const uint8_t
      89             :  *
      90             :  * @param[in]  in  pointer to const char
      91             :  *
      92             :  * @return The typecast value
      93             :  */
      94        1439 : inline const uint8_t * from_const_char(const char * in)
      95             : {
      96             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast char array as uint8_t array");
      97             : #ifdef __cplusplus
      98        1439 :     return reinterpret_cast<const uint8_t *>(in);
      99             : #else
     100             :     return (const uint8_t *) in;
     101             : #endif
     102             : }
     103             : 
     104             : /**
     105             :  * Safely typecast a pointer to uint8_t to unsigned char
     106             :  *
     107             :  * @param[in]  in  pointer to uint8_t
     108             :  *
     109             :  * @return The typecast value
     110             :  */
     111      185895 : inline unsigned char * to_uchar(uint8_t * in)
     112             : {
     113             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast uint8_t array to unsigned char array");
     114             : #ifdef __cplusplus
     115      185895 :     return reinterpret_cast<unsigned char *>(in);
     116             : #else
     117             :     return (unsigned char *) in;
     118             : #endif
     119             : }
     120             : 
     121             : /**
     122             :  * Safely typecast a pointer to const uint8_t to const unsigned char
     123             :  *
     124             :  * @param[in]  in  pointer to const uint8_t
     125             :  *
     126             :  * @return The typecast value
     127             :  */
     128       77114 : inline const unsigned char * to_const_uchar(const uint8_t * in)
     129             : {
     130             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast uint8_t array to unsigned char array");
     131             : #ifdef __cplusplus
     132       77114 :     return reinterpret_cast<const unsigned char *>(in);
     133             : #else
     134             :     return (const unsigned char *) in;
     135             : #endif
     136             : }
     137             : 
     138             : /**
     139             :  * Safely typecast a pointer to uint8_t to char
     140             :  *
     141             :  * @param[in]  in  pointer to uint8_t
     142             :  *
     143             :  * @return The typecast value
     144             :  */
     145             : inline char * to_char(uint8_t * in)
     146             : {
     147             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast uint8_t array to char array");
     148             : #ifdef __cplusplus
     149             :     return reinterpret_cast<char *>(in);
     150             : #else
     151             :     return (char *) in;
     152             : #endif
     153             : }
     154             : 
     155             : /**
     156             :  * Safely typecast a pointer to const uint8_t to char
     157             :  *
     158             :  * @param[in]  in  pointer to const uint8_t
     159             :  *
     160             :  * @return The typecast value
     161             :  */
     162         367 : inline const char * to_const_char(const uint8_t * in)
     163             : {
     164             :     nlSTATIC_ASSERT_PRINT(CHAR_BIT == 8, "Can't type cast uint8_t array to char array");
     165             : #ifdef __cplusplus
     166         367 :     return reinterpret_cast<const char *>(in);
     167             : #else
     168             :     return (const char *) in;
     169             : #endif
     170             : }
     171             : 
     172             : } // namespace Uint8
     173             : } // namespace chip

Generated by: LCOV version 1.14