~ubuntu-branches/ubuntu/gutsy/wireshark/gutsy-security

« back to all changes in this revision

Viewing changes to epan/strutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2007-04-01 08:58:40 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070401085840-or3qhrpv8alt1bwg
Tags: 0.99.5-1
* New upstream release.
* debian/patches/09_idl2wrs.dpatch: updated to patch idl2wrs.sh.in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* strutil.c
2
2
 * String utility routines
3
3
 *
4
 
 * $Id: strutil.c 19592 2006-10-18 17:55:29Z gerald $
 
4
 * $Id: strutil.c 20625 2007-01-30 17:44:11Z gerald $
5
5
 *
6
6
 * Wireshark - Network traffic analyzer
7
7
 * By Gerald Combs <gerald@wireshark.org>
39
39
#include <wchar.h>
40
40
#endif
41
41
 
 
42
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
 
43
                              '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
 
44
 
42
45
/*
43
46
 * Given a pointer into a data buffer, and to the end of the buffer,
44
47
 * find the end of the (putative) line at that position in the data
387
390
  gchar        *cur;
388
391
  gchar        *p;
389
392
  int           len;
390
 
  static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
391
 
                                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
392
393
 
393
394
  cur=ep_alloc(MAX_BYTE_STR_LEN+3+1);
394
395
  p = cur;
505
506
        return TRUE;
506
507
}
507
508
 
 
509
/*
 
510
 * Turn an RFC 3986 percent-encoded string into a byte array.
 
511
 * XXX - We don't check for reserved characters.
 
512
 */
 
513
#define HEX_DIGIT_BUF_LEN 3
 
514
gboolean
 
515
uri_str_to_bytes(const char *uri_str, GByteArray *bytes) {
 
516
        guint8          val;
 
517
        const char      *p;
 
518
        char            hex_digit[HEX_DIGIT_BUF_LEN];
 
519
 
 
520
        g_byte_array_set_size(bytes, 0);
 
521
        if (! uri_str) {
 
522
                return FALSE;
 
523
        }
 
524
 
 
525
        p = uri_str;
 
526
 
 
527
        while (*p) {
 
528
                if (! isascii(*p) || ! isprint(*p))
 
529
                        return FALSE;
 
530
                if (*p == '%') {
 
531
                        p++;
 
532
                        if (*p == '\0') return FALSE;
 
533
                        hex_digit[0] = *p;
 
534
                        p++;
 
535
                        if (*p == '\0') return FALSE;
 
536
                        hex_digit[1] = *p;
 
537
                        hex_digit[2] = '\0';
 
538
                        if (! isxdigit(hex_digit[0]) || ! isxdigit(hex_digit[1]))
 
539
                                return FALSE;
 
540
                        val = (guint8) strtoul(hex_digit, NULL, 16);
 
541
                        g_byte_array_append(bytes, &val, 1);
 
542
                } else {
 
543
                        g_byte_array_append(bytes, (guint8 *) p, 1);
 
544
                }
 
545
                p++;
 
546
 
 
547
        }
 
548
        return TRUE;
 
549
}
 
550
 
 
551
/*
 
552
 * Given a GByteArray, generate a string from it that shows non-printable
 
553
 * characters as percent-style escapes, and return a pointer to it.
 
554
 */
 
555
gchar *
 
556
format_uri(const GByteArray *bytes, const gchar *reserved_chars)
 
