LCOV - code coverage report
Current view: top level - lib/core - CHIPEncoding.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 69 71 97.2 %
Date: 2024-02-15 08:20:41 Functions: 28 29 96.6 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2020 Project CHIP Authors
       4             :  *    Copyright (c) 2013-2017 Nest Labs, Inc.
       5             :  *
       6             :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7             :  *    you may not use this file except in compliance with the License.
       8             :  *    You may obtain a copy of the License at
       9             :  *
      10             :  *        http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  *    Unless required by applicable law or agreed to in writing, software
      13             :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14             :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15             :  *    See the License for the specific language governing permissions and
      16             :  *    limitations under the License.
      17             :  */
      18             : 
      19             : /**
      20             :  *    @file
      21             :  *      This file provides functions for:
      22             :  *
      23             :  *      <ul>
      24             :  *         <li>Performing byte reordering by value for 16-, 32-, and 64-bit
      25             :  *         types.</li>
      26             :  *         <li>Safely performing simple, efficient memory-mapped
      27             :  *         accesses, potentially to unaligned memory locations, with or
      28             :  *         without byte reordering, to 8-, 16-, 32-, and 64-bit
      29             :  *         quantities, both with and without pointer management.</li>
      30             :  *      </ul>
      31             :  *
      32             :  */
      33             : 
      34             : #pragma once
      35             : 
      36             : // The nlio headers use [inout] instead of [in,out], which makes the clang
      37             : // documentation warning unhappy.  Suppress it for those headers.
      38             : #pragma GCC diagnostic push
      39             : #ifdef __clang__
      40             : #pragma GCC diagnostic ignored "-Wdocumentation"
      41             : #endif // __clang__
      42             : 
      43             : #include <nlbyteorder.hpp>
      44             : #include <nlio-byteorder.hpp>
      45             : #include <nlio.hpp>
      46             : 
      47             : #pragma GCC diagnostic pop
      48             : 
      49             : #include <stdint.h>
      50             : 
      51             : namespace chip {
      52             : 
      53             : /**
      54             :  *  @namespace chip::Encoding
      55             :  *
      56             :  *  This namespace provides functions for:
      57             :  *
      58             :  *  <ul>
      59             :  *     <li>Performing byte reordering by value for 16-, 32-, and 64-bit
      60             :  *     types.</li>
      61             :  *     <li>Safely performing simple, efficient memory-mapped
      62             :  *     accesses, potentially to unaligned memory locations, with or
      63             :  *     without byte reordering, to 8-, 16-, 32-, and 64-bit
      64             :  *     quantities, both with and without pointer management.</li>
      65             :  *  </ul>
      66             :  *
      67             :  */
      68             : namespace Encoding {
      69             : 
      70             : /**
      71             :  * This unconditionally performs a byte order swap by value of the
      72             :  * specified 16-bit value.
      73             :  *
      74             :  * @param[in]  v  The 16-bit value to be byte order swapped.
      75             :  *
      76             :  * @return The input value, byte order swapped.
      77             :  */
      78             : inline uint16_t Swap16(uint16_t v)
      79             : {
      80             :     return nl::ByteOrder::Swap16(v);
      81             : }
      82             : 
      83             : /**
      84             :  * This unconditionally performs a byte order swap by value of the
      85             :  * specified 32-bit value.
      86             :  *
      87             :  * @param[in]  v  The 32-bit value to be byte order swapped.
      88             :  *
      89             :  * @return The input value, byte order swapped.
      90             :  */
      91             : inline uint32_t Swap32(uint32_t v)
      92             : {
      93             :     return nl::ByteOrder::Swap32(v);
      94             : }
      95             : 
      96             : /**
      97             :  * This unconditionally performs a byte order swap by value of the
      98             :  * specified 64-bit value.
      99             :  *
     100             :  * @param[in]  v  The 64-bit value to be byte order swapped.
     101             :  *
     102             :  * @return The input value, byte order swapped.
     103             :  */
     104             : inline uint64_t Swap64(uint64_t v)
     105             : {
     106             :     return nl::ByteOrder::Swap64(v);
     107             : }
     108             : 
     109             : /**
     110             :  * Perform a, potentially unaligned, memory read of the 8-bit value
     111             :  * from the specified pointer address.
     112             :  *
     113             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     114             :  *                    the 8-bit value from.
     115             :  *
     116             :  * @return The 8-bit value at the specified pointer address.
     117             :  */
     118             : inline uint8_t Get8(const uint8_t * p)
     119             : {
     120             :     return nl::IO::Get8(p);
     121             : }
     122             : 
     123             : /**
     124             :  * Perform a, potentially unaligned, memory write of the target system
     125             :  * byte ordered 8-bit value to the specified pointer address.
     126             :  *
     127             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     128             :  *                    the 8-bit value to.
     129             :  *
     130             :  * @param[in]  v      The 8-bit value to write.
     131             :  *
     132             :  */
     133             : inline void Put8(uint8_t * p, uint8_t v)
     134             : {
     135             :     nl::IO::Put8(p, v);
     136             : }
     137             : 
     138             : /**
     139             :  * Perform a, potentially unaligned, memory read of the 8-bit value
     140             :  * from the specified pointer address and increment the pointer by
     141             :  * 8-bits (1 byte).
     142             :  *
     143             :  * @param[in,out] p   A reference to a constant pointer address,
     144             :  *                    potentially unaligned, to read the 8-bit value
     145             :  *                    from and to then increment by 8-bits (1 byte).
     146             :  *
     147             :  * @return The 8-bit value at the specified pointer address.
     148             :  */
     149     6311880 : inline uint8_t Read8(const uint8_t *& p)
     150             : {
     151     6311880 :     return nl::IO::Read8(reinterpret_cast<const void *&>(p));
     152             : }
     153             : 
     154             : /**
     155             :  * Perform a, potentially unaligned, memory read of the 8-bit value
     156             :  * from the specified pointer address and increment the pointer by
     157             :  * 8-bits (1 byte).
     158             :  *
     159             :  * @param[in,out] p   A reference to a pointer address, potentially
     160             :  *                    unaligned, to read the 8-bit value from and to
     161             :  *                    then increment by 8-bits (1 byte).
     162             :  *
     163             :  * @return The 8-bit value at the specified pointer address.
     164             :  */
     165             : inline uint8_t Read8(uint8_t *& p)
     166             : {
     167             :     return Read8(const_cast<const uint8_t *&>(p));
     168             : }
     169             : 
     170             : /**
     171             :  * Perform a, potentially unaligned, memory write of the 8-bit value
     172             :  * to the specified pointer address and increment the pointer by
     173             :  * 8-bits (1 byte).
     174             :  *
     175             :  * @param[in,out] p   A reference to a pointer address, potentially
     176             :  *                    unaligned, to read the 8-bit value from and to
     177             :  *                    then increment by 8-bits (1 byte).
     178             :  *
     179             :  * @param[in]     v   The 8-bit value to write.
     180             :  *
     181             :  */
     182      655885 : inline void Write8(uint8_t *& p, uint8_t v)
     183             : {
     184      655885 :     *p++ = v;
     185      655885 : }
     186             : 
     187             : /**
     188             :  *  @namespace chip::Encoding::LittleEndian
     189             :  *
     190             :  *  This namespace provides functions for:
     191             :  *
     192             :  *  <ul>
     193             :  *     <li>Performing byte reordering, relative to the host system, by
     194             :  *     value of little endian byte ordered values for 16-, 32-, and
     195             :  *     64-bit types.</li>
     196             :  *     <li>Safely performing simple, efficient memory-mapped accesses,
     197             :  *     potentially to unaligned memory locations, with or without byte
     198             :  *     reordering, to 8-, 16-, 32-, and 64-bit little endian byte
     199             :  *     ordered quantities, both with and without pointer
     200             :  *     management.</li>
     201             :  *  </ul>
     202             :  *
     203             :  *  On little endian host systems no actual byte reordering will
     204             :  *  occur. On other systems, byte reordering is performed as
     205             :  *  appropriate.
     206             :  *
     207             :  */
     208             : namespace LittleEndian {
     209             : 
     210             : /**
     211             :  * This conditionally performs, as necessary for the target system, a
     212             :  * byte order swap by value of the specified value, presumed to be in
     213             :  * little endian byte ordering to the target system (i.e. host)
     214             :  * byte ordering.
     215             :  */
     216             : template <typename T>
     217             : inline T HostSwap(T v);
     218             : 
     219             : // For completeness of the set, we have identity.
     220             : template <>
     221       81054 : inline uint8_t HostSwap<uint8_t>(uint8_t v)
     222             : {
     223       81054 :     return v;
     224             : }
     225             : 
     226             : /**
     227             :  * This conditionally performs, as necessary for the target system, a
     228             :  * byte order swap by value of the specified 16-bit value, presumed to
     229             :  * be in little endian byte ordering to the target system (i.e. host)
     230             :  * byte ordering.
     231             :  *
     232             :  * Consequently, on little endian target systems, this is a no-op and
     233             :  * on big endian target systems, this performs a reordering.
     234             :  *
     235             :  * @param[in]  v  The 16-bit value to be byte order swapped.
     236             :  *
     237             :  * @return The input value, if necessary, byte order swapped.
     238             :  */
     239       46129 : inline uint16_t HostSwap16(uint16_t v)
     240             : {
     241       46129 :     return nl::ByteOrder::Swap16LittleToHost(v);
     242             : }
     243             : 
     244             : template <>
     245       46129 : inline uint16_t HostSwap<uint16_t>(uint16_t v)
     246             : {
     247       46129 :     return HostSwap16(v);
     248             : }
     249             : 
     250             : /**
     251             :  * This conditionally performs, as necessary for the target system, a
     252             :  * byte order swap by value of the specified 32-bit value, presumed to
     253             :  * be in little endian byte ordering to the target system (i.e. host)
     254             :  * byte ordering.
     255             :  *
     256             :  * Consequently, on little endian target systems, this is a no-op and
     257             :  * on big endian target systems, this performs a reordering.
     258             :  *
     259             :  * @param[in]  v  The 32-bit value to be byte order swapped.
     260             :  *
     261             :  * @return The input value, if necessary, byte order swapped.
     262             :  */
     263       25394 : inline uint32_t HostSwap32(uint32_t v)
     264             : {
     265       25394 :     return nl::ByteOrder::Swap32LittleToHost(v);
     266             : }
     267             : 
     268             : template <>
     269       25394 : inline uint32_t HostSwap<uint32_t>(uint32_t v)
     270             : {
     271       25394 :     return HostSwap32(v);
     272             : }
     273             : 
     274             : /**
     275             :  * This conditionally performs, as necessary for the target system, a
     276             :  * byte order swap by value of the specified 64-bit value, presumed to
     277             :  * be in little endian byte ordering to the target system (i.e. host)
     278             :  * byte ordering.
     279             :  *
     280             :  * Consequently, on little endian target systems, this is a no-op and
     281             :  * on big endian target systems, this performs a reordering.
     282             :  *
     283             :  * @param[in]  v  The 64-bit value to be byte order swapped.
     284             :  *
     285             :  * @return The input value, if necessary, byte order swapped.
     286             :  */
     287         273 : inline uint64_t HostSwap64(uint64_t v)
     288             : {
     289         273 :     return nl::ByteOrder::Swap64LittleToHost(v);
     290             : }
     291             : 
     292             : template <>
     293         273 : inline uint64_t HostSwap<uint64_t>(uint64_t v)
     294             : {
     295         273 :     return HostSwap64(v);
     296             : }
     297             : 
     298             : /**
     299             :  * Perform a, potentially unaligned, memory read of the little endian
     300             :  * byte ordered 16-bit value from the specified pointer address,
     301             :  * perform byte reordering, as necessary, for the target system to
     302             :  * put the value in target system byte ordering.
     303             :  *
     304             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     305             :  *                    the 16-bit little endian byte ordered value from.
     306             :  *
     307             :  * @return The 16-bit value at the specified pointer address, if
     308             :  *         necessary, byte reordered.
     309             :  */
     310           0 : inline uint16_t Get16(const uint8_t * p)
     311             : {
     312           0 :     return nl::IO::LittleEndian::GetUnaligned16(p);
     313             : }
     314             : 
     315             : /**
     316             :  * Perform a, potentially unaligned, memory read of the little endian
     317             :  * byte ordered 32-bit value from the specified pointer address,
     318             :  * perform byte reordering, as necessary, for the target system to
     319             :  * put the value in target system byte ordering.
     320             :  *
     321             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     322             :  *                    the 32-bit little endian byte ordered value from.
     323             :  *
     324             :  * @return The 32-bit value at the specified pointer address, if
     325             :  *         necessary, byte reordered.
     326             :  */
     327          12 : inline uint32_t Get32(const uint8_t * p)
     328             : {
     329          12 :     return nl::IO::LittleEndian::GetUnaligned32(p);
     330             : }
     331             : 
     332             : /**
     333             :  * Perform a, potentially unaligned, memory read of the little endian
     334             :  * byte ordered 64-bit value from the specified pointer address,
     335             :  * perform byte reordering, as necessary, for the target system to
     336             :  * put the value in target system byte ordering.
     337             :  *
     338             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     339             :  *                    the 64-bit little endian byte ordered value from.
     340             :  *
     341             :  * @return The 64-bit value at the specified pointer address, if
     342             :  *         necessary, byte reordered.
     343             :  */
     344             : inline uint64_t Get64(const uint8_t * p)
     345             : {
     346             :     return nl::IO::LittleEndian::GetUnaligned64(p);
     347             : }
     348             : 
     349             : /**
     350             :  * Perform a, potentially unaligned, memory write of the target system
     351             :  * byte ordered 16-bit value to the specified pointer address,
     352             :  * perform byte reordering, as necessary, for the target system to
     353             :  * put the value in little endian byte ordering.
     354             :  *
     355             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     356             :  *                    the target system byte ordered 16-bit value to in little
     357             :  *                    endian byte ordering.
     358             :  *
     359             :  * @param[in]  v      The 16-bit value to write, if necessary, byte reordered.
     360             :  *
     361             :  */
     362       25015 : inline void Put16(uint8_t * p, uint16_t v)
     363             : {
     364       25015 :     nl::IO::LittleEndian::PutUnaligned16(p, v);
     365       25015 : }
     366             : 
     367             : /**
     368             :  * Perform a, potentially unaligned, memory write of the target system
     369             :  * byte ordered 32-bit value to the specified pointer address,
     370             :  * perform byte reordering, as necessary, for the target system to
     371             :  * put the value in little endian byte ordering.
     372             :  *
     373             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     374             :  *                    the target system byte ordered 32-bit value to in little
     375             :  *                    endian byte ordering.
     376             :  *
     377             :  * @param[in]  v      The 32-bit value to write, if necessary, byte reordered.
     378             :  *
     379             :  */
     380       57672 : inline void Put32(uint8_t * p, uint32_t v)
     381             : {
     382       57672 :     nl::IO::LittleEndian::PutUnaligned32(p, v);
     383       57672 : }
     384             : 
     385             : /**
     386             :  * Perform a, potentially unaligned, memory write of the target system
     387             :  * byte ordered 64-bit value to the specified pointer address,
     388             :  * perform byte reordering, as necessary, for the target system to
     389             :  * put the value in little endian byte ordering.
     390             :  *
     391             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     392             :  *                    the target system byte ordered 64-bit value to in little
     393             :  *                    endian byte ordering.
     394             :  *
     395             :  * @param[in]  v      The 64-bit value to write, if necessary, byte reordered.
     396             :  *
     397             :  */
     398        4486 : inline void Put64(uint8_t * p, uint64_t v)
     399             : {
     400        4486 :     nl::IO::LittleEndian::PutUnaligned64(p, v);
     401        4486 : }
     402             : 
     403             : /**
     404             :  * Perform a, potentially unaligned, memory read of the little endian
     405             :  * byte ordered 16-bit value from the specified pointer address,
     406             :  * perform byte reordering, as necessary, for the target system to put
     407             :  * the value in target system byte ordering, and increment the pointer
     408             :  * by 16-bits (2 bytes).
     409             :  *
     410             :  * @param[in,out] p   A reference to a constant pointer address, potentially
     411             :  *                    unaligned, to read the 16-bit little endian byte
     412             :  *                    ordered value from and to then increment by 16-
     413             :  *                    bits (2 bytes).
     414             :  *
     415             :  * @return The 16-bit value at the specified pointer address, if necessary,
     416             :  *         byte order swapped.
     417             :  */
     418    10000080 : inline uint16_t Read16(const uint8_t *& p)
     419             : {
     420    10000080 :     return nl::IO::LittleEndian::ReadUnaligned16(reinterpret_cast<const void *&>(p));
     421             : }
     422             : 
     423             : /**
     424             :  * Perform a, potentially unaligned, memory read of the little endian
     425             :  * byte ordered 16-bit value from the specified pointer address,
     426             :  * perform byte reordering, as necessary, for the target system to put
     427             :  * the value in target system byte ordering, and increment the pointer
     428             :  * by 16-bits (2 bytes).
     429             :  *
     430             :  * @param[in,out] p   A reference to a pointer address, potentially
     431             :  *                    unaligned, to read the 16-bit little endian byte
     432             :  *                    ordered value from and to then increment by 16-
     433             :  *                    bits (2 bytes).
     434             :  *
     435             :  * @return The 16-bit value at the specified pointer address, if necessary,
     436             :  *         byte order swapped.
     437             :  */
     438             : inline uint16_t Read16(uint8_t *& p)
     439             : {
     440             :     return Read16(const_cast<const uint8_t *&>(p));
     441             : }
     442             : 
     443             : /**
     444             :  * Perform a, potentially unaligned, memory read of the little endian
     445             :  * byte ordered 32-bit value from the specified pointer address,
     446             :  * perform byte reordering, as necessary, for the target system to put
     447             :  * the value in target system byte ordering, and increment the pointer
     448             :  * by 32-bits (4 bytes).
     449             :  *
     450             :  * @param[in,out] p   A reference to a constant pointer address, potentially
     451             :  *                    unaligned, to read the 32-bit little endian byte
     452             :  *                    ordered value from and to then increment by 32-
     453             :  *                    bits (4 bytes).
     454             :  *
     455             :  * @return The 32-bit value at the specified pointer address, if necessary,
     456             :  *         byte order swapped.
     457             :  */
     458      342820 : inline uint32_t Read32(const uint8_t *& p)
     459             : {
     460      342820 :     return nl::IO::LittleEndian::ReadUnaligned32(reinterpret_cast<const void *&>(p));
     461             : }
     462             : 
     463             : /**
     464             :  * Perform a, potentially unaligned, memory read of the little endian
     465             :  * byte ordered 32-bit value from the specified pointer address,
     466             :  * perform byte reordering, as necessary, for the target system to put
     467             :  * the value in target system byte ordering, and increment the pointer
     468             :  * by 32-bits (4 bytes).
     469             :  *
     470             :  * @param[in,out] p   A reference to a pointer address, potentially
     471             :  *                    unaligned, to read the 32-bit little endian byte
     472             :  *                    ordered value from and to then increment by 32-
     473             :  *                    bits (4 bytes).
     474             :  *
     475             :  * @return The 32-bit value at the specified pointer address, if necessary,
     476             :  *         byte order swapped.
     477             :  */
     478             : inline uint32_t Read32(uint8_t *& p)
     479             : {
     480             :     return Read32(const_cast<const uint8_t *&>(p));
     481             : }
     482             : 
     483             : /**
     484             :  * Perform a, potentially unaligned, memory read of the little endian
     485             :  * byte ordered 64-bit value from the specified pointer address,
     486             :  * perform byte reordering, as necessary, for the target system to put
     487             :  * the value in target system byte ordering, and increment the pointer
     488             :  * by 64-bits (8 bytes).
     489             :  *
     490             :  * @param[in,out] p   A reference to a constant pointer address, potentially
     491             :  *                    unaligned, to read the 64-bit little endian byte
     492             :  *                    ordered value from and to then increment by 64-
     493             :  *                    bits (8 bytes).
     494             :  *
     495             :  * @return The 64-bit value at the specified pointer address, if necessary,
     496             :  *         byte order swapped.
     497             :  */
     498       91349 : inline uint64_t Read64(const uint8_t *& p)
     499             : {
     500       91349 :     return nl::IO::LittleEndian::ReadUnaligned64(reinterpret_cast<const void *&>(p));
     501             : }
     502             : 
     503             : /**
     504             :  * Perform a, potentially unaligned, memory read of the little endian
     505             :  * byte ordered 64-bit value from the specified pointer address,
     506             :  * perform byte reordering, as necessary, for the target system to put
     507             :  * the value in target system byte ordering, and increment the pointer
     508             :  * by 64-bits (8 bytes).
     509             :  *
     510             :  * @param[in,out] p   A reference to a pointer address, potentially
     511             :  *                    unaligned, to read the 64-bit little endian byte
     512             :  *                    ordered value from and to then increment by 64-
     513             :  *                    bits (8 bytes).
     514             :  *
     515             :  * @return The 64-bit value at the specified pointer address, if necessary,
     516             :  *         byte order swapped.
     517             :  */
     518             : inline uint64_t Read64(uint8_t *& p)
     519             : {
     520             :     return Read64(const_cast<const uint8_t *&>(p));
     521             : }
     522             : 
     523             : /**
     524             :  * Perform a, potentially unaligned, memory write of the target system
     525             :  * byte ordered 16-bit value to the specified pointer address,
     526             :  * perform byte reordering, as necessary, for the target system to
     527             :  * put the value in little endian byte ordering.
     528             :  *
     529             :  * @param[in]  p      A reference to a pointer address, potentially
     530             :  *                    unaligned, to write the target system byte
     531             :  *                    ordered 16-bit value to in little endian byte
     532             :  *                    ordering and to then increment by 16-bits (2
     533             :  *                    bytes).
     534             :  *
     535             :  * @param[in]  v      The 16-bit value to write, if necessary, byte order
     536             :  *                    swapped.
     537             :  *
     538             :  */
     539       66589 : inline void Write16(uint8_t *& p, uint16_t v)
     540             : {
     541       66589 :     Put16(p, v);
     542       66589 :     p += sizeof(uint16_t);
     543       66589 : }
     544             : 
     545             : /**
     546             :  * Perform a, potentially unaligned, memory write of the target system
     547             :  * byte ordered 32-bit value to the specified pointer address,
     548             :  * perform byte reordering, as necessary, for the target system to
     549             :  * put the value in little endian byte ordering.
     550             :  *
     551             :  * @param[in]  p      A reference to a pointer address, potentially
     552             :  *                    unaligned, to write the target system byte
     553             :  *                    ordered 32-bit value to in little endian byte
     554             :  *                    ordering and to then increment by 32-bits (4
     555             :  *                    bytes).
     556             :  *
     557             :  * @param[in]  v      The 32-bit value to write, if necessary, byte order
     558             :  *                    swapped.
     559             :  *
     560             :  */
     561       57493 : inline void Write32(uint8_t *& p, uint32_t v)
     562             : {
     563       57493 :     Put32(p, v);
     564       57493 :     p += sizeof(uint32_t);
     565       57493 : }
     566             : 
     567             : /**
     568             :  * Perform a, potentially unaligned, memory write of the target system
     569             :  * byte ordered 64-bit value to the specified pointer address,
     570             :  * perform byte reordering, as necessary, for the target system to
     571             :  * put the value in little endian byte ordering.
     572             :  *
     573             :  * @param[in]  p      A reference to a pointer address, potentially
     574             :  *                    unaligned, to write the target system byte
     575             :  *                    ordered 64-bit value to in little endian byte
     576             :  *                    ordering and to then increment by 64-bits (8
     577             :  *                    bytes).
     578             :  *
     579             :  * @param[in]  v      The 64-bit value to write, if necessary, byte order
     580             :  *                    swapped.
     581             :  *
     582             :  */
     583        4486 : inline void Write64(uint8_t *& p, uint64_t v)
     584             : {
     585        4486 :     Put64(p, v);
     586        4486 :     p += sizeof(uint64_t);
     587        4486 : }
     588             : 
     589             : } // namespace LittleEndian
     590             : 
     591             : /**
     592             :  *  @namespace chip::Encoding::BigEndian
     593             :  *
     594             :  *  This namespace provides functions for:
     595             :  *
     596             :  *  <ul>
     597             :  *     <li>Performing byte reordering, relative to the host system, by
     598             :  *     value of big endian byte ordered values for 16-, 32-, and
     599             :  *     64-bit types.</li>
     600             :  *     <li>Safely performing simple, efficient memory-mapped accesses,
     601             :  *     potentially to unaligned memory locations, with or without byte
     602             :  *     reordering, to 8-, 16-, 32-, and 64-bit big endian byte
     603             :  *     ordered quantities, both with and without pointer
     604             :  *     management.</li>
     605             :  *  </ul>
     606             :  *
     607             :  *  On big endian host systems no actual byte reordering will
     608             :  *  occur. On other systems, byte reordering is performed as
     609             :  *  appropriate.
     610             :  *
     611             :  */
     612             : namespace BigEndian {
     613             : 
     614             : /**
     615             :  * This conditionally performs, as necessary for the target system, a
     616             :  * byte order swap by value of the specified 16-bit value, presumed to
     617             :  * be in big endian byte ordering to the target system (i.e. host)
     618             :  * byte ordering.
     619             :  *
     620             :  * Consequently, on bit endian target systems, this is a no-op and
     621             :  * on big endian target systems, this performs a reordering.
     622             :  *
     623             :  * @param[in]  v  The 16-bit value to be byte order swapped.
     624             :  *
     625             :  * @return The input value, if necessary, byte order swapped.
     626             :  */
     627             : inline uint16_t HostSwap16(uint16_t v)
     628             : {
     629             :     return nl::ByteOrder::Swap16BigToHost(v);
     630             : }
     631             : 
     632             : /**
     633             :  * This conditionally performs, as necessary for the target system, a
     634             :  * byte order swap by value of the specified 32-bit value, presumed to
     635             :  * be in big endian byte ordering to the target system (i.e. host)
     636             :  * byte ordering.
     637             :  *
     638             :  * Consequently, on bit endian target systems, this is a no-op and
     639             :  * on big endian target systems, this performs a reordering.
     640             :  *
     641             :  * @param[in]  v  The 32-bit value to be byte order swapped.
     642             :  *
     643             :  * @return The input value, if necessary, byte order swapped.
     644             :  */
     645          53 : inline uint32_t HostSwap32(uint32_t v)
     646             : {
     647          53 :     return nl::ByteOrder::Swap32BigToHost(v);
     648             : }
     649             : 
     650             : /**
     651             :  * This conditionally performs, as necessary for the target system, a
     652             :  * byte order swap by value of the specified 64-bit value, presumed to
     653             :  * be in big endian byte ordering to the target system (i.e. host)
     654             :  * byte ordering.
     655             :  *
     656             :  * Consequently, on bit endian target systems, this is a no-op and
     657             :  * on big endian target systems, this performs a reordering.
     658             :  *
     659             :  * @param[in]  v  The 64-bit value to be byte order swapped.
     660             :  *
     661             :  * @return The input value, if necessary, byte order swapped.
     662             :  */
     663             : inline uint64_t HostSwap64(uint64_t v)
     664             : {
     665             :     return nl::ByteOrder::Swap64BigToHost(v);
     666             : }
     667             : 
     668             : /**
     669             :  * Perform a, potentially unaligned, memory read of the big endian
     670             :  * byte ordered 16-bit value from the specified pointer address,
     671             :  * perform byte reordering, as necessary, for the target system to
     672             :  * put the value in target system byte ordering.
     673             :  *
     674             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     675             :  *                    the 16-bit big endian byte ordered value from.
     676             :  *
     677             :  * @return The 16-bit value at the specified pointer address, if
     678             :  *         necessary, byte reordered.
     679             :  */
     680        3159 : inline uint16_t Get16(const uint8_t * p)
     681             : {
     682        3159 :     return nl::IO::BigEndian::GetUnaligned16(p);
     683             : }
     684             : 
     685             : /**
     686             :  * Perform a, potentially unaligned, memory read of the big endian
     687             :  * byte ordered 32-bit value from the specified pointer address,
     688             :  * perform byte reordering, as necessary, for the target system to
     689             :  * put the value in target system byte ordering.
     690             :  *
     691             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     692             :  *                    the 32-bit big endian byte ordered value from.
     693             :  *
     694             :  * @return The 32-bit value at the specified pointer address, if
     695             :  *         necessary, byte reordered.
     696             :  */
     697          47 : inline uint32_t Get32(const uint8_t * p)
     698             : {
     699          47 :     return nl::IO::BigEndian::GetUnaligned32(p);
     700             : }
     701             : 
     702             : /**
     703             :  * Perform a, potentially unaligned, memory read of the big endian
     704             :  * byte ordered 64-bit value from the specified pointer address,
     705             :  * perform byte reordering, as necessary, for the target system to
     706             :  * put the value in target system byte ordering.
     707             :  *
     708             :  * @param[in]  p      A pointer address, potentially unaligned, to read
     709             :  *                    the 64-bit big endian byte ordered value from.
     710             :  *
     711             :  * @return The 64-bit value at the specified pointer address, if
     712             :  *         necessary, byte reordered.
     713             :  */
     714        1513 : inline uint64_t Get64(const uint8_t * p)
     715             : {
     716        1513 :     return nl::IO::BigEndian::GetUnaligned64(p);
     717             : }
     718             : 
     719             : /**
     720             :  * Perform a, potentially unaligned, memory write of the target system
     721             :  * byte ordered 16-bit value to the specified pointer address,
     722             :  * perform byte reordering, as necessary, for the target system to
     723             :  * put the value in big endian byte ordering.
     724             :  *
     725             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     726             :  *                    the target system byte ordered 16-bit value to in big
     727             :  *                    endian byte ordering.
     728             :  *
     729             :  * @param[in]  v      The 16-bit value to write, if necessary, byte order
     730             :  *                    swapped.
     731             :  *
     732             :  */
     733         556 : inline void Put16(uint8_t * p, uint16_t v)
     734             : {
     735         556 :     nl::IO::BigEndian::PutUnaligned16(p, v);
     736         556 : }
     737             : 
     738             : /**
     739             :  * Perform a, potentially unaligned, memory write of the target system
     740             :  * byte ordered 32-bit value to the specified pointer address,
     741             :  * perform byte reordering, as necessary, for the target system to
     742             :  * put the value in big endian byte ordering.
     743             :  *
     744             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     745             :  *                    the target system byte ordered 32-bit value to in big
     746             :  *                    endian byte ordering.
     747             :  *
     748             :  * @param[in]  v      The 32-bit value to write, if necessary, byte order
     749             :  *                    swapped.
     750             :  *
     751             :  */
     752         123 : inline void Put32(uint8_t * p, uint32_t v)
     753             : {
     754         123 :     nl::IO::BigEndian::PutUnaligned32(p, v);
     755         123 : }
     756             : 
     757             : /**
     758             :  * Perform a, potentially unaligned, memory write of the target system
     759             :  * byte ordered 64-bit value to the specified pointer address,
     760             :  * perform byte reordering, as necessary, for the target system to
     761             :  * put the value in big endian byte ordering.
     762             :  *
     763             :  * @param[in]  p      A pointer address, potentially unaligned, to write
     764             :  *                    the target system byte ordered 64-bit value to in big
     765             :  *                    endian byte ordering.
     766             :  *
     767             :  * @param[in]  v      The 64-bit value to write, if necessary, byte order
     768             :  *                    swapped.
     769             :  *
     770             :  */
     771       10713 : inline void Put64(uint8_t * p, uint64_t v)
     772             : {
     773       10713 :     nl::IO::BigEndian::PutUnaligned64(p, v);
     774       10713 : }
     775             : 
     776             : /**
     777             :  * Perform a, potentially unaligned, memory read of the big endian
     778             :  * byte ordered 16-bit value from the specified pointer address,
     779             :  * perform byte reordering, as necessary, for the target system to put
     780             :  * the value in target system byte ordering, and increment the pointer
     781             :  * by 16-bits (2 bytes).
     782             :  *
     783             :  * @param[in,out] p   A reference to a constant pointer address, potentially
     784             :  *                    unaligned, to read the 16-bit big endian byte
     785             :  *                    ordered value from and to then increment by 16-
     786             :  *                    bits (2 bytes).
     787             :  *
     788             :  * @return The 16-bit value at the specified pointer address, if necessary,
     789             :  *         byte order swapped.
     790             :  */
     791        2371 : inline uint16_t Read16(const uint8_t *& p)
     792             : {
     793        2371 :     return nl::IO::BigEndian::ReadUnaligned16(reinterpret_cast<const void *&>(p));
     794             : }
     795             : 
     796             : /**
     797             :  * Perform a, potentially unaligned, memory read of the big endian
     798             :  * byte ordered 16-bit value from the specified pointer address,
     799             :  * perform byte reordering, as necessary, for the target system to put
     800             :  * the value in target system byte ordering, and increment the pointer
     801             :  * by 16-bits (2 bytes).
     802             :  *
     803             :  * @param[in,out] p   A reference to a pointer address, potentially
     804             :  *                    unaligned, to read the 16-bit big endian byte
     805             :  *                    ordered value from and to then increment by 16-
     806             :  *                    bits (2 bytes).
     807             :  *
     808             :  * @return The 16-bit value at the specified pointer address, if necessary,
     809             :  *         byte order swapped.
     810             :  */
     811             : inline uint16_t Read16(uint8_t *& p)
     812             : {
     813             :     return Read16(const_cast<const uint8_t *&>(p));
     814             : }
     815             : 
     816             : /**
     817             :  * Perform a, potentially unaligned, memory read of the big endian
     818             :  * byte ordered 32-bit value from the specified pointer address,
     819             :  * perform byte reordering, as necessary, for the target system to put
     820             :  * the value in target system byte ordering, and increment the pointer
     821             :  * by 32-bits (4 bytes).
     822             :  *
     823             :  * @param[in,out] p   A reference to a constant pointer address, potentially
     824             :  *                    unaligned, to read the 32-bit big endian byte
     825             :  *                    ordered value from and to then increment by 32-
     826             :  *                    bits (4 bytes).
     827             :  *
     828             :  * @return The 32-bit value at the specified pointer address, if necessary,
     829             :  *         byte order swapped.
     830             :  */
     831         688 : inline uint32_t Read32(const uint8_t *& p)
     832             : {
     833         688 :     return nl::IO::BigEndian::ReadUnaligned32(reinterpret_cast<const void *&>(p));
     834             : }
     835             : 
     836             : /**
     837             :  * Perform a, potentially unaligned, memory read of the big endian
     838             :  * byte ordered 32-bit value from the specified pointer address,
     839             :  * perform byte reordering, as necessary, for the target system to put
     840             :  * the value in target system byte ordering, and increment the pointer
     841             :  * by 32-bits (4 bytes).
     842             :  *
     843             :  * @param[in,out] p   A reference to a pointer address, potentially
     844             :  *                    unaligned, to read the 32-bit big endian byte
     845             :  *                    ordered value from and to then increment by 32-
     846             :  *                    bits (4 bytes).
     847             :  *
     848             :  * @return The 32-bit value at the specified pointer address, if necessary,
     849             :  *         byte order swapped.
     850             :  */
     851             : inline uint32_t Read32(uint8_t *& p)
     852             : {
     853             :     return Read32(const_cast<const uint8_t *&>(p));
     854             : }
     855             : 
     856             : /**
     857             :  * Perform a, potentially unaligned, memory read of the big endian
     858             :  * byte ordered 64-bit value from the specified pointer address,
     859             :  * perform byte reordering, as necessary, for the target system to put
     860             :  * the value in target system byte ordering, and increment the pointer
     861             :  * by 64-bits (8 bytes).
     862             :  *
     863             :  * @param[in,out] p   A reference to a constant pointer address, potentially
     864             :  *                    unaligned, to read the 64-bit big endian byte
     865             :  *                    ordered value from and to then increment by 64-
     866             :  *                    bits (8 bytes).
     867             :  *
     868             :  * @return The 64-bit value at the specified pointer address, if necessary,
     869             :  *         byte order swapped.
     870             :  */
     871             : inline uint64_t Read64(const uint8_t *& p)
     872             : {
     873             :     return nl::IO::BigEndian::ReadUnaligned64(reinterpret_cast<const void *&>(p));
     874             : }
     875             : 
     876             : /**
     877             :  * Perform a, potentially unaligned, memory read of the big endian
     878             :  * byte ordered 64-bit value from the specified pointer address,
     879             :  * perform byte reordering, as necessary, for the target system to put
     880             :  * the value in target system byte ordering, and increment the pointer
     881             :  * by 64-bits (8 bytes).
     882             :  *
     883             :  * @param[in,out] p   A reference to a pointer address, potentially
     884             :  *                    unaligned, to read the 64-bit big endian byte
     885             :  *                    ordered value from and to then increment by 64-
     886             :  *                    bits (8 bytes).
     887             :  *
     888             :  * @return The 64-bit value at the specified pointer address, if necessary,
     889             :  *         byte order swapped.
     890             :  */
     891             : inline uint64_t Read64(uint8_t *& p)
     892             : {
     893             :     return Read64(const_cast<const uint8_t *&>(p));
     894             : }
     895             : 
     896             : /**
     897             :  * Perform a, potentially unaligned, memory write of the target system
     898             :  * byte ordered 16-bit value to the specified pointer address,
     899             :  * perform byte reordering, as necessary, for the target system to
     900             :  * put the value in big endian byte ordering.
     901             :  *
     902             :  * @param[in]  p      A reference to a pointer address, potentially
     903             :  *                    unaligned, to write the target system byte
     904             :  *                    ordered 16-bit value to in big endian byte
     905             :  *                    ordering and to then increment by 16-bits (2
     906             :  *                    bytes).
     907             :  *
     908             :  * @param[in]  v      The 16-bit value to write, if necessary, byte order
     909             :  *                    swapped.
     910             :  *
     911             :  */
     912             : inline void Write16(uint8_t *& p, uint16_t v)
     913             : {
     914             :     Put16(p, v);
     915             :     p += sizeof(uint16_t);
     916             : }
     917             : 
     918             : /**
     919             :  * Perform a, potentially unaligned, memory write of the target system
     920             :  * byte ordered 32-bit value to the specified pointer address,
     921             :  * perform byte reordering, as necessary, for the target system to
     922             :  * put the value in big endian byte ordering.
     923             :  *
     924             :  * @param[in]  p      A reference to a pointer address, potentially
     925             :  *                    unaligned, to write the target system byte
     926             :  *                    ordered 32-bit value to in big endian byte
     927             :  *                    ordering and to then increment by 32-bits (4
     928             :  *                    bytes).
     929             :  *
     930             :  * @param[in]  v      The 32-bit value to write, if necessary, byte order
     931             :  *                    swapped.
     932             :  *
     933             :  */
     934             : inline void Write32(uint8_t *& p, uint32_t v)
     935             : {
     936             :     Put32(p, v);
     937             :     p += sizeof(uint32_t);
     938             : }
     939             : 
     940             : /**
     941             :  * Perform a, potentially unaligned, memory write of the target system
     942             :  * byte ordered 64-bit value to the specified pointer address,
     943             :  * perform byte reordering, as necessary, for the target system to
     944             :  * put the value in big endian byte ordering.
     945             :  *
     946             :  * @param[in]  p      A reference to a pointer address, potentially
     947             :  *                    unaligned, to write the target system byte
     948             :  *                    ordered 64-bit value to in big endian byte
     949             :  *                    ordering and to then increment by 64-bits (8
     950             :  *                    bytes).
     951             :  *
     952             :  * @param[in]  v      The 64-bit value to write, if necessary, byte order
     953             :  *                    swapped.
     954             :  *
     955             :  */
     956             : inline void Write64(uint8_t *& p, uint64_t v)
     957             : {
     958             :     Put64(p, v);
     959             :     p += sizeof(uint64_t);
     960             : }
     961             : 
     962             : } // namespace BigEndian
     963             : 
     964             : } // namespace Encoding
     965             : } // namespace chip

Generated by: LCOV version 1.14