Browse code

misctest: print input data in hex format

Daniel-Constantin Mierla authored on 08/09/2021 06:23:55
Showing 1 changed files
... ...
@@ -53,6 +53,7 @@ static void mod_destroy(void);
53 53
 
54 54
 static int misctest_memory_init(void);
55 55
 static int misctest_message_init(void);
56
+int misctest_hexprint(void *data, size_t length, int linelen, int split);
56 57
 
57 58
 static int misctest_memory = 0;
58 59
 static int misctest_message = 0;
... ...
@@ -313,7 +314,9 @@ static int misctest_message_init(void)
313 314
 
314 315
     tmsg.buf = tbuf;
315 316
 
316
-    LM_INFO("using data: [[%.*s]]\n", tmsg.len, tmsg.buf);
317
+    LM_INFO("using data: [[%.*s]] (%d)\n", tmsg.len, tmsg.buf, tmsg.len);
318
+
319
+	misctest_hexprint(tmsg.buf, tmsg.len, 20, 10);
317 320
 
318 321
     if (parse_msg(tmsg.buf, tmsg.len, &tmsg) < 0) {
319 322
         goto cleanup;
... ...
@@ -342,6 +345,90 @@ cleanup:
342 345
 
343 346
 }
344 347
 
348
+/**
349
+ *	misctest_hexprint - output a hex dump of a buffer
350
+ *
351
+ *	data - pointer to the buffer
352
+ *	length - length of buffer to write
353
+ *	linelen - number of chars to output per line
354
+ *	split - number of chars in each chunk on a line
355
+ */
356
+
357
+int misctest_hexprint(void *data, size_t length, int linelen, int split)
358
+{
359
+	char buffer[512];
360
+	char *ptr;
361
+	const void *inptr;
362
+	int pos;
363
+	int remaining = length;
364
+
365
+	inptr = data;
366
+
367
+	if(sizeof(buffer) <= (3 + (4 * (linelen / split)) + (linelen * 4))) {
368
+		LM_ERR("buffer size is too small\n");
369
+		return -1;
370
+	}
371
+
372
+	while (remaining > 0) {
373
+		int lrem;
374
+		int splitcount;
375
+		ptr = buffer;
376
+
377
+		lrem = remaining;
378
+		splitcount = 0;
379
+		for (pos = 0; pos < linelen; pos++) {
380
+
381
+			if (split == splitcount++) {
382
+				sprintf(ptr, "  ");
383
+				ptr += 2;
384
+				splitcount = 1;
385
+			}
386
+
387
+			if (lrem) {
388
+				sprintf(ptr, "%0.2x ", *((unsigned char *) inptr + pos));
389
+				lrem--;
390
+			} else {
391
+				sprintf(ptr, "   ");
392
+			}
393
+			ptr += 3;
394
+		}
395
+
396
+		*ptr++ = ' ';
397
+		*ptr++ = ' ';
398
+
399
+		lrem = remaining;
400
+		splitcount = 0;
401
+		for (pos = 0; pos < linelen; pos++) {
402
+			unsigned char c;
403
+
404
+			if (split == splitcount++) {
405
+				sprintf(ptr, "  ");
406
+				ptr += 2;
407
+				splitcount = 1;
408
+			}
409
+
410
+			if (lrem) {
411
+				c = *((unsigned char *) inptr + pos);
412
+				if (c > 31 && c < 127) {
413
+					sprintf(ptr, "%c", c);
414
+				} else {
415
+					sprintf(ptr, ".");
416
+				}
417
+				lrem--;
418
+			}
419
+			ptr++;
420
+		}
421
+
422
+		*ptr = '\0';
423
+		LM_INFO("%s\n", buffer);
424
+
425
+		inptr += linelen;
426
+		remaining -= linelen;
427
+	}
428
+
429
+	return 0;
430
+}
431
+
345 432
 /** record a memory chunk list entry.
346 433
  * @param addr - address of the newly allocated memory
347 434
  * @oaram size - size