Debian Codegraph @

c/src/core/util.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "util.h"
23
24 #include "buffer.h"
25 #include "memory.h"
26
27 #include <proton/error.h>
28 #include <proton/types.h>
29 #include <proton/type_compat.h>
30
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <ctype.h>
35 #include <string.h>
36
37 ssize_t pn_quote_data(char *dst, size_t capacity, const char *src, size_t size)
38 {
39 int idx = 0;
40 for (unsigned i = 0; i < size; i++)
41 {
42 uint8_t c = src[i];
43 // output printable ASCII, ensure '\' always introduces hex escape, escape quote marks
44 if (c < 128 && c != '\\' && c != '\"' && c != '\'' && isprint(c)) {
45 if (idx < (int) (capacity - 1)) {
46 dst[idx++] = c;
47 } else {
48 if (idx > 0) {
49 dst[idx] = '\0';
50 }
51 return PN_OVERFLOW;
52 }
53 } else {
54 if (idx < (int) (capacity - 4)) {
55 idx += sprintf(dst + idx, "\\x%.2x", c);
56 } else {
57 if (idx > 0) {
58 dst[idx] = '\0';
59 }
60 return PN_OVERFLOW;
61 }
62 }
63 }
64
65 dst[idx] = '\0';
66 return idx;
67 }
68
69 int pn_quote(pn_string_t *dst, const char *src, size_t size)
70 {
71 while (true) {
72 size_t str_size = pn_string_size(dst);
73 char *str = pn_string_buffer(dst) + str_size;
74 size_t capacity = pn_string_capacity(dst) - str_size;
75 ssize_t ssize = pn_quote_data(str, capacity, src, size);
76 if (ssize == PN_OVERFLOW) {
77 int err = pn_string_grow(dst, (str_size + capacity) ? 2*(str_size + capacity) : 16);
78 if (err) return err;
79 } else if (ssize >= 0) {
80 return pn_string_resize(dst, str_size + ssize);
81 } else {
82 return ssize;
83 }
84 }
85 }
86
87 int pn_strcasecmp(const char *a, const char *b)
88 {
89 int diff;
90 while (*b) {
91 char aa = *a++, bb = *b++;
92 diff = tolower(aa)-tolower(bb);
93 if ( diff!=0 ) return diff;
94 }
95 return *a;
96 }
97
98 int pn_strncasecmp(const char* a, const char* b, size_t len)
99 {
100 int diff = 0;
101 while (len > 0 && *b) {
102 char aa = *a++, bb = *b++;
103 diff = tolower(aa)-tolower(bb);
104 if ( diff!=0 ) return diff;
105 --len;
106 };
107 return len==0 ? diff : *a;
108 }
109
110 bool pn_env_bool(const char *name)
111 {
112 char *v = getenv(name);
113 return v && (!pn_strcasecmp(v, "true") || !pn_strcasecmp(v, "1") ||
114 !pn_strcasecmp(v, "yes") || !pn_strcasecmp(v, "on"));
115 }
116
117 PN_STRUCT_CLASSDEF(pn_strdup)
118
119 char *pn_strdup(const char *src)
120 {
121 if (!src) return NULL;
122 char *dest = (char *) pni_mem_allocate(PN_CLASSCLASS(pn_strdup), strlen(src)+1);
123 if (!dest) return NULL;
124 return strcpy(dest, src);
125 }
126
127 char *pn_strndup(const char *src, size_t n)
128 {
129 if (src) {
130 unsigned size = 0;
131 for (const char *c = src; size < n && *c; c++) {
132 size++;
133 }
134
135 char *dest = (char *) pni_mem_allocate(PN_CLASSCLASS(pn_strdup), size + 1);
136 if (!dest) return NULL;
137 strncpy(dest, src, pn_min(n, size));
138 dest[size] = '\0';
139 return dest;
140 } else {
141 return NULL;
142 }
143 }
144
145 // which timestamp will expire next, or zero if none set
146 pn_timestamp_t pn_timestamp_min( pn_timestamp_t a, pn_timestamp_t b )
147 {
148 if (a && b) return pn_min(a, b);
149 if (a) return a;
150 return b;
151 }
152