summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/plist.c231
1 files changed, 231 insertions, 0 deletions
diff --git a/src/plist.c b/src/plist.c
index 87be488..f58148b 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -21,6 +21,9 @@
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */ 22 */
23 23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
24 27
25#include <string.h> 28#include <string.h>
26#include "plist.h" 29#include "plist.h"
@@ -29,6 +32,7 @@
29#include <math.h> 32#include <math.h>
30#include <assert.h> 33#include <assert.h>
31#include <limits.h> 34#include <limits.h>
35#include <float.h>
32 36
33#ifdef WIN32 37#ifdef WIN32
34#include <windows.h> 38#include <windows.h>
@@ -111,6 +115,55 @@ static void __attribute__((destructor)) libplist_deinitialize(void)
111 115
112#endif 116#endif
113 117
118#ifndef HAVE_MEMMEM
119// see https://sourceware.org/legacy-ml/libc-alpha/2007-12/msg00000.html
120
121#ifndef _LIBC
122# define __builtin_expect(expr, val) (expr)
123#endif
124
125#undef memmem
126
127/* Return the first occurrence of NEEDLE in HAYSTACK. */
128void* memmem(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len)
129{
130 /* not really Rabin-Karp, just using additive hashing */
131 char* haystack_ = (char*)haystack;
132 char* needle_ = (char*)needle;
133 int hash = 0; /* this is the static hash value of the needle */
134 int hay_hash = 0; /* rolling hash over the haystack */
135 char* last;
136 size_t i;
137
138 if (haystack_len < needle_len)
139 return NULL;
140
141 if (!needle_len)
142 return haystack_;
143
144 /* initialize hashes */
145 for (i = needle_len; i; --i) {
146 hash += *needle_++;
147 hay_hash += *haystack_++;
148 }
149
150 /* iterate over the haystack */
151 haystack_ = (char*)haystack;
152 needle_ = (char*)needle;
153 last = haystack_+(haystack_len - needle_len + 1);
154 for (; haystack_ < last; ++haystack_) {
155 if (__builtin_expect(hash == hay_hash, 0)
156 && *haystack_ == *needle_ /* prevent calling memcmp, was a optimization from existing glibc */
157 && !memcmp (haystack_, needle_, needle_len)) {
158 return haystack_;
159 }
160 /* roll the hash */
161 hay_hash -= *haystack_;
162 hay_hash += *(haystack_+needle_len);
163 }
164 return NULL;
165}
166#endif
114 167
115PLIST_API int plist_is_binary(const char *plist_data, uint32_t length) 168PLIST_API int plist_is_binary(const char *plist_data, uint32_t length)
116{ 169{
@@ -1132,3 +1185,181 @@ PLIST_API void plist_set_date_val(plist_t node, int32_t sec, int32_t usec)
1132 plist_set_element_val(node, PLIST_DATE, &val, sizeof(struct timeval)); 1185 plist_set_element_val(node, PLIST_DATE, &val, sizeof(struct timeval));
1133} 1186}
1134 1187
1188PLIST_API int plist_boolean_is_true(plist_t boolnode)
1189{
1190 if (!PLIST_IS_BOOLEAN(boolnode)) {
1191 return -1;
1192 }
1193 uint8_t bv = 0;
1194 plist_get_bool_val(boolnode, &bv);
1195 return (bv == 1);
1196}
1197
1198PLIST_API int plist_uint_val_compare(plist_t uintnode, uint64_t cmpval)
1199{
1200 if (!PLIST_IS_UINT(uintnode)) {
1201 return -1;
1202 }
1203 uint64_t uintval = 0;
1204 plist_get_uint_val(uintnode, &uintval);
1205 if (uintval == cmpval) {
1206 return 0;
1207 } else if (uintval < cmpval) {
1208 return -1;
1209 } else {
1210 return 1;
1211 }
1212}
1213
1214PLIST_API int plist_uid_val_compare(plist_t uidnode, uint64_t cmpval)
1215{
1216 if (!PLIST_IS_UID(uidnode)) {
1217 return -1;
1218 }
1219 uint64_t uidval = 0;
1220 plist_get_uid_val(uidnode, &uidval);
1221 if (uidval == cmpval) {
1222 return 0;
1223 } else if (uidval < cmpval) {
1224 return -1;
1225 } else {
1226 return 1;
1227 }
1228}
1229
1230PLIST_API int plist_real_val_compare(plist_t realnode, double cmpval)
1231{
1232 if (!PLIST_IS_REAL(realnode)) {
1233 return -1;
1234 }
1235 double a = 0;
1236 double b = cmpval;
1237 plist_get_real_val(realnode, &a);
1238 double abs_a = fabs(a);
1239 double abs_b = fabs(b);
1240 double diff = fabs(a - b);
1241 if (a == b) {
1242 return 0;
1243 } else if (a == 0 || b == 0 || (abs_a + abs_b < DBL_MIN)) {
1244 if (diff < (DBL_EPSILON * DBL_MIN)) {
1245 return 0;
1246 } else if (a < b) {
1247 return -1;
1248 }
1249 } else {
1250 if ((diff / fmin(abs_a + abs_b, DBL_MAX)) < DBL_EPSILON) {
1251 return 0;
1252 } else if (a < b) {
1253 return -1;
1254 }
1255 }
1256 return 1;
1257}
1258
1259PLIST_API int plist_date_val_compare(plist_t datenode, int32_t cmpsec, int32_t cmpusec)
1260{
1261 if (!PLIST_IS_DATE(datenode)) {
1262 return -1;
1263 }
1264 int32_t sec = 0;
1265 int32_t usec = 0;
1266 plist_get_date_val(datenode, &sec, &usec);
1267 uint64_t dateval = ((int64_t)sec << 32) | usec;
1268 uint64_t cmpval = ((int64_t)cmpsec << 32) | cmpusec;
1269 if (dateval == cmpval) {
1270 return 0;
1271 } else if (dateval < cmpval) {
1272 return -1;
1273 } else {
1274 return 1;
1275 }
1276}
1277
1278PLIST_API int plist_string_val_compare(plist_t strnode, const char* cmpval)
1279{
1280 if (!PLIST_IS_STRING(strnode)) {
1281 return -1;
1282 }
1283 plist_data_t data = plist_get_data(strnode);
1284 return strcmp(data->strval, cmpval);
1285}
1286
1287PLIST_API int plist_string_val_compare_with_size(plist_t strnode, const char* cmpval, size_t n)
1288{
1289 if (!PLIST_IS_STRING(strnode)) {
1290 return -1;
1291 }
1292 plist_data_t data = plist_get_data(strnode);
1293 return strncmp(data->strval, cmpval, n);
1294}
1295
1296PLIST_API int plist_string_val_contains(plist_t strnode, const char* substr)
1297{
1298 if (!PLIST_IS_STRING(strnode)) {
1299 return 0;
1300 }
1301 plist_data_t data = plist_get_data(strnode);
1302 return (strstr(data->strval, substr) != NULL);
1303}
1304
1305PLIST_API int plist_key_val_compare(plist_t keynode, const char* cmpval)
1306{
1307 if (!PLIST_IS_KEY(keynode)) {
1308 return -1;
1309 }
1310 plist_data_t data = plist_get_data(keynode);
1311 return strcmp(data->strval, cmpval);
1312}
1313
1314PLIST_API int plist_key_val_compare_with_size(plist_t keynode, const char* cmpval, size_t n)
1315{
1316 if (!PLIST_IS_KEY(keynode)) {
1317 return -1;
1318 }
1319 plist_data_t data = plist_get_data(keynode);
1320 return strncmp(data->strval, cmpval, n);
1321}
1322
1323PLIST_API int plist_key_val_contains(plist_t keynode, const char* substr)
1324{
1325 if (!PLIST_IS_KEY(keynode)) {
1326 return 0;
1327 }
1328 plist_data_t data = plist_get_data(keynode);
1329 return (strstr(data->strval, substr) != NULL);
1330}
1331
1332PLIST_API int plist_data_val_compare(plist_t datanode, const uint8_t* cmpval, size_t n)
1333{
1334 if (!PLIST_IS_DATA(datanode)) {
1335 return -1;
1336 }
1337 plist_data_t data = plist_get_data(datanode);
1338 if (data->length < n) {
1339 return -1;
1340 } else if (data->length > n) {
1341 return 1;
1342 }
1343 return memcmp(data->buff, cmpval, n);
1344}
1345
1346PLIST_API int plist_data_val_compare_with_size(plist_t datanode, const uint8_t* cmpval, size_t n)
1347{
1348 if (!PLIST_IS_DATA(datanode)) {
1349 return -1;
1350 }
1351 plist_data_t data = plist_get_data(datanode);
1352 if (data->length < n) {
1353 return -1;
1354 }
1355 return memcmp(data->buff, cmpval, n);
1356}
1357
1358PLIST_API int plist_data_val_contains(plist_t datanode, const uint8_t* cmpval, size_t n)
1359{
1360 if (!PLIST_IS_DATA(datanode)) {
1361 return -1;
1362 }
1363 plist_data_t data = plist_get_data(datanode);
1364 return (memmem(data->buff, data->length, cmpval, n) != NULL);
1365}