Exemple #1
0
static gboolean
update_kill_ring(GntEntry *entry, GntEntryAction action, const char *text, int len)
{
	if (action < 0) {
		entry->killring->last = action;
		return FALSE;
	}

	if (len == 0)
		len = strlen(text);
	else if (len < 0) {
		text += len;
		len = -len;
	}

	if (action != entry->killring->last) {
		struct {
			GntEntryAction one;
			GntEntryAction two;
		} merges[] = {
			{ENTRY_DEL_BWD_WORD, ENTRY_DEL_FWD_WORD},
			{ENTRY_DEL_BWD_CHAR, ENTRY_DEL_FWD_CHAR},
			{ENTRY_DEL_BOL, ENTRY_DEL_EOL},
			{ENTRY_JAIL, ENTRY_JAIL},
		};
		int i;

		for (i = 0; merges[i].one != ENTRY_JAIL; i++) {
			if (merges[i].one == entry->killring->last &&
					merges[i].two == action) {
				g_string_append_len(entry->killring->buffer, text, len);
				break;
			} else if (merges[i].one == action &&
					merges[i].two == entry->killring->last) {
				g_string_prepend_len(entry->killring->buffer, text, len);
				break;
			}
		}
		if (merges[i].one == ENTRY_JAIL) {
			g_string_assign(entry->killring->buffer, text);
			g_string_truncate(entry->killring->buffer, len);
		}
		entry->killring->last = action;
	} else {
		if (action == ENTRY_DEL_BWD_CHAR || action == ENTRY_DEL_BWD_WORD)
			g_string_prepend_len(entry->killring->buffer, text, len);
		else
			g_string_append_len(entry->killring->buffer, text, len);
	}
	return TRUE;
}
static void
pack (GString *buffer)
{
    guint32 content_size;
    gchar content_string[sizeof(guint32)];

    content_size = g_htonl(buffer->len);
    memcpy(content_string, &content_size, sizeof(content_size));
    g_string_prepend_len(buffer, content_string, sizeof(content_size));
}
Exemple #3
0
static void
vp_trans_replace_prefix(ValuePairsTransform *t, SBGString *key)
{
  VPTransReplacePrefix *self = (VPTransReplacePrefix *)t;

  if (strncmp(self->old_prefix, sb_gstring_string(key)->str,
              self->old_prefix_len) != 0)
    return;

  g_string_erase(sb_gstring_string(key), 0, self->old_prefix_len);
  g_string_prepend_len(sb_gstring_string(key),
                       self->new_prefix, self->new_prefix_len);
}
static GError *
decode (void)
{
    guint32 content_size;
    gchar content_string[sizeof(guint32)];
    GError *error = NULL;

    content_size = g_htonl(buffer->len);
    memcpy(content_string, &content_size, sizeof(content_size));
    g_string_prepend_len(buffer, content_string, sizeof(content_size));

    milter_decoder_decode(decoder, buffer->str, buffer->len, &error);
    g_string_truncate(buffer, 0);

    return error;
}
Exemple #5
0
void raw_handler(SERVER_REC * server, char *data)
{
	GString *decrypted;
	char channel[CONTACT_SIZE], *ptr, *ptr_bak;
	int len;

	if (IsNULLorEmpty(data))
		return;

	ptr = strchr(data, ' ');	// point to command
	if (ptr == NULL)
		return;
	ptr++;

	if (strncmp(ptr, "332 ", 4) != 0)
		return;		// 332 = TOPIC

	ptr_bak = ptr;
	ptr = strchr(ptr, '#');	// point to #channel
	if (ptr == NULL) {
		ptr = strchr(ptr_bak, '&');	// point to &channel
		if (ptr == NULL) {
			ptr = strchr(ptr_bak, '!');	// point to !channel
			if (ptr == NULL)
				return;
		}
	}

	len = strchr(ptr, ' ') - ptr;
	if (len >= CONTACT_SIZE - 2)
		return;		// channel string too long, something went wrong
	strncpy(channel, ptr, len);
	channel[len] = '\0';

	ptr = strchr(ptr, ':');	// point to topic msg start
	if (ptr == NULL)
		return;
	ptr++;

	decrypted = g_string_new("");
	if (FiSH_decrypt(server, ptr, channel, decrypted)) {
		g_string_prepend_len(decrypted, data, strlen(data) - strlen(ptr));
		signal_continue(2, server, decrypted->str);
		ZeroMemory(decrypted->str, decrypted->len);
	}
	g_string_free(decrypted, TRUE);
}
static void
end_element (GMarkupParseContext  *context,
             const gchar          *element_name,
             gpointer              user_data,
             GError              **error)
{
  ParseState *state = user_data;

  if (strcmp (element_name, "default") == 0)
    {
      state->value = g_variant_parse (state->type, state->string->str,
                                      NULL, NULL, error);
      if (state->value == NULL)
        return;

      if (state->l10n)
        {
          if (state->context)
            {
              gint len;

              /* Contextified messages are supported by prepending the
               * context, followed by '\004' to the start of the message
               * string.  We do that here to save GSettings the work
               * later on.
               *
               * Note: we are about to g_free() the context anyway...
               */
              len = strlen (state->context);
              state->context[len] = '\004';
              g_string_prepend_len (state->string, state->context, len + 1);
            }

          g_variant_builder_add (&state->key_options, "{sv}", "l10n",
                                 g_variant_new ("(ys)",
                                                state->l10n,
                                                state->string->str));
        }

      g_string_free (state->string, TRUE);
      state->string = NULL;
      g_free (state->context);
      state->context = NULL;
    }

  else if (strcmp (element_name, "key") == 0)
    {
      if (state->value == NULL)
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
                               "element <default> is required in <key>\n");
          return;
        }

      if (state->min != NULL)
        {
          if (g_variant_compare (state->value, state->min) < 0 ||
              g_variant_compare (state->value, state->max) > 0)
            {
              g_set_error (error, G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
                           "<default> is not contained in the specified range");
              return;
            }

          state->min = state->max = NULL;
        }
      else if (state->choices != NULL)
        {
          if (!is_valid_choices (state->value, state->choices->str))
            {
              g_set_error_literal (error, G_MARKUP_ERROR,
                                   G_MARKUP_ERROR_INVALID_CONTENT,
                                   "<default> contains string not in <choices>");
              return;
            }

          state->choices = NULL;
        }

      gvdb_item_set_value (state->key, state->value);
      gvdb_item_set_options (state->key,
                             g_variant_builder_end (&state->key_options));

      state->value = NULL;
    }

  else if (strcmp (element_name, "summary") == 0 ||
           strcmp (element_name, "description") == 0)
    {
      g_string_free (state->string, TRUE);
      state->string = NULL;
    }

  else if (strcmp (element_name, "choices") == 0)
    {
      gchar *choices;

      choices = g_string_free (state->choices, FALSE);
      g_variant_builder_add (&state->key_options, "{sv}", "choices",
                             g_variant_new_byte_array (choices, -1));
      g_free (choices);
    }
}
Exemple #7
0
static gboolean
xmms_faad_init (xmms_xform_t *xform)
{
	xmms_faad_data_t *data;
	xmms_error_t error;

	NeAACDecConfigurationPtr config;
	gint bytes_read;
	gulong samplerate;
	guchar channels;

	g_return_val_if_fail (xform, FALSE);

	data = g_new0 (xmms_faad_data_t, 1);
	data->outbuf = g_string_new (NULL);
	data->buffer_size = FAAD_BUFFER_SIZE;

	xmms_xform_private_data_set (xform, data);

	data->decoder = NeAACDecOpen ();
	config = NeAACDecGetCurrentConfiguration (data->decoder);
	config->defObjectType = LC;
	config->defSampleRate = 44100;
	config->outputFormat = FAAD_FMT_16BIT;
	config->downMatrix = 0;
	config->dontUpSampleImplicitSBR = 0;
	NeAACDecSetConfiguration (data->decoder, config);

	switch (config->outputFormat) {
	case FAAD_FMT_16BIT:
		data->sampleformat = XMMS_SAMPLE_FORMAT_S16;
		break;
	case FAAD_FMT_24BIT:
		/* we don't have 24-bit format to use in xmms2 */
		data->sampleformat = XMMS_SAMPLE_FORMAT_S32;
		break;
	case FAAD_FMT_32BIT:
		data->sampleformat = XMMS_SAMPLE_FORMAT_S32;
		break;
	case FAAD_FMT_FLOAT:
		data->sampleformat = XMMS_SAMPLE_FORMAT_FLOAT;
		break;
	case FAAD_FMT_DOUBLE:
		data->sampleformat = XMMS_SAMPLE_FORMAT_DOUBLE;
		break;
	}

	while (data->buffer_length < 8) {
		xmms_error_reset (&error);
		bytes_read = xmms_xform_read (xform,
		                              (gchar *) data->buffer + data->buffer_length,
		                              data->buffer_size - data->buffer_length,
		                              &error);
		data->buffer_length += bytes_read;

		if (bytes_read < 0) {
			xmms_log_error ("Error while trying to read data on init");
			goto err;
		} else if (bytes_read == 0) {
			XMMS_DBG ("Not enough bytes to check the AAC header");
			goto err;
		}
	}

	/* which type of file are we dealing with? */
	data->filetype = FAAD_TYPE_UNKNOWN;
	if (xmms_xform_auxdata_has_val (xform, "decoder_config")) {
		data->filetype = FAAD_TYPE_MP4;
	} else if (!strncmp ((char *) data->buffer, "ADIF", 4)) {
		data->filetype = FAAD_TYPE_ADIF;
	} else {
		int i;

		/* ADTS mpeg file can be a stream and start in the middle of a
		 * frame so we need to have extra loop check here */
		for (i=0; i<data->buffer_length-1; i++) {
			if (data->buffer[i] == 0xff && (data->buffer[i+1]&0xf6) == 0xf0) {
				data->filetype = FAAD_TYPE_ADTS;
				g_memmove (data->buffer, data->buffer+i, data->buffer_length-i);
				data->buffer_length -= i;
				break;
			}
		}
	}

	if (data->filetype == FAAD_TYPE_ADTS || data->filetype == FAAD_TYPE_ADIF) {
		bytes_read = NeAACDecInit (data->decoder, data->buffer,
		                          data->buffer_length, &samplerate,
		                          &channels);
	} else if (data->filetype == FAAD_TYPE_MP4) {
		const guchar *tmpbuf;
		gsize tmpbuflen;
		guchar *copy;

		if (!xmms_xform_auxdata_get_bin (xform, "decoder_config", &tmpbuf,
		                                 &tmpbuflen)) {
			XMMS_DBG ("AAC decoder config data found but it's wrong type! (something broken?)");
			goto err;
		}

		copy = g_memdup (tmpbuf, tmpbuflen);
		bytes_read = NeAACDecInit2 (data->decoder, copy, tmpbuflen,
		                           &samplerate, &channels);
		g_free (copy);
	}

	if (bytes_read < 0) {
		XMMS_DBG ("Error initializing decoder library.");
		goto err;
	}

	/* Get mediainfo and skip the possible header */
	xmms_faad_get_mediainfo (xform);
	g_memmove (data->buffer, data->buffer + bytes_read,
	           data->buffer_length - bytes_read);
	data->buffer_length -= bytes_read;

	data->samplerate = samplerate;
	data->channels = channels;

	/* Because for HE AAC files some versions of libfaad return the wrong
	 * samplerate in init, we have to do one read and let it decide the
	 * real parameters. After changing sample parameters and format is
	 * supported, this hack should be removed and handled in read instead. */
	{
		gchar tmpbuf[1024];

		xmms_error_reset (&error);
		bytes_read = xmms_faad_read (xform, tmpbuf, 1024, &error);
		if (bytes_read <= 0) {
			XMMS_DBG ("First read from faad decoder failed!");
			return FALSE;
		}
		g_string_prepend_len (data->outbuf, tmpbuf, bytes_read);
	}

	xmms_xform_outdata_type_add (xform,
	                             XMMS_STREAM_TYPE_MIMETYPE,
	                             "audio/pcm",
	                             XMMS_STREAM_TYPE_FMT_FORMAT,
	                             data->sampleformat,
	                             XMMS_STREAM_TYPE_FMT_CHANNELS,
	                             data->channels,
	                             XMMS_STREAM_TYPE_FMT_SAMPLERATE,
	                             data->samplerate,
	                             XMMS_STREAM_TYPE_END);

	XMMS_DBG ("AAC decoder inited successfully!");

	return TRUE;

err:
	g_string_free (data->outbuf, TRUE);
	g_free (data);

	return FALSE;
}
int
main (int   argc,
      char *argv[])
{
    GStringChunk *string_chunk;

    gchar *tmp_string = NULL, *tmp_string_2;
    gint i;
    GString *string1, *string2;

    string_chunk = g_string_chunk_new (1024);

    for (i = 0; i < 100000; i ++)
    {
        tmp_string = g_string_chunk_insert (string_chunk, "hi pete");

        if (strcmp ("hi pete", tmp_string) != 0)
            g_error ("string chunks are broken.\n");
    }

    tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);

    g_assert (tmp_string_2 != tmp_string &&
              strcmp(tmp_string_2, tmp_string) == 0);

    tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);

    g_assert (tmp_string_2 == tmp_string);

    g_string_chunk_free (string_chunk);

    string1 = g_string_new ("hi pete!");
    string2 = g_string_new ("");

    g_assert (string1 != NULL);
    g_assert (string2 != NULL);
    g_assert (strlen (string1->str) == string1->len);
    g_assert (strlen (string2->str) == string2->len);
    g_assert (string2->len == 0);
    g_assert (strcmp ("hi pete!", string1->str) == 0);
    g_assert (strcmp ("", string2->str) == 0);

    for (i = 0; i < 10000; i++)
        g_string_append_c (string1, 'a'+(i%26));

    g_assert((strlen("hi pete!") + 10000) == string1->len);
    g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));

