aboutsummaryrefslogtreecommitdiffstats
path: root/epan/to_str.c
blob: cda834f11c776b4ebc41e83636e12beab917a62a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
/* to_str.c
 * Routines for utilities to convert various other types to strings.
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <glib.h>

#include "emem.h"
#include "proto.h"
#include "to_str.h"

/*
 * If a user _does_ pass in a too-small buffer, this is probably
 * going to be too long to fit.  However, even a partial string
 * starting with "[Buf" should provide enough of a clue to be
 * useful.
 */
#define BUF_TOO_SMALL_ERR "[Buffer too small]"

static inline char *
byte_to_hex(char *out, guint32 dword) {
  /* At least one version of Apple's C compiler/linker is buggy, causing
     a complaint from the linker about the "literal C string section"
     not ending with '\0' if we initialize a 16-element "char" array with
     a 16-character string, the fact that initializing such an array with
     such a string is perfectly legitimate ANSI C nonwithstanding, the 17th
     '\0' byte in the string nonwithstanding. */
  static const gchar hex_digits[16] =
      { '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

  *out++ = hex_digits[(dword >> 4) & 0xF];
  *out++ = hex_digits[dword & 0xF];
  return out;
}

char *
word_to_hex(char *out, guint16 word) {
  out = byte_to_hex(out, word >> 8);
  out = byte_to_hex(out, word);
  return out;
}

char *
word_to_hex_npad(char *out, guint16 word) {
  static const gchar hex_digits[16] =
      { '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	if (word >= 0x1000)
		*out++ = hex_digits[(word >> 12) & 0xF];
	if (word >= 0x0100)
		*out++ = hex_digits[(word >> 8) & 0xF];
	if (word >= 0x0010)
		*out++ = hex_digits[(word >> 4) & 0xF];
	*out++ = hex_digits[word & 0xF];
	return out;
}

char *
dword_to_hex(char *out, guint32 dword) {
  out = byte_to_hex(out, dword >> 24);
  out = byte_to_hex(out, dword >> 16);
  out = byte_to_hex(out, dword >>  8);
  out = byte_to_hex(out, dword);
  return out;
}

char *
dword_to_hex_punct(char *out, guint32 dword, char punct) {
  out = byte_to_hex(out, dword >> 24);
  *out++ = punct;
  out = byte_to_hex(out, dword >> 16);
  *out++ = punct;
  out = byte_to_hex(out, dword >>  8);
  *out++ = punct;
  out = byte_to_hex(out, dword);
  return out;
}

/*
 * This does *not* null-terminate the string.  It returns a pointer
 * to the position in the string following the last character it
 * puts there, so that the caller can either put the null terminator
 * in or can append more stuff to the buffer.
 *
 * There needs to be at least len * 2 bytes left in the buffer.
 */
char *
bytes_to_hexstr(char *out, const guint8 *ad, guint32 len) {
  guint32 i;

  if (!ad)
    REPORT_DISSECTOR_BUG("Null pointer passed to bytes_to_hexstr()");

  for (i = 0; i < len; i++)
    out = byte_to_hex(out, ad[i]);
  return out;
}

/*
 * This does *not* null-terminate the string.  It returns a pointer
 * to the position in the string following the last character it
 * puts there, so that the caller can either put the null terminator
 * in or can append more stuff to the buffer.
 *
 * There needs to be at least len * 3 - 1 bytes left in the buffer.
 */
char *
bytes_to_hexstr_punct(char *out, const guint8 *ad, guint32 len, char punct) {
  guint32 i;

  if (!ad)
    REPORT_DISSECTOR_BUG("Null pointer passed to bytes_to_hexstr_punct()");

  out = byte_to_hex(out, ad[0]);
  for (i = 1; i < len; i++) {
    *out++ = punct;
    out = byte_to_hex(out, ad[i]);
  }
  return out;
}

/* Routine to convert a sequence of bytes to a hex string, one byte/two hex
 * digits at at a time, with a specified punctuation character between
 * the bytes.
 *
 * If punct is '\0', no punctuation is applied (and thus
 * the resulting string is (len-1) bytes shorter)
 */
gchar *
bytestring_to_str(const guint8 *ad, const guint32 len, const char punct) {
  gchar *buf;
  size_t       buflen;

  if (!ad)
    REPORT_DISSECTOR_BUG("Null pointer passed to bytestring_to_str()");

  /* XXX, Old code was using int as iterator... Why len is guint32 anyway?! (darkjames) */
  if ( ((int) len) < 0)
     return "";

  if (!len)
     return "";

  if (punct)
    buflen=len*3;
  else
    buflen=len*2 + 1;

  buf=ep_alloc(buflen);

  if (punct)
    bytes_to_hexstr_punct(buf, ad, len, punct);
  else
    bytes_to_hexstr(buf, ad, len);

  buf[buflen-1] = '\0';
  return buf;
}

/* Max string length for displaying byte string.  */
#define	MAX_BYTE_STR_LEN	48

gchar *
bytes_to_str(const guint8 *bd, int bd_len) {
  gchar *cur;
  gchar *cur_ptr;
  int truncated = 0;

  if (!bd)
    REPORT_DISSECTOR_BUG("Null pointer passed to bytes_to_str()");

  cur=ep_alloc(MAX_BYTE_STR_LEN+3+1);
  if (bd_len <= 0) { cur[0] = '\0'; return cur; }

  if (bd_len > MAX_BYTE_STR_LEN/2) {	/* bd_len > 24 */
    truncated = 1;
    bd_len = MAX_BYTE_STR_LEN/2;
  }

  cur_ptr = bytes_to_hexstr(cur, bd, bd_len);	/* max MAX_BYTE_STR_LEN bytes */

  if (truncated)
    cur_ptr = g_stpcpy(cur_ptr, "...");		/* 3 bytes */

  *cur_ptr = '\0';				/* 1 byte */
  return cur;
}

/* Turn an array of bytes into a string showing the bytes in hex with
 * punct as a bytes separator.
 */
gchar *
bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct) {
  gchar *cur;
  gchar *cur_ptr;
  int truncated = 0;

  if (!punct)
    return bytes_to_str(bd, bd_len);

  cur=ep_alloc(MAX_BYTE_STR_LEN+3+1);
  if (bd_len <= 0) { cur[0] = '\0'; return cur; }

  if (bd_len > MAX_BYTE_STR_LEN/3) {	/* bd_len > 16 */
   truncated = 1;
   bd_len = MAX_BYTE_STR_LEN/3;
  }

  cur_ptr = bytes_to_hexstr_punct(cur, bd, bd_len, punct); /* max MAX_BYTE_STR_LEN-1 bytes */

  if (truncated) {
    *cur_ptr++ = punct;				/* 1 byte */
    cur_ptr    = g_stpcpy(cur_ptr, "...");	/* 3 bytes */
  }

  *cur_ptr = '\0';
  return cur;
}

static int
guint32_to_str_buf_len(const guint32 u) {
    if (u >= 1000000000)return 10;
    if (u >= 100000000) return 9;
    if (u >= 10000000)	return 8;
    if (u >= 1000000)	return 7;
    if (u >= 100000)	return 6;
    if (u >= 10000)	return 5;
    if (u >= 1000)	return 4;
    if (u >= 100)	return 3;
    if (u >= 10)	return 2;

    return 1;
}

static const char * const fast_strings[] = {
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31",
"32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "50", "51", "52", "53", "54", "55",
"56", "57", "58", "59", "60", "61", "62", "63",
"64", "65", "66", "67", "68", "69", "70", "71",
"72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87",
"88", "89", "90", "91", "92", "93", "94", "95",
"96", "97", "98", "99", "100", "101", "102", "103",
"104", "105", "106", "107", "108", "109", "110", "111",
"112", "113", "114", "115", "116", "117", "118", "119",
"120", "121", "122", "123", "124", "125", "126", "127",
"128", "129", "130", "131", "132", "133", "134", "135",
"136", "137", "138", "139", "140", "141", "142", "143",
"144", "145", "146", "147", "148", "149", "150", "151",
"152", "153", "154", "155", "156", "157", "158", "159",
"160", "161", "162", "163", "164", "165", "166", "167",
"168", "169", "170", "171", "172", "173", "174", "175",
"176", "177", "178", "179", "180", "181", "182", "183",
"184", "185", "186", "187", "188", "189", "190", "191",
"192", "193", "194", "195", "196", "197", "198", "199",
"200", "201", "202", "203", "204", "205", "206", "207",
"208", "209", "210", "211", "212", "213", "214", "215",
"216", "217", "218", "219", "220", "221", "222", "223",
"224", "225", "226", "227", "228", "229", "230", "231",
"232", "233", "234", "235", "236", "237", "238", "239",
"240", "241", "242", "243", "244", "245", "246", "247",
"248", "249", "250", "251", "252", "253", "254", "255"
};

void
guint32_to_str_buf(guint32 u, gchar *buf, int buf_len) {
  int str_len = guint32_to_str_buf_len(u)+1;

  gchar *bp = &buf[str_len];
  gchar const *p;

  if (buf_len < str_len) {
    g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);	/* Let the unexpected value alert user */
    return;
  }

  *--bp = '\0';

  while (u >= 10) {
    p = fast_strings[100 + (u % 100)];

    *--bp = p[2];
    *--bp = p[1];

    u /= 100;
  }

  if (bp != buf) /* ugly, fixme! */
    *--bp = (u % 10) | '0';
}

