libubox
C utility functions for OpenWrt.
utils.h File Reference
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <machine/endian.h>

Go to the source code of this file.

Macros

#define calloc_a(len, ...)   __calloc_a(len, ##__VA_ARGS__, NULL)
 
#define ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]))
 
#define __BUILD_BUG_ON(condition)   ((void)sizeof(char[1 - 2*!!(condition)]))
 
#define BUILD_BUG_ON   __BUILD_BUG_ON
 
#define _GNUC_MIN_VER(maj, min)   0
 
#define __BYTE_ORDER   BYTE_ORDER
 
#define __BIG_ENDIAN   BIG_ENDIAN
 
#define __LITTLE_ENDIAN   LITTLE_ENDIAN
 
#define __constant_swap16(x)
 
#define __constant_swap32(x)
 
#define __constant_swap64(x)
 
#define __is_constant(x)    (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1)))
 
#define __eval_once(func, x)    ({ __typeof__(x) __x = x; func(__x); })
 
#define __eval_safe(func, x)
 
#define const_cpu_to_be64(x)   __constant_swap64(x)
 
#define const_cpu_to_be32(x)   __constant_swap32(x)
 
#define const_cpu_to_be16(x)   __constant_swap16(x)
 
#define const_be64_to_cpu(x)   __constant_swap64(x)
 
#define const_be32_to_cpu(x)   __constant_swap32(x)
 
#define const_be16_to_cpu(x)   __constant_swap16(x)
 
#define const_cpu_to_le64(x)   (x)
 
#define const_cpu_to_le32(x)   (x)
 
#define const_cpu_to_le16(x)   (x)
 
#define const_le64_to_cpu(x)   (x)
 
#define const_le32_to_cpu(x)   (x)
 
#define const_le16_to_cpu(x)   (x)
 
#define cpu_to_be64(x)   __eval_safe(__constant_swap64, x)
 
#define cpu_to_be32(x)   __eval_safe(__constant_swap32, x)
 
#define cpu_to_be16(x)   __eval_safe(__constant_swap16, x)
 
#define be64_to_cpu(x)   __eval_safe(__constant_swap64, x)
 
#define be32_to_cpu(x)   __eval_safe(__constant_swap32, x)
 
#define be16_to_cpu(x)   __eval_safe(__constant_swap16, x)
 
#define cpu_to_le64(x)   (x)
 
#define cpu_to_le32(x)   (x)
 
#define cpu_to_le16(x)   (x)
 
#define le64_to_cpu(x)   (x)
 
#define le32_to_cpu(x)   (x)
 
#define le16_to_cpu(x)   (x)
 
#define __packed   __attribute__((packed))
 
#define __constructor   __attribute__((constructor))
 
#define __destructor   __attribute__((destructor))
 
#define __hidden   __attribute__((visibility("hidden")))
 
#define __has_attribute(x)   0
 
#define fallthrough   do {} while (0) /* fallthrough */
 
#define B64_ENCODE_LEN(_len)   ((((_len) + 2) / 3) * 4 + 1)
 
#define B64_DECODE_LEN(_len)   (((_len) / 4) * 3 + 1)
 

Functions

void * __calloc_a (size_t len,...)
 
int b64_encode (const void *src, size_t src_len, void *dest, size_t dest_len)
 
int b64_decode (const void *src, void *dest, size_t dest_len)
 
static unsigned int cbuf_order (unsigned int x)
 
static unsigned long cbuf_size (int order)
 
void * cbuf_alloc (unsigned int order)
 
void cbuf_free (void *ptr, unsigned int order)
 
int mkdir_p (char *dir, mode_t mask)
 

Macro Definition Documentation

◆ __BIG_ENDIAN

#define __BIG_ENDIAN   BIG_ENDIAN

Definition at line 95 of file utils.h.

◆ __BUILD_BUG_ON

#define __BUILD_BUG_ON (   condition)    ((void)sizeof(char[1 - 2*!!(condition)]))

Definition at line 47 of file utils.h.

◆ __BYTE_ORDER

#define __BYTE_ORDER   BYTE_ORDER

Definition at line 92 of file utils.h.

◆ __constant_swap16