#ifndef G_OS_WIN32
    /* MSVC and mingw32 use the same run-time C library, which doesn't like
       the %10000.10000f format... */
    g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
                      "this pete guy sure is a wuss, like he's the number ",
                      1,
                      " wuss.  everyone agrees.\n",
                      string1->str,
                      10, 666, 15, 15, 666.666666666, 666.666666666);
#else
    g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
                      "this pete guy sure is a wuss, like he's the number ",
                      1,
                      " wuss.  everyone agrees.\n",
                      string1->str,
                      10, 666, 15, 15, 666.666666666, 666.666666666);
#endif

    g_string_free (string1, TRUE);
    g_string_free (string2, TRUE);

    /* append */
    string1 = g_string_new ("firsthalf");
    g_string_append (string1, "lasthalf");
    g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
    g_string_free (string1, TRUE);

    /* append_len */

    string1 = g_string_new ("firsthalf");
    g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
    g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
    g_string_free (string1, TRUE);

    /* prepend */
    string1 = g_string_new ("lasthalf");
    g_string_prepend (string1, "firsthalf");
    g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
    g_string_free (string1, TRUE);

    /* prepend_len */
    string1 = g_string_new ("lasthalf");
    g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
    g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
    g_string_free (string1, TRUE);

    /* insert */
    string1 = g_string_new ("firstlast");
    g_string_insert (string1, 5, "middle");
    g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
    g_string_free (string1, TRUE);

    /* insert with pos == end of the string */
    string1 = g_string_new ("firstmiddle");
    g_string_insert (string1, strlen ("firstmiddle"), "last");
    g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
    g_string_free (string1, TRUE);

    /* insert_len */

    string1 = g_string_new ("firstlast");
    g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
    g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
    g_string_free (string1, TRUE);

    /* insert_len with magic -1 pos for append */
    string1 = g_string_new ("first");
    g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
    g_assert (strcmp (string1->str, "firstlast") == 0);
    g_string_free (string1, TRUE);

    /* insert_len with magic -1 len for strlen-the-string */
    string1 = g_string_new ("first");
    g_string_insert_len (string1, 5, "last", -1);
    g_assert (strcmp (string1->str, "firstlast") == 0);
    g_string_free (string1, TRUE);

    /* g_string_equal */
    string1 = g_string_new ("test");
    string2 = g_string_new ("te");
    g_assert (! g_string_equal(string1, string2));
    g_string_append (string2, "st");
    g_assert (g_string_equal(string1, string2));
    g_string_free (string1, TRUE);
    g_string_free (string2, TRUE);

    /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
    string1 = g_string_new ("fiddle");
    string2 = g_string_new ("fiddle");
    g_assert (g_string_equal(string1, string2));
    g_string_append_c(string1, '\0');
    g_assert (! g_string_equal(string1, string2));
    g_string_append_c(string2, '\0');
    g_assert (g_string_equal(string1, string2));
    g_string_append_c(string1, 'x');
    g_string_append_c(string2, 'y');
    g_assert (! g_string_equal(string1, string2));
    g_assert (string1->len == 8);
    g_string_append(string1, "yzzy");
    g_assert (string1->len == 12);
    g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
    g_string_insert(string1, 1, "QED");
    g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
    g_string_free (string1, TRUE);
    g_string_free (string2, TRUE);

    return 0;
}