gchar *
guint32_to_str(const guint32 u) {
  int str_len = 16; /* guint32_to_str_buf_len(u)+1; */

  gchar *bp = ep_alloc(str_len);
  guint32_to_str_buf(u, bp, str_len);

  return bp;
}

#define	PLURALIZE(n)	(((n) > 1) ? "s" : "")
#define	COMMA(do_it)	((do_it) ? ", " : "")

/*
 * Maximum length of a string showing days/hours/minutes/seconds.
 * (Does not include the terminating '\0'.)
 * Includes space for a '-' sign for any negative components.
 * -12345 days, 12 hours, 12 minutes, 12.123 seconds
 */
#define TIME_SECS_LEN	(10+1+4+2+2+5+2+2+7+2+2+7+4)

/*
 * Convert a value in seconds and fractions of a second to a string,
 * giving time in days, hours, minutes, and seconds, and put the result
 * into a buffer.
 * "is_nsecs" says that "frac" is microseconds if true and milliseconds
 * if false.
 * If time is negative, add a '-' to all non-null components.
 */
static void
time_secs_to_str_buf(gint32 time_val, const guint32 frac, const gboolean is_nsecs,
			   emem_strbuf_t *buf)
{
  int hours, mins, secs;
  const gchar *msign = "";
  gboolean do_comma = FALSE;

  if(time_val == G_MININT32) {	/* That Which Shall Not Be Negated */
    ep_strbuf_append_printf(buf, "Unable to cope with time value %d", time_val);
    return;
  }

  if(time_val < 0){
    time_val = -time_val;
    msign = "-";
  }

  secs = time_val % 60;
  time_val /= 60;
  mins = time_val % 60;
  time_val /= 60;
  hours = time_val % 24;
  time_val /= 24;

  if (time_val != 0) {
    ep_strbuf_append_printf(buf, "%s%u day%s", msign, time_val, PLURALIZE(time_val));
    do_comma = TRUE;
    msign="";
  }
  if (hours != 0) {
    ep_strbuf_append_printf(buf, "%s%s%u hour%s", COMMA(do_comma), msign, hours, PLURALIZE(hours));
    do_comma = TRUE;
    msign="";
  }
  if (mins != 0) {
    ep_strbuf_append_printf(buf, "%s%s%u minute%s", COMMA(do_comma), msign, mins, PLURALIZE(mins));
    do_comma = TRUE;
    msign="";
  }
  if (secs != 0 || frac != 0) {
    if (frac != 0) {
      if (is_nsecs)
        ep_strbuf_append_printf(buf, "%s%s%u.%09u seconds", COMMA(do_comma), msign, secs, frac);
      else
        ep_strbuf_append_printf(buf, "%s%s%u.%03u seconds", COMMA(do_comma), msign, secs, frac);
    } else
      ep_strbuf_append_printf(buf, "%s%s%u second%s", COMMA(do_comma), msign, secs, PLURALIZE(secs));
  }
}