#define __constant_swap16 (   x)
Value:
((uint16_t)( \
(((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
(((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))

Definition at line 101 of file utils.h.

◆ __constant_swap32

#define __constant_swap32 (   x)
Value:
((uint32_t)( \
(((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
(((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))

Definition at line 105 of file utils.h.

◆ __constant_swap64

#define __constant_swap64 (   x)
Value:
((uint64_t)( \
(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))

Definition at line 111 of file utils.h.

◆ __constructor

#define __constructor   __attribute__((constructor))

Definition at line 219 of file utils.h.

◆ __destructor

#define __destructor   __attribute__((destructor))

Definition at line 223 of file utils.h.

◆ __eval_once

#define __eval_once (   func,
 
)     ({ __typeof__(x) __x = x; func(__x); })

Definition at line 128 of file utils.h.

◆ __eval_safe

#define __eval_safe (   func,
 
)
Value:
__builtin_choose_expr(__is_constant(x), \
func(x), __eval_once(func, x))
#define __is_constant(x)
Definition: utils.h:125
#define __eval_once(func, x)
Definition: utils.h:128

Definition at line 139 of file utils.h.

◆ __has_attribute

#define __has_attribute (   x)    0

Definition at line 231 of file utils.h.

◆ __hidden

#define __hidden   __attribute__((visibility("hidden")))

Definition at line 227 of file utils.h.

◆ __is_constant

#define __is_constant (   x)     (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1)))

Definition at line 125 of file utils.h.

◆ __LITTLE_ENDIAN

#define __LITTLE_ENDIAN   LITTLE_ENDIAN

Definition at line 98 of file utils.h.

◆ __packed

#define __packed   __attribute__((packed))

Definition at line 215 of file utils.h.

◆ _GNUC_MIN_VER

#define _GNUC_MIN_VER (   maj,
  min 
)    0

Definition at line 75 of file utils.h.

◆ ARRAY_SIZE

#define ARRAY_SIZE (   arr)    (sizeof(arr) / sizeof((arr)[0]))

Definition at line 44 of file utils.h.

◆ B64_DECODE_LEN

#define B64_DECODE_LEN (   _len)    (((_len) / 4) * 3 + 1)

Definition at line 248 of file utils.h.

◆ B64_ENCODE_LEN

#define B64_ENCODE_LEN (   _len)    ((((_len) + 2) / 3) * 4 + 1)

Definition at line 247 of file utils.h.

◆ be16_to_cpu

#define be16_to_cpu (   x)    __eval_safe(__constant_swap16, x)

Definition at line 168 of file utils.h.

◆ be32_to_cpu

#define be32_to_cpu (   x)    __eval_safe(__constant_swap32, x)

Definition at line 167 of file utils.h.

◆ be64_to_cpu

#define be64_to_cpu (   x)    __eval_safe(__constant_swap64, x)

Definition at line 166 of file utils.h.

◆ BUILD_BUG_ON

#define BUILD_BUG_ON   __BUILD_BUG_ON

Definition at line 58 of file utils.h.

◆ calloc_a

#define calloc_a (   len,
  ... 
)    __calloc_a(len, ##__VA_ARGS__, NULL)

Definition at line 39 of file utils.h.

◆ const_be16_to_cpu

#define const_be16_to_cpu (   x)    __constant_swap16(x)

Definition at line 152 of file utils.h.

◆ const_be32_to_cpu

#define const_be32_to_cpu (   x)    __constant_swap32(x)

Definition at line 151 of file utils.h.

◆ const_be64_to_cpu

#define const_be64_to_cpu (   x)    __constant_swap64(x)

Definition at line 150 of file utils.h.

◆ const_cpu_to_be16

#define const_cpu_to_be16 (   x)    __constant_swap16(x)

Definition at line 148 of file utils.h.

◆ const_cpu_to_be32

#define const_cpu_to_be32 (   x)    __constant_swap32(x)

Definition at line 147 of file utils.h.

◆ const_cpu_to_be64

#define const_cpu_to_be64 (   x)    __constant_swap64(x)

Definition at line 146 of file utils.h.

◆ const_cpu_to_le16

#define const_cpu_to_le16 (   x)    (x)

Definition at line 156 of file utils.h.

◆ const_cpu_to_le32

#define const_cpu_to_le32 (   x)    (x)

Definition at line 155 of file utils.h.

◆ const_cpu_to_le64

#define const_cpu_to_le64 (   x)    (x)

Definition at line 154 of file utils.h.

◆ const_le16_to_cpu

#define const_le16_to_cpu (   x)    (x)

Definition at line 160 of file utils.h.

◆ const_le32_to_cpu

#define const_le32_to_cpu (   x)    (x)

Definition at line 159 of file utils.h.

◆ const_le64_to_cpu

#define const_le64_to_cpu (   x)    (x)

Definition at line 158 of file utils.h.

◆ cpu_to_be16

#define cpu_to_be16 (   x)    __eval_safe(__constant_swap16, x)

Definition at line 164 of file utils.h.

◆ cpu_to_be32

#define cpu_to_be32 (   x)    __eval_safe(__constant_swap32, x)

Definition at line 163 of file utils.h.

◆ cpu_to_be64

#define cpu_to_be64 (   x)    __eval_safe(__constant_swap64, x)

Definition at line 162 of file utils.h.

◆ cpu_to_le16

#define cpu_to_le16 (   x)    (x)

Definition at line 172 of file utils.h.

◆ cpu_to_le32

#define cpu_to_le32 (   x)    (x)

Definition at line 171 of file utils.h.

◆ cpu_to_le64

#define cpu_to_le64 (   x)    (x)

Definition at line 170 of file utils.h.

◆ fallthrough

#define fallthrough   do {} while (0) /* fallthrough */

Definition at line 238 of file utils.h.

◆ le16_to_cpu

#define le16_to_cpu (   x)    (x)

Definition at line 176 of file utils.h.

◆ le32_to_cpu

#define le32_to_cpu (   x)    (x)

Definition at line 175 of file utils.h.

◆ le64_to_cpu

#define le64_to_cpu (   x)    (x)

Definition at line 174 of file utils.h.

Function Documentation

◆ __calloc_a()

void* __calloc_a ( size_t  len,
  ... 
)

Definition at line 35 of file utils.c.

36 {
37  va_list ap, ap1;
38  void *ret;
39  void **cur_addr;
40  size_t cur_len;
41  int alloc_len = 0;
42  char *ptr;
43 
44  va_start(ap, len);
45 
46  va_copy(ap1, ap);
47  foreach_arg(ap1, cur_addr, cur_len, &ret, len)
48  alloc_len += (cur_len + C_PTR_ALIGN - 1 ) & C_PTR_MASK;
49  va_end(ap1);
50 
51  ptr = calloc(1, alloc_len);
52  if (!ptr) {
53  va_end(ap);
54  return NULL;
55  }
56 
57  alloc_len = 0;
58  foreach_arg(ap, cur_addr, cur_len, &ret, len) {
59  *cur_addr = &ptr[alloc_len];
60  alloc_len += (cur_len + C_PTR_ALIGN - 1) & C_PTR_MASK;
61  }
62  va_end(ap);
63 
64  return ret;
65 }
#define foreach_arg(_arg, _addr, _len, _first_addr, _first_len)
Definition: utils.c:27
#define C_PTR_ALIGN
Definition: utils.c:32
#define C_PTR_MASK
Definition: utils.c:33

◆ b64_decode()

int b64_decode ( const void *  src,
void *  dest,
size_t  dest_len 
)

Definition at line 203 of file base64.c.

204 {
205  const char *src = _src;
206  unsigned char *target = dest;
207  int state, ch;
208  size_t tarindex;
209  u_char nextbyte;
210  char *pos;
211 
212  state = 0;
213  tarindex = 0;
214 
215  assert(dest && targsize > 0);
216 
217  while ((ch = (unsigned char)*src++) != '\0') {
218  if (isspace(ch)) /* Skip whitespace anywhere. */
219  continue;
220 
221  if (ch == Pad64)
222  break;
223 
224  pos = strchr(Base64, ch);
225  if (pos == 0) /* A non-base64 character. */
226  return (-1);
227 
228  switch (state) {
229  case 0:
230  if (target) {
231  if (tarindex >= targsize)
232  return (-1);
233  target[tarindex] = (pos - Base64) << 2;
234  }
235  state = 1;
236  break;
237  case 1:
238  if (target) {
239  if (tarindex >= targsize)
240  return (-1);
241  target[tarindex] |= (pos - Base64) >> 4;
242  nextbyte = ((pos - Base64) & 0x0f) << 4;
243  if (tarindex + 1 < targsize)
244  target[tarindex+1] = nextbyte;
245  else if (nextbyte)
246  return (-1);
247  }
248  tarindex++;
249  state = 2;
250  break;
251  case 2:
252  if (target) {
253  if (tarindex >= targsize)
254  return (-1);
255  target[tarindex] |= (pos - Base64) >> 2;
256  nextbyte = ((pos - Base64) & 0x03) << 6;
257  if (tarindex + 1 < targsize)
258  target[tarindex+1] = nextbyte;
259  else if (nextbyte)
260  return (-1);
261  }
262  tarindex++;
263  state = 3;
264  break;
265  case 3:
266  if (target) {
267  if (tarindex >= targsize)
268  return (-1);
269  target[tarindex] |= (pos - Base64);
270  }
271  tarindex++;
272  state = 0;
273  break;
274  }
275  }
276 
277  /*
278  * We are done decoding Base-64 chars. Let's see if we ended
279  * on a byte boundary, and/or with erroneous trailing characters.
280  */
281 
282  if (ch == Pad64) { /* We got a pad char. */
283  ch = (unsigned char)*src++; /* Skip it, get next. */
284  switch (state) {
285  case 0: /* Invalid = in first position */
286  case 1: /* Invalid = in second position */
287  return (-1);
288 
289  case 2: /* Valid, means one byte of info */
290  /* Skip any number of spaces. */
291  for (; ch != '\0'; ch = (unsigned char)*src++)
292  if (!isspace(ch))
293  break;
294  /* Make sure there is another trailing = sign. */
295  if (ch != Pad64)
296  return (-1);
297  ch = (unsigned char)*src++; /* Skip the = */
298  /* Fall through to "single trailing =" case. */
299  /* FALLTHROUGH */
300 
301  case 3: /* Valid, means two bytes of info */
302  /*
303  * We know this char is an =. Is there anything but
304  * whitespace after it?
305  */
306  for (; ch != '\0'; ch = (unsigned char)*src++)
307  if (!isspace(ch))
308  return (-1);
309 
310  /*
311  * Now make sure for cases 2 and 3 that the "extra"
312  * bits that slopped past the last full byte were
313  * zeros. If we don't check them, they become a
314  * subliminal channel.
315  */
316  if (target && tarindex < targsize &&
317  target[tarindex] != 0)
318  return (-1);
319  }
320  } else {
321  /*
322  * We ended by seeing the end of the string. Make sure we
323  * have no partial bytes lying around.
324  */
325  if (state != 0)
326  return (-1);
327  }
328 
329  /* Null-terminate if we have room left */
330  if (tarindex < targsize)
331  target[tarindex] = 0;
332 
333  return (tarindex);
334 }
static const char Base64[]
Definition: base64.c:72
static const char Pad64
Definition: base64.c:74
static lua_State * state
Definition: uloop.c:54
Here is the caller graph for this function:

◆ b64_encode()

int b64_encode ( const void *  src,
size_t  src_len,
void *  dest,
size_t  dest_len 
)

Definition at line 139 of file base64.c.

141 {
142  const unsigned char *src = _src;
143  char *target = dest;
144  size_t datalength = 0;
145  u_char input[3] = {0};
146  u_char output[4];
147  size_t i;
148 
149  assert(dest && targsize > 0);
150 
151  while (2 < srclength) {
152  input[0] = *src++;
153  input[1] = *src++;
154  input[2] = *src++;
155  srclength -= 3;
156 
157  output[0] = input[0] >> 2;
158  output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
159  output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
160  output[3] = input[2] & 0x3f;
161 
162  if (datalength + 4 > targsize)
163  return (-1);
164  target[datalength++] = Base64[output[0]];
165  target[datalength++] = Base64[output[1]];
166  target[datalength++] = Base64[output[2]];
167  target[datalength++] = Base64[output[3]];
168  }
169 
170  /* Now we worry about padding. */
171  if (0 != srclength) {
172  /* Get what's left. */
173  input[0] = input[1] = input[2] = '\0';
174  for (i = 0; i < srclength; i++)
175  input[i] = *src++;
176 
177  output[0] = input[0] >> 2;
178  output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
179  output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
180 
181  if (datalength + 4 > targsize)
182  return (-1);
183  target[datalength++] = Base64[output[0]];
184  target[datalength++] = Base64[output[1]];
185  if (srclength == 1)
186  target[datalength++] = Pad64;
187  else
188  target[datalength++] = Base64[output[2]];
189  target[datalength++] = Pad64;
190  }
191  if (datalength >= targsize)
192  return (-1);
193  target[datalength] = '\0'; /* Returned value doesn't count \0. */
194  return (datalength);
195 }
Here is the caller graph for this function:

◆ cbuf_alloc()

void* cbuf_alloc ( unsigned int  order)

Definition at line 116 of file utils.c.

117 {
118  char path[] = "/tmp/cbuf-XXXXXX";
119  unsigned long size = cbuf_size(order);
120  void *ret = NULL;
121  int fd;
122 
123  fd = mkstemp(path);
124  if (fd < 0)
125  return NULL;
126 
127  if (unlink(path))
128  goto close;
129 
130  if (ftruncate(fd, cbuf_size(order)))
131  goto close;
132 
133 #ifndef MAP_ANONYMOUS
134 #define MAP_ANONYMOUS MAP_ANON
135 #endif
136 
137  ret = mmap(NULL, size * 2, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
138  if (ret == MAP_FAILED) {
139  ret = NULL;
140  goto close;
141  }
142 
143  if (mmap(ret, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED,
144  fd, 0) != ret ||
145  mmap(ret + size, size, PROT_READ | PROT_WRITE,
146  MAP_FIXED | MAP_SHARED, fd, 0) != ret + size) {
147  munmap(ret, size * 2);
148  ret = NULL;
149  }
150 
151 close:
152  close(fd);
153  return ret;
154 }
int fd
Definition: udebug-priv.h:27
static unsigned long cbuf_size(int order)
Definition: utils.h:255
Here is the call graph for this function:

◆ cbuf_free()

void cbuf_free ( void *  ptr,
unsigned int  order 
)

Definition at line 156 of file utils.c.

157 {
158  munmap(ptr, cbuf_size(order) * 2);
159 }
Here is the call graph for this function:

◆ cbuf_order()

static unsigned int cbuf_order ( unsigned int  x)
inlinestatic

Definition at line 250 of file utils.h.

251 {
252  return 32 - __builtin_clz(x - 1);
253 }

◆ cbuf_size()

static unsigned long cbuf_size ( int  order)
inlinestatic

Definition at line 255 of file utils.h.

256 {
257  unsigned long page_size = sysconf(_SC_PAGESIZE);
258  unsigned long ret = 1ULL << order;
259 
260  if (ret < page_size)
261  ret = page_size;
262 
263  return ret;
264 }
static unsigned int page_size
Definition: udebug.c:40
Here is the caller graph for this function:

◆ mkdir_p()

int mkdir_p ( char *  dir,
mode_t  mask 
)

Definition at line 161 of file utils.c.

162 {
163  char *l;
164  int ret;
165 
166  ret = mkdir(dir, mask);
167  if (!ret || errno == EEXIST)
168  return 0;
169 
170  if (ret && (errno != ENOENT))
171  return -1;
172 
173  l = strrchr(dir, '/');
174  if (!l || l == dir)
175  return -1;
176 
177  *l = '\0';
178 
179  if (mkdir_p(dir, mask))
180  return -1;
181 
182  *l = '/';
183 
184  ret = mkdir(dir, mask);
185  if (!ret || errno == EEXIST)
186  return 0;
187  else
188  return -1;
189 }
int mkdir_p(char *dir, mode_t mask)
Definition: utils.c:161