| 1 |
#ifndef _PROTON_SRC_UTIL_H |
| 2 |
#define _PROTON_SRC_UTIL_H 1 |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "buffer.h" |
| 26 |
|
| 27 |
#include <errno.h> |
| 28 |
#ifndef __cplusplus |
| 29 |
#include <stdbool.h> |
| 30 |
#endif |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <proton/types.h> |
| 35 |
#include <proton/object.h> |
| 36 |
|
| 37 |
#if __cplusplus |
| 38 |
extern "C" { |
| 39 |
#endif |
| 40 |
|
| 41 |
ssize_t pn_quote_data(char *dst, size_t capacity, const char *src, size_t size); |
| 42 |
int pn_quote(pn_string_t *dst, const char *src, size_t size); |
| 43 |
bool pn_env_bool(const char *name); |
| 44 |
pn_timestamp_t pn_timestamp_min(pn_timestamp_t a, pn_timestamp_t b); |
| 45 |
|
| 46 |
extern const pn_class_t PN_CLASSCLASS(pn_strdup)[]; |
| 47 |
char *pn_strdup(const char *src); |
| 48 |
char *pn_strndup(const char *src, size_t n); |
| 49 |
|
| 50 |
int pn_strcasecmp(const char* a, const char* b); |
| 51 |
int pn_strncasecmp(const char* a, const char* b, size_t len); |
| 52 |
|
| 53 |
static inline bool pn_bytes_equal(const pn_bytes_t a, const pn_bytes_t b) { |
| 54 |
return (a.size == b.size && !memcmp(a.start, b.start, a.size)); |
| 55 |
} |
| 56 |
|
| 57 |
static inline pn_bytes_t pn_string_bytes(pn_string_t *s) { |
| 58 |
return pn_bytes(pn_string_size(s), pn_string_get(s)); |
| 59 |
} |
| 60 |
|
| 61 |
static inline void pni_write16(char *bytes, uint16_t value) |
| 62 |
{ |
| 63 |
bytes[0] = 0xFF & (value >> 8); |
| 64 |
bytes[1] = 0xFF & (value ); |
| 65 |
} |
| 66 |
|
| 67 |
static inline void pni_write32(char *bytes, uint32_t value) |
| 68 |
{ |
| 69 |
bytes[0] = 0xFF & (value >> 24); |
| 70 |
bytes[1] = 0xFF & (value >> 16); |
| 71 |
bytes[2] = 0xFF & (value >> 8); |
| 72 |
bytes[3] = 0xFF & (value ); |
| 73 |
} |
| 74 |
|
| 75 |
static inline uint16_t pni_read16(const char *bytes) |
| 76 |
{ |
| 77 |
uint16_t a = (uint8_t) bytes[0]; |
| 78 |
uint16_t b = (uint8_t) bytes[1]; |
| 79 |
uint16_t r = a << 8 | b; |
| 80 |
return r; |
| 81 |
} |
| 82 |
|
| 83 |
static inline uint32_t pni_read32(const char *bytes) |
| 84 |
{ |
| 85 |
uint32_t a = pni_read16(bytes); |
| 86 |
uint32_t b = pni_read16(&bytes[2]); |
| 87 |
uint32_t r = a << 16 | b; |
| 88 |
return r; |
| 89 |
} |
| 90 |
|
| 91 |
static inline uint64_t pni_read64(const char *bytes) |
| 92 |
{ |
| 93 |
uint64_t a = pni_read32(bytes); |
| 94 |
uint64_t b = pni_read32(&bytes[4]); |
| 95 |
uint64_t r = a << 32 | b; |
| 96 |
return r; |
| 97 |
} |
| 98 |
|
| 99 |
/* Create a literal bytes value, e.g. PN_BYTES_LITERAL(foo) == pn_bytes(3, "foo") */ |
| 100 |
#define PN_BYTES_LITERAL(X) (pn_bytes(sizeof(#X)-1, #X)) |
| 101 |
|
| 102 |
#define DIE_IFR(EXPR, STRERR) \ |
| 103 |
do { \ |
| 104 |
int __code__ = (EXPR); \ |
| 105 |
if (__code__) { \ |
| 106 |
fprintf(stderr, "%s:%d: %s: %s (%d)\n", __FILE__, __LINE__, \ |
| 107 |
#EXPR, STRERR(__code__), __code__); \ |
| 108 |
exit(-1); \ |
| 109 |
} \ |
| 110 |
} while (0) |
| 111 |
|
| 112 |
#define DIE_IFE(EXPR) \ |
| 113 |
do { \ |
| 114 |
if ((EXPR) == -1) { \ |
| 115 |
int __code__ = errno; \ |
| 116 |
fprintf(stderr, "%s:%d: %s: %s (%d)\n", __FILE__, __LINE__, \ |
| 117 |
#EXPR, strerror(__code__), __code__); \ |
| 118 |
exit(-1); \ |
| 119 |
} \ |
| 120 |
} while (0) |
| 121 |
|
| 122 |
|
| 123 |
#define LL_HEAD(ROOT, LIST) ((ROOT)-> LIST ## _head) |
| 124 |
#define LL_TAIL(ROOT, LIST) ((ROOT)-> LIST ## _tail) |
| 125 |
#define LL_ADD(ROOT, LIST, NODE) \ |
| 126 |
{ \ |
| 127 |
(NODE)-> LIST ## _next = NULL; \ |
| 128 |
(NODE)-> LIST ## _prev = (ROOT)-> LIST ## _tail; \ |
| 129 |
if (LL_TAIL(ROOT, LIST)) \ |
| 130 |
LL_TAIL(ROOT, LIST)-> LIST ## _next = (NODE); \ |
| 131 |
LL_TAIL(ROOT, LIST) = (NODE); \ |
| 132 |
if (!LL_HEAD(ROOT, LIST)) LL_HEAD(ROOT, LIST) = (NODE); \ |
| 133 |
} |
| 134 |
|
| 135 |
#define LL_POP(ROOT, LIST, TYPE) \ |
| 136 |
{ \ |
| 137 |
if (LL_HEAD(ROOT, LIST)) { \ |
| 138 |
TYPE *_old = LL_HEAD(ROOT, LIST); \ |
| 139 |
LL_HEAD(ROOT, LIST) = LL_HEAD(ROOT, LIST)-> LIST ## _next; \ |
| 140 |
_old-> LIST ## _next = NULL; \ |
| 141 |
if (_old == LL_TAIL(ROOT, LIST)) { \ |
| 142 |
LL_TAIL(ROOT, LIST) = NULL; \ |
| 143 |
} else { \ |
| 144 |
LL_HEAD(ROOT, LIST)-> LIST ## _prev = NULL; \ |
| 145 |
} \ |
| 146 |
} \ |
| 147 |
} |
| 148 |
|
| 149 |
#define LL_REMOVE(ROOT, LIST, NODE) \ |
| 150 |
{ \ |
| 151 |
if ((NODE)-> LIST ## _prev) \ |
| 152 |
(NODE)-> LIST ## _prev-> LIST ## _next = (NODE)-> LIST ## _next; \ |
| 153 |
if ((NODE)-> LIST ## _next) \ |
| 154 |
(NODE)-> LIST ## _next-> LIST ## _prev = (NODE)-> LIST ## _prev; \ |
| 155 |
if ((NODE) == LL_HEAD(ROOT, LIST)) \ |
| 156 |
LL_HEAD(ROOT, LIST) = (NODE)-> LIST ## _next; \ |
| 157 |
if ((NODE) == LL_TAIL(ROOT, LIST)) \ |
| 158 |
LL_TAIL(ROOT, LIST) = (NODE)-> LIST ## _prev; \ |
| 159 |
} |
| 160 |
|
| 161 |
#define pn_min(X,Y) ((X) > (Y) ? (Y) : (X)) |
| 162 |
#define pn_max(X,Y) ((X) < (Y) ? (Y) : (X)) |
| 163 |
|
| 164 |
#if __cplusplus |
| 165 |
} |
| 166 |
#endif |
| 167 |
|
| 168 |
#endif /* util.h */ |