gchar *
time_secs_to_str(const gint32 time_val)
{
  emem_strbuf_t *buf;

  buf=ep_strbuf_sized_new(TIME_SECS_LEN+1, TIME_SECS_LEN+1);

  if (time_val == 0) {
    ep_strbuf_append(buf, "0 seconds");
    return buf->str;
  }

  time_secs_to_str_buf(time_val, 0, FALSE, buf);
  return buf->str;
}

static void
time_secs_to_str_buf_unsigned(guint32 time_val, const guint32 frac, const gboolean is_nsecs,
			 emem_strbuf_t *buf)
{
  int hours, mins, secs;
  gboolean do_comma = FALSE;

  secs = time_val % 60;
  time_val /= 60;
  mins = time_val % 60;
  time_val /= 60;
  hours = time_val % 24;
  time_val /= 24;

  if (time_val != 0) {
    ep_strbuf_append_printf(buf, "%u day%s", time_val, PLURALIZE(time_val));
    do_comma = TRUE;
  }
  if (hours != 0) {
    ep_strbuf_append_printf(buf, "%s%u hour%s", COMMA(do_comma), hours, PLURALIZE(hours));
    do_comma = TRUE;
  }
  if (mins != 0) {
    ep_strbuf_append_printf(buf, "%s%u minute%s", COMMA(do_comma), mins, PLURALIZE(mins));
    do_comma = TRUE;
  }
  if (secs != 0 || frac != 0) {
    if (frac != 0) {
      if (is_nsecs)
        ep_strbuf_append_printf(buf, "%s%u.%09u seconds", COMMA(do_comma), secs, frac);
      else
        ep_strbuf_append_printf(buf, "%s%u.%03u seconds", COMMA(do_comma), secs, frac);
    } else
      ep_strbuf_append_printf(buf, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
  }
}

gchar *
time_secs_to_str_unsigned(const guint32 time_val)
{
  emem_strbuf_t *buf;

  buf=ep_strbuf_sized_new(TIME_SECS_LEN+1, TIME_SECS_LEN+1);

  if (time_val == 0) {
    ep_strbuf_append(buf, "0 seconds");
    return buf->str;
  }

  time_secs_to_str_buf_unsigned(time_val, 0, FALSE, buf);
  return buf->str;
}


gchar *
time_msecs_to_str(gint32 time_val)
{
  emem_strbuf_t *buf;
  int msecs;

  buf=ep_strbuf_sized_new(TIME_SECS_LEN+1+3+1, TIME_SECS_LEN+1+3+1);

  if (time_val == 0) {
    ep_strbuf_append(buf, "0 seconds");
    return buf->str;
  }

  if(time_val<0){
    /* oops we got passed a negative time */
    time_val= -time_val;
    msecs = time_val % 1000;
    time_val /= 1000;
    time_val= -time_val;
  } else {
    msecs = time_val % 1000;
    time_val /= 1000;
  }

  time_secs_to_str_buf(time_val, msecs, FALSE, buf);
  return buf->str;
}

static const char *mon_names[12] = {
	"Jan",
	"Feb",
	"Mar",
	"Apr",
	"May",
	"Jun",
	"Jul",
	"Aug",
	"Sep",
	"Oct",
	"Nov",
	"Dec"
};

static const gchar *get_zonename(struct tm *tmp) {
#if defined(HAVE_TM_ZONE)
	return tmp->tm_zone;
#else
	if ((tmp->tm_isdst != 0) && (tmp->tm_isdst != 1)) {
		return "???";
	}
# if defined(HAVE_TZNAME)
	return tzname[tmp->tm_isdst];

# elif defined(_WIN32)
	/* Windows C Runtime:                                                 */
	/*   _tzname is encoded using the "system default ansi code page"     */
	/*     ("which is not necessarily the same as the C library locale"). */
	/*     So: _tzname must be converted to UTF8 before use.              */
	/*   Alternative: use Windows GetTimeZoneInformation() to get the     */
        /*     timezone name in UTF16 and convert same to UTF8.               */
	/*   XXX: the result is that the timezone name will be based upon the */
	/*    system code page (iow: the charset of the system).              */
	/*    Since Wireshark is not internationalized, it would seem more    */
	/*    correct to show the timezone name in English, no matter what    */
        /*    the system code page, but I don't how to do that (or if it's    */
        /*    really even possible).                                          */
        /*    In any case converting to UTF8 presumably at least keeps GTK    */
        /*    happy. (A bug was reported wherein Wireshark crashed in GDK     */
        /*    on a "Japanese version of Windows XP" when trying to copy       */
        /*    the date/time string (containing a copy of _tz_name) to the     */
        /*    clipboard).                                                     */

	{
		static char *ws_tzname[2] = {NULL, NULL};

		/* The g_malloc'd value returned from g_locale_to_utf8() is   */
		/*  cached for all further use so there's no need to ever     */
		/*  g_free() that value.                                      */
		if (ws_tzname[tmp->tm_isdst] == NULL) {
			ws_tzname[tmp->tm_isdst] = g_locale_to_utf8(_tzname[tmp->tm_isdst], -1, NULL, NULL, NULL);
			if (ws_tzname[tmp->tm_isdst] == NULL) {
				ws_tzname[tmp->tm_isdst] = "???";
			}
		}
		return ws_tzname[tmp->tm_isdst];
	}
# else
	return tmp->tm_isdst ? "?DT" : "?ST";

# endif
#endif
}

gchar *
abs_time_to_str(const nstime_t *abs_time, const absolute_time_display_e fmt,
   gboolean show_zone)
{
        struct tm *tmp = NULL;
        const char *zonename = "???";
        gchar *buf = NULL;

#ifdef _MSC_VER
        /* calling localtime() on MSVC 2005 with huge values causes it to crash */
        /* XXX - find the exact value that still does work */
        /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
        if(abs_time->secs > 2000000000) {
            tmp = NULL;
        } else
#endif
        switch (fmt) {

        case ABSOLUTE_TIME_UTC:
        case ABSOLUTE_TIME_DOY_UTC:
                tmp = gmtime(&abs_time->secs);
                zonename = "UTC";
                break;

	case ABSOLUTE_TIME_LOCAL:
                tmp = localtime(&abs_time->secs);
                if (tmp) {
			zonename = get_zonename(tmp);
                }
                break;
        }
        if (tmp) {
                switch (fmt) {

                case ABSOLUTE_TIME_DOY_UTC:
                	if (show_zone) {
	                        buf = ep_strdup_printf("%04d/%03d:%02d:%02d:%02d.%09ld %s",
        	                    tmp->tm_year + 1900,
                	            tmp->tm_yday + 1,
                        	    tmp->tm_hour,
	                            tmp->tm_min,
        	                    tmp->tm_sec,
                	            (long)abs_time->nsecs,
                        	    zonename);
                        } else {
	                        buf = ep_strdup_printf("%04d/%03d:%02d:%02d:%02d.%09ld",
        	                    tmp->tm_year + 1900,
                	            tmp->tm_yday + 1,
                        	    tmp->tm_hour,
	                            tmp->tm_min,
        	                    tmp->tm_sec,
                	            (long)abs_time->nsecs);
                        }
                        break;

                case ABSOLUTE_TIME_UTC:
                case ABSOLUTE_TIME_LOCAL:
                	if (show_zone) {
	                        buf = ep_strdup_printf("%s %2d, %d %02d:%02d:%02d.%09ld %s",
        	                    mon_names[tmp->tm_mon],
                	            tmp->tm_mday,
                        	    tmp->tm_year + 1900,
	                            tmp->tm_hour,
        	                    tmp->tm_min,
                	            tmp->tm_sec,
                        	    (long)abs_time->nsecs,
	                            zonename);
	                } else {
	                        buf = ep_strdup_printf("%s %2d, %d %02d:%02d:%02d.%09ld",
        	                    mon_names[tmp->tm_mon],
                	            tmp->tm_mday,
                        	    tmp->tm_year + 1900,
	                            tmp->tm_hour,
        	                    tmp->tm_min,
                	            tmp->tm_sec,
                        	    (long)abs_time->nsecs);
	                }
                        break;
                }
        } else
                buf = ep_strdup("Not representable");
        return buf;
}

gchar *
abs_time_secs_to_str(const time_t abs_time, const absolute_time_display_e fmt,
    gboolean show_zone)
{
        struct tm *tmp = NULL;
        const char *zonename = "???";
        gchar *buf = NULL;

#ifdef _MSC_VER
        /* calling localtime() on MSVC 2005 with huge values causes it to crash */
        /* XXX - find the exact value that still does work */
        /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
        if(abs_time > 2000000000) {
            tmp = NULL;
        } else
#endif
        switch (fmt) {

        case ABSOLUTE_TIME_UTC:
        case ABSOLUTE_TIME_DOY_UTC:
                tmp = gmtime(&abs_time);
                zonename = "UTC";
                break;

	case ABSOLUTE_TIME_LOCAL:
                tmp = localtime(&abs_time);
                if (tmp) {
			zonename = get_zonename(tmp);
                }
                break;
        }
        if (tmp) {
                switch (fmt) {

                case ABSOLUTE_TIME_DOY_UTC:
                	if (show_zone) {
	                        buf = ep_strdup_printf("%04d/%03d:%02d:%02d:%02d %s",
        	                    tmp->tm_year + 1900,
                	            tmp->tm_yday + 1,
                        	    tmp->tm_hour,
	                            tmp->tm_min,
        	                    tmp->tm_sec,
                	            zonename);
                	} else {
	                        buf = ep_strdup_printf("%04d/%03d:%02d:%02d:%02d",
        	                    tmp->tm_year + 1900,
                	            tmp->tm_yday + 1,
                        	    tmp->tm_hour,
	                            tmp->tm_min,
        	                    tmp->tm_sec);
                	}
                        break;

                case ABSOLUTE_TIME_UTC:
                case ABSOLUTE_TIME_LOCAL:
                	if (show_zone) {
	                        buf = ep_strdup_printf("%s %2d, %d %02d:%02d:%02d %s",
        	                    mon_names[tmp->tm_mon],
                	            tmp->tm_mday,
                        	    tmp->tm_year + 1900,
	                            tmp->tm_hour,
        	                    tmp->tm_min,
                	            tmp->tm_sec,
                        	    zonename);
                        } else {
	                        buf = ep_strdup_printf("%s %2d, %d %02d:%02d:%02d",
        	                    mon_names[tmp->tm_mon],
                	            tmp->tm_mday,
                        	    tmp->tm_year + 1900,
	                            tmp->tm_hour,
        	                    tmp->tm_min,
                	            tmp->tm_sec);
                        }
                        break;
                }
        } else
                buf = ep_strdup("Not representable");
        return buf;
}

void
display_signed_time(gchar *buf, int buflen, const gint32 sec, gint32 frac,
    const to_str_time_res_t units)
{
	/* If the fractional part of the time stamp is negative,
	   print its absolute value and, if the seconds part isn't
	   (the seconds part should be zero in that case), stick
	   a "-" in front of the entire time stamp. */
	if (frac < 0) {
		frac = -frac;
		if (sec >= 0) {
			if (buflen < 1) {
			  return;
			}
			buf[0] = '-';
			buf++;
			buflen--;
		}
	}
	switch (units) {

	case TO_STR_TIME_RES_T_SECS:
		g_snprintf(buf, buflen, "%d", sec);
		break;

	case TO_STR_TIME_RES_T_DSECS:
		g_snprintf(buf, buflen, "%d.%01d", sec, frac);
		break;

	case TO_STR_TIME_RES_T_CSECS:
		g_snprintf(buf, buflen, "%d.%02d", sec, frac);
		break;

	case TO_STR_TIME_RES_T_MSECS:
		g_snprintf(buf, buflen, "%d.%03d", sec, frac);
		break;

	case TO_STR_TIME_RES_T_USECS:
		g_snprintf(buf, buflen, "%d.%06d", sec, frac);
		break;

	case TO_STR_TIME_RES_T_NSECS:
		g_snprintf(buf, buflen, "%d.%09d", sec, frac);
		break;
	}
}


void
display_epoch_time(gchar *buf, int buflen, const time_t sec, gint32 frac,
    const to_str_time_res_t units)
{
	double elapsed_secs;

	elapsed_secs = difftime(sec,(time_t)0);

	/* This code copied from display_signed_time; keep it in case anyone
	   is looking at captures from before 1970 (???).
	   If the fractional part of the time stamp is negative,
	   print its absolute value and, if the seconds part isn't
	   (the seconds part should be zero in that case), stick
	   a "-" in front of the entire time stamp. */
	if (frac < 0) {
		frac = -frac;
		if (elapsed_secs >= 0) {
			if (buflen < 1) {
			  return;
			}
			buf[0] = '-';
			buf++;
			buflen--;
		}
	}
	switch (units) {

	case TO_STR_TIME_RES_T_SECS:
		g_snprintf(buf, buflen, "%0.0f", elapsed_secs);
		break;

	case TO_STR_TIME_RES_T_DSECS:
		g_snprintf(buf, buflen, "%0.0f.%01d", elapsed_secs, frac);
		break;

	case TO_STR_TIME_RES_T_CSECS:
		g_snprintf(buf, buflen, "%0.0f.%02d", elapsed_secs, frac);
		break;

	case TO_STR_TIME_RES_T_MSECS:
		g_snprintf(buf, buflen, "%0.0f.%03d", elapsed_secs, frac);
		break;

	case TO_STR_TIME_RES_T_USECS:
		g_snprintf(buf, buflen, "%0.0f.%06d", elapsed_secs, frac);
		break;

	case TO_STR_TIME_RES_T_NSECS:
		g_snprintf(buf, buflen, "%0.0f.%09d", elapsed_secs, frac);
		break;
	}
}

/*
 * Display a relative time as days/hours/minutes/seconds.
 */
gchar *
rel_time_to_str(const nstime_t *rel_time)
{
	emem_strbuf_t *buf;
	gint32 time_val;
	gint32 nsec;

	buf=ep_strbuf_sized_new(1+TIME_SECS_LEN+1+6+1, 1+TIME_SECS_LEN+1+6+1);

	/* If the nanoseconds part of the time stamp is negative,
	   print its absolute value and, if the seconds part isn't
	   (the seconds part should be zero in that case), stick
	   a "-" in front of the entire time stamp. */
	time_val = (gint) rel_time->secs;
	nsec = rel_time->nsecs;
	if (time_val == 0 && nsec == 0) {
		ep_strbuf_append(buf, "0.000000000 seconds");
		return buf->str;
	}
	if (nsec < 0) {
		nsec = -nsec;
		ep_strbuf_append_c(buf, '-');

		/*
		 * We assume here that "rel_time->secs" is negative
		 * or zero; if it's not, the time stamp is bogus,
		 * with a positive seconds and negative microseconds.
		 */
		time_val = (gint) -rel_time->secs;
	}

	time_secs_to_str_buf(time_val, nsec, TRUE, buf);
	return buf->str;
}

#define REL_TIME_SECS_LEN	(1+10+1+9+1)

/*
 * Display a relative time as seconds.
 */
gchar *
rel_time_to_secs_str(const nstime_t *rel_time)
{
        gchar *buf;

	buf=ep_alloc(REL_TIME_SECS_LEN);

        display_signed_time(buf, REL_TIME_SECS_LEN, (gint32) rel_time->secs,
            rel_time->nsecs, TO_STR_TIME_RES_T_NSECS);
        return buf;
}

/*
 * Generates a string representing the bits in a bitfield at "bit_offset" from an 8 bit boundary
 * with the length in bits of no_of_bits based on value.
 * Ex: ..xx x...
 */

char *
decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint64 value)
{
	guint64 mask = 0,tmp;
	char *str;
	int bit;
	int i;

	mask = 1;
	mask = mask << (no_of_bits-1);

	/* prepare the string */
	str=ep_alloc(256);
	str[0]='\0';
	for(bit=0;bit<((int)(bit_offset&0x07));bit++){
		if(bit&&(!(bit%4))){
			g_strlcat(str, " ", 256);
		}
		g_strlcat(str, ".", 256);
	}

	/* read the bits for the int */
	for(i=0;i<no_of_bits;i++){
		if(bit&&(!(bit%4))){
			g_strlcat(str, " ", 256);
		}
		if(bit&&(!(bit%8))){
			g_strlcat(str, " ", 256);
		}
		bit++;
		tmp = value & mask;
		if(tmp != 0){
			g_strlcat(str, "1", 256);
		} else {
			g_strlcat(str, "0", 256);
		}
		mask = mask>>1;
	}

	for(;bit%8;bit++){
		if(bit&&(!(bit%4))){
			g_strlcat(str, " ", 256);
		}
		g_strlcat(str, ".", 256);
	}
	return str;
}

