diff options
Diffstat (limited to 'include/osmocom/core/bitXXgen.h.tpl')
-rw-r--r-- | include/osmocom/core/bitXXgen.h.tpl | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/include/osmocom/core/bitXXgen.h.tpl b/include/osmocom/core/bitXXgen.h.tpl index 6881d87d..ab54ba9c 100644 --- a/include/osmocom/core/bitXXgen.h.tpl +++ b/include/osmocom/core/bitXXgen.h.tpl @@ -14,15 +14,13 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once -/*! load unaligned n-byte integer (little-endian encoding) into uintXX_t +#include <osmocom/core/utils.h> + +/*! load unaligned n-byte integer (little-endian encoding) into uintXX_t, into the least significant octets. * \param[in] p Buffer where integer is stored * \param[in] n Number of bytes stored in p * \returns XX bit unsigned integer @@ -32,11 +30,14 @@ static inline uintXX_t osmo_loadXXle_ext(const void *p, uint8_t n) uint8_t i; uintXX_t r = 0; const uint8_t *q = (uint8_t *)p; + OSMO_ASSERT(n <= sizeof(r)); for(i = 0; i < n; r |= ((uintXX_t)q[i] << (8 * i)), i++); return r; } -/*! load unaligned n-byte integer (big-endian encoding) into uintXX_t +/*! load unaligned n-byte integer (big-endian encoding) into uintXX_t, into the MOST significant octets. + * WARNING: for n < sizeof(uintXX_t), the result is not returned in the least significant octets, as one might expect. + * To always return the same value as fed to osmo_storeXXbe_ext() before, use osmo_loadXXbe_ext_2(). * \param[in] p Buffer where integer is stored * \param[in] n Number of bytes stored in p * \returns XX bit unsigned integer @@ -46,10 +47,26 @@ static inline uintXX_t osmo_loadXXbe_ext(const void *p, uint8_t n) uint8_t i; uintXX_t r = 0; const uint8_t *q = (uint8_t *)p; + OSMO_ASSERT(n <= sizeof(r)); for(i = 0; i < n; r |= ((uintXX_t)q[i] << (XX - 8* (1 + i))), i++); return r; } +/*! load unaligned n-byte integer (big-endian encoding) into uintXX_t, into the least significant octets. + * \param[in] p Buffer where integer is stored + * \param[in] n Number of bytes stored in p + * \returns XX bit unsigned integer + */ +static inline uintXX_t osmo_loadXXbe_ext_2(const void *p, uint8_t n) +{ + uint8_t i; + uintXX_t r = 0; + const uint8_t *q = (uint8_t *)p; + OSMO_ASSERT(n <= sizeof(r)); + for(i = 0; i < n; r |= ((uintXX_t)q[i] << (XX - 8* (1 + i + (sizeof(r) - n)))), i++); + return r; +} + /*! store unaligned n-byte integer (little-endian encoding) from uintXX_t * \param[in] x unsigned XX bit integer @@ -60,6 +77,7 @@ static inline void osmo_storeXXle_ext(uintXX_t x, void *p, uint8_t n) { uint8_t i; uint8_t *q = (uint8_t *)p; + OSMO_ASSERT(n <= sizeof(x)); for(i = 0; i < n; q[i] = (x >> i * 8) & 0xFF, i++); } @@ -72,6 +90,7 @@ static inline void osmo_storeXXbe_ext(uintXX_t x, void *p, uint8_t n) { uint8_t i; uint8_t *q = (uint8_t *)p; + OSMO_ASSERT(n <= sizeof(x)); for(i = 0; i < n; q[i] = (x >> ((n - 1 - i) * 8)) & 0xFF, i++); } |