"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/src/libknot/wire.h" (16 Jul 2019, 3611 Bytes) of package /linux/misc/dns/knot-2.8.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "wire.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15  */
   16 /*!
   17  * \file
   18  *
   19  * \brief Wire integer operations.
   20  *
   21  * \addtogroup wire
   22  * @{
   23  */
   24 
   25 #pragma once
   26 
   27 #include <assert.h>
   28 #include <stdint.h>
   29 #include <string.h>
   30 
   31 #include "libknot/endian.h"
   32 
   33 /*!
   34  * \brief Reads 2 bytes from the wireformat data.
   35  *
   36  * \param pos Data to read the 2 bytes from.
   37  *
   38  * \return The 2 bytes read, in host byte order.
   39  */
   40 inline static uint16_t knot_wire_read_u16(const uint8_t *pos)
   41 {
   42     assert(pos);
   43     uint16_t result;
   44     memcpy(&result, pos, sizeof(result));
   45     return be16toh(result);
   46 }
   47 
   48 /*!
   49  * \brief Reads 4 bytes from the wireformat data.
   50  *
   51  * \param pos Data to read the 4 bytes from.
   52  *
   53  * \return The 4 bytes read, in host byte order.
   54  */
   55 inline static uint32_t knot_wire_read_u32(const uint8_t *pos)
   56 {
   57     assert(pos);
   58     uint32_t result;
   59     memcpy(&result, pos, sizeof(result));
   60     return be32toh(result);
   61 }
   62 
   63 /*!
   64  * \brief Reads 6 bytes from the wireformat data.
   65  *
   66  * \param pos Data to read the 6 bytes from.
   67  *
   68  * \return The 6 bytes read, in host byte order.
   69  */
   70 inline static uint64_t knot_wire_read_u48(const uint8_t *pos)
   71 {
   72     assert(pos);
   73     uint64_t input = 0;
   74     memcpy((uint8_t *)&input + 1, pos, 6);
   75     return be64toh(input) >> 8;
   76 }
   77 
   78 /*!
   79  * \brief Read 8 bytes from the wireformat data.
   80  *
   81  * \param pos Data to read the 8 bytes from.
   82  *
   83  * \return The 8 bytes read, in host byte order.
   84  */
   85 inline static uint64_t knot_wire_read_u64(const uint8_t *pos)
   86 {
   87     assert(pos);
   88     uint64_t result;
   89     memcpy(&result, pos, sizeof(result));
   90     return be64toh(result);
   91 }
   92 
   93 /*!
   94  * \brief Writes 2 bytes in wireformat.
   95  *
   96  * The data are stored in network byte order (big endian).
   97  *
   98  * \param pos Position where to put the 2 bytes.
   99  * \param data Data to put.
  100  */
  101 inline static void knot_wire_write_u16(uint8_t *pos, uint16_t data)
  102 {
  103     assert(pos);
  104     uint16_t beval = htobe16(data);
  105     memcpy(pos, &beval, sizeof(beval));
  106 }
  107 
  108 /*!
  109  * \brief Writes 4 bytes in wireformat.
  110  *
  111  * The data are stored in network byte order (big endian).
  112  *
  113  * \param pos Position where to put the 4 bytes.
  114  * \param data Data to put.
  115  */
  116 inline static void knot_wire_write_u32(uint8_t *pos, uint32_t data)
  117 {
  118     assert(pos);
  119     uint32_t beval = htobe32(data);
  120     memcpy(pos, &beval, sizeof(beval));
  121 }
  122 
  123 /*!
  124  * \brief Writes 6 bytes in wireformat.
  125  *
  126  * The data are stored in network byte order (big endian).
  127  *
  128  * \param pos Position where to put the 4 bytes.
  129  * \param data Data to put.
  130  */
  131 inline static void knot_wire_write_u48(uint8_t *pos, uint64_t data)
  132 {
  133     assert(pos);
  134     uint64_t swapped = htobe64(data << 8);
  135     memcpy(pos, (uint8_t *)&swapped + 1, 6);
  136 }
  137 
  138 /*!
  139  * \brief Writes 8 bytes in wireformat.
  140  *
  141  * The data are stored in network byte order (big endian).
  142  *
  143  * \param pos Position where to put the 8 bytes.
  144  * \param data Data to put.
  145  */
  146 inline static void knot_wire_write_u64(uint8_t *pos, uint64_t data)
  147 {
  148     assert(pos);
  149     uint64_t beval = htobe64(data);
  150     memcpy(pos, &beval, sizeof(beval));
  151 }
  152 
  153 /*! @} */