/* Generate, into "buf", a string showing the bits of a bitfield.
   Return a pointer to the character after that string. */
/*XXX this needs a buf_len check */
char *
other_decode_bitfield_value(char *buf, const guint32 val, const guint32 mask, const int width)
{
  int i;
  guint32 bit;
  char *p;

  i = 0;
  p = buf;
  bit = 1 << (width - 1);
  for (;;) {
    if (mask & bit) {
      /* This bit is part of the field.  Show its value. */
      if (val & bit)
        *p++ = '1';
      else
        *p++ = '0';
    } else {
      /* This bit is not part of the field. */
      *p++ = '.';
    }
    bit >>= 1;
    i++;
    if (i >= width)
      break;
    if (i % 4 == 0)
      *p++ = ' ';
  }
  *p = '\0';
  return p;
}

char *
decode_bitfield_value(char *buf, const guint32 val, const guint32 mask, const int width)
{
  char *p;

  p = other_decode_bitfield_value(buf, val, mask, width);
  strcpy(p, " = ");
  p += 3;
  return p;
}

/* Generate a string describing a Boolean bitfield (a one-bit field that
   says something is either true or false). */
const char *
decode_boolean_bitfield(const guint32 val, const guint32 mask, const int width,
    const char *truedesc, const char *falsedesc)
{
  char *buf;
  char *p;

  buf=ep_alloc(1025); /* is this a bit overkill? */
  p = decode_bitfield_value(buf, val, mask, width);
  if (val & mask)
    strcpy(p, truedesc);
  else
    strcpy(p, falsedesc);
  return buf;
}

