Beispiel #1
0
/*
 * Parse 0x4D Short Event Descriptor
 */
static void parseEventDescription(void *data, enum ER round) {
	assert(GetDescriptorTag(data) == 0x4D);
	struct descr_short_event *evtdesc = CastShortEventDescriptor(data);
	char evt[256];
	char dsc[256];

	int evtlen = evtdesc->event_name_length;
	if (round == TITLE) {
		if (!evtlen)
			return;
		strncpy(evt, (char *) &evtdesc->data, evtlen);
		evt[evtlen] = '\0';
		printf("\t<title lang=\"%s\">%s</title>\n", lookup_language(
				&evtdesc->lang_code1), convert_text(evt));
		return;
	}

	if (round == SUB_TITLE) {
		int dsclen = evtdesc->data[evtlen];
		strncpy(dsc, (char *) &evtdesc->data[evtlen + 1], dsclen);
		dsc[dsclen] = '\0';

		if (*dsc) {
			const char *d = convert_text(dsc);
			if (d && *d)
				printf("\t<sub-title lang=\"%s\">%s</sub-title>\n",
						lookup_language(&evtdesc->lang_code1), d);
		}
	}
}
Beispiel #2
0
static void process(ErlDrvData handle, ErlIOVec *ev) {
    baberl_drv_t* driver_data = (baberl_drv_t*) handle;
    ErlDrvBinary* args = ev->binv[1];
    char *data = args->orig_bytes;
    char *from_encoding, *to_encoding, *text;
    converted_text_t cv;

    from_encoding = read_string(&data);
    to_encoding = read_string(&data);
    text = read_string(&data);

    cv.error = 0;
    convert_text(from_encoding, to_encoding, text, strlen(text), &cv);

    if (cv.error == 0) {
        ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("ok"),
                                 ERL_DRV_BUF2BINARY, cv.text, cv.text_size,
                                 ERL_DRV_TUPLE, 2
                                };
        driver_output_term(driver_data->port, spec, sizeof(spec) / sizeof(spec[0]));
    }
    else {
        ErlDrvTermData spec[] = {ERL_DRV_ATOM, driver_mk_atom("error"),
                                 ERL_DRV_ATOM, driver_mk_atom("iconv_coding"),
                                 ERL_DRV_TUPLE, 2
                                };
        driver_output_term(driver_data->port, spec, sizeof(spec) / sizeof(spec[0]));
    }

    driver_free(from_encoding);
    driver_free(to_encoding);
    driver_free(text);
    driver_free(cv.text);
}
Beispiel #3
0
/*
 * Parse 0x4E Extended Event Descriptor
 */
