示例#1
0
static char *
get_uri_from_nemo_metafile_name (const char *filename)
{
	GString *s;
	char c;
	char *base_name, *p;
	int len;

	base_name = g_path_get_basename (filename);
	len = strlen (base_name);
	if (len <=  4 ||
	    strcmp (base_name + len - 4, ".xml") != 0) {
		g_free (base_name);
		return NULL;
	}
	base_name[len-4] = 0;

	s = g_string_new (NULL);

	p = base_name;
	while (*p) {
		c = *p++;
		if (c == '%') {
			c = g_ascii_xdigit_value (p[0]) << 4 |
			    g_ascii_xdigit_value (p[1]);
			p += 2;
		}
		g_string_append_c (s, c);
	}
	g_free (base_name);

	return g_string_free (s, FALSE);
}
static gchar*
decode_object_identifier (const gchar* enc, gssize length)
{
	GString *result;

	g_assert (enc);

	if (length < 0)
		length = strlen (enc);

	result = g_string_sized_new (length);
	while (length > 0) {
		char ch = *(enc++);
		--length;

		/* Underscores get special handling */
		if (G_UNLIKELY (ch == '_' &&
		                g_ascii_isxdigit(enc[0]) &&
		                g_ascii_isxdigit (enc[1]))) {
			ch = (g_ascii_xdigit_value (enc[0]) * 16) +
			     (g_ascii_xdigit_value (enc[1]));
			enc += 2;
			length -= 2;
		}

		g_string_append_c (result, ch);
	}

	return g_string_free (result, FALSE);
}
示例#3
0
MXFUMID *
mxf_umid_from_string (const gchar * str, MXFUMID * umid)
{
  gint len;
  guint i, j;

  g_return_val_if_fail (str != NULL, NULL);
  len = strlen (str);

  memset (umid, 0, 32);

  if (len != 95) {
    GST_ERROR ("Invalid UMID string length %d", len);
    return NULL;
  }

  for (i = 0, j = 0; i < 32; i++) {
    if (!g_ascii_isxdigit (str[j]) ||
        !g_ascii_isxdigit (str[j + 1]) ||
        (str[j + 2] != '.' && str[j + 2] != '\0')) {
      GST_ERROR ("Invalid UMID string '%s'", str);
      return NULL;
    }

    umid->u[i] =
        (g_ascii_xdigit_value (str[j]) << 4) | (g_ascii_xdigit_value (str[j +
                1]));
    j += 3;
  }
  return umid;
}
示例#4
0
/**
 * CacheUtilBytesFromString:
 * @value: String containing an even number of hex-digits.
 * @result: (out): Raw bytes used to encode the hex value.
 *
 * Returns: #TRUE if @result was modified or #FALSE if the string was not valid
 * hex-encoded bytes.
 */
gboolean CacheUtilBytesFromString(const gchar *value, GBytes **result) {
  g_assert(result);
  if (!value)
    return FALSE;

  gsize value_len = strlen(value);
  gsize result_len = value_len / 2;
  guint8 result_data[result_len];

  // Hex strings must have a length that's a multiple of two.
  if (value_len & 0x01)
    return FALSE;

  for (guint i = 0; i < value_len; i += 2) {
    gint nibble1 = g_ascii_xdigit_value(value[i]);
    gint nibble2 = g_ascii_xdigit_value(value[i + 1]);
    if (nibble1 < 0 || nibble2 < 0) {
      return FALSE;
    }
    result_data[i >> 1] = (nibble1 << 4) | nibble2;
  }

  *result = g_bytes_new(result_data, result_len);
  return TRUE;
}
示例#5
0
文件: compiler.c 项目: pwithnall/mcus
static gboolean
lex_constant (MCUSCompiler *self, guchar *constant, GError **error)
{
	/* In EBNF:
	 * hex-digit ::= "0" | "1" | ... | "9" | "A" | ... | "F"
	 * constant ::= hex-digit , hex-digit */

	guint length = 0;

	/* Find where the constant ends and copy it to a string that length */
	while (g_ascii_isxdigit (*(self->priv->i + length)))
		length++;

	/* Check we actually have a constant to lex */
	if (length != 2) {
		gchar following_section[COMPILER_ERROR_CONTEXT_LENGTH+1] = { '\0', };
		g_memmove (following_section, self->priv->i + length, COMPILER_ERROR_CONTEXT_LENGTH);
		self->priv->error_length = length;

		g_set_error (error, MCUS_COMPILER_ERROR, MCUS_COMPILER_ERROR_INVALID_CONSTANT,
		             _("A required constant had an incorrect length around line %u before \"%s\"."),
		             self->priv->line_number,
		             following_section);
		return FALSE;
	}

	*constant = g_ascii_xdigit_value (self->priv->i[0]) * 16 + g_ascii_xdigit_value (self->priv->i[1]);
	self->priv->i += length;

	return TRUE;
}
示例#6
0
/*
 * Simple routine to convert an ASCII hex string to binary data
 * Requires ASCII hex data and buffer to populate with binary data
 */