/* Generate a string describing a numeric bitfield (an N-bit field whose
   value is just a number). */
const char *
decode_numeric_bitfield(const guint32 val, const guint32 mask, const int width,
    const char *fmt)
{
  char *buf;
  char *p;
  int shift = 0;

  buf=ep_alloc(1025); /* isnt this a bit overkill? */
  /* Compute the number of bits we have to shift the bitfield right
     to extract its value. */
  while ((mask & (1<<shift)) == 0)
    shift++;

  p = decode_bitfield_value(buf, val, mask, width);
  g_snprintf(p, (gulong) (1025-(p-buf)), fmt, (val & mask) >> shift);
  return buf;
}

/*
 This function is very fast and this function is called a lot.
 XXX update the ep_address_to_str stuff to use this function.
*/
void
ip_to_str_buf(const guint8 *ad, gchar *buf, const int buf_len)
{
	register gchar const *p;
	register gchar *b=buf;

	if (buf_len < MAX_IP_STR_LEN) {
		g_snprintf ( buf, buf_len, BUF_TOO_SMALL_ERR );                 /* Let the unexpected value alert user */
		return;
	}

	p=fast_strings[*ad++];
	do {
		*b++=*p;
		p++;
	} while(*p);
	*b++='.';

	p=fast_strings[*ad++];
	do {
		*b++=*p;
		p++;
	} while(*p);
	*b++='.';

	p=fast_strings[*ad++];
	do {
		*b++=*p;
		p++;
	} while(*p);
	*b++='.';

	p=fast_strings[*ad];
	do {
		*b++=*p;
		p++;
	} while(*p);
	*b=0;
}

gchar* guid_to_str(const e_guid_t *guid) {
  gchar *buf;

  buf=ep_alloc(GUID_STR_LEN);
  return guid_to_str_buf(guid, buf, GUID_STR_LEN);
}

gchar* guid_to_str_buf(const e_guid_t *guid, gchar *buf, int buf_len) {
  char *tempptr = buf;

  if (buf_len < GUID_STR_LEN) {
     g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);/* Let the unexpected value alert user */
     return buf;
  }

  /* 37 bytes */
  tempptr    = dword_to_hex(tempptr, guid->data1);		/*  8 bytes */
  *tempptr++ = '-';						/*  1 byte */
  tempptr    = word_to_hex(tempptr, guid->data2);		/*  4 bytes */
  *tempptr++ = '-';						/*  1 byte */
  tempptr    = word_to_hex(tempptr, guid->data3);		/*  4 bytes */
  *tempptr++ = '-';						/*  1 byte */
  tempptr    = bytes_to_hexstr(tempptr, &guid->data4[0], 2);	/*  4 bytes */
  *tempptr++ = '-';						/*  1 byte */
  tempptr    = bytes_to_hexstr(tempptr, &guid->data4[2], 6);	/* 12 bytes */

  *tempptr   = '\0';
  return buf;
}