Landfall

pkg/devices/util/src/byteorder.cc

263:18edc9c73263
8 months ago Paul Boddie Exposed the card registry and added support for inspecting partition tables. cpm-library-improvements
     1 /*     2  * Byte ordering utilities.     3  *     4  * Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk>     5  *     6  * This program is free software; you can redistribute it and/or     7  * modify it under the terms of the GNU General Public License as     8  * published by the Free Software Foundation; either version 2 of     9  * the License, or (at your option) any later version.    10  *    11  * This program is distributed in the hope that it will be useful,    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    14  * GNU General Public License for more details.    15  *    16  * You should have received a copy of the GNU General Public License    17  * along with this program; if not, write to the Free Software    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,    19  * Boston, MA  02110-1301, USA    20  */    21     22 #include "byteorder.h"    23     24     25     26 uint32_t get_stored_value(const uint8_t data[], uint8_t bytes, bool big_endian)    27 {     28   uint32_t value = 0;    29       30   if (big_endian)    31   {     32     for (uint8_t byte = 0; byte < bytes; byte++)    33       value = (value << 8) | data[byte];    34   }    35   else    36   {     37     for (uint8_t byte = bytes; byte; byte--)    38       value = (value << 8) | data[byte - 1];    39   }    40       41   return value;    42 }    43     44 void set_stored_value(uint32_t value, uint8_t data[], uint8_t bytes, bool big_endian)    45 {     46   if (big_endian)    47   {     48     for (uint8_t byte = bytes; byte; byte--)    49     {     50       data[byte - 1] = value & 0xff;    51       value >>= 8;    52     }    53   }    54   else    55   {     56     for (uint8_t byte = 0; byte < bytes; byte++)    57     {     58       data[byte] = value & 0xff;    59       value >>= 8;    60     }    61   }    62 }