static void parseLongEventDescription(void *data) {
	assert(GetDescriptorTag(data) == 0x4E);
	struct descr_extended_event *levt = CastExtendedEventDescriptor(data);
	char dsc[256];
	bool non_empty = (levt->descriptor_number || levt->last_descriptor_number
			|| levt->length_of_items || levt->data[0]);

	if (non_empty && levt->descriptor_number == 0)
		printf("\t<desc lang=\"%s\">", lookup_language(&levt->lang_code1));

	u_char *p = (u_char *) &levt->data;
	u_char *data_end = (u_char *) (CastExtendedEventDescriptor(data)
			+ DESCR_GEN_LEN + GetDescriptorLength(data));
	while (p < (u_char *) levt->data + levt->length_of_items) {
		struct item_extended_event *name = (struct item_extended_event *) p;
		int name_len = name->item_description_length;
		assert(p + ITEM_EXTENDED_EVENT_LEN + name_len < data_end);
		strncpy(dsc, (char *) &name->data, name_len);
		dsc[name_len] = '\0';
		printf("%s: ", convert_text(dsc));

		p += ITEM_EXTENDED_EVENT_LEN + name_len;

		struct item_extended_event *value = (struct item_extended_event *) p;
		int value_len = value->item_description_length;
		assert(p + ITEM_EXTENDED_EVENT_LEN + value_len < data_end);
		strncpy(dsc, (char *) &value->data, value_len);
		dsc[value_len] = '\0';
		printf("%s; ", convert_text(dsc));

		p += ITEM_EXTENDED_EVENT_LEN + value_len;
	}
	struct item_extended_event *text = (struct item_extended_event *) p;
	int len = text->item_description_length;
	if (non_empty && len) {
		strncpy(dsc, (char *) &text->data, len);
		dsc[len] = '\0';
		printf("%s", convert_text(dsc));
	}
	//printf("/%d/%d/%s", levt->descriptor_number, levt->last_descriptor_number, convert_text(dsc));
	if (non_empty && levt->descriptor_number == levt->last_descriptor_number)
		printf("</desc>\n");
}
Beispiel #4
0
static gboolean decode_comment_frame (const guchar * _data, gint size, gchar * *
 lang, gchar * * type, gchar * * value)
{
    const gchar * data = (const gchar *) _data;
    gchar * pair, * sep;
    gint converted;

    pair = convert_text (data + 4, size - 4, data[0], FALSE, & converted, NULL);

    if (pair == NULL || (sep = memchr (pair, 0, converted)) == NULL)
        return FALSE;

    * lang = g_strndup (data + 1, 3);
    * type = g_strdup (pair);
    * value = g_strdup (sep + 1);

    g_free (pair);
    return TRUE;
}
Beispiel #5
0
static gboolean parse_apic (const guchar * _data, gint size, gchar * * mime,
 gint * type, gchar * * desc, void * * image_data, gint * image_size)
{
    const gchar * data = (const gchar *) _data;
    const gchar * sep, * after;

    if (size < 2 || (sep = memchr (data + 1, 0, size - 2)) == NULL)
        return FALSE;

    if ((* desc = convert_text (sep + 2, data + size - sep - 2, data[0], TRUE,
     NULL, & after)) == NULL)
        return FALSE;

    * mime = g_strdup (data + 1);
    * type = sep[1];
    * image_data = g_memdup (after, data + size - after);
    * image_size = data + size - after;

    TAGDBG ("APIC: mime = %s, type = %d, desc = %s, size = %d.\n", * mime,
     * type, * desc, * image_size);
    return TRUE;
}
static gboolean
gedit_document_output_stream_flush (GOutputStream  *stream,
                                    GCancellable   *cancellable,
                                    GError        **error)
{
	GeditDocumentOutputStream *ostream;

	ostream = GEDIT_DOCUMENT_OUTPUT_STREAM (stream);

	if (ostream->priv->is_closed)
	{
		return TRUE;
	}

	/* if we have converted something flush residual data, validate and insert */
	if (ostream->priv->iconv != NULL)
	{
		gchar *outbuf;
		gsize outbuf_len;

		if (convert_text (ostream, NULL, 0, &outbuf, &outbuf_len, error))
		{
			validate_and_insert (ostream, outbuf, outbuf_len);
			g_free (outbuf);
		}
		else
		{
			return FALSE;
		}
	}

	if (ostream->priv->buflen > 0 && *ostream->priv->buffer != '\r')
	{
		/* If we reached here is because the last insertion was a half
		   correct char, which has to be inserted as fallback */
		gchar *text;

		if (ostream->priv->error_offset == -1)
		{
			ostream->priv->error_offset = gtk_text_iter_get_offset (&ostream->priv->pos);
		}

		text = ostream->priv->buffer;
		while (ostream->priv->buflen != 0)
		{
			insert_fallback (ostream, text);
			++text;
			--ostream->priv->buflen;
		}

		g_free (ostream->priv->buffer);
		ostream->priv->buffer = NULL;
	}
	else if (ostream->priv->buflen == 1 && *ostream->priv->buffer == '\r')
	{
		/* The previous chars can be invalid */
		apply_error_tag (ostream);

		/* See special case above, flush this */
		gtk_text_buffer_insert (GTK_TEXT_BUFFER (ostream->priv->doc),
		                        &ostream->priv->pos,
		                        "\r",
		                        1);

		g_free (ostream->priv->buffer);
		ostream->priv->buffer = NULL;
		ostream->priv->buflen = 0;
	}

	if (ostream->priv->iconv_buflen > 0 )
	{
		/* If we reached here is because the last insertion was a half
		   correct char, which has to be inserted as fallback */
		gchar *text;

		if (ostream->priv->error_offset == -1)
		{
			ostream->priv->error_offset = gtk_text_iter_get_offset (&ostream->priv->pos);
		}

		text = ostream->priv->iconv_buffer;
		while (ostream->priv->iconv_buflen != 0)
		{
			insert_fallback (ostream, text);
			++text;
			--ostream->priv->iconv_buflen;
		}

		g_free (ostream->priv->iconv_buffer);
		ostream->priv->iconv_buffer = NULL;
	}

	apply_error_tag (ostream);

	return TRUE;
}
static gssize
gedit_document_output_stream_write (GOutputStream            *stream,
				    const void               *buffer,
				    gsize                     count,
				    GCancellable             *cancellable,
				    GError                  **error)
{
	GeditDocumentOutputStream *ostream;
	gchar *text;
	gsize len;
	gboolean freetext = FALSE;

	if (g_cancellable_set_error_if_cancelled (cancellable, error))
	{
		return -1;
	}

	ostream = GEDIT_DOCUMENT_OUTPUT_STREAM (stream);

	if (!ostream->priv->is_initialized)
	{
		ostream->priv->charset_conv = guess_encoding (ostream, buffer, count);

		/* If we still have the previous case is that we didn't guess
		   anything */
		if (ostream->priv->charset_conv == NULL &&
		    !ostream->priv->is_utf8)
		{
			g_set_error_literal (error, GEDIT_DOCUMENT_ERROR,
			                     GEDIT_DOCUMENT_ERROR_ENCODING_AUTO_DETECTION_FAILED,
			                     _("It is not possible to detect the encoding automatically"));

			return -1;
		}

		/* Do not initialize iconv if we are not going to convert anything */
		if (!ostream->priv->is_utf8)
		{
			gchar *from_charset;

			/* Initialize iconv */
			g_object_get (G_OBJECT (ostream->priv->charset_conv),
				      "from-charset", &from_charset,
				      NULL);

			ostream->priv->iconv = g_iconv_open ("UTF-8", from_charset);

			if (ostream->priv->iconv == (GIConv)-1)
			{
				if (errno == EINVAL)
				{
					g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
						     _("Conversion from character set '%s' to 'UTF-8' is not supported"),
						     from_charset);
				}
				else
				{
					g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
						     _("Could not open converter from '%s' to 'UTF-8'"),
						     from_charset);
				}

				g_free (from_charset);
				g_clear_object (&ostream->priv->charset_conv);

				return -1;
			}

			g_free (from_charset);
		}

		/* Init the undoable action */
		gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (ostream->priv->doc));

		gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (ostream->priv->doc),
		                                &ostream->priv->pos);

		ostream->priv->is_initialized = TRUE;
	}

	if (ostream->priv->buflen > 0)
	{
		len = ostream->priv->buflen + count;
		text = g_malloc (len + 1);

		memcpy (text, ostream->priv->buffer, ostream->priv->buflen);
		memcpy (text + ostream->priv->buflen, buffer, count);

		text[len] = '\0';

		g_free (ostream->priv->buffer);

		ostream->priv->buffer = NULL;
		ostream->priv->buflen = 0;

		freetext = TRUE;
	}
	else
	{
		text = (gchar *) buffer;
		len = count;
	}

	if (!ostream->priv->is_utf8)
	{
		gchar *outbuf;
		gsize outbuf_len;

		/* check if iconv was correctly initializated, this shouldn't
		   happen but better be safe */
		if (ostream->priv->iconv == NULL)
		{
			g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
			                     _("Invalid object, not initialized"));

			if (freetext)
			{
				g_free (text);
			}

			return -1;
		}

		/* manage the previous conversion buffer */
		if (ostream->priv->iconv_buflen > 0)
		{
			gchar *text2;
			gsize len2;

			len2 = len + ostream->priv->iconv_buflen;
			text2 = g_malloc (len2 + 1);

			memcpy (text2, ostream->priv->iconv_buffer, ostream->priv->iconv_buflen);
			memcpy (text2 + ostream->priv->iconv_buflen, text, len);

			text2[len2] = '\0';

			if (freetext)
			{
				g_free (text);
			}

			text = text2;
			len = len2;

			g_free (ostream->priv->iconv_buffer);

			ostream->priv->iconv_buffer = NULL;
			ostream->priv->iconv_buflen = 0;

			freetext = TRUE;
		}

		if (!convert_text (ostream, text, len, &outbuf, &outbuf_len, error))
		{
			if (freetext)
			{
				g_free (text);
			}

			return -1;
		}

		if (freetext)
		{
			g_free (text);
		}

		/* set the converted text as the text to validate */
		text = outbuf;
		len = outbuf_len;
	}

	validate_and_insert (ostream, text, len);

	if (freetext)
	{
		g_free (text);
	}

	return count;
}
Beispiel #8
0
PyObject *MemoryEntryToPython(const GSM_MemoryEntry * entry)
{
	PyObject *v;
	PyObject *f;
	PyObject *r;
	PyObject *d;
	PyObject *l;
	int i;
	int j;
	Py_UNICODE *s;
	char *t;
	const GSM_BinaryPicture *bitmap;
	const char *bmptype;

	v = PyList_New(0);
	if (v == NULL)
		return NULL;

	for (i = 0; i < entry->EntriesNum; i++) {
		f = Py_None;
		switch (entry->Entries[i].EntryType) {
			case PBK_Number_General:
				convert_number("General");
				break;
			case PBK_Number_Mobile:
				convert_number("Mobile");
				break;
			case PBK_Number_Fax:
				convert_number("Fax");
				break;
			case PBK_Number_Pager:
				convert_number("Pager");
				break;
			case PBK_Number_Other:
				convert_number("Other");
				break;
			case PBK_Text_Note:
				convert_text("Text_Note");
				break;
			case PBK_Text_Postal:
				convert_text("Text_Postal");
				break;
			case PBK_Text_Email:
				convert_text("Text_Email");
				break;
			case PBK_Text_Email2:
				convert_text("Text_Email2");
				break;
			case PBK_Text_URL:
				convert_text("Text_URL");
				break;
			case PBK_Date:
				d = BuildPythonDateTime(&
							(entry->Entries[i].
							 Date));
				if (d == NULL) {
					Py_DECREF(v);
					return NULL;
				}
				f = Py_BuildValue("{s:s,s:O}", "Type", "Date",
						  "Value", d);
				Py_DECREF(d);
				break;
			case PBK_LastModified:
				d = BuildPythonDateTime(&
							(entry->Entries[i].
							 Date));
				if (d == NULL) {
					Py_DECREF(v);
					return NULL;
				}
				f = Py_BuildValue("{s:s,s:O}", "Type",
						  "LastModified", "Value", d);
				Py_DECREF(d);
				break;
			case PBK_Caller_Group:
				f = Py_BuildValue("{s:s,s:i}", "Type",
						  "Caller_Group", "Value",
						  entry->Entries[i].Number);
				break;
			case PBK_Text_Name:
				convert_text("Text_Name");
				break;
			case PBK_Text_LastName:
				convert_text("Text_LastName");
				break;
			case PBK_Text_FirstName:
				convert_text("Text_FirstName");
				break;
			case PBK_Text_SecondName:
				convert_text("Text_SecondName");
				break;
			case PBK_Text_NickName:
				convert_text("Text_NickName");
				break;
			case PBK_Text_FormalName:
				convert_text("Text_FormalName");
				break;
			case PBK_Text_NamePrefix:
				convert_text("Text_NamePrefix");
				break;
			case PBK_Text_NameSuffix:
				convert_text("Text_NameSuffix");
				break;
			case PBK_Text_Company:
				convert_text("Text_Company");
				break;
			case PBK_Text_JobTitle:
				convert_text("Text_JobTitle");
				break;
			case PBK_Category:
				if (entry->Entries[i].Number == -1) {
					convert_text("Category");
				} else {
					f = Py_BuildValue("{s:s,s:i}", "Type",
							  "Category", "Value",
							  entry->Entries[i].
							  Number);
				}
				break;
			case PBK_Private:
				f = Py_BuildValue("{s:s,s:i}", "Type",
						  "Private", "Value",
						  entry->Entries[i].Number);
				break;
			case PBK_Text_StreetAddress:
				convert_text("Text_StreetAddress");
				break;
			case PBK_Text_City:
				convert_text("Text_City");
				break;
			case PBK_Text_State:
				convert_text("Text_State");
				break;
			case PBK_Text_Zip:
				convert_text("Text_Zip");
				break;
			case PBK_Text_Country:
				convert_text("Text_Country");
				break;
			case PBK_Text_Custom1:
				convert_text("Text_Custom1");
				break;
			case PBK_Text_Custom2:
				convert_text("Text_Custom2");
				break;
			case PBK_Text_Custom3:
				convert_text("Text_Custom3");
				break;
			case PBK_Text_Custom4:
				convert_text("Text_Custom4");
				break;
			case PBK_Text_LUID:
				convert_text("Text_LUID");
				break;
			case PBK_Text_VOIP:
				convert_text("Text_VOIP");
				break;
			case PBK_Text_SWIS:
				convert_text("Text_SWIS");
				break;
			case PBK_Text_WVID:
				convert_text("Text_WVID");
				break;
			case PBK_Text_SIP:
				convert_text("Text_SIP");
				break;
			case PBK_Text_DTMF:
				convert_text("Text_DTMF");
				break;
			case PBK_Text_UserID:
				convert_text("Text_UserID");
				break;
			case PBK_Text_PictureName:
				convert_text("Text_PictureName");
				break;
			case PBK_RingtoneID:
				f = Py_BuildValue("{s:s,s:O}", "Type",
						  "RingtoneID", "Value",
						  PyLong_FromUnsignedLong
						  (entry->Entries[i].Number));
				break;
			case PBK_PictureID:
				f = Py_BuildValue("{s:s,s:O}", "Type",
						  "PictureID", "Value",
						  PyLong_FromUnsignedLong
						  (entry->Entries[i].Number));
				break;
			case PBK_CallLength:
				f = Py_BuildValue("{s:s,s:i}", "Type",
						  "CallLength", "Value",
						  entry->Entries[i].CallLength);
				break;
			case PBK_Number_Messaging:
				convert_number("Messaging");
				break;
			case PBK_Number_Video:
				convert_number("Video");
				break;
			case PBK_PushToTalkID:
				convert_text("PushToTalkID");
				break;
			case PBK_Photo:
				bitmap = &(entry->Entries[i].Picture);
				d = PyString_FromStringAndSize((char *)bitmap->
							       Buffer,
							       bitmap->Length);
				if (d == NULL) {
					Py_DECREF(v);
					return NULL;
				}
				bmptype = "";
				switch (bitmap->Type) {
					case PICTURE_BMP:
						bmptype = "BMP";
						break;
					case PICTURE_GIF:
						bmptype = "GIF";
						break;
					case PICTURE_JPG:
						bmptype = "JPG";
						break;
					case PICTURE_ICN:
						bmptype = "ICN";
						break;
					case PICTURE_PNG:
						bmptype = "PNG";
						break;
				}
				f = Py_BuildValue("{s:s,s:O,s:s}", "Type",
						  "Photo", "Value", d,
						  "PictureType", bmptype);
				Py_DECREF(d);
				break;
		}

		if (f == Py_None) {
			Py_DECREF(v);
			PyErr_Format(PyExc_ValueError,
				     "Bad MemoryEntry item type from gammu: %d",
				     entry->Entries[i].EntryType);
			return NULL;
		}

		if (f == NULL) {
			Py_DECREF(v);
			return NULL;
		}

		if (PyList_Append(v, f) != 0) {
			Py_DECREF(f);
			Py_DECREF(v);
			return NULL;
		}
		Py_DECREF(f);
	}

	t = MemoryTypeToString(entry->MemoryType);

	if (t == NULL) {
		Py_DECREF(v);
		return NULL;
	}

	r = Py_BuildValue("{s:i,s:s,s:O}",
			  "Location", entry->Location,
			  "MemoryType", t, "Entries", v);
	free(t);
	Py_DECREF(v);
	return r;
}
Beispiel #9
0
static gchar * decode_text_frame (const guchar * data, gint size)
{
    return convert_text ((const gchar *) data + 1, size - 1, data[0], FALSE,
     NULL, NULL);
}
Beispiel #10
0
static char * decode_text_frame (const unsigned char * data, int size)
{
    return convert_text ((const char *) data + 1, size - 1, data[0], FALSE,
     NULL, NULL);
}