diff options
Diffstat (limited to 'src/regex/gnunet-regex-profiler.c')
-rw-r--r-- | src/regex/gnunet-regex-profiler.c | 134 |
1 files changed, 103 insertions, 31 deletions
diff --git a/src/regex/gnunet-regex-profiler.c b/src/regex/gnunet-regex-profiler.c index 7d907fec6..73ca7c809 100644 --- a/src/regex/gnunet-regex-profiler.c +++ b/src/regex/gnunet-regex-profiler.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2011 - 2013 GNUnet e.V. | 3 | Copyright (C) 2011 - 2017 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -1198,6 +1198,94 @@ master_controller_cb (void *cls, | |||
1198 | /*************************** TESTBED PEER SETUP *****************************/ | 1198 | /*************************** TESTBED PEER SETUP *****************************/ |
1199 | /******************************************************************************/ | 1199 | /******************************************************************************/ |
1200 | 1200 | ||
1201 | /** | ||
1202 | * Process the text buffer counting the non-empty lines and separating them | ||
1203 | * with NULL characters, for later ease of copy using (as)printf. | ||
1204 | * | ||
1205 | * @param data Memory buffer with strings. | ||
1206 | * @param data_size Size of the @a data buffer in bytes. | ||
1207 | * @param str_max Maximum number of strings to return. | ||
1208 | * @return Positive number of lines found in the buffer, | ||
1209 | * #GNUNET_SYSERR otherwise. | ||
1210 | */ | ||
1211 | static int | ||
1212 | count_and_separate_strings (char *data, | ||
1213 | uint64_t data_size, | ||
1214 | unsigned int str_max) | ||
1215 | { | ||
1216 | char *buf; // Keep track of last string to skip blank lines | ||
1217 | unsigned int offset; | ||
1218 | unsigned int str_cnt; | ||
1219 | |||
1220 | buf = data; | ||
1221 | offset = 0; | ||
1222 | str_cnt = 0; | ||
1223 | while ( (offset < (data_size - 1)) && (str_cnt < str_max) ) | ||
1224 | { | ||
1225 | offset++; | ||
1226 | if ( ((data[offset] == '\n')) && | ||
1227 | (buf != &data[offset]) ) | ||
1228 | { | ||
1229 | data[offset] = '\0'; | ||
1230 | str_cnt++; | ||
1231 | buf = &data[offset + 1]; | ||
1232 | } | ||
1233 | else if ( (data[offset] == '\n') || | ||
1234 | (data[offset] == '\0') ) | ||
1235 | buf = &data[offset + 1]; | ||
1236 | } | ||
1237 | return str_cnt; | ||
1238 | } | ||
1239 | |||
1240 | |||
1241 | /** | ||
1242 | * Allocate a string array and fill it with the prefixed strings | ||
1243 | * from a pre-processed, NULL-separated memory region. | ||
1244 | * | ||
1245 | * @param data Preprocessed memory with strings | ||
1246 | * @param data_size Size of the @a data buffer in bytes. | ||
1247 | * @param strings Address of the string array to be created. | ||
1248 | * Must be freed by caller if function end in success. | ||
1249 | * @param str_cnt String count. The @a data buffer should contain | ||
1250 | * at least this many NULL-separated strings. | ||
1251 | * @return #GNUNET_OK in ase of success, #GNUNET_SYSERR otherwise. | ||
1252 | * In case of error @a strings must not be freed. | ||
1253 | */ | ||
1254 | static int | ||
1255 | create_string_array (char *data, uint64_t data_size, | ||
1256 | char ***strings, unsigned int str_cnt) | ||
1257 | { | ||
1258 | uint64_t offset; | ||
1259 | uint64_t len; | ||
1260 | unsigned int i; | ||
1261 | |||
1262 | *strings = GNUNET_malloc (sizeof (char *) * str_cnt); | ||
1263 | offset = 0; | ||
1264 | for (i = 0; i < str_cnt; i++) | ||
1265 | { | ||
1266 | len = strlen (&data[offset]); | ||
1267 | if (offset + len >= data_size) | ||
1268 | { | ||
1269 | GNUNET_free (*strings); | ||
1270 | *strings = NULL; | ||
1271 | return GNUNET_SYSERR; | ||
1272 | } | ||
1273 | if (0 == len) // empty line | ||
1274 | { | ||
1275 | offset++; | ||
1276 | i--; | ||
1277 | continue; | ||
1278 | } | ||
1279 | |||
1280 | GNUNET_asprintf (&(*strings)[i], | ||
1281 | "%s%s", | ||
1282 | regex_prefix, | ||
1283 | &data[offset]); | ||
1284 | offset += len + 1; | ||
1285 | } | ||
1286 | return GNUNET_OK; | ||
1287 | } | ||
1288 | |||
1201 | 1289 | ||
1202 | /** | 1290 | /** |
1203 | * Load search strings from given filename. One search string per line. | 1291 | * Load search strings from given filename. One search string per line. |
@@ -1214,17 +1302,14 @@ load_search_strings (const char *filename, | |||
1214 | unsigned int limit) | 1302 | unsigned int limit) |
1215 | { | 1303 | { |
1216 | char *data; | 1304 | char *data; |
1217 | char *buf; | ||
1218 | uint64_t filesize; | 1305 | uint64_t filesize; |
1219 | unsigned int offset; | ||
1220 | int str_cnt; | 1306 | int str_cnt; |
1221 | unsigned int i; | ||
1222 | 1307 | ||
1308 | /* Sanity checks */ | ||
1223 | if (NULL == filename) | 1309 | if (NULL == filename) |
1224 | { | 1310 | { |
1225 | return GNUNET_SYSERR; | 1311 | return GNUNET_SYSERR; |
1226 | } | 1312 | } |
1227 | |||
1228 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | 1313 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) |
1229 | { | 1314 | { |
1230 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1315 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
@@ -1236,7 +1321,12 @@ load_search_strings (const char *filename, | |||
1236 | &filesize, | 1321 | &filesize, |
1237 | GNUNET_YES, | 1322 | GNUNET_YES, |
1238 | GNUNET_YES)) | 1323 | GNUNET_YES)) |
1239 | filesize = 0; | 1324 | { |
1325 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1326 | "Search strings file %s cannot be read.\n", | ||
1327 | filename); | ||
1328 | return GNUNET_SYSERR; | ||
1329 | } | ||
1240 | if (0 == filesize) | 1330 | if (0 == filesize) |
1241 | { | 1331 | { |
1242 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1332 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
@@ -1244,6 +1334,8 @@ load_search_strings (const char *filename, | |||
1244 | filename); | 1334 | filename); |
1245 | return GNUNET_SYSERR; | 1335 | return GNUNET_SYSERR; |
1246 | } | 1336 | } |
1337 | |||
1338 | /* Read data into memory */ | ||
1247 | data = GNUNET_malloc (filesize + 1); | 1339 | data = GNUNET_malloc (filesize + 1); |
1248 | if (filesize != GNUNET_DISK_fn_read (filename, | 1340 | if (filesize != GNUNET_DISK_fn_read (filename, |
1249 | data, | 1341 | data, |
@@ -1255,32 +1347,12 @@ load_search_strings (const char *filename, | |||
1255 | filename); | 1347 | filename); |
1256 | return GNUNET_SYSERR; | 1348 | return GNUNET_SYSERR; |
1257 | } | 1349 | } |
1258 | buf = data; | 1350 | |
1259 | offset = 0; | 1351 | /* Process buffer and build array */ |
1260 | str_cnt = 0; | 1352 | str_cnt = count_and_separate_strings (data, filesize, limit); |
1261 | while ( (offset < (filesize - 1)) && (str_cnt < limit) ) | 1353 | if (GNUNET_OK != create_string_array (data, filesize, strings, str_cnt)) |
1262 | { | ||
1263 | offset++; | ||
1264 | if ( ((data[offset] == '\n')) && | ||
1265 | (buf != &data[offset]) ) | ||
1266 | { | ||
1267 | data[offset] = '\0'; | ||
1268 | str_cnt++; | ||
1269 | buf = &data[offset + 1]; | ||
1270 | } | ||
1271 | else if ( (data[offset] == '\n') || | ||
1272 | (data[offset] == '\0') ) | ||
1273 | buf = &data[offset + 1]; | ||
1274 | } | ||
1275 | *strings = GNUNET_malloc (sizeof (char *) * str_cnt); | ||
1276 | offset = 0; | ||
1277 | for (i = 0; i < str_cnt; i++) | ||
1278 | { | 1354 | { |
1279 | GNUNET_asprintf (&(*strings)[i], | 1355 | str_cnt = GNUNET_SYSERR; |
1280 | "%s%s", | ||
1281 | regex_prefix, | ||
1282 | &data[offset]); | ||
1283 | offset += strlen (&data[offset]) + 1; | ||
1284 | } | 1356 | } |
1285 | GNUNET_free (data); | 1357 | GNUNET_free (data); |
1286 | return str_cnt; | 1358 | return str_cnt; |