2042f0002dd1c5dde319e20dc4c9a452f4f452e6
[metze/wireshark/wip.git] / epan / emem.c
1 /* emem.c
2  * Wireshark memory management and garbage collection functions
3  * Ronnie Sahlberg 2005
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34
35 #include <time.h>
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #include <glib.h>
45
46 #include "proto.h"
47 #include "emem.h"
48
49 #ifdef _WIN32
50 #include <windows.h>    /* VirtualAlloc, VirtualProtect */
51 #include <process.h>    /* getpid */
52 #endif
53
54 /* Print out statistics about our memory allocations? */
55 /*#define SHOW_EMEM_STATS*/
56
57 /* Do we want to use guardpages? if available */
58 #define WANT_GUARD_PAGES 1
59
60 #ifdef WANT_GUARD_PAGES
61 /* Add guard pages at each end of our allocated memory */
62 #if defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H)
63 #include <stdint.h>
64 #ifdef HAVE_SYS_TYPES_H
65 #include <sys/types.h>
66 #endif
67 #include <sys/mman.h>
68 #if defined(MAP_ANONYMOUS)
69 #define ANON_PAGE_MODE  (MAP_ANONYMOUS|MAP_PRIVATE)
70 #elif defined(MAP_ANON)
71 #define ANON_PAGE_MODE  (MAP_ANON|MAP_PRIVATE)
72 #else
73 #define ANON_PAGE_MODE  (MAP_PRIVATE)   /* have to map /dev/zero */
74 #define NEED_DEV_ZERO
75 #endif
76 #ifdef NEED_DEV_ZERO
77 #include <fcntl.h>
78 static int dev_zero_fd;
79 #define ANON_FD dev_zero_fd
80 #else
81 #define ANON_FD -1
82 #endif
83 #define USE_GUARD_PAGES 1
84 #endif
85 #endif
86
87 /* When required, allocate more memory from the OS in this size chunks */
88 #define EMEM_PACKET_CHUNK_SIZE (10 * 1024 * 1024)
89
90 /* The canary between allocations is at least 8 bytes and up to 16 bytes to
91  * allow future allocations to be 4- or 8-byte aligned.
92  * All but the last byte of the canary are randomly generated; the last byte is
93  * NULL to separate the canary and the pointer to the next canary.
94  *
95  * For example, if the allocation is a multiple of 8 bytes, the canary and
96  * pointer would look like:
97  *   |0|1|2|3|4|5|6|7||0|1|2|3|4|5|6|7|
98  *   |c|c|c|c|c|c|c|0||p|p|p|p|p|p|p|p| (64-bit), or:
99  *   |c|c|c|c|c|c|c|0||p|p|p|p|         (32-bit)
100  *
101  * If the allocation was, for example, 12 bytes, the canary would look like:
102  *        |0|1|2|3|4|5|6|7||0|1|2|3|4|5|6|7|
103  *   [...]|a|a|a|a|c|c|c|c||c|c|c|c|c|c|c|0| (followed by the pointer)
104  */
105 #define EMEM_CANARY_SIZE 8
106 #define EMEM_CANARY_DATA_SIZE (EMEM_CANARY_SIZE * 2 - 1)
107
108 typedef struct _emem_chunk_t {
109         struct _emem_chunk_t *next;
110         char            *buf;
111         unsigned int    amount_free_init;
112         unsigned int    amount_free;
113         unsigned int    free_offset_init;
114         unsigned int    free_offset;
115         void            *canary_last;
116 } emem_chunk_t;
117
118 typedef struct _emem_header_t {
119         emem_chunk_t *free_list;
120         emem_chunk_t *used_list;
121
122         emem_tree_t *trees;             /* only used by se_mem allocator */
123
124         guint8 canary[EMEM_CANARY_DATA_SIZE];
125         void *(*memory_alloc)(size_t size, struct _emem_header_t *);
126
127         /*
128          * Tools like Valgrind and ElectricFence don't work well with memchunks.
129          * Export the following environment variables to make {ep|se}_alloc() allocate each
130          * object individually.
131          *
132          * WIRESHARK_DEBUG_EP_NO_CHUNKS
133          * WIRESHARK_DEBUG_SE_NO_CHUNKS
134          */
135         gboolean debug_use_chunks;
136
137         /* Do we want to use canaries?
138          * Export the following environment variables to disable/enable canaries
139          *
140          * WIRESHARK_DEBUG_EP_NO_CANARY
141          * For SE memory use of canary is default off as the memory overhead
142          * is considerable.
143          * WIRESHARK_DEBUG_SE_USE_CANARY
144          */
145         gboolean debug_use_canary;
146
147         /*  Do we want to verify no one is using a pointer to an ep_ or se_
148          *  allocated thing where they shouldn't be?
149          *
150          * Export WIRESHARK_EP_VERIFY_POINTERS or WIRESHARK_SE_VERIFY_POINTERS
151          * to turn this on.
152          */
153         gboolean debug_verify_pointers;
154
155 } emem_header_t;
156
157 static emem_header_t ep_packet_mem;
158 static emem_header_t se_packet_mem;
159
160 /*
161  *  Memory scrubbing is expensive but can be useful to ensure we don't:
162  *    - use memory before initializing it
163  *    - use memory after freeing it
164  *  Export WIRESHARK_DEBUG_SCRUB_MEMORY to turn it on.
165  */
166 static gboolean debug_use_memory_scrubber = FALSE;
167
168 #if defined (_WIN32)
169 static SYSTEM_INFO sysinfo;
170 static OSVERSIONINFO versinfo;
171 static int pagesize;
172 #elif defined(USE_GUARD_PAGES)
173 static intptr_t pagesize;
174 #endif /* _WIN32 / USE_GUARD_PAGES */
175
176 static void *emem_alloc_chunk(size_t size, emem_header_t *mem);
177 static void *emem_alloc_glib(size_t size, emem_header_t *mem);
178
179 /*
180  * Set a canary value to be placed between memchunks.
181  */
182 static void
183 emem_canary_init(guint8 *canary)
184 {
185         int i;
186         static GRand *rand_state = NULL;
187
188         if (rand_state == NULL) {
189                 rand_state = g_rand_new();
190         }
191         for (i = 0; i < EMEM_CANARY_DATA_SIZE; i ++) {
192                 canary[i] = (guint8) g_rand_int_range(rand_state, 1, 0x100);
193         }
194         return;
195 }
196
197 static void *
198 emem_canary_next(guint8 *mem_canary, guint8 *canary, int *len)
199 {
200         void *ptr;
201         int i;
202
203         for (i = 0; i < EMEM_CANARY_SIZE-1; i++)
204                 if (mem_canary[i] != canary[i])
205                         return (void *) -1;
206
207         for (; i < EMEM_CANARY_DATA_SIZE; i++) {
208                 if (canary[i] == '\0') {
209                         memcpy(&ptr, &canary[i+1], sizeof(void *));
210
211                         if (len)
212                                 *len = i + 1 + sizeof(void *);
213                         return ptr;
214                 }
215
216                 if (mem_canary[i] != canary[i])
217                         return (void *) -1;
218         }
219
220         return (void *) -1;
221 }
222
223 /*
224  * Given an allocation size, return the amount of room needed for the canary
225  * (with a minimum of 8 bytes) while using the canary to pad to an 8-byte
226  * boundary.
227  */
228 static guint8
229 emem_canary_pad (size_t allocation)
230 {
231         guint8 pad;
232
233         pad = EMEM_CANARY_SIZE - (allocation % EMEM_CANARY_SIZE);
234         if (pad < EMEM_CANARY_SIZE)
235                 pad += EMEM_CANARY_SIZE;
236
237         return pad;
238 }
239
240 /* used for debugging canaries, will block */
241 #ifdef DEBUG_INTENSE_CANARY_CHECKS
242 gboolean intense_canary_checking = FALSE;
243
244 /*  used to intensivelly check ep canaries
245  */
246 void
247 ep_check_canary_integrity(const char* fmt, ...)
248 {
249         va_list ap;
250         static gchar there[128] = {
251                 'L','a','u','n','c','h',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
252                 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
253                 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
254                 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
255         gchar here[128];
256         emem_chunk_t* npc = NULL;
257
258         if (! intense_canary_checking ) return;
259
260         va_start(ap,fmt);
261         g_vsnprintf(here, sizeof(here), fmt, ap);
262         va_end(ap);
263
264         for (npc = ep_packet_mem.free_list; npc != NULL; npc = npc->next) {
265                 void *canary_next = npc->canary_last;
266
267                 while (canary_next != NULL) {
268                         canary_next = emem_canary_next(ep_packet_mem.canary, canary_next, NULL);
269                         /* XXX, check if canary_next is inside allocated memory? */
270
271                         if (canary_next == (void *) -1)
272                                 g_error("Per-packet memory corrupted\nbetween: %s\nand: %s", there, here);
273                 }
274         }
275
276         g_strlcpy(there, here, sizeof(there));
277 }
278 #endif
279
280 static void
281 emem_init_chunk(emem_header_t *mem)
282 {
283         if (mem->debug_use_canary)
284                 emem_canary_init(mem->canary);
285
286         if (mem->debug_use_chunks)
287                 mem->memory_alloc = emem_alloc_chunk;
288         else
289                 mem->memory_alloc = emem_alloc_glib;
290 }
291
292
293 /* Initialize the packet-lifetime memory allocation pool.
294  * This function should be called only once when Wireshark or TShark starts
295  * up.
296  */
297 static void
298 ep_init_chunk(void)
299 {
300         ep_packet_mem.free_list=NULL;
301         ep_packet_mem.used_list=NULL;
302         ep_packet_mem.trees=NULL;       /* not used by this allocator */
303
304         ep_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_EP_NO_CHUNKS") == NULL);
305         ep_packet_mem.debug_use_canary = ep_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_EP_NO_CANARY") == NULL);
306         ep_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_EP_VERIFY_POINTERS") != NULL);
307
308 #ifdef DEBUG_INTENSE_CANARY_CHECKS
309         intense_canary_checking = (getenv("WIRESHARK_DEBUG_EP_INTENSE_CANARY") != NULL);
310 #endif
311
312         emem_init_chunk(&ep_packet_mem);
313 }
314
315 /* Initialize the capture-lifetime memory allocation pool.
316  * This function should be called only once when Wireshark or TShark starts
317  * up.
318  */
319 static void
320 se_init_chunk(void)
321 {
322         se_packet_mem.free_list = NULL;
323         se_packet_mem.used_list = NULL;
324         se_packet_mem.trees = NULL;
325
326         se_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_SE_NO_CHUNKS") == NULL);
327         se_packet_mem.debug_use_canary = se_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_SE_USE_CANARY") != NULL);
328         se_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_SE_VERIFY_POINTERS") != NULL);
329
330         emem_init_chunk(&se_packet_mem);
331 }
332
333 /*  Initialize all the allocators here.
334  *  This function should be called only once when Wireshark or TShark starts
335  *  up.
336  */
337 void
338 emem_init(void)
339 {
340         ep_init_chunk();
341         se_init_chunk();
342
343         if (getenv("WIRESHARK_DEBUG_SCRUB_MEMORY"))
344                 debug_use_memory_scrubber  = TRUE;
345
346 #if defined (_WIN32)
347         /* Set up our guard page info for Win32 */
348         GetSystemInfo(&sysinfo);
349         pagesize = sysinfo.dwPageSize;
350
351         /* calling GetVersionEx using the OSVERSIONINFO structure.
352          * OSVERSIONINFOEX requires Win NT4 with SP6 or newer NT Versions.
353          * OSVERSIONINFOEX will fail on Win9x and older NT Versions.
354          * See also:
355          * http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
356          * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
357          * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfoex_str.asp
358          */
359         versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
360         GetVersionEx(&versinfo);
361
362 #elif defined(USE_GUARD_PAGES)
363         pagesize = sysconf(_SC_PAGESIZE);
364 #ifdef NEED_DEV_ZERO
365         dev_zero_fd = ws_open("/dev/zero", O_RDWR);
366         g_assert(dev_zero_fd != -1);
367 #endif
368 #endif /* _WIN32 / USE_GUARD_PAGES */
369 }
370
371 #ifdef SHOW_EMEM_STATS
372 #define NUM_ALLOC_DIST 10
373 static guint allocations[NUM_ALLOC_DIST] = { 0 };
374 static guint total_no_chunks = 0;
375
376 static void
377 print_alloc_stats()
378 {
379         guint num_chunks = 0;
380         guint num_allocs = 0;
381         guint total_used = 0;
382         guint total_allocation = 0;
383         guint total_free = 0;
384         guint used_for_canaries = 0;
385         guint total_headers;
386         guint i;
387         emem_chunk_t *chunk;
388         guint total_space_allocated_from_os, total_space_wasted;
389         gboolean ep_stat=TRUE;
390
391         fprintf(stderr, "\n-------- EP allocator statistics --------\n");
392         fprintf(stderr, "%s chunks, %s canaries, %s memory scrubber\n",
393                ep_packet_mem.debug_use_chunks ? "Using" : "Not using",
394                ep_packet_mem.debug_use_canary ? "using" : "not using",
395                debug_use_memory_scrubber ? "using" : "not using");
396
397         if (! (ep_packet_mem.free_list || !ep_packet_mem.used_list)) {
398                 fprintf(stderr, "No memory allocated\n");
399                 ep_stat = FALSE;
400         }
401         if (ep_packet_mem.debug_use_chunks && ep_stat) {
402                 /* Nothing interesting without chunks */
403                 /*  Only look at the used_list since those chunks are fully
404                  *  used.  Looking at the free list would skew our view of what
405                  *  we have wasted.
406                  */
407                 for (chunk = ep_packet_mem.used_list; chunk; chunk = chunk->next) {
408                         num_chunks++;
409                         total_used += (chunk->amount_free_init - chunk->amount_free);
410                         total_allocation += chunk->amount_free_init;
411                         total_free += chunk->amount_free;
412                 }
413                 if (num_chunks > 0) {
414                         fprintf (stderr, "\n");
415                         fprintf (stderr, "\n---- Buffer space ----\n");
416                         fprintf (stderr, "\tChunk allocation size: %10u\n", EMEM_PACKET_CHUNK_SIZE);
417                         fprintf (stderr, "\t*    Number of chunks: %10u\n", num_chunks);
418                         fprintf (stderr, "\t-------------------------------------------\n");
419                         fprintf (stderr, "\t= %u (%u including guard pages) total space used for buffers\n",
420                         total_allocation, EMEM_PACKET_CHUNK_SIZE * num_chunks);
421                         fprintf (stderr, "\t-------------------------------------------\n");
422                         total_space_allocated_from_os = total_allocation
423                                 + sizeof(emem_chunk_t) * num_chunks;
424                         fprintf (stderr, "Total allocated from OS: %u\n\n",
425                                 total_space_allocated_from_os);
426                 }else{
427                         fprintf (stderr, "No fully used chunks, nothing to do\n");
428                 }
429                 /* Reset stats */
430                 num_chunks = 0;
431                 num_allocs = 0;
432                 total_used = 0;
433                 total_allocation = 0;
434                 total_free = 0;
435                 used_for_canaries = 0;
436         }
437
438
439         fprintf(stderr, "\n-------- SE allocator statistics --------\n");
440         fprintf(stderr, "Total number of chunk allocations %u\n",
441                 total_no_chunks);
442         fprintf(stderr, "%s chunks, %s canaries\n",
443                se_packet_mem.debug_use_chunks ? "Using" : "Not using",
444                se_packet_mem.debug_use_canary ? "using" : "not using");
445
446         if (! (se_packet_mem.free_list || !se_packet_mem.used_list)) {
447                 fprintf(stderr, "No memory allocated\n");
448                 return;
449         }
450
451         if (!se_packet_mem.debug_use_chunks )
452                 return; /* Nothing interesting without chunks?? */
453
454         /*  Only look at the used_list since those chunks are fully used.
455          *  Looking at the free list would skew our view of what we have wasted.
456          */
457         for (chunk = se_packet_mem.used_list; chunk; chunk = chunk->next) {
458                 num_chunks++;
459                 total_used += (chunk->amount_free_init - chunk->amount_free);
460                 total_allocation += chunk->amount_free_init;
461                 total_free += chunk->amount_free;
462
463                 if (se_packet_mem.debug_use_canary){
464                         void *ptr = chunk->canary_last;
465                         int len;
466
467                         while (ptr != NULL) {
468                                 ptr = emem_canary_next(se_packet_mem.canary, ptr, &len);
469
470                                 if (ptr == (void *) -1)
471                                         g_error("Memory corrupted");
472                                 used_for_canaries += len;
473                         }
474                 }
475         }
476
477         if (num_chunks == 0) {
478
479                 fprintf (stderr, "No fully used chunks, nothing to do\n");
480                 return;
481         }
482
483         fprintf (stderr, "\n");
484         fprintf (stderr, "---------- Allocations from the OS ----------\n");
485         fprintf (stderr, "---- Headers ----\n");
486         fprintf (stderr, "\t(    Chunk header size: %10lu\n",
487                  sizeof(emem_chunk_t));
488         fprintf (stderr, "\t*     Number of chunks: %10u\n", num_chunks);
489         fprintf (stderr, "\t-------------------------------------------\n");
490
491         total_headers = sizeof(emem_chunk_t) * num_chunks;
492         fprintf (stderr, "\t= %u bytes used for headers\n", total_headers);
493         fprintf (stderr, "\n---- Buffer space ----\n");
494         fprintf (stderr, "\tChunk allocation size: %10u\n",
495                  EMEM_PACKET_CHUNK_SIZE);
496         fprintf (stderr, "\t*    Number of chunks: %10u\n", num_chunks);
497         fprintf (stderr, "\t-------------------------------------------\n");
498         fprintf (stderr, "\t= %u (%u including guard pages) bytes used for buffers\n",
499                 total_allocation, EMEM_PACKET_CHUNK_SIZE * num_chunks);
500         fprintf (stderr, "\t-------------------------------------------\n");
501         total_space_allocated_from_os = (EMEM_PACKET_CHUNK_SIZE * num_chunks)
502                                         + total_headers;
503         fprintf (stderr, "Total bytes allocated from the OS: %u\n\n",
504                 total_space_allocated_from_os);
505
506         for (i = 0; i < NUM_ALLOC_DIST; i++)
507                 num_allocs += allocations[i];
508
509         fprintf (stderr, "---------- Allocations from the SE pool ----------\n");
510         fprintf (stderr, "                Number of SE allocations: %10u\n",
511                  num_allocs);
512         fprintf (stderr, "             Bytes used (incl. canaries): %10u\n",
513                  total_used);
514         fprintf (stderr, "                 Bytes used for canaries: %10u\n",
515                  used_for_canaries);
516         fprintf (stderr, "Bytes unused (wasted, excl. guard pages): %10u\n",
517                  total_allocation - total_used);
518         fprintf (stderr, "Bytes unused (wasted, incl. guard pages): %10u\n\n",
519                  total_space_allocated_from_os - total_used);
520
521         fprintf (stderr, "---------- Statistics ----------\n");
522         fprintf (stderr, "Average SE allocation size (incl. canaries): %6.2f\n",
523                 (float)total_used/(float)num_allocs);
524         fprintf (stderr, "Average SE allocation size (excl. canaries): %6.2f\n",
525                 (float)(total_used - used_for_canaries)/(float)num_allocs);
526         fprintf (stderr, "        Average wasted bytes per allocation: %6.2f\n",
527                 (total_allocation - total_used)/(float)num_allocs);
528         total_space_wasted = (total_allocation - total_used)
529                 + (sizeof(emem_chunk_t));
530         fprintf (stderr, " Space used for headers + unused allocation: %8u\n",
531                 total_space_wasted);
532         fprintf (stderr, "--> %% overhead/waste: %4.2f\n",
533                 100 * (float)total_space_wasted/(float)total_space_allocated_from_os);
534
535         fprintf (stderr, "\nAllocation distribution (sizes include canaries):\n");
536         for (i = 0; i < (NUM_ALLOC_DIST-1); i++)
537                 fprintf (stderr, "size < %5d: %8u\n", 32<<i, allocations[i]);
538         fprintf (stderr, "size > %5d: %8u\n", 32<<i, allocations[i]);
539 }
540 #endif
541
542 static gboolean
543 emem_verify_pointer_list(const emem_chunk_t *chunk_list, const void *ptr)
544 {
545         const gchar *cptr = ptr;
546         const emem_chunk_t *chunk;
547
548         for (chunk = chunk_list; chunk; chunk = chunk->next) {
549                 if (cptr >= (chunk->buf + chunk->free_offset_init) && cptr < (chunk->buf + chunk->free_offset))
550                         return TRUE;
551         }
552         return FALSE;
553 }
554
555 static gboolean
556 emem_verify_pointer(const emem_header_t *hdr, const void *ptr)
557 {
558         return emem_verify_pointer_list(hdr->free_list, ptr) || emem_verify_pointer_list(hdr->used_list, ptr);
559 }
560
561 gboolean
562 ep_verify_pointer(const void *ptr)
563 {
564         if (ep_packet_mem.debug_verify_pointers)
565                 return emem_verify_pointer(&ep_packet_mem, ptr);
566         else
567                 return FALSE;
568 }
569
570 gboolean
571 se_verify_pointer(const void *ptr)
572 {
573         if (se_packet_mem.debug_verify_pointers)
574                 return emem_verify_pointer(&se_packet_mem, ptr);
575         else
576                 return FALSE;
577 }
578
579 static void
580 emem_scrub_memory(char *buf, size_t size, gboolean alloc)
581 {
582         guint scrubbed_value;
583         guint offset;
584
585         if (!debug_use_memory_scrubber)
586                 return;
587
588         if (alloc) /* this memory is being allocated */
589                 scrubbed_value = 0xBADDCAFE;
590         else /* this memory is being freed */
591                 scrubbed_value = 0xDEADBEEF;
592
593         /*  We shouldn't need to check the alignment of the starting address
594          *  since this is malloc'd memory (or 'pagesize' bytes into malloc'd
595          *  memory).
596          */
597
598         /* XXX - if the above is *NOT* true, we should use memcpy here,
599          * in order to avoid problems on alignment-sensitive platforms, e.g.
600          * http://stackoverflow.com/questions/108866/is-there-memset-that-accepts-integers-larger-than-char
601          */
602
603         for (offset = 0; offset + sizeof(guint) <= size; offset += sizeof(guint))
604                 *(guint*)(void*)(buf+offset) = scrubbed_value;
605
606         /* Initialize the last bytes, if any */
607         if (offset < size) {
608                 *(guint8*)(buf+offset) = scrubbed_value >> 24;
609                 offset++;
610                 if (offset < size) {
611                         *(guint8*)(buf+offset) = (scrubbed_value >> 16) & 0xFF;
612                         offset++;
613                         if (offset < size) {
614                                 *(guint8*)(buf+offset) = (scrubbed_value >> 8) & 0xFF;
615                         }
616                 }
617         }
618
619
620 }
621
622 static emem_chunk_t *
623 emem_create_chunk(void) {
624 #if defined (_WIN32)
625         BOOL ret;
626         char *buf_end, *prot1, *prot2;
627         DWORD oldprot;
628 #elif defined(USE_GUARD_PAGES)
629         int ret;
630         char *buf_end, *prot1, *prot2;
631 #endif /* _WIN32 / USE_GUARD_PAGES */
632         emem_chunk_t *npc;
633
634         npc = g_new(emem_chunk_t, 1);
635         npc->next = NULL;
636         npc->canary_last = NULL;
637
638 #if defined (_WIN32)
639         /*
640          * MSDN documents VirtualAlloc/VirtualProtect at
641          * http://msdn.microsoft.com/library/en-us/memory/base/creating_guard_pages.asp
642          */
643
644         /* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
645         npc->buf = VirtualAlloc(NULL, EMEM_PACKET_CHUNK_SIZE,
646                 MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
647
648         if (npc->buf == NULL) {
649                 g_free(npc);
650                 THROW(OutOfMemoryError);
651         }
652
653 #elif defined(USE_GUARD_PAGES)
654         npc->buf = mmap(NULL, EMEM_PACKET_CHUNK_SIZE,
655                 PROT_READ|PROT_WRITE, ANON_PAGE_MODE, ANON_FD, 0);
656
657         if (npc->buf == MAP_FAILED) {
658                 g_free(npc);
659                 THROW(OutOfMemoryError);
660         }
661
662 #else /* Is there a draft in here? */
663         npc->buf = g_malloc(EMEM_PACKET_CHUNK_SIZE);
664         /* g_malloc() can't fail */
665 #endif
666
667 #ifdef SHOW_EMEM_STATS
668         total_no_chunks++;
669 #endif
670
671 #if defined (_WIN32)
672         buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
673
674         /* Align our guard pages on page-sized boundaries */
675         prot1 = (char *) ((((int) npc->buf + pagesize - 1) / pagesize) * pagesize);
676         prot2 = (char *) ((((int) buf_end - (1 * pagesize)) / pagesize) * pagesize);
677
678         ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
679         g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
680         ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
681         g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
682
683         npc->amount_free_init = (unsigned int) (prot2 - prot1 - pagesize);
684         npc->free_offset_init = (unsigned int) (prot1 - npc->buf) + pagesize;
685 #elif defined(USE_GUARD_PAGES)
686         buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
687
688         /* Align our guard pages on page-sized boundaries */
689         prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
690         prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
691
692         ret = mprotect(prot1, pagesize, PROT_NONE);
693         g_assert(ret != -1);
694         ret = mprotect(prot2, pagesize, PROT_NONE);
695         g_assert(ret != -1);
696
697         npc->amount_free_init = prot2 - prot1 - pagesize;
698         npc->free_offset_init = (prot1 - npc->buf) + pagesize;
699 #else
700         npc->amount_free_init = EMEM_PACKET_CHUNK_SIZE;
701         npc->free_offset_init = 0;
702 #endif /* USE_GUARD_PAGES */
703
704         npc->amount_free = npc->amount_free_init;
705         npc->free_offset = npc->free_offset_init;
706         return npc;
707 }
708
709 static void *
710 emem_alloc_chunk(size_t size, emem_header_t *mem)
711 {
712         void *buf;
713
714         size_t asize = size;
715         gboolean use_canary = mem->debug_use_canary;
716         guint8 pad;
717         emem_chunk_t *free_list;
718
719         /* Allocate room for at least 8 bytes of canary plus some padding
720          * so the canary ends on an 8-byte boundary.
721          * Then add the room needed for the pointer to the next canary.
722          */
723          if (use_canary) {
724                 pad = emem_canary_pad(asize);
725                 asize += sizeof(void *);
726         } else
727                 pad = (G_MEM_ALIGN - (asize & (G_MEM_ALIGN-1))) & (G_MEM_ALIGN-1);
728
729         asize += pad;
730
731 #ifdef SHOW_EMEM_STATS
732         /* Do this check here so we can include the canary size */
733         if (mem == &se_packet_mem) {
734                 if (asize < 32)
735                         allocations[0]++;
736                 else if (asize < 64)
737                         allocations[1]++;
738                 else if (asize < 128)
739                         allocations[2]++;
740                 else if (asize < 256)
741                         allocations[3]++;
742                 else if (asize < 512)
743                         allocations[4]++;
744                 else if (asize < 1024)
745                         allocations[5]++;
746                 else if (asize < 2048)
747                         allocations[6]++;
748                 else if (asize < 4096)
749                         allocations[7]++;
750                 else if (asize < 8192)
751                         allocations[8]++;
752                 else if (asize < 16384)
753                         allocations[8]++;
754                 else
755                         allocations[(NUM_ALLOC_DIST-1)]++;
756         }
757 #endif
758
759         /* make sure we dont try to allocate too much (arbitrary limit) */
760         DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
761
762         if (!mem->free_list)
763                 mem->free_list = emem_create_chunk();
764
765         /* oops, we need to allocate more memory to serve this request
766          * than we have free. move this node to the used list and try again
767          */
768         if(asize > mem->free_list->amount_free) {
769                 emem_chunk_t *npc;
770                 npc=mem->free_list;
771                 mem->free_list=mem->free_list->next;
772                 npc->next=mem->used_list;
773                 mem->used_list=npc;
774
775                 if (!mem->free_list)
776                         mem->free_list = emem_create_chunk();
777         }
778
779         free_list = mem->free_list;
780
781         buf = free_list->buf + free_list->free_offset;
782
783         free_list->amount_free -= (unsigned int) asize;
784         free_list->free_offset += (unsigned int) asize;
785
786         if (use_canary) {
787                 char *cptr = (char *)buf + size;
788
789                 memcpy(cptr, mem->canary, pad-1);
790                 cptr[pad-1] = '\0';
791                 memcpy(cptr + pad, &free_list->canary_last, sizeof(void *));
792
793                 free_list->canary_last = cptr;
794         }
795
796         return buf;
797 }
798
799 static void *
800 emem_alloc_glib(size_t size, emem_header_t *mem)
801 {
802         emem_chunk_t *npc;
803
804         npc=g_new(emem_chunk_t, 1);
805         npc->next=mem->used_list;
806         npc->buf=g_malloc(size);
807         npc->canary_last = NULL;
808         mem->used_list=npc;
809         /* There's no padding/alignment involved (from our point of view) when
810          * we fetch the memory directly from the system pool, so WYSIWYG */
811         npc->free_offset = npc->free_offset_init = 0;
812         npc->amount_free = npc->amount_free_init = (unsigned int) size;
813
814         return npc->buf;
815 }
816
817 /* allocate 'size' amount of memory. */
818 static void *
819 emem_alloc(size_t size, emem_header_t *mem)
820 {
821         void *buf = mem->memory_alloc(size, mem);
822
823         /*  XXX - this is a waste of time if the allocator function is going to
824          *  memset this straight back to 0.
825          */
826         emem_scrub_memory(buf, size, TRUE);
827
828         return buf;
829 }
830
831 /* allocate 'size' amount of memory with an allocation lifetime until the
832  * next packet.
833  */
834 void *
835 ep_alloc(size_t size)
836 {
837         return emem_alloc(size, &ep_packet_mem);
838 }
839
840 /* allocate 'size' amount of memory with an allocation lifetime until the
841  * next capture.
842  */
843 void *
844 se_alloc(size_t size)
845 {
846         return emem_alloc(size, &se_packet_mem);
847 }
848
849 void *
850 ep_alloc0(size_t size)
851 {
852         return memset(ep_alloc(size),'\0',size);
853 }
854
855 void *
856 se_alloc0(size_t size)
857 {
858         return memset(se_alloc(size),'\0',size);
859 }
860
861
862 static gchar *
863 emem_strdup(const gchar *src, void *allocator(size_t))
864 {
865         guint len;
866         gchar *dst;
867
868         /* If str is NULL, just return the string "<NULL>" so that the callers don't
869          * have to bother checking it.
870          */
871         if(!src)
872                 return "<NULL>";
873
874         len = (guint) strlen(src);
875         dst = memcpy(allocator(len+1), src, len+1);
876
877         return dst;
878 }
879
880 gchar *
881 ep_strdup(const gchar *src)
882 {
883         return emem_strdup(src, ep_alloc);
884 }
885
886 gchar *
887 se_strdup(const gchar *src)
888 {
889         return emem_strdup(src, se_alloc);
890 }
891
892 static gchar *
893 emem_strndup(const gchar *src, size_t len, void *allocator(size_t))
894 {
895         gchar *dst = allocator(len+1);
896         guint i;
897
898         for (i = 0; (i < len) && src[i]; i++)
899                 dst[i] = src[i];
900
901         dst[i] = '\0';
902
903         return dst;
904 }
905
906 gchar *
907 ep_strndup(const gchar *src, size_t len)
908 {
909         return emem_strndup(src, len, ep_alloc);
910 }
911
912 gchar *
913 se_strndup(const gchar *src, size_t len)
914 {
915         return emem_strndup(src, len, se_alloc);
916 }
917
918
919
920 void *
921 ep_memdup(const void* src, size_t len)
922 {
923         return memcpy(ep_alloc(len), src, len);
924 }
925
926 void *
927 se_memdup(const void* src, size_t len)
928 {
929         return memcpy(se_alloc(len), src, len);
930 }
931
932 static gchar *
933 emem_strdup_vprintf(const gchar *fmt, va_list ap, void *allocator(size_t))
934 {
935         va_list ap2;
936         gsize len;
937         gchar* dst;
938
939         G_VA_COPY(ap2, ap);
940
941         len = g_printf_string_upper_bound(fmt, ap);
942
943         dst = allocator(len+1);
944         g_vsnprintf (dst, (gulong) len, fmt, ap2);
945         va_end(ap2);
946
947         return dst;
948 }
949
950 gchar *
951 ep_strdup_vprintf(const gchar *fmt, va_list ap)
952 {
953         return emem_strdup_vprintf(fmt, ap, ep_alloc);
954 }
955
956 gchar *
957 se_strdup_vprintf(const gchar* fmt, va_list ap)
958 {
959         return emem_strdup_vprintf(fmt, ap, se_alloc);
960 }
961
962 gchar *
963 ep_strdup_printf(const gchar *fmt, ...)
964 {
965         va_list ap;
966         gchar *dst;
967
968         va_start(ap, fmt);
969         dst = ep_strdup_vprintf(fmt, ap);
970         va_end(ap);
971         return dst;
972 }
973
974 gchar *
975 se_strdup_printf(const gchar *fmt, ...)
976 {
977         va_list ap;
978         gchar *dst;
979
980         va_start(ap, fmt);
981         dst = se_strdup_vprintf(fmt, ap);
982         va_end(ap);
983         return dst;
984 }
985
986 gchar **
987 ep_strsplit(const gchar* string, const gchar* sep, int max_tokens)
988 {
989         gchar* splitted;
990         gchar* s;
991         guint tokens;
992         guint str_len;
993         guint sep_len;
994         guint i;
995         gchar** vec;
996         enum { AT_START, IN_PAD, IN_TOKEN } state;
997         guint curr_tok = 0;
998
999         if (    ! string
1000              || ! sep
1001              || ! sep[0])
1002                 return NULL;
1003
1004         s = splitted = ep_strdup(string);
1005         str_len = (guint) strlen(splitted);
1006         sep_len = (guint) strlen(sep);
1007
1008         if (max_tokens < 1) max_tokens = INT_MAX;
1009
1010         tokens = 1;
1011
1012
1013         while (tokens <= (guint)max_tokens && ( s = strstr(s,sep) )) {
1014                 tokens++;
1015
1016                 for(i=0; i < sep_len; i++ )
1017                         s[i] = '\0';
1018
1019                 s += sep_len;
1020
1021         }
1022
1023         vec = ep_alloc_array(gchar*,tokens+1);
1024         state = AT_START;
1025
1026         for (i=0; i< str_len; i++) {
1027                 switch(state) {
1028                         case AT_START:
1029                                 switch(splitted[i]) {
1030                                         case '\0':
1031                                                 state  = IN_PAD;
1032                                                 continue;
1033                                         default:
1034                                                 vec[curr_tok] = &(splitted[i]);
1035                                                 curr_tok++;
1036                                                 state = IN_TOKEN;
1037                                                 continue;
1038                                 }
1039                         case IN_TOKEN:
1040                                 switch(splitted[i]) {
1041                                         case '\0':
1042                                                 state = IN_PAD;
1043                                         default:
1044                                                 continue;
1045                                 }
1046                         case IN_PAD:
1047                                 switch(splitted[i]) {
1048                                         default:
1049                                                 vec[curr_tok] = &(splitted[i]);
1050                                                 curr_tok++;
1051                                                 state = IN_TOKEN;
1052                                         case '\0':
1053                                                 continue;
1054                                 }
1055                 }
1056         }
1057
1058         vec[curr_tok] = NULL;
1059
1060         return vec;
1061 }
1062
1063 gchar *
1064 ep_strconcat(const gchar *string1, ...)
1065 {
1066         gsize   l;
1067         va_list args;
1068         gchar   *s;
1069         gchar   *concat;
1070         gchar   *ptr;
1071
1072         if (!string1)
1073                 return NULL;
1074
1075         l = 1 + strlen(string1);
1076         va_start(args, string1);
1077         s = va_arg(args, gchar*);
1078         while (s) {
1079                 l += strlen(s);
1080                 s = va_arg(args, gchar*);
1081         }
1082         va_end(args);
1083
1084         concat = ep_alloc(l);
1085         ptr = concat;
1086
1087         ptr = g_stpcpy(ptr, string1);
1088         va_start(args, string1);
1089         s = va_arg(args, gchar*);
1090         while (s) {
1091                 ptr = g_stpcpy(ptr, s);
1092                 s = va_arg(args, gchar*);
1093         }
1094         va_end(args);
1095
1096         return concat;
1097 }
1098
1099
1100
1101 /* release all allocated memory back to the pool. */
1102 static void
1103 emem_free_all(emem_header_t *mem)
1104 {
1105         gboolean use_chunks = mem->debug_use_chunks;
1106
1107         emem_chunk_t *npc;
1108         emem_tree_t *tree_list;
1109
1110         /* move all used chunks over to the free list */
1111         while(mem->used_list){
1112                 npc=mem->used_list;
1113                 mem->used_list=mem->used_list->next;
1114                 npc->next=mem->free_list;
1115                 mem->free_list=npc;
1116         }
1117
1118         /* clear them all out */
1119         npc = mem->free_list;
1120         while (npc != NULL) {
1121                 if (use_chunks) {
1122                         while (npc->canary_last != NULL) {
1123                                 npc->canary_last = emem_canary_next(mem->canary, npc->canary_last, NULL);
1124                                 /* XXX, check if canary_last is inside allocated memory? */
1125
1126                                 if (npc->canary_last == (void *) -1)
1127                                         g_error("Memory corrupted");
1128                         }
1129
1130                         emem_scrub_memory((npc->buf + npc->free_offset_init),
1131                                           (npc->free_offset - npc->free_offset_init),
1132                                           FALSE);
1133
1134                         npc->amount_free = npc->amount_free_init;
1135                         npc->free_offset = npc->free_offset_init;
1136                         npc = npc->next;
1137                 } else {
1138                         emem_chunk_t *next = npc->next;
1139
1140                         emem_scrub_memory(npc->buf, npc->amount_free_init, FALSE);
1141
1142                         g_free(npc->buf);
1143                         g_free(npc);
1144                         npc = next;
1145                 }
1146         }
1147
1148         if (!use_chunks) {
1149                 /* We've freed all this memory already */
1150                 mem->free_list = NULL;
1151         }
1152
1153         /* release/reset all allocated trees */
1154         for(tree_list=mem->trees;tree_list;tree_list=tree_list->next){
1155                 tree_list->tree=NULL;
1156         }
1157 }
1158
1159 /* release all allocated memory back to the pool. */
1160 void
1161 ep_free_all(void)
1162 {
1163         emem_free_all(&ep_packet_mem);
1164 }
1165
1166 /* release all allocated memory back to the pool. */
1167 void
1168 se_free_all(void)
1169 {
1170 #ifdef SHOW_EMEM_STATS
1171         print_alloc_stats();
1172 #endif
1173
1174         emem_free_all(&se_packet_mem);
1175 }
1176
1177 ep_stack_t
1178 ep_stack_new(void) {
1179         ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
1180         *s = ep_new0(struct _ep_stack_frame_t);
1181         return s;
1182 }
1183
1184 /*  for ep_stack_t we'll keep the popped frames so we reuse them instead
1185 of allocating new ones.
1186 */
1187
1188 void *
1189 ep_stack_push(ep_stack_t stack, void* data)
1190 {
1191         struct _ep_stack_frame_t* frame;
1192         struct _ep_stack_frame_t* head = (*stack);
1193
1194         if (head->above) {
1195                 frame = head->above;
1196         } else {
1197                 frame = ep_new(struct _ep_stack_frame_t);
1198                 head->above = frame;
1199                 frame->below = head;
1200                 frame->above = NULL;
1201         }
1202
1203         frame->payload = data;
1204         (*stack) = frame;
1205
1206         return data;
1207 }
1208
1209 void *
1210 ep_stack_pop(ep_stack_t stack)
1211 {
1212
1213         if ((*stack)->below) {
1214                 (*stack) = (*stack)->below;
1215                 return (*stack)->above->payload;
1216         } else {
1217                 return NULL;
1218         }
1219 }
1220
1221 emem_tree_t *
1222 se_tree_create(int type, const char *name)
1223 {
1224         emem_tree_t *tree_list;
1225
1226         tree_list=g_malloc(sizeof(emem_tree_t));
1227         tree_list->next=se_packet_mem.trees;
1228         tree_list->type=type;
1229         tree_list->tree=NULL;
1230         tree_list->name=name;
1231         tree_list->malloc=se_alloc;
1232         se_packet_mem.trees=tree_list;
1233
1234         return tree_list;
1235 }
1236
1237 void *
1238 emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
1239 {
1240         emem_tree_node_t *node;
1241
1242         node=se_tree->tree;
1243
1244         while(node){
1245                 if(key==node->key32){
1246                         return node->data;
1247                 }
1248                 if(key<node->key32){
1249                         node=node->left;
1250                         continue;
1251                 }
1252                 if(key>node->key32){
1253                         node=node->right;
1254                         continue;
1255                 }
1256         }
1257         return NULL;
1258 }
1259
1260 void *
1261 emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key)
1262 {
1263         emem_tree_node_t *node;
1264
1265         node=se_tree->tree;
1266
1267         if(!node){
1268                 return NULL;
1269         }
1270
1271
1272         while(node){
1273                 if(key==node->key32){
1274                         return node->data;
1275                 }
1276                 if(key<node->key32){
1277                         if(node->left){
1278                                 node=node->left;
1279                                 continue;
1280                         } else {
1281                                 break;
1282                         }
1283                 }
1284                 if(key>node->key32){
1285                         if(node->right){
1286                                 node=node->right;
1287                                 continue;
1288                         } else {
1289                                 break;
1290                         }
1291                 }
1292         }
1293
1294
1295         if(!node){
1296                 return NULL;
1297         }
1298
1299         /* If we are still at the root of the tree this means that this node
1300          * is either smaller than the search key and then we return this
1301          * node or else there is no smaller key available and then
1302          * we return NULL.
1303          */
1304         if(!node->parent){
1305                 if(key>node->key32){
1306                         return node->data;
1307                 } else {
1308                         return NULL;
1309                 }
1310         }
1311
1312         if(node->parent->left==node){
1313                 /* left child */
1314
1315                 if(key>node->key32){
1316                         /* if this is a left child and its key is smaller than
1317                          * the search key, then this is the node we want.
1318                          */
1319                         return node->data;
1320                 } else {
1321                         /* if this is a left child and its key is bigger than
1322                          * the search key, we have to check if any
1323                          * of our ancestors are smaller than the search key.
1324                          */
1325                         while(node){
1326                                 if(key>node->key32){
1327                                         return node->data;
1328                                 }
1329                                 node=node->parent;
1330                         }
1331                         return NULL;
1332                 }
1333         } else {
1334                 /* right child */
1335
1336                 if(node->key32<key){
1337                         /* if this is the right child and its key is smaller
1338                          * than the search key then this is the one we want.
1339                          */
1340                         return node->data;
1341                 } else {
1342                         /* if this is the right child and its key is larger
1343                          * than the search key then our parent is the one we
1344                          * want.
1345                          */
1346                         return node->parent->data;
1347                 }
1348         }
1349
1350 }
1351
1352
1353 static inline emem_tree_node_t *
1354 emem_tree_parent(emem_tree_node_t *node)
1355 {
1356         return node->parent;
1357 }
1358
1359 static inline emem_tree_node_t *
1360 emem_tree_grandparent(emem_tree_node_t *node)
1361 {
1362         emem_tree_node_t *parent;
1363
1364         parent=emem_tree_parent(node);
1365         if(parent){
1366                 return parent->parent;
1367         }
1368         return NULL;
1369 }
1370
1371 static inline emem_tree_node_t *
1372 emem_tree_uncle(emem_tree_node_t *node)
1373 {
1374         emem_tree_node_t *parent, *grandparent;
1375
1376         parent=emem_tree_parent(node);
1377         if(!parent){
1378                 return NULL;
1379         }
1380         grandparent=emem_tree_parent(parent);
1381         if(!grandparent){
1382                 return NULL;
1383         }
1384         if(parent==grandparent->left){
1385                 return grandparent->right;
1386         }
1387         return grandparent->left;
1388 }
1389
1390 static inline void rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node);
1391 static inline void rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node);
1392
1393 static inline void
1394 rotate_left(emem_tree_t *se_tree, emem_tree_node_t *node)
1395 {
1396         if(node->parent){
1397                 if(node->parent->left==node){
1398                         node->parent->left=node->right;
1399                 } else {
1400                         node->parent->right=node->right;
1401                 }
1402         } else {
1403                 se_tree->tree=node->right;
1404         }
1405         node->right->parent=node->parent;
1406         node->parent=node->right;
1407         node->right=node->right->left;
1408         if(node->right){
1409                 node->right->parent=node;
1410         }
1411         node->parent->left=node;
1412 }
1413
1414 static inline void
1415 rotate_right(emem_tree_t *se_tree, emem_tree_node_t *node)
1416 {
1417         if(node->parent){
1418                 if(node->parent->left==node){
1419                         node->parent->left=node->left;
1420                 } else {
1421                         node->parent->right=node->left;
1422                 }
1423         } else {
1424                 se_tree->tree=node->left;
1425         }
1426         node->left->parent=node->parent;
1427         node->parent=node->left;
1428         node->left=node->left->right;
1429         if(node->left){
1430                 node->left->parent=node;
1431         }
1432         node->parent->right=node;
1433 }
1434
1435 static inline void
1436 rb_insert_case5(emem_tree_t *se_tree, emem_tree_node_t *node)
1437 {
1438         emem_tree_node_t *grandparent;
1439         emem_tree_node_t *parent;
1440
1441         parent=emem_tree_parent(node);
1442         grandparent=emem_tree_parent(parent);
1443         parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1444         grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1445         if( (node==parent->left) && (parent==grandparent->left) ){
1446                 rotate_right(se_tree, grandparent);
1447         } else {
1448                 rotate_left(se_tree, grandparent);
1449         }
1450 }
1451
1452 static inline void
1453 rb_insert_case4(emem_tree_t *se_tree, emem_tree_node_t *node)
1454 {
1455         emem_tree_node_t *grandparent;
1456         emem_tree_node_t *parent;
1457
1458         parent=emem_tree_parent(node);
1459         grandparent=emem_tree_parent(parent);
1460         if(!grandparent){
1461                 return;
1462         }
1463         if( (node==parent->right) && (parent==grandparent->left) ){
1464                 rotate_left(se_tree, parent);
1465                 node=node->left;
1466         } else if( (node==parent->left) && (parent==grandparent->right) ){
1467                 rotate_right(se_tree, parent);
1468                 node=node->right;
1469         }
1470         rb_insert_case5(se_tree, node);
1471 }
1472
1473 static inline void
1474 rb_insert_case3(emem_tree_t *se_tree, emem_tree_node_t *node)
1475 {
1476         emem_tree_node_t *grandparent;
1477         emem_tree_node_t *parent;
1478         emem_tree_node_t *uncle;
1479
1480         uncle=emem_tree_uncle(node);
1481         if(uncle && (uncle->u.rb_color==EMEM_TREE_RB_COLOR_RED)){
1482                 parent=emem_tree_parent(node);
1483                 parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1484                 uncle->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1485                 grandparent=emem_tree_grandparent(node);
1486                 grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1487                 rb_insert_case1(se_tree, grandparent);
1488         } else {
1489                 rb_insert_case4(se_tree, node);
1490         }
1491 }
1492
1493 static inline void
1494 rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node)
1495 {
1496         emem_tree_node_t *parent;
1497
1498         parent=emem_tree_parent(node);
1499         /* parent is always non-NULL here */
1500         if(parent->u.rb_color==EMEM_TREE_RB_COLOR_BLACK){
1501                 return;
1502         }
1503         rb_insert_case3(se_tree, node);
1504 }
1505
1506 static inline void
1507 rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node)
1508 {
1509         emem_tree_node_t *parent;
1510
1511         parent=emem_tree_parent(node);
1512         if(!parent){
1513                 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1514                 return;
1515         }
1516         rb_insert_case2(se_tree, node);
1517 }
1518
1519 /* insert a new node in the tree. if this node matches an already existing node
1520  * then just replace the data for that node */
1521 void
1522 emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
1523 {
1524         emem_tree_node_t *node;
1525
1526         node=se_tree->tree;
1527
1528         /* is this the first node ?*/
1529         if(!node){
1530                 node=se_tree->malloc(sizeof(emem_tree_node_t));
1531                 switch(se_tree->type){
1532                 case EMEM_TREE_TYPE_RED_BLACK:
1533                         node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1534                         break;
1535                 }
1536                 node->parent=NULL;
1537                 node->left=NULL;
1538                 node->right=NULL;
1539                 node->key32=key;
1540                 node->data=data;
1541                 node->u.is_subtree = EMEM_TREE_NODE_IS_DATA;
1542                 se_tree->tree=node;
1543                 return;
1544         }
1545
1546         /* it was not the new root so walk the tree until we find where to
1547          * insert this new leaf.
1548          */
1549         while(1){
1550                 /* this node already exists, so just replace the data pointer*/
1551                 if(key==node->key32){
1552                         node->data=data;
1553                         return;
1554                 }
1555                 if(key<node->key32) {
1556                         if(!node->left){
1557                                 /* new node to the left */
1558                                 emem_tree_node_t *new_node;
1559                                 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1560                                 node->left=new_node;
1561                                 new_node->parent=node;
1562                                 new_node->left=NULL;
1563                                 new_node->right=NULL;
1564                                 new_node->key32=key;
1565                                 new_node->data=data;
1566                                 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1567                                 node=new_node;
1568                                 break;
1569                         }
1570                         node=node->left;
1571                         continue;
1572                 }
1573                 if(key>node->key32) {
1574                         if(!node->right){
1575                                 /* new node to the right */
1576                                 emem_tree_node_t *new_node;
1577                                 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1578                                 node->right=new_node;
1579                                 new_node->parent=node;
1580                                 new_node->left=NULL;
1581                                 new_node->right=NULL;
1582                                 new_node->key32=key;
1583                                 new_node->data=data;
1584                                 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1585                                 node=new_node;
1586                                 break;
1587                         }
1588                         node=node->right;
1589                         continue;
1590                 }
1591         }
1592
1593         /* node will now point to the newly created node */
1594         switch(se_tree->type){
1595         case EMEM_TREE_TYPE_RED_BLACK:
1596                 node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1597                 rb_insert_case1(se_tree, node);
1598                 break;
1599         }
1600 }
1601
1602 static void *
1603 lookup_or_insert32(emem_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud, int is_subtree)
1604 {
1605         emem_tree_node_t *node;
1606
1607         node=se_tree->tree;
1608
1609         /* is this the first node ?*/
1610         if(!node){
1611                 node=se_tree->malloc(sizeof(emem_tree_node_t));
1612                 switch(se_tree->type){
1613                         case EMEM_TREE_TYPE_RED_BLACK:
1614                                 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1615                                 break;
1616                 }
1617                 node->parent=NULL;
1618                 node->left=NULL;
1619                 node->right=NULL;
1620                 node->key32=key;
1621                 node->data= func(ud);
1622                 node->u.is_subtree = is_subtree;
1623                 se_tree->tree=node;
1624                 return node->data;
1625         }
1626
1627         /* it was not the new root so walk the tree until we find where to
1628                 * insert this new leaf.
1629                 */
1630         while(1){
1631                 /* this node already exists, so just return the data pointer*/
1632                 if(key==node->key32){
1633                         return node->data;
1634                 }
1635                 if(key<node->key32) {
1636                         if(!node->left){
1637                                 /* new node to the left */
1638                                 emem_tree_node_t *new_node;
1639                                 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1640                                 node->left=new_node;
1641                                 new_node->parent=node;
1642                                 new_node->left=NULL;
1643                                 new_node->right=NULL;
1644                                 new_node->key32=key;
1645                                 new_node->data= func(ud);
1646                                 new_node->u.is_subtree = is_subtree;
1647                                 node=new_node;
1648                                 break;
1649                         }
1650                         node=node->left;
1651                         continue;
1652                 }
1653                 if(key>node->key32) {
1654                         if(!node->right){
1655                                 /* new node to the right */
1656                                 emem_tree_node_t *new_node;
1657                                 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1658                                 node->right=new_node;
1659                                 new_node->parent=node;
1660                                 new_node->left=NULL;
1661                                 new_node->right=NULL;
1662                                 new_node->key32=key;
1663                                 new_node->data= func(ud);
1664                                 new_node->u.is_subtree = is_subtree;
1665                                 node=new_node;
1666                                 break;
1667                         }
1668                         node=node->right;
1669                         continue;
1670                 }
1671         }
1672
1673         /* node will now point to the newly created node */
1674         switch(se_tree->type){
1675                 case EMEM_TREE_TYPE_RED_BLACK:
1676                         node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1677                         rb_insert_case1(se_tree, node);
1678                         break;
1679         }
1680
1681         return node->data;
1682 }
1683
1684 /* When the se data is released, this entire tree will dissapear as if it
1685  * never existed including all metadata associated with the tree.
1686  */
1687 emem_tree_t *
1688 se_tree_create_non_persistent(int type, const char *name)
1689 {
1690         emem_tree_t *tree_list;
1691
1692         tree_list=se_alloc(sizeof(emem_tree_t));
1693         tree_list->next=NULL;
1694         tree_list->type=type;
1695         tree_list->tree=NULL;
1696         tree_list->name=name;
1697         tree_list->malloc=se_alloc;
1698
1699         return tree_list;
1700 }
1701
1702 /* This tree is PErmanent and will never be released
1703  */
1704 emem_tree_t *
1705 pe_tree_create(int type, const char *name)
1706 {
1707         emem_tree_t *tree_list;
1708
1709         tree_list=g_new(emem_tree_t, 1);
1710         tree_list->next=NULL;
1711         tree_list->type=type;
1712         tree_list->tree=NULL;
1713         tree_list->name=name;
1714         tree_list->malloc=(void *(*)(size_t)) g_malloc;
1715
1716         return tree_list;
1717 }
1718
1719 /* create another (sub)tree using the same memory allocation scope
1720  * as the parent tree.
1721  */
1722 static emem_tree_t *
1723 emem_tree_create_subtree(emem_tree_t *parent_tree, const char *name)
1724 {
1725         emem_tree_t *tree_list;
1726
1727         tree_list=parent_tree->malloc(sizeof(emem_tree_t));
1728         tree_list->next=NULL;
1729         tree_list->type=parent_tree->type;
1730         tree_list->tree=NULL;
1731         tree_list->name=name;
1732         tree_list->malloc=parent_tree->malloc;
1733
1734         return tree_list;
1735 }
1736
1737 static void *
1738 create_sub_tree(void* d)
1739 {
1740         emem_tree_t *se_tree = d;
1741         return emem_tree_create_subtree(se_tree, "subtree");
1742 }
1743
1744 /* insert a new node in the tree. if this node matches an already existing node
1745  * then just replace the data for that node */
1746
1747 void
1748 emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data)
1749 {
1750         emem_tree_t *next_tree;
1751
1752         if((key[0].length<1)||(key[0].length>100)){
1753                 DISSECTOR_ASSERT_NOT_REACHED();
1754         }
1755         if((key[0].length==1)&&(key[1].length==0)){
1756                 emem_tree_insert32(se_tree, *key[0].key, data);
1757                 return;
1758         }
1759
1760         next_tree=lookup_or_insert32(se_tree, *key[0].key, create_sub_tree, se_tree, EMEM_TREE_NODE_IS_SUBTREE);
1761
1762         if(key[0].length==1){
1763                 key++;
1764         } else {
1765                 key[0].length--;
1766                 key[0].key++;
1767         }
1768         emem_tree_insert32_array(next_tree, key, data);
1769 }
1770
1771 void *
1772 emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key)
1773 {
1774         emem_tree_t *next_tree;
1775
1776         if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
1777
1778         if((key[0].length<1)||(key[0].length>100)){
1779                 DISSECTOR_ASSERT_NOT_REACHED();
1780         }
1781         if((key[0].length==1)&&(key[1].length==0)){
1782                 return emem_tree_lookup32(se_tree, *key[0].key);
1783         }
1784         next_tree=emem_tree_lookup32(se_tree, *key[0].key);
1785         if(!next_tree){
1786                 return NULL;
1787         }
1788         if(key[0].length==1){
1789                 key++;
1790         } else {
1791                 key[0].length--;
1792                 key[0].key++;
1793         }
1794         return emem_tree_lookup32_array(next_tree, key);
1795 }
1796
1797 void *
1798 emem_tree_lookup32_array_le(emem_tree_t *se_tree, emem_tree_key_t *key)
1799 {
1800         emem_tree_t *next_tree;
1801
1802         if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
1803
1804         if((key[0].length<1)||(key[0].length>100)){
1805                 DISSECTOR_ASSERT_NOT_REACHED();
1806         }
1807         if((key[0].length==1)&&(key[1].length==0)){ /* last key in key array */
1808                 return emem_tree_lookup32_le(se_tree, *key[0].key);
1809         }
1810         next_tree=emem_tree_lookup32(se_tree, *key[0].key);
1811         /* key[0].key not found so find le and return */
1812         if(!next_tree)
1813                 return emem_tree_lookup32_le(se_tree, *key[0].key);
1814
1815         /* key[0].key found so inc key pointer and try again */
1816         if(key[0].length==1){
1817                 key++;
1818         } else {
1819                 key[0].length--;
1820                 key[0].key++;
1821         }
1822         return emem_tree_lookup32_array_le(next_tree, key);
1823 }
1824
1825 /* Strings are stored as an array of uint32 containing the string characters
1826    with 4 characters in each uint32.
1827    The first byte of the string is stored as the most significant byte.
1828    If the string is not a multiple of 4 characters in length the last
1829    uint32 containing the string bytes are padded with 0 bytes.
1830    After the uint32's containing the string, there is one final terminator
1831    uint32 with the value 0x00000001
1832 */
1833 void
1834 emem_tree_insert_string(emem_tree_t* se_tree, const gchar* k, void* v, guint32 flags)
1835 {
1836         emem_tree_key_t key[2];
1837         guint32 *aligned=NULL;
1838         guint32 len = (guint32) strlen(k);
1839         guint32 divx = (len+3)/4+1;
1840         guint32 i;
1841         guint32 tmp;
1842
1843         aligned = g_malloc(divx * sizeof (guint32));
1844
1845         /* pack the bytes one one by one into guint32s */
1846         tmp = 0;
1847         for (i = 0;i < len;i++) {
1848                 unsigned char ch;
1849
1850                 ch = (unsigned char)k[i];
1851                 if (flags & EMEM_TREE_STRING_NOCASE) {
1852                         if(isupper(ch)) {
1853                                 ch = tolower(ch);
1854                         }
1855                 }
1856                 tmp <<= 8;
1857                 tmp |= ch;
1858                 if (i%4 == 3) {
1859                         aligned[i/4] = tmp;
1860                         tmp = 0;
1861                 }
1862         }
1863         /* add required padding to the last uint32 */
1864         if (i%4 != 0) {
1865                 while (i%4 != 0) {
1866                         i++;
1867                         tmp <<= 8;
1868                 }
1869                 aligned[i/4-1] = tmp;
1870         }
1871
1872         /* add the terminator */
1873         aligned[divx-1] = 0x00000001;
1874
1875         key[0].length = divx;
1876         key[0].key = aligned;
1877         key[1].length = 0;
1878         key[1].key = NULL;
1879
1880
1881         emem_tree_insert32_array(se_tree, key, v);
1882         g_free(aligned);
1883 }
1884
1885 void *
1886 emem_tree_lookup_string(emem_tree_t* se_tree, const gchar* k, guint32 flags)
1887 {
1888         emem_tree_key_t key[2];
1889         guint32 *aligned=NULL;
1890         guint32 len = (guint) strlen(k);
1891         guint32 divx = (len+3)/4+1;
1892         guint32 i;
1893         guint32 tmp;
1894         void *ret;
1895
1896         aligned = g_malloc(divx * sizeof (guint32));
1897
1898         /* pack the bytes one one by one into guint32s */
1899         tmp = 0;
1900         for (i = 0;i < len;i++) {
1901                 unsigned char ch;
1902
1903                 ch = (unsigned char)k[i];
1904                 if (flags & EMEM_TREE_STRING_NOCASE) {
1905                         if(isupper(ch)) {
1906                                 ch = tolower(ch);
1907                         }
1908                 }
1909                 tmp <<= 8;
1910                 tmp |= ch;
1911                 if (i%4 == 3) {
1912                         aligned[i/4] = tmp;
1913                         tmp = 0;
1914                 }
1915         }
1916         /* add required padding to the last uint32 */
1917         if (i%4 != 0) {
1918                 while (i%4 != 0) {
1919                         i++;
1920                         tmp <<= 8;
1921                 }
1922                 aligned[i/4-1] = tmp;
1923         }
1924
1925         /* add the terminator */
1926         aligned[divx-1] = 0x00000001;
1927
1928         key[0].length = divx;
1929         key[0].key = aligned;
1930         key[1].length = 0;
1931         key[1].key = NULL;
1932
1933
1934         ret = emem_tree_lookup32_array(se_tree, key);
1935         g_free(aligned);
1936         return ret;
1937 }
1938
1939 static gboolean
1940 emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
1941 {
1942         gboolean stop_traverse = FALSE;
1943
1944         if (!node)
1945                 return FALSE;
1946
1947         if(node->left) {
1948                 stop_traverse = emem_tree_foreach_nodes(node->left, callback, user_data);
1949                 if (stop_traverse) {
1950                         return TRUE;
1951                 }
1952         }
1953
1954         if (node->u.is_subtree == EMEM_TREE_NODE_IS_SUBTREE) {
1955                 stop_traverse = emem_tree_foreach(node->data, callback, user_data);
1956         } else {
1957                 stop_traverse = callback(node->data, user_data);
1958         }
1959
1960         if (stop_traverse) {
1961                 return TRUE;
1962         }
1963
1964         if(node->right) {
1965                 stop_traverse = emem_tree_foreach_nodes(node->right, callback, user_data);
1966                 if (stop_traverse) {
1967                         return TRUE;
1968                 }
1969         }
1970
1971         return FALSE;
1972 }
1973
1974 gboolean
1975 emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data)
1976 {
1977         if (!emem_tree)
1978                 return FALSE;
1979
1980         if(!emem_tree->tree)
1981                 return FALSE;
1982
1983         return emem_tree_foreach_nodes(emem_tree->tree, callback, user_data);
1984 }
1985
1986
1987 static void
1988 emem_tree_print_nodes(emem_tree_node_t* node, int level)
1989 {
1990         int i;
1991
1992         if (!node)
1993                 return;
1994
1995         for(i=0;i<level;i++){
1996                 printf("    ");
1997         }
1998
1999         printf("NODE:%p parent:%p left:0x%p right:%px key:%d data:%p\n",
2000                 (void *)node,(void *)(node->parent),(void *)(node->left),(void *)(node->right),
2001                 (node->key32),node->data);
2002         if(node->left)
2003                 emem_tree_print_nodes(node->left, level+1);
2004         if(node->right)
2005                 emem_tree_print_nodes(node->right, level+1);
2006 }
2007 void
2008 emem_print_tree(emem_tree_t* emem_tree)
2009 {
2010         if (!emem_tree)
2011                 return;
2012
2013         printf("EMEM tree type:%d name:%s tree:%p\n",emem_tree->type,emem_tree->name,(void *)(emem_tree->tree));
2014         if(emem_tree->tree)
2015                 emem_tree_print_nodes(emem_tree->tree, 0);
2016 }
2017
2018 /*
2019  * String buffers
2020  */
2021
2022 /*
2023  * Presumably we're using these routines for building strings for the tree.
2024  * Use ITEM_LABEL_LENGTH as the basis for our default lengths.
2025  */
2026
2027 #define DEFAULT_STRBUF_LEN (ITEM_LABEL_LENGTH / 10)
2028 #define MAX_STRBUF_LEN 65536
2029
2030 static gsize
2031 next_size(gsize cur_alloc_len, gsize wanted_alloc_len, gsize max_alloc_len)
2032 {
2033         if (max_alloc_len < 1 || max_alloc_len > MAX_STRBUF_LEN) {
2034                 max_alloc_len = MAX_STRBUF_LEN;
2035         }
2036
2037         if (cur_alloc_len < 1) {
2038                 cur_alloc_len = DEFAULT_STRBUF_LEN;
2039         }
2040
2041         while (cur_alloc_len < wanted_alloc_len) {
2042                 cur_alloc_len *= 2;
2043         }
2044
2045         return cur_alloc_len < max_alloc_len ? cur_alloc_len : max_alloc_len;
2046 }
2047
2048 static void
2049 ep_strbuf_grow(emem_strbuf_t *strbuf, gsize wanted_alloc_len)
2050 {
2051         gsize new_alloc_len;
2052         gchar *new_str;
2053
2054         if (!strbuf || (wanted_alloc_len <= strbuf->alloc_len) || (strbuf->alloc_len >= strbuf->max_alloc_len)) {
2055                 return;
2056         }
2057
2058         new_alloc_len = next_size(strbuf->alloc_len, wanted_alloc_len, strbuf->max_alloc_len);
2059         new_str = ep_alloc(new_alloc_len);
2060         g_strlcpy(new_str, strbuf->str, new_alloc_len);
2061
2062         strbuf->alloc_len = new_alloc_len;
2063         strbuf->str = new_str;
2064 }
2065
2066 emem_strbuf_t *
2067 ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len)
2068 {
2069         emem_strbuf_t *strbuf;
2070
2071         strbuf = ep_alloc(sizeof(emem_strbuf_t));
2072
2073         if ((max_alloc_len == 0) || (max_alloc_len > MAX_STRBUF_LEN))
2074                 max_alloc_len = MAX_STRBUF_LEN;
2075         if (alloc_len == 0)
2076                 alloc_len = 1;
2077         else if (alloc_len > max_alloc_len)
2078                 alloc_len = max_alloc_len;
2079
2080         strbuf->str = ep_alloc(alloc_len);
2081         strbuf->str[0] = '\0';
2082
2083         strbuf->len = 0;
2084         strbuf->alloc_len = alloc_len;
2085         strbuf->max_alloc_len = max_alloc_len;
2086
2087         return strbuf;
2088 }
2089
2090 emem_strbuf_t *
2091 ep_strbuf_new(const gchar *init)
2092 {
2093         emem_strbuf_t *strbuf;
2094
2095         strbuf = ep_strbuf_sized_new(next_size(0, init?strlen(init)+1:0, 0), 0);  /* +1 for NULL terminator */
2096         if (init) {
2097                 gsize full_len;
2098                 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
2099                 strbuf->len = MIN(full_len, strbuf->alloc_len-1);
2100         }
2101
2102         return strbuf;
2103 }
2104
2105 emem_strbuf_t *
2106 ep_strbuf_new_label(const gchar *init)
2107 {
2108         emem_strbuf_t *strbuf;
2109         gsize full_len;
2110
2111         /* Be optimistic: Allocate default size strbuf string and only      */
2112         /*  request an increase if needed.                                  */
2113         /* XXX: Is it reasonable to assume that much of the usage of        */
2114         /*  ep_strbuf_new_label will have  init==NULL or                    */
2115         /*   strlen(init) < DEFAULT_STRBUF_LEN) ???                         */
2116         strbuf = ep_strbuf_sized_new(DEFAULT_STRBUF_LEN, ITEM_LABEL_LENGTH);
2117
2118         if (!init)
2119                 return strbuf;
2120
2121         /* full_len does not count the trailing '\0'.                       */
2122         full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
2123         if (full_len < strbuf->alloc_len) {
2124                 strbuf->len += full_len;
2125         } else {
2126                 strbuf = ep_strbuf_sized_new(full_len+1, ITEM_LABEL_LENGTH);
2127                 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
2128                 strbuf->len = MIN(full_len, strbuf->alloc_len-1);
2129         }
2130
2131         return strbuf;
2132 }
2133
2134 emem_strbuf_t *
2135 ep_strbuf_append(emem_strbuf_t *strbuf, const gchar *str)
2136 {
2137         gsize add_len, full_len;
2138
2139         if (!strbuf || !str || str[0] == '\0') {
2140                 return strbuf;
2141         }
2142
2143         /* Be optimistic; try the g_strlcpy first & see if enough room.                 */
2144         /* Note: full_len doesn't count the trailing '\0'; add_len does allow for same  */
2145         add_len = strbuf->alloc_len - strbuf->len;
2146         full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
2147         if (full_len < add_len) {
2148                 strbuf->len += full_len;
2149         } else {
2150                 strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
2151                 ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
2152                 add_len = strbuf->alloc_len - strbuf->len;
2153                 full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
2154                 strbuf->len += MIN(add_len-1, full_len);
2155         }
2156
2157         return strbuf;
2158 }
2159
2160 void
2161 ep_strbuf_append_vprintf(emem_strbuf_t *strbuf, const gchar *format, va_list ap)
2162 {
2163         va_list ap2;
2164         gsize add_len, full_len;
2165
2166         G_VA_COPY(ap2, ap);
2167
2168         /* Be optimistic; try the g_vsnprintf first & see if enough room.               */
2169         /* Note: full_len doesn't count the trailing '\0'; add_len does allow for same. */
2170         add_len = strbuf->alloc_len - strbuf->len;
2171         full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap);
2172         if (full_len < add_len) {
2173                 strbuf->len += full_len;
2174         } else {
2175                 strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
2176                 ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
2177                 add_len = strbuf->alloc_len - strbuf->len;
2178                 full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap2);
2179                 strbuf->len += MIN(add_len-1, full_len);
2180         }
2181
2182         va_end(ap2);
2183 }
2184
2185 void
2186 ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
2187 {
2188         va_list ap;
2189
2190         va_start(ap, format);
2191         ep_strbuf_append_vprintf(strbuf, format, ap);
2192         va_end(ap);
2193 }
2194
2195 void
2196 ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
2197 {
2198         va_list ap;
2199         if (!strbuf) {
2200                 return;
2201         }
2202
2203         strbuf->len = 0;
2204
2205         va_start(ap, format);
2206         ep_strbuf_append_vprintf(strbuf, format, ap);
2207         va_end(ap);
2208 }
2209
2210 emem_strbuf_t *
2211 ep_strbuf_append_c(emem_strbuf_t *strbuf, const gchar c)
2212 {
2213         if (!strbuf) {
2214                 return strbuf;
2215         }
2216
2217         /* +1 for the new character & +1 for the trailing '\0'. */
2218         if (strbuf->alloc_len < strbuf->len + 1 + 1) {
2219                 ep_strbuf_grow(strbuf, strbuf->len + 1 + 1);
2220         }
2221         if (strbuf->alloc_len >= strbuf->len + 1 + 1) {
2222                 strbuf->str[strbuf->len] = c;
2223                 strbuf->len++;
2224                 strbuf->str[strbuf->len] = '\0';
2225         }
2226
2227         return strbuf;
2228 }
2229
2230 emem_strbuf_t *
2231 ep_strbuf_truncate(emem_strbuf_t *strbuf, gsize len)
2232 {
2233         if (!strbuf || len >= strbuf->len) {
2234                 return strbuf;
2235         }
2236
2237         strbuf->str[len] = '\0';
2238         strbuf->len = len;
2239
2240         return strbuf;
2241 }
2242
2243 /*
2244  * Editor modelines
2245  *
2246  * Local Variables:
2247  * c-basic-offset: 8
2248  * tab-width: 8
2249  * indent-tabs-mode: t
2250  * End:
2251  *
2252  * ex: set shiftwidth=8 tabstop=8 noexpandtab
2253  * :indentSize=8:tabSize=8:noTabs=false:
2254  */