static gboolean
iseries_parse_hex_string (const char * ascii, guint8 * buf, size_t len)
{
    size_t i;
    int byte;
    gint   hexvalue;
    guint8 bytevalue;

    byte = 0;
    for (i = 0; i < len; i++)
    {
        hexvalue = g_ascii_xdigit_value(ascii[i]);
        i++;
        if (hexvalue == -1)
            return FALSE;        /* not a valid hex digit */
        bytevalue = (guint8)(hexvalue << 4);
        if (i >= len)
            return FALSE;        /* only one hex digit of the byte is present */
        hexvalue = g_ascii_xdigit_value(ascii[i]);
        if (hexvalue == -1)
            return FALSE;        /* not a valid hex digit */
        bytevalue |= (guint8) hexvalue;
        buf[byte] = bytevalue;
        byte++;
    }
    return TRUE;
}
示例#7
0
static const gchar* read_reg(const gchar *s, guint8 *r, int *line, GError **error)
{
	guint num = 0;

	s = skip_ws(s, line);
	if (!s || !*s) {
		g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Waiting for register name at line %d", *line);
		return NULL;
	}

	/* Special case: const */
	if (g_ascii_isxdigit(*s) && g_ascii_isxdigit(s[1]) && (s[2] == 0 || g_ascii_isspace(s[2]))) {
		num = g_ascii_xdigit_value(s[0]) * 16 + g_ascii_xdigit_value(s[1]);
		*r = num;
		s += 2;

		return skip_ws(s, line);
	}

	if (*s != 'r' && *s != 'R') {
		g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Waiting for register name at line %d", *line);
		return NULL;
	}

	++s;
	if (!g_ascii_isdigit(*s)) {
		g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Waiting for register name at line %d", *line);
		return NULL;
	}

	num = g_ascii_digit_value(*s);
	++s;
	if (g_ascii_isdigit(*s)) {
		num = num * 10 + g_ascii_digit_value(*s);
		++s;
		if (!*s || g_ascii_isspace(*s)) {
			*r = num;
			if (num > 31) {
				g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "We've got only 32 registers (at line %d)", *line);
				return NULL;
			}
			return skip_ws(s, line);
		} else {
			g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Waiting for register name at line %d", *line);
			return NULL;
		}
	} else if (!*s || g_ascii_isspace(*s)) {
		*r = num;
		return skip_ws(s, line);
	} else {
		g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Waiting for register name at line %d", *line);
		return NULL;
	}

	return NULL;
}
示例#8
0
gchar *ascii2native(const gchar * str)
{
    gint i;                     /* Temp variable */
    gint len = strlen(str);

    /**
     * This allocates enough space, and probably a little too much.
     */
    gchar *outstr = g_malloc(len * sizeof(guchar));

    gint pos = 0;               /* Current position in the output string. */
    for (i = 0; i < len; i++) {
        /**
         * If we see a '\u' then parse it.
         */
        if (((char) *(str + i) == '\\')
            && (((char) *(str + i + 1)) == 'u')
            && (g_ascii_isxdigit((char) *(str + i + 2)))
            && (g_ascii_isxdigit((char) *(str + i + 3)))
            && (g_ascii_isxdigit((char) *(str + i + 4)))
            && (g_ascii_isxdigit((char) *(str + i + 5)))) {

            gint bytes;
            gchar unibuf[6];
            gunichar value = 0;

            /**
             * Assumption that the /u will be followed by 4 hex digits.
             * <<12 to multiply by 16^3, <<8 for 16^2, <<4 for 16
             * I.e., each hex digit
             */
            value = (g_ascii_xdigit_value((gchar) * (str + i + 2)) << 12) +
                (g_ascii_xdigit_value((gchar) * (str + i + 3)) << 8) +
                (g_ascii_xdigit_value((gchar) * (str + i + 4)) << 4) +
                (g_ascii_xdigit_value((gchar) * (str + i + 5)));

            bytes = g_unichar_to_utf8(value, unibuf);
            int j;
            for (j = 0; j < bytes; j++) {
                outstr[pos++] = unibuf[j];
            }
            /**
             * Move past the entire escape sequence.
             */
            i += 5;
        }
        /**
         * Otherwise, just copy the byte.
         */
        else {
            outstr[pos++] = str[i];
        }
    }
    return outstr;
}
示例#9
0
/**
 * udisks_decode_udev_string:
 * @str: An udev-encoded string or %NULL.
 *
 * Unescapes sequences like \x20 to " " and ensures the returned string is valid UTF-8.
 *
 * If the string is not valid UTF-8, try as hard as possible to convert to UTF-8.
 *
 * If %NULL is passed, then %NULL is returned.
 *
 * See udev_util_encode_string() in libudev/libudev-util.c in the udev
 * tree for what kinds of strings can be used.
 *
 * Returns: A valid UTF-8 string that must be freed with g_free().
 */
