1 /*
2  * MDCacheD client library for C
3  * Implements interfaces defined in mc_client.h
4  * (c) 2007.-2012 Ivan Voras <ivoras@gmail.com>
5  * Released under the 2-clause BSDL
6  * $Id: common_utils.c 448 2012-03-20 13:19:46Z ivoras $
7  */
8 
9 #include <sys/time.h>
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "common_utils.h"
16 #include "mc_protocol.h"
17 
18 /** Does a pretty hexdump of possibly human-readable data */
19 void
_dump_data(void * data,unsigned size)20 _dump_data(void *data, unsigned size) {
21 	unsigned char *bdata = data;
22 	char line[100];
23 	unsigned char buf[40];
24 	unsigned lcount = 0;
25 	unsigned bpos = 0;
26 	unsigned i,j;
27 
28 	if (size > _DUMP_BPL) {
29 		sprintf(line, "%06d : ", lcount++);
30 		printf("** Dumping %u bytes at %p:\n", size, data);
31 	} else
32 		line[0] = 0;
33 
34 	for (i = 0; i < size; i++) {
35 		bpos = i % _DUMP_BPL;
36 		buf[bpos] = *(bdata++);
37 
38 		if (bpos == _DUMP_BPL-1) {
39 			for (j = 0; j < bpos+1; j++) {
40 				char bb[5];
41 				sprintf(bb, "%02x ", buf[j]);
42 				strcat(line, bb);
43 			}
44 			strcat(line, "| ");
45 			for (j = 0; j < bpos+1; j++) {
46 				unsigned char c[2];
47 				c[0] = buf[j];
48 				if (c[0] < 32 || c[0] > 127)
49 					c[0] = '.';
50 				c[1] = 0;
51 				strcat(line, (char*)c);
52 			}
53 			printf("%s\n", line);
54 			sprintf(line, "%06d : ", _DUMP_BPL * lcount++);
55 		}
56 	}
57 
58 	if (bpos != _DUMP_BPL-1) {
59 /*		memset(&buf[bpos+1], 0, 20-bpos);
60 		bpos = 19;*/
61 		for (j = 0; j < bpos+1; j++) {
62 			char bb[5];
63 			sprintf(bb, "%02x ", buf[j]);
64 			strcat(line, bb);
65 		}
66 		if (size > _DUMP_BPL)
67 			for (j = 0; j < _DUMP_BPL - bpos - 1; j++)
68 				strcat(line, "   ");
69 		strcat(line, "| ");
70 		for (j = 0; j < bpos+1; j++) {
71 			unsigned char c[2];
72 			c[0] = buf[j];
73 			if (c[0] < 32 || c[0] > 127)
74 				c[0] = '.';
75 			c[1] = 0;
76 			strcat(line, (char*)c);
77 		}
78 		printf("%s\n", line);
79 	}
80 
81 }
82 
83 
84 /* Returns current time as number of seconds */
85 double
now()86 now()
87 {
88 	struct timeval tv;
89 	gettimeofday(&tv, NULL);
90 	return (double)(tv.tv_sec)+(double)(tv.tv_usec)/1e6;
91 }
92 
93 
94 /**
95  * Generates a pretty dump of the packet.
96  */
97 void
_dump_data_entry(struct mc_data_entry * mde)98 _dump_data_entry(struct mc_data_entry *mde)
99 {
100 	int i;
101 	struct mc_tag *t;
102 
103 	printf("key: *-------------------------------------------------------------\n");
104 	_dump_data(mc_data_entry_key(mde), mc_data_entry_key_size(mde));
105 	printf("data: -------------------------------------------------------------\ntags: ");
106 	_dump_data(mc_data_entry_value(mde), mc_data_entry_value_size(mde));
107 	for (i = 0; i < mc_data_entry_n_tags(mde); i++) {
108 		t = mc_data_entry_tag(mde, i);
109 #ifdef TAGS_64BIT
110 		printf("(%lld,%lld)\t", (long long)(t->key), (long long)(t->value));
111 #else
112 		printf("(%d,%d)\t", t->key, t->value);
113 #endif
114 	}
115 	printf("\n");
116 }
117