557
{
 
558
  static gchar *fmtbuf[3];
 
559
  static guint fmtbuf_len[3];
 
560
  static guint idx;
 
561
  const gchar *reserved_def = ":/?#[]@!$&'()*+,;= ";
 
562
  const gchar *reserved = reserved_def;
 
563
  guint8 c;
 
564
  guint column, i;
 
565
  gboolean is_reserved = FALSE;
 
566
 
 
567
  if (! bytes)
 
568
    return "";
 
569
 
 
570
  idx = (idx + 1) % 3;
 
571
  if (reserved_chars)
 
572
    reserved = reserved_chars;
 
573
 
 
574
  /*
 
575
   * Allocate the buffer if it's not already allocated.
 
576
   */
 
577
  if (fmtbuf[idx] == NULL) {
 
578
    fmtbuf[idx] = g_malloc(INITIAL_FMTBUF_SIZE);
 
579
    fmtbuf_len[idx] = INITIAL_FMTBUF_SIZE;
 
580
  }
 
581
  for (column = 0; column < bytes->len; column++) {
 
582
    /*
 
583
     * Is there enough room for this character, if it expands to
 
584
     * a percent plus 2 hex digits (which is the most it can
 
585
     * expand to), and also enough room for a terminating '\0'?
 
586
     */
 
587
    if (column+2+1 >= fmtbuf_len[idx]) {
 
588
      /*
 
589
       * Double the buffer's size if it's not big enough.
 
590
       * The size of the buffer starts at 128, so doubling its size
 
591
       * adds at least another 128 bytes, which is more than enough
 
592
       * for one more character plus a terminating '\0'.
 
593
       */
 
594
      fmtbuf_len[idx] = fmtbuf_len[idx] * 2;
 
595
      fmtbuf[idx] = g_realloc(fmtbuf[idx], fmtbuf_len[idx]);
 
596
    }
 
597
    c = bytes->data[column];
 
598
 
 
599
    if (!isascii(c) || !isprint(c) || c == '%') {
 
600
      is_reserved = TRUE;
 
601
    }
 
602
 
 
603
    for (i = 0; i < strlen(reserved); i++) {
 
604
      if (c == reserved[i])
 
605
        is_reserved = TRUE;
 
606
    }
 
607
 
 
608
    if (!is_reserved) {
 
609
      fmtbuf[idx][column] = c;
 
610
    } else {
 
611
      fmtbuf[idx][column] = '%';
 
612
      column++;
 
613
      fmtbuf[idx][column] = hex[c >> 4];
 
614
      column++;
 
615
      fmtbuf[idx][column] = hex[c & 0xF];
 
616
    }
 
617
  }
 
618
  fmtbuf[idx][column] = '\0';
 
619
  return fmtbuf[idx];
 
620
}
 
621
 
 
622
/**
 
623
 * Create a copy of a GByteArray
 
624
 *
 
625
 * @param ba The byte array to be copied.
 
626
 * @return If ba exists, a freshly allocated copy.  NULL otherwise.
 
627
 *
 
628
 * XXX - Should this be in strutil.c?
 
629
 */
 
630
GByteArray *
 
631
byte_array_dup(GByteArray *ba) {
 
632
    GByteArray *new_ba;
 
633
 
 
634
    if (!ba)
 
635
        return NULL;
 
636
 
 
637
    new_ba = g_byte_array_new();
 
638
    g_byte_array_append(new_ba, ba->data, ba->len);
 
639
    return new_ba;
 
640
}
 
641
 
508
642
#define SUBID_BUF_LEN 5
509
643
gboolean
510
644
oid_str_to_bytes(const char *oid_str, GByteArray *bytes) {
563
697
  return TRUE;
564
698
}
565
699
 
 
700
/**
 
701
 * Compare the contents of two GByteArrays
 
702
 *
 
703
 * @param ba1 A byte array
 
704
 * @param ba2 A byte array
 
705
 * @return If both arrays are non-NULL and their lengths are equal and
 
706
 *         their contents are equal, returns TRUE.  Otherwise, returns
 
707
 *         FALSE.
 
708
 *
 
709
 * XXX - Should this be in strutil.c?
 
710
 */
 
711
gboolean
 
712
byte_array_equal(GByteArray *ba1, GByteArray *ba2) {
 
713
    if (!ba1 || !ba2)
 
714
        return FALSE;
 
715
 
 
716
    if (ba1->len != ba2->len)
 
717
        return FALSE;
 
718
 
 
719
    if (memcmp(ba1->data, ba2->data, ba1->len) != 0)
 
720
        return FALSE;
 
721
 
 
722
    return TRUE;
 
723
}
 
724
 
566
725
 
567
726
/* Return a XML escaped representation of the unescaped string.
568
727
 * The returned string must be freed when no longer in use. */