gchar *
udisks_decode_udev_string (const gchar *str)
{
  GString *s;
  gchar *ret;
  const gchar *end_valid;
  guint n;

  if (str == NULL)
    {
      ret = NULL;
      goto out;
    }

  s = g_string_new (NULL);
  for (n = 0; str[n] != '\0'; n++)
    {
      if (str[n] == '\\')
        {
          gint val;

          if (str[n + 1] != 'x' || str[n + 2] == '\0' || str[n + 3] == '\0')
            {
              udisks_warning ("**** NOTE: malformed encoded string `%s'", str);
              break;
            }

          val = (g_ascii_xdigit_value (str[n + 2]) << 4) | g_ascii_xdigit_value (str[n + 3]);

          g_string_append_c (s, val);

          n += 3;
        }
      else
        {
          g_string_append_c (s, str[n]);
        }
    }

  if (!g_utf8_validate (s->str, -1, &end_valid))
    {
      udisks_warning ("The string `%s' is not valid UTF-8. Invalid characters begins at `%s'", s->str, end_valid);
      ret = g_strndup (s->str, end_valid - s->str);
      g_string_free (s, TRUE);
    }
  else
    {
      ret = g_string_free (s, FALSE);
    }

 out:
  return ret;
}
示例#10
0
char *
gra2cairo_get_utf8_str(const char *cstr, int symbol)
{
  char *tmp;
  size_t l, i, j;

  l = strlen(cstr);
  tmp = g_malloc(l * 6 + 1);
  if (tmp == NULL) {
    return NULL;
  }

  for (j = i = 0; i <= l; i++) {
    if (cstr[i] == '\\') {
      if (cstr[i + 1] == 'x' &&
	  g_ascii_isxdigit(cstr[i + 2]) &&
	  g_ascii_isxdigit(cstr[i + 3])) {
	char buf[8];
	int len, k;
	gunichar wc;

	wc = g_ascii_xdigit_value(cstr[i + 2]) * 16 + g_ascii_xdigit_value(cstr[i + 3]);
	len = g_unichar_to_utf8(wc, buf);
	for (k = 0; k < len; k++) {
	  tmp[j++] = buf[k];
	}
	i += 3;
      } else {
	i += 1;
	tmp[j++] = cstr[i];
      }
    } else {
      tmp[j++] = cstr[i];
    }
    tmp[j] = '\0';
  }

  if (symbol) {
    char *ptr;

    ptr = ascii2greece(tmp);
    if (ptr) {
      g_free(tmp);
      tmp = ptr;
    }
  }

  return tmp;
}
示例#11
0
static const gchar* read_data(const gchar *s, GByteArray *data, int *line, GError **error)
{
	unsigned char c;
	const gchar *p;

	++s;

	while (*s) {
		s = skip_ws(s, line);

		if (*s == '}') {
			++s;
			return s;
		}

		if (!g_ascii_isxdigit(*s)) {
			g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Invalid symbol at line %d `%16s'", *line, s);
			return NULL;
		}

		c = g_ascii_xdigit_value(*s) * 16;
		++s;

		if (!g_ascii_isxdigit(*s)) {
			g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Invalid symbol at line %d `%16s'", *line, s);
			return NULL;
		}

		c += g_ascii_xdigit_value(*s);
		++s;

		g_byte_array_append(data, &c, 1);

		p = skip_ws(s, line);
		if (p == s && *s != '}') {
			g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Invalid symbol at line %d `%16s'", *line, s);
			return NULL;
		}

		s = p;
	}

	c = 0;
	while (data->len % 4 != 0)
		g_byte_array_append(data, &c, 1);

	return NULL;
}
示例#12
0
static int
unescape_character (const char *scanner)
{
  int first_digit;
  int second_digit;
  
  first_digit = g_ascii_xdigit_value (*scanner++);
  if (first_digit < 0)
    return -1;

  second_digit = g_ascii_xdigit_value (*scanner++);
  if (second_digit < 0)
    return -1;

  return (first_digit << 4) | second_digit;
}
示例#13
0
/* Read next hex value in the input stream, return -1 if EOF */
static int
next_int (FILE *fstream)
{
	int ch;
	int value = 0;
	int gotone = 0;
	int done = 0;
    
	/* loop, accumulate hex value until find delimiter 
	   skip any initial delimiters found in read stream */

	while (!done) {
		ch = getc (fstream);
		if (ch == EOF) {
			value = -1;
			done++;
		} else {
			/* trim high bits, check type and accumulate */
			ch &= 0xff;
			if (g_ascii_isxdigit (ch)) {
				value = (value << 4) + g_ascii_xdigit_value (ch);
				gotone++;
			} else if ((hex_table[ch]) < 0 && gotone) {
				done++;
			}
		}
	}
	return value;
}
示例#14
0
static gint
unescape_character (const gchar *scanner)
{
    gint first_digit;
    gint second_digit;

    first_digit = g_ascii_xdigit_value (scanner[0]);
    if (first_digit < 0)
        return -1;

    second_digit = g_ascii_xdigit_value (scanner[1]);
    if (second_digit < 0)
        return -1;

    return (first_digit << 4) | second_digit;
}
示例#15
0
文件: gdbusauth.c 项目: Andais/glib
static gchar *
hexdecode (const gchar  *str,
           gsize        *out_len,
           GError      **error)
{
  gchar *ret;
  GString *s;
  guint n;

  ret = NULL;
  s = g_string_new (NULL);

  for (n = 0; str[n] != '\0'; n += 2)
    {
      gint upper_nibble;
      gint lower_nibble;
      guint value;

      upper_nibble = g_ascii_xdigit_value (str[n]);
      lower_nibble = g_ascii_xdigit_value (str[n + 1]);
      if (upper_nibble == -1 || lower_nibble == -1)
        {
          g_set_error (error,
                       G_IO_ERROR,
                       G_IO_ERROR_FAILED,
                       "Error hexdecoding string `%s' around position %d",
                       str, n);
          goto out;
        }
      value = (upper_nibble<<4) | lower_nibble;
      g_string_append_c (s, value);
    }

  ret = g_string_free (s, FALSE);
  s = NULL;

 out:
  if (s != NULL)
    g_string_free (s, TRUE);
  return ret;
}
示例#16
0
static int convert(guint8* out, guint8* raw, gsize len) {
	g_printf("Size: %d Raw: %s\n", len, raw);
	guint32 i = 0;
	gsize newlen = 0;

	if (len % 2 == 1) {
		out[i] = g_ascii_xdigit_value(raw[i]);
		++i; ++newlen;
	}

	for ( ; i < len; i += 2) {
		guint8 first, second;

		first = g_ascii_xdigit_value(raw[i]) << 4;
		second = g_ascii_xdigit_value(raw[i+ 1]);

		out[i / 2 + i % 2] = first | second;
		++newlen;
	}

	return newlen;
}
示例#17
0
static gchar *prv_object_name_to_id(const gchar *object_name)
{
	gchar *retval = NULL;
	unsigned int object_len = strlen(object_name);
	unsigned int i;
	gint hex;
	gchar byte;

	if (object_len & 1)
		goto on_error;

	retval = g_malloc((object_len >> 1) + 1);

	for (i = 0; i < object_len; i += 2) {
		hex = g_ascii_xdigit_value(object_name[i]);

		if (hex == -1)
			goto on_error;

		byte = hex << 4;
		hex = g_ascii_xdigit_value(object_name[i + 1]);

		if (hex == -1)
			goto on_error;

		byte |= hex;
		retval[i >> 1] = byte;
	}
	retval[i >> 1] = 0;

	return retval;

on_error:

	g_free(retval);

	return NULL;
}
示例#18
0
static gboolean
hex (const char *spec,
    int len,
    unsigned int *c)
{
  const char *end;
  *c = 0;
  for (end = spec + len; spec != end; spec++)
    if (g_ascii_isxdigit (*spec))
      *c = (*c << 4) | g_ascii_xdigit_value (*spec);
    else
      return FALSE;
  return TRUE;
}
示例#19
0
static void
checksum_to_bytes (const char *checksum,
                   guchar     *buf)
{
  guint i;
  guint j;

  for (i = 0, j = 0; i < 32; i += 1, j += 2)
    {
      gint big, little;

      g_assert (checksum[j]);
      g_assert (checksum[j+1]);

      big = g_ascii_xdigit_value (checksum[j]);
      little = g_ascii_xdigit_value (checksum[j+1]);

      g_assert (big != -1);
      g_assert (little != -1);

      buf[i] = (big << 4) | little;
    }
}
示例#20
0
文件: sim-util.c 项目: DuVale/phpzdl
guint8 *sim_hex2bin(gchar *data){
  int i,j=0,k;
	size_t l;
	gchar *st=NULL;
	gchar temp[3];
	if (data!=NULL){
		  l = strlen(data);
			if (l % 2) return NULL;
			st=g_new(gchar,l/2);
			if (st!=NULL){
				for(i=0;i<l;i+=2){
					if (g_ascii_isxdigit(data[i]) && g_ascii_isxdigit(data[i+1])){
						st[j++] =   g_ascii_xdigit_value(data[i])*16+  g_ascii_xdigit_value(data[i+1]);
					}else{
						g_free(st);
						st = NULL;
						break;
					}
				}
			}
	  }
	return st;
}
示例#21
0
static gboolean
hex (const gchar *spec,
     gint         len,
     guint       *c)
{
        const gchar *end;

        *c = 0;
        for (end = spec + len; spec != end; spec++) {
                if (!g_ascii_isxdigit (*spec))
                        return FALSE;

                *c = (*c << 4) | g_ascii_xdigit_value (*spec);
        }

        return TRUE;
}
示例#22
0
文件: csv.c 项目: russdill/libsigrok
static int parse_octstr(const char *str, struct context *ctx)
{
	gsize i, j, k, length;
	uint8_t value;
	char c;

	length = strlen(str);

	if (!length) {
		sr_err("Column %zu in line %zu is empty.", ctx->single_column,
			ctx->line_number);
		return SR_ERR;
	}

	/* Clear buffer in order to set bits only. */
	memset(ctx->sample_buffer, 0, (ctx->num_probes + 7) >> 3);

	/* Calculate the position of the first octal digit. */
	i = ctx->first_probe / 3;

	for (j = 0; i < length && j < ctx->num_probes; i++) {
		c = str[length - i - 1];

		if (c < '0' || c > '7') {
			sr_err("Invalid value '%s' in column %zu in line %zu.",
				str, ctx->single_column, ctx->line_number);
			return SR_ERR;
		}

		value = g_ascii_xdigit_value(c);

		k = (ctx->first_probe + j) % 3;

		for (; j < ctx->num_probes && k < 3; k++) {
			if (value & (1 << k))
				ctx->sample_buffer[j / 8] |= (1 << (j % 8));

			j++;
		}
	}

	return SR_OK;
}
示例#23
0
文件: ussd.c 项目: leinomii/ofono
static void ril_ussd_notify(struct ril_msg *message, gpointer user_data)
{
	struct ofono_ussd *ussd = user_data;
	struct parcel rilp;
	gchar *ussd_from_network;
	gchar *type;
	gint ussdtype;

	ril_util_init_parcel(message, &rilp);
	parcel_r_int32(&rilp);
	type = parcel_r_string(&rilp);
	ussdtype = g_ascii_xdigit_value(*type);
	ussd_from_network = parcel_r_string(&rilp);

	if (ussd_from_network)
		ofono_ussd_notify(ussd, ussdtype, 0xFF,
			(const unsigned char *)ussd_from_network,
			strlen(ussd_from_network));
	else
		ofono_ussd_notify(ussd, ussdtype, 0, NULL, 0);

	return;
}
示例#24
0
static const gchar* read_num(const gchar *s, RobotVMWord *result, int line, GError **error)
{
	RobotVMWord res = 0;
	RobotVMWord r2 = 0;

	if (s[0] == '0' && s[1] == 'x') { /* Read hex */
		s += 2;
		while (g_ascii_isxdigit(*s)) {
			r2 = res * 16 + g_ascii_xdigit_value(*s);
			++s;

			if (r2 < res) {
				g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Integer overflow at line %d", line);
				return NULL;
			}

			res = r2;
		}
	} else {
		while (g_ascii_isdigit(*s)) {
			r2 = res * 10 + g_ascii_digit_value(*s);
			++s;

			if (r2 < res) {
				g_set_error(error, ROBOT_ERROR, ROBOT_ERROR_SYNTAX, "Integer overflow at line %d", line);
				return NULL;
			}

			res = r2;
		}
	}

	*result = res;

	return s;
}
示例#25
0
gboolean
r_parser_ipv6(guint8 *str, gint *len, const gchar *param, gpointer state, RParserMatch *match)
{
  gint colons = 0;
  gint dots = 0;
  gint octet = 0;
  gint digit = 16;
  gboolean shortened = FALSE;

  *len = 0;

  while (1)
    {
      if (str[*len] == ':')
        {
          if (G_UNLIKELY(octet > 0xffff || (octet == -1 && shortened) || digit == 10))
            return FALSE;

          if (octet == -1)
            shortened = TRUE;

          if (G_UNLIKELY(colons == 7))
            break;

          colons++;
          octet = -1;
        }
      else if (g_ascii_isxdigit(str[*len]))
        {
          if (octet == -1)
            octet = 0;
          else
            octet *= digit;

          octet += g_ascii_xdigit_value(str[*len]);
        }
      else if (str[*len] == '.')
        {
          if (G_UNLIKELY((digit == 10 && octet > 255)))
              return FALSE;

          if (G_UNLIKELY((digit == 16 && octet > 597) || octet == -1 || colons == 7 || dots == 3))
            break;

          dots++;
          octet = -1;
          digit = 10;
        }
      else
        break;

      (*len)++;
    }

  if (G_UNLIKELY(str[*len - 1] == '.'))
    {
      (*len)--;
      dots--;
    }
  else if (G_UNLIKELY(str[*len - 1] == ':' && str[*len - 2] != ':'))
    {
      (*len)--;
      colons--;
    }

  if (colons < 2 || colons > 7 || (digit == 10 && octet > 255) || (digit == 16 && octet > 0xffff) ||
      !(dots == 0 || dots == 3) || (!shortened && colons < 7 && dots == 0))
    return FALSE;

  return TRUE;
}
示例#26
0
gboolean string_to_field_value(const char* str, FieldTypeIdentifier type, FieldValue* value)
{
    switch (type) {
        int i;
        int len;


    case FieldTypeUInt32:
        if(!is_number(str)) {
            DBG1("not a number %s", str);
            return FALSE;
        }
        value->u32 = atoi(str);
        break;
    case FieldTypeInt32:
        if(!is_number(str)) {
            DBG1("not a number %s", str);
            return FALSE;
        }
        value->i32 = atoi(str);
        break;
    case FieldTypeUInt64:
        if(!is_number(str)) {
            DBG1("not a number %s", str);
            return FALSE;
        }
        value->u64 = g_ascii_strtoull(str, NULL, 10);
        break;
    case FieldTypeInt64:
        if(!is_number(str)) {
            DBG1("not a number %s", str);
            return FALSE;
        }
        value->i64 = g_ascii_strtoll(str, NULL, 10);
        break;

    case FieldTypeDecimal:
        if(!string_to_decimal_value (str, value)) {
            return FALSE;
        }
        break;

    case FieldTypeAsciiString:
    case FieldTypeUnicodeString:
        value->bytevec.nbytes = strlen(str);
        value->bytevec.bytes = (guint8*)g_strdup(str);
        break;

    case FieldTypeByteVector:
        len = strlen(str);
        value->bytevec.nbytes = len/2;
        value->bytevec.bytes = g_malloc(1+value->bytevec.nbytes);
        for(i=0; i + 1 < len; i += 2) {
            int nibble1;
            int nibble2;
            nibble1 = g_ascii_xdigit_value(str[i]);
            nibble2 = g_ascii_xdigit_value(str[i+1]);

            if(-1 != nibble1 && -1 != nibble2) {
                value->bytevec.bytes[i/2] = (nibble1 << 4) | nibble2;
            }
            else {
                value->bytevec.bytes[i/2] = 0;
                DBG2("invalid character: index %d in string [%s]", i, str);
                return FALSE;
            }
        }

        break;
    default:
        DBG0("Called with bad type.");
        return FALSE;
        break;
    }
    return TRUE;
}
示例#27
0
static gboolean
checkout_one_file_at (OstreeRepo                        *repo,
                      OstreeRepoCheckoutOptions         *options,
                      GFile                             *source,
                      GFileInfo                         *source_info,
                      int                                destination_dfd,
                      const char                        *destination_name,
                      GCancellable                      *cancellable,
                      GError                           **error)
{
  gboolean ret = FALSE;
  const char *checksum;
  gboolean is_symlink;
  gboolean can_cache;
  gboolean need_copy = TRUE;
  char loose_path_buf[_OSTREE_LOOSE_PATH_MAX];
  g_autoptr(GInputStream) input = NULL;
  g_autoptr(GVariant) xattrs = NULL;
  gboolean is_whiteout;

  is_symlink = g_file_info_get_file_type (source_info) == G_FILE_TYPE_SYMBOLIC_LINK;

  checksum = ostree_repo_file_get_checksum ((OstreeRepoFile*)source);

  is_whiteout = !is_symlink && options->process_whiteouts &&
    g_str_has_prefix (destination_name, WHITEOUT_PREFIX);

  /* First, see if it's a Docker whiteout,
   * https://github.com/docker/docker/blob/1a714e76a2cb9008cd19609059e9988ff1660b78/pkg/archive/whiteouts.go
   */
  if (is_whiteout)
    {
      const char *name = destination_name + (sizeof (WHITEOUT_PREFIX) - 1);

      if (!name[0])
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                       "Invalid empty whiteout '%s'", name);
          goto out;
        }

      g_assert (name[0] != '/'); /* Sanity */

      if (!glnx_shutil_rm_rf_at (destination_dfd, name, cancellable, error))
        goto out;

      need_copy = FALSE;
    }
  else if (!is_symlink)
    {
      gboolean did_hardlink = FALSE;
      /* Try to do a hardlink first, if it's a regular file.  This also
       * traverses all parent repos.
       */
      OstreeRepo *current_repo = repo;

      while (current_repo)
        {
          gboolean is_bare = ((current_repo->mode == OSTREE_REPO_MODE_BARE
                               && options->mode == OSTREE_REPO_CHECKOUT_MODE_NONE) ||
                              (current_repo->mode == OSTREE_REPO_MODE_BARE_USER
                               && options->mode == OSTREE_REPO_CHECKOUT_MODE_USER));
          gboolean current_can_cache = (options->enable_uncompressed_cache
                                        && current_repo->enable_uncompressed_cache);
          gboolean is_archive_z2_with_cache = (current_repo->mode == OSTREE_REPO_MODE_ARCHIVE_Z2
                                               && options->mode == OSTREE_REPO_CHECKOUT_MODE_USER
                                               && current_can_cache);

          /* But only under these conditions */
          if (is_bare || is_archive_z2_with_cache)
            {
              /* Override repo mode; for archive-z2 we're looking in
                 the cache, which is in "bare" form */
              _ostree_loose_path (loose_path_buf, checksum, OSTREE_OBJECT_TYPE_FILE, OSTREE_REPO_MODE_BARE);
              if (!checkout_file_hardlink (current_repo,
                                           options,
                                           loose_path_buf,
                                           destination_dfd, destination_name,
                                           TRUE, &did_hardlink,
                                           cancellable, error))
                goto out;

              if (did_hardlink && options->devino_to_csum_cache)
                {
                  struct stat stbuf;
                  OstreeDevIno *key;
                  
                  if (TEMP_FAILURE_RETRY (fstatat (destination_dfd, destination_name, &stbuf, AT_SYMLINK_NOFOLLOW)) != 0)
                    {
                      glnx_set_error_from_errno (error);
                      goto out;
                    }
                  
                  key = g_new (OstreeDevIno, 1);
                  key->dev = stbuf.st_dev;
                  key->ino = stbuf.st_ino;
                  memcpy (key->checksum, checksum, OSTREE_SHA256_STRING_LEN+1);
                  
                  g_hash_table_add ((GHashTable*)options->devino_to_csum_cache, key);
                }

              if (did_hardlink)
                break;
            }
          current_repo = current_repo->parent_repo;
        }

      need_copy = !did_hardlink;
    }

  can_cache = (options->enable_uncompressed_cache
               && repo->enable_uncompressed_cache);

  /* Ok, if we're archive-z2 and we didn't find an object, uncompress
   * it now, stick it in the cache, and then hardlink to that.
   */
  if (can_cache
      && !is_whiteout
      && !is_symlink
      && need_copy
      && repo->mode == OSTREE_REPO_MODE_ARCHIVE_Z2
      && options->mode == OSTREE_REPO_CHECKOUT_MODE_USER)
    {
      gboolean did_hardlink;
      
      if (!ostree_repo_load_file (repo, checksum, &input, NULL, NULL,
                                  cancellable, error))
        goto out;

      /* Overwrite any parent repo from earlier */
      _ostree_loose_path (loose_path_buf, checksum, OSTREE_OBJECT_TYPE_FILE, OSTREE_REPO_MODE_BARE);

      if (!checkout_object_for_uncompressed_cache (repo, loose_path_buf,
                                                   source_info, input,
                                                   cancellable, error))
        {
          g_prefix_error (error, "Unpacking loose object %s: ", checksum);
          goto out;
        }
      
      g_clear_object (&input);

      /* Store the 2-byte objdir prefix (e.g. e3) in a set.  The basic
       * idea here is that if we had to unpack an object, it's very
       * likely we're replacing some other object, so we may need a GC.
       *
       * This model ensures that we do work roughly proportional to
       * the size of the changes.  For example, we don't scan any
       * directories if we didn't modify anything, meaning you can
       * checkout the same tree multiple times very quickly.
       *
       * This is also scale independent; we don't hardcode e.g. looking
       * at 1000 objects.
       *
       * The downside is that if we're unlucky, we may not free
       * an object for quite some time.
       */
      g_mutex_lock (&repo->cache_lock);
      {
        gpointer key = GUINT_TO_POINTER ((g_ascii_xdigit_value (checksum[0]) << 4) + 
                                         g_ascii_xdigit_value (checksum[1]));
        if (repo->updated_uncompressed_dirs == NULL)
          repo->updated_uncompressed_dirs = g_hash_table_new (NULL, NULL);
        g_hash_table_insert (repo->updated_uncompressed_dirs, key, key);
      }
      g_mutex_unlock (&repo->cache_lock);

      if (!checkout_file_hardlink (repo, options, loose_path_buf,
                                   destination_dfd, destination_name,
                                   FALSE, &did_hardlink,
                                   cancellable, error))
        {
          g_prefix_error (error, "Using new cached uncompressed hardlink of %s to %s: ", checksum, destination_name);
          goto out;
        }

      need_copy = !did_hardlink;
    }

  /* Fall back to copy if we couldn't hardlink */
  if (need_copy)
    {
      if (!ostree_repo_load_file (repo, checksum, &input, NULL, &xattrs,
                                  cancellable, error))
        goto out;

      if (options->overwrite_mode == OSTREE_REPO_CHECKOUT_OVERWRITE_UNION_FILES)
        {
          if (!checkout_file_unioning_from_input_at (repo, options, source_info, xattrs, input,
                                                     destination_dfd,
                                                     destination_name,
                                                     cancellable, error)) 
            {
              g_prefix_error (error, "Union checkout of %s to %s: ", checksum, destination_name);
              goto out;
            }
        }
      else
        {
          if (!checkout_file_from_input_at (repo, options, source_info, xattrs, input,
                                            destination_dfd,
                                            destination_name,
                                            cancellable, error))
            {
              g_prefix_error (error, "Checkout of %s to %s: ", checksum, destination_name);
              goto out;
            }
        }

      if (input)
        {
          if (!g_input_stream_close (input, cancellable, error))
            goto out;
        }
    }

  ret = TRUE;
 out:
  return ret;
}
示例#28
0
/* Parsing something like
 * <rawMsg
 *   protocol="Diameter"
 *   version="1">
 *    [truncated]010001244000012C01000...
 * </rawMsg>
 */
static wtap_open_return_val
write_packet_data(wtap_dumper *wdh, struct wtap_pkthdr *phdr, int *err, gchar **err_info, guint8 *file_buf)
{
	char *curr_pos, *next_pos;
	char proto_name_str[16];
	int tag_str_len = 0;
	int proto_str_len, raw_data_len, pkt_data_len,  exp_pdu_tags_len, i, j;
	guint8 *packet_buf;
	gchar chr;
	gint val1, val2;

	memset(proto_name_str, 0, sizeof(proto_name_str));
	/* Extract the protocol name */
	curr_pos = strstr(file_buf, "protocol=\"");
	if (!curr_pos){
		return WTAP_OPEN_ERROR;
	}
	curr_pos = curr_pos + 10;
	next_pos = strstr(curr_pos, "\"");
	proto_str_len = (int)(next_pos - curr_pos);
	if (proto_str_len > 15){
		return WTAP_OPEN_ERROR;
	}

	g_strlcpy(proto_name_str, curr_pos, proto_str_len+1);
	ascii_strdown_inplace(proto_name_str);

	/* Do string matching and replace with Wiresharks protocol name */
	if (strcmp(proto_name_str, "gtpv2-c") == 0){
		/* Change to gtpv2 */
		proto_name_str[5] = '\0';
		proto_name_str[6] = '\0';
		proto_str_len = 5;
	}
	/* XXX Do we need to check for function="S1" */
	if (strcmp(proto_name_str, "nas") == 0){
		/* Change to nas-eps_plain */
		g_strlcpy(proto_name_str, "nas-eps_plain", 14);
		proto_name_str[13] = '\0';
		proto_str_len = 13;
	}
	/* Find the start of the raw data*/
	curr_pos = strstr(next_pos, ">") + 1;
	next_pos = strstr(next_pos, "<");

	raw_data_len = (int)(next_pos - curr_pos);

	/* Calculate the space needed for exp pdu tags*/
	tag_str_len = (proto_str_len + 3) & 0xfffffffc;
	exp_pdu_tags_len = tag_str_len + 4;


	/* Allocate the packet buf */
	pkt_data_len = raw_data_len / 2;
	packet_buf = (guint8 *)g_malloc0(pkt_data_len + exp_pdu_tags_len +4);

	/* Fill packet buff */
	packet_buf[0] = 0;
	packet_buf[1] = 12; /* EXP_PDU_TAG_PROTO_NAME */
	packet_buf[2] = 0;
	packet_buf[3] = tag_str_len;
	for (i = 4, j = 0; j < tag_str_len; i++, j++){
		packet_buf[i] = proto_name_str[j];
	}

	/* Add end of options */
	packet_buf[i] = 0;
	i++;
	packet_buf[i] = 0;
	i++;
	packet_buf[i] = 0;
	i++;
	packet_buf[i] = 0;
	i++;
	exp_pdu_tags_len = exp_pdu_tags_len + 4;

	/* Convert the hex raw msg data to binary and write to the packet buf*/
	for (; i < (pkt_data_len + exp_pdu_tags_len); i++){
		chr = *curr_pos;
		val1 = g_ascii_xdigit_value(chr);
		curr_pos++;
		chr = *curr_pos;
		val2 = g_ascii_xdigit_value(chr);
		if ((val1 != -1) && (val2 != -1)){
			packet_buf[i] = ((guint8)val1 * 16) + val2;
		}
		else{
			/* Something wrong, bail out */
			g_free(packet_buf);
			return WTAP_OPEN_ERROR;
		}
		curr_pos++;
	}
	/* Construct the phdr */
	memset(phdr, 0, sizeof(struct wtap_pkthdr));
	phdr->rec_type = REC_TYPE_PACKET;
	phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */

	phdr->caplen = pkt_data_len + exp_pdu_tags_len;
	phdr->len = pkt_data_len + exp_pdu_tags_len;

	phdr->ts.secs = 0;
	phdr->ts.nsecs = 0;

	if (!wtap_dump(wdh, phdr, packet_buf, err, err_info)) {
		switch (*err) {

		case WTAP_ERR_UNWRITABLE_REC_DATA:
			g_free(err_info);
			break;

		default:
			break;
		}
		g_free(packet_buf);
		return WTAP_OPEN_ERROR;
	}

	g_free(packet_buf);
	return WTAP_OPEN_MINE;
}
/*FUNCTION:------------------------------------------------------
 *  NAME
 *      zbee_security_parse_key
 *  DESCRIPTION
 *      Parses a key string from left to right into a buffer with
 *      increasing (normal byte order) or decreasing (reverse byte
 *      order) address.
 *  PARAMETERS
 *      const gchar    *key_str - pointer to the string
 *      guint8         *key_buf - destination buffer in memory
 *      gboolean        big_end - fill key_buf with incrementing address
 *  RETURNS
 *      gboolean
 *---------------------------------------------------------------
 */
static gboolean
zbee_security_parse_key(const gchar *key_str, guint8 *key_buf, gboolean byte_order)
{
    int             i, j;
    gchar           temp;
    gboolean        string_mode = FALSE;

    /* Clear the key. */
    memset(key_buf, 0, ZBEE_SEC_CONST_KEYSIZE);
    if (key_str == NULL) {
        return FALSE;
    }

    /*
     * Attempt to parse the key string. The key string must
     * be at least 16 pairs of hexidecimal digits with the
     * following optional separators: ':', '-', " ", or 16
     * alphanumeric characters after a double-quote.
     */
    if ( (temp = *key_str++) == '"') {
        string_mode = TRUE;
        temp = *key_str++;
    }

    j = byte_order?ZBEE_SEC_CONST_KEYSIZE-1:0;
    for (i=ZBEE_SEC_CONST_KEYSIZE-1; i>=0; i--) {
        if ( string_mode ) {
            if ( g_ascii_isprint(temp) ) {
                key_buf[j] = temp;
                temp = *key_str++;
            } else {
                return FALSE;
            }
        }
        else {
            /* If this character is a separator, skip it. */
            if ( (temp == ':') || (temp == '-') || (temp == ' ') ) temp = *(key_str++);

            /* Process a nibble. */
            if ( g_ascii_isxdigit (temp) ) key_buf[j] = g_ascii_xdigit_value(temp)<<4;
            else return FALSE;

            /* Get the next nibble. */
            temp = *(key_str++);

            /* Process another nibble. */
            if ( g_ascii_isxdigit (temp) ) key_buf[j] |= g_ascii_xdigit_value(temp);
            else return FALSE;

            /* Get the next nibble. */
            temp = *(key_str++);
        }

        /* Move key_buf pointer */
        if ( byte_order ) {
            j--;
        } else {
            j++;
        }

    } /* for */

    /* If we get this far, then the key was good. */
    return TRUE;
} /* zbee_security_parse_key */
示例#30
0
CORBA_Object
CORBA_ORB_string_to_object (CORBA_ORB          orb,
			    const CORBA_char  *string,
			    CORBA_Environment *ev)
{
	CORBA_Object         retval = CORBA_OBJECT_NIL;
	CORBA_unsigned_long  len;
	GIOPRecvBuffer      *buf;
#if defined ENABLE_HTTP
	gchar               *ior = NULL;
#endif
	guchar              *tmpbuf;
	int                  i;

	if (strncmp (string, "IOR:", strlen("IOR:"))            &&
	    strncmp (string, "corbaloc:", strlen ("corbaloc:")) &&
	    strncmp (string, "iiop:", strlen ("iiop:"))         &&
	    strncmp (string, "iiops:", strlen ("iiops:"))       &&
	    strncmp (string, "ssliop:", strlen ("ssliop:"))     &&
	    strncmp (string, "uiop:", strlen ("uiop:"))) {

#if defined ENABLE_HTTP
		if (matecorba_use_http_iors &&
		    strstr (string, "://")) {
			/* FIXME: this code is a security hazard */
			ior = orb_http_resolve (string);
			if (!ior) {
				/* FIXME, set error minor code
				 * (vendor's error code) to tell user
				 * initial location of error, ie my
				 * local ORB, proxy's ORB, server's
				 * ORB, etc. */
				CORBA_exception_set_system (
					ev,
					ex_CORBA_BAD_PARAM,
					CORBA_COMPLETED_NO);

				return CORBA_OBJECT_NIL;
			}
			string = ior;
		} else
#endif
		{
			CORBA_exception_set_system (
					ev,
					ex_CORBA_BAD_PARAM,
					CORBA_COMPLETED_NO);

			return CORBA_OBJECT_NIL;
		}
	}

	if (!strncmp (string, "IOR:", strlen ("IOR:")))
	{
		string += 4;
		len = strlen (string);
		while (len > 0 && !g_ascii_isxdigit (string [len - 1]))
			len--;

		if (len % 2) {
#if defined ENABLE_HTTP
			g_free (ior);
#endif
			return CORBA_OBJECT_NIL;
		}

		tmpbuf = g_alloca (len / 2);

		for (i = 0; i < len; i += 2)
			tmpbuf [i/2] = (g_ascii_xdigit_value (string [i]) << 4) |
				g_ascii_xdigit_value (string [i + 1]);

		buf = giop_recv_buffer_use_encaps (tmpbuf, len / 2);

		if (MateCORBA_demarshal_object (&retval, buf, orb)) {
			CORBA_exception_set_system (
				ev,
				ex_CORBA_MARSHAL,
				CORBA_COMPLETED_NO);

			retval = CORBA_OBJECT_NIL;
		}

		giop_recv_buffer_unuse (buf);
#if defined ENABLE_HTTP
		g_free (ior);
#endif
		return retval;
	} else {
		return MateCORBA_object_by_corbaloc (orb, string, ev);
	}
}