Coverage Report

Created: 2026-06-15 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rtpproxy/external/libre/src/sys/endian.c
Line
Count
Source
1
/**
2
 * @file endian.c  Endianness converting routines
3
 *
4
 * Copyright (C) 2010 Creytiv.com
5
 */
6
#include <re_types.h>
7
#include <re_mbuf.h>
8
#include <re_sys.h>
9
10
11
/*
12
 * These routes are working on both little-endian and big-endian platforms.
13
 */
14
15
16
/**
17
 * Convert a 16-bit value from host order to little endian
18
 *
19
 * @param v 16-bit in host order
20
 *
21
 * @return 16-bit little endian value
22
 */
23
uint16_t sys_htols(uint16_t v)
24
0
{
25
0
  uint8_t *p = (uint8_t *)&v;
26
0
  uint16_t l = 0;
27
28
0
  l |= (uint16_t)*p++ << 0;
29
0
  l |= (uint16_t)*p   << 8;
30
31
0
  return l;
32
0
}
33
34
35
/**
36
 * Convert a 32-bit value from host order to little endian
37
 *
38
 * @param v 32-bit in host order
39
 *
40
 * @return 32-bit little endian value
41
 */
42
uint32_t sys_htoll(uint32_t v)
43
0
{
44
0
  uint8_t *p = (uint8_t *)&v;
45
0
  uint32_t l = 0;
46
47
0
  l |= (uint32_t)*p++ << 0;
48
0
  l |= (uint32_t)*p++ << 8;
49
0
  l |= (uint32_t)*p++ << 16;
50
0
  l |= (uint32_t)*p   << 24;
51
52
0
  return l;
53
0
}
54
55
56
/**
57
 * Convert a 16-bit value from little endian to host order
58
 *
59
 * @param v 16-bit little endian value
60
 *
61
 * @return 16-bit value in host order
62
 */
63
uint16_t sys_ltohs(uint16_t v)
64
0
{
65
0
  uint16_t s;
66
0
  uint8_t *p = (uint8_t *)&s;
67
68
0
  *p++ = v>>0 & 0xff;
69
0
  *p   = v>>8 & 0xff;
70
71
0
  return s;
72
0
}
73
74
75
/**
76
 * Convert a 32-bit value from little endian to host order
77
 *
78
 * @param v 32-bit little endian value
79
 *
80
 * @return 32-bit value in host order
81
 */
82
uint32_t sys_ltohl(uint32_t v)
83
0
{
84
0
  uint32_t h;
85
0
  uint8_t *p = (uint8_t *)&h;
86
87
0
  *p++ = v>>0  & 0xff;
88
0
  *p++ = v>>8  & 0xff;
89
0
  *p++ = v>>16 & 0xff;
90
0
  *p   = v>>24 & 0xff;
91
92
0
  return h;
93
0
}
94
95
96
/**
97
 * Convert a 64-bit value from host to network byte-order
98
 *
99
 * @param v 64-bit host byte-order value
100
 *
101
 * @return 64-bit value in network byte-order
102
 */
103
uint64_t sys_htonll(uint64_t v)
104
0
{
105
0
  uint64_t h = 0;
106
0
  uint8_t *p = (uint8_t *)&v;
107
108
0
  h |= (uint64_t)*p++ << 56;
109
0
  h |= (uint64_t)*p++ << 48;
110
0
  h |= (uint64_t)*p++ << 40;
111
0
  h |= (uint64_t)*p++ << 32;
112
0
  h |= (uint64_t)*p++ << 24;
113
0
  h |= (uint64_t)*p++ << 16;
114
0
  h |= (uint64_t)*p++ << 8;
115
0
  h |= (uint64_t)*p   << 0;
116
117
0
  return h;
118
0
}
119
120
121
/**
122
 * Convert a 64-bit value from network to host byte-order
123
 *
124
 * @param v 64-bit network byte-order value
125
 *
126
 * @return 64-bit value in host byte-order
127
 */
128
uint64_t sys_ntohll(uint64_t v)
129
716
{
130
716
  uint64_t h;
131
716
  uint8_t *p = (uint8_t *)&h;
132
133
716
  *p++ = (uint8_t) (v>>56 & 0xff);
134
716
  *p++ = (uint8_t) (v>>48 & 0xff);
135
716
  *p++ = (uint8_t) (v>>40 & 0xff);
136
716
  *p++ = (uint8_t) (v>>32 & 0xff);
137
716
  *p++ = (uint8_t) (v>>24 & 0xff);
138
716
  *p++ = (uint8_t) (v>>16 & 0xff);
139
716
  *p++ = (uint8_t) (v>>8  & 0xff);
140
716
  *p   = (uint8_t) (v>>0  & 0xff);
141
142
716
  return h;
143
716
}