Logo Search packages:      
Sourcecode: wireshark version File versions

gboolean hex_str_to_bytes ( const char *  hex_str,
GByteArray *  bytes,
gboolean  force_separators 
)

Turn a string of hex digits with optional separators (defined by is_byte_sep() into a byte array.

Parameters:
hex_str The string of hex digits.
bytes The GByteArray that will receive the bytes. This must be initialized by the caller.
force_separators If set to TRUE, separators MUST exist between bytes.
Returns:
True if the string was converted successfully

Definition at line 432 of file strutil.c.

                                                                                    {
      guint8            val;
      const guchar      *p, *q, *r, *s, *punct;
      char        four_digits_first_half[3];
      char        four_digits_second_half[3];
      char        two_digits[3];
      char        one_digit[2];

      if (! hex_str || ! bytes) {
            return FALSE;
      }
      g_byte_array_set_size(bytes, 0);
      p = (const guchar *)hex_str;
      while (*p) {
            q = p+1;
            r = p+2;
            s = p+3;

            if (*q && *r && *s
                && isxdigit(*p) && isxdigit(*q) &&
                isxdigit(*r) && isxdigit(*s)) {
                  four_digits_first_half[0] = *p;
                  four_digits_first_half[1] = *q;
                  four_digits_first_half[2] = '\0';
                  four_digits_second_half[0] = *r;
                  four_digits_second_half[1] = *s;
                  four_digits_second_half[2] = '\0';

                  /*
                   * Four or more hex digits in a row.
                   */
                  val = (guint8) strtoul(four_digits_first_half, NULL, 16);
                  g_byte_array_append(bytes, &val, 1);
                  val = (guint8) strtoul(four_digits_second_half, NULL, 16);
                  g_byte_array_append(bytes, &val, 1);

                  punct = s + 1;
                  if (*punct) {
                        /*
                         * Make sure the character after
                         * the forth hex digit is a byte
                         * separator, i.e. that we don't have
                         * more than four hex digits, or a
                         * bogus character.
                         */
                        if (is_byte_sep(*punct)) {
                              p = punct + 1;
                              continue;
                        }
                        else if (force_separators) {
                              return FALSE;
                              break;
                        }
                  }
                  p = punct;
                  continue;
            }

            else if (*q && isxdigit(*p) && isxdigit(*q)) {
                  two_digits[0] = *p;
                  two_digits[1] = *q;
                  two_digits[2] = '\0';

                  /*
                   * Two hex digits in a row.
                   */
                  val = (guint8) strtoul(two_digits, NULL, 16);
                  g_byte_array_append(bytes, &val, 1);
                  punct = q + 1;
                  if (*punct) {
                        /*
                         * Make sure the character after
                         * the second hex digit is a byte
                         * separator, i.e. that we don't have
                         * more than two hex digits, or a
                         * bogus character.
                         */
                        if (is_byte_sep(*punct)) {
                              p = punct + 1;
                              continue;
                        }
                        else if (force_separators) {
                              return FALSE;
                              break;
                        }
                  }
                  p = punct;
                  continue;
            }
            else if (*q && isxdigit(*p) && is_byte_sep(*q)) {
                  one_digit[0] = *p;
                  one_digit[1] = '\0';

                  /*
                   * Only one hex digit (not at the end of the string)
                   */
                  val = (guint8) strtoul(one_digit, NULL, 16);
                  g_byte_array_append(bytes, &val, 1);
                  p = q + 1;
                  continue;
            }
            else if (!*q && isxdigit(*p)) {
                  one_digit[0] = *p;
                  one_digit[1] = '\0';

                  /*
                   * Only one hex digit (at the end of the string)
                   */
                  val = (guint8) strtoul(one_digit, NULL, 16);
                  g_byte_array_append(bytes, &val, 1);
                  p = q;
                  continue;
            }
            else {
                  return FALSE;
            }
      }
      return TRUE;
}


Generated by  Doxygen 1.6.0   Back to index