Пример #1
0
gchar *get_part_as_string(MimeInfo *mimeinfo)
{
	gchar *textdata = NULL;
	gchar *filename = NULL;
	FILE *fp;

	cm_return_val_if_fail(mimeinfo != NULL, 0);
	procmime_decode_content(mimeinfo);
	
	if (mimeinfo->content == MIMECONTENT_MEM)
		textdata = g_strdup(mimeinfo->data.mem);
	else {
		filename = procmime_get_tmp_file_name(mimeinfo);
		if (procmime_get_part(filename, mimeinfo) < 0) {
			g_warning("error dumping temporary file '%s'", filename);
			g_free(filename);
			return NULL;
		}
		fp = claws_fopen(filename,"rb");
		if (!fp) {
			g_warning("error opening temporary file '%s'", filename);
			g_free(filename);
			return NULL;
		}
		textdata = fp_read_noconv(fp);
		claws_fclose(fp);
		g_unlink(filename);
		g_free(filename);
	}

	if (!g_utf8_validate(textdata, -1, NULL)) {
		gchar *tmp = NULL;
		codeconv_set_strict(TRUE);
		if (procmime_mimeinfo_get_parameter(mimeinfo, "charset")) {
			tmp = conv_codeset_strdup(textdata,
				procmime_mimeinfo_get_parameter(mimeinfo, "charset"),
				CS_UTF_8);
		}
		if (!tmp) {
			tmp = conv_codeset_strdup(textdata,
				conv_get_locale_charset_str_no_utf8(), 
				CS_UTF_8);
		}
		codeconv_set_strict(FALSE);
		if (!tmp) {
			tmp = conv_codeset_strdup(textdata,
				conv_get_locale_charset_str_no_utf8(), 
				CS_UTF_8);
		}
		if (tmp) {
			g_free(textdata);
			textdata = tmp;
		}
	}

	return textdata;	
}
Пример #2
0
gchar *xml_get_element(XMLFile *file)
{
	gchar *str;
	gchar *new_str;
	gchar *end;

	while ((end = strchr(file->bufp, '<')) == NULL)
		if (xml_read_line(file) < 0) return NULL;

	if (end == file->bufp)
		return NULL;

	str = g_strndup(file->bufp, end - file->bufp);
	/* this is not XML1.0 strict */
	g_strstrip(str);
	xml_unescape_str(str);

	file->bufp = end;
	xml_truncate_buf(file);

	if (str[0] == '\0') {
		g_free(str);
		return NULL;
	}

	if (!file->need_codeconv)
		return str;

	new_str = conv_codeset_strdup(str, file->encoding, CS_INTERNAL);
	if (!new_str)
		new_str = g_strdup(str);
	g_free(str);

	return new_str;
}
Пример #3
0
static gchar *jpilot_convert_encoding(const gchar *str)
{
	if (convert_charcode)
		return conv_codeset_strdup(str, CS_SHIFT_JIS, CS_INTERNAL);

	return g_strdup(str);
}
Пример #4
0
/**
 * Specify common name for person object.
 * \param person Person object.
 * \param value name.
 */
void addritem_person_set_common_name( ItemPerson *person, const gchar *value ) {
	if (!value || g_utf8_validate(value, -1, NULL))
		ADDRITEM_NAME(person) = mgu_replace_string( ADDRITEM_NAME(person), value );
	else {
		gchar *out = conv_codeset_strdup(value, 
				conv_get_locale_charset_str_no_utf8(),
				CS_INTERNAL);
		if (out)
			ADDRITEM_NAME(person) = mgu_replace_string( ADDRITEM_NAME(person), out );
		g_free(out);
	}
}
Пример #5
0
/**
 * Specify nick name for person object.
 * \param person Person object.
 * \param value name.
 */
void addritem_person_set_nick_name( ItemPerson *person, const gchar *value ) {
	if (!value || g_utf8_validate(value, -1, NULL))
		person->nickName = mgu_replace_string( person->nickName, value );
	else {
		gchar *out = conv_codeset_strdup(value, 
				conv_get_locale_charset_str_no_utf8(),
				CS_INTERNAL);
		if (out)
			person->nickName = mgu_replace_string( person->nickName, out );
		g_free(out);
	}
}
Пример #6
0
static GtkWidget *about_create_child_page_release_notes(void)
{
	GtkWidget *scrolledwin;
	GtkWidget *text;
	GtkTextBuffer *buffer;
	GtkTextIter iter;
	gchar *path, buf[1024];
	FILE *fp;

	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
			GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
			GTK_SHADOW_IN);
	text = gtk_text_view_new();
	gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
	gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
	gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
	gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
	gtk_container_add(GTK_CONTAINER(scrolledwin), text);

	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
	gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);

	path = g_strconcat(DOCDIR, G_DIR_SEPARATOR_S, RELEASE_NOTES_FILE, NULL);
	if ((fp = g_fopen(path, "rb")) == NULL) {
		if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
		g_free(path);
		return scrolledwin;
	}
	g_free(path);

	while (fgets(buf, sizeof(buf), fp) != NULL) {
		const gchar *src_codeset = conv_get_locale_charset_str();
		const gchar *dest_codeset = CS_UTF_8;
		gchar *tmp;

		tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
		if (!tmp) {
			g_warning("Failed to convert character set of action configuration");
			tmp = g_strdup(buf);
		}

		gtk_text_buffer_insert(buffer, &iter, tmp, -1);
		g_free(tmp);
	}
	fclose(fp);

	return scrolledwin;
}
Пример #7
0
void prefs_actions_read_config(void)
{
	gchar *rcpath;
	FILE *fp;
	gchar buf[PREFSBUFSIZE];
	gchar *act;

	debug_print("Reading actions configurations...\n");

	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
	if ((fp = g_fopen(rcpath, "rb")) == NULL) {
		if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
		g_free(rcpath);
		return;
	}
	g_free(rcpath);

	while (prefs_common.actions_list != NULL) {
		act = (gchar *)prefs_common.actions_list->data;
		prefs_common.actions_list =
			g_slist_remove(prefs_common.actions_list, act);
		g_free(act);
	}

	while (fgets(buf, sizeof(buf), fp) != NULL) {
		const gchar *src_codeset = conv_get_locale_charset_str();
		const gchar *dest_codeset = CS_UTF_8;
		gchar *tmp;

		tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
		if (!tmp) {
			g_warning("Failed to convert character set of action configuration\n");
			tmp = g_strdup(buf);
		}

		g_strchomp(tmp);
		act = strstr(tmp, ": ");
		if (act && act[2] && 
		    action_get_type(&act[2]) != ACTION_ERROR)
			prefs_common.actions_list =
				g_slist_append(prefs_common.actions_list,
					       tmp);
		else
			g_free(tmp);
	}
	fclose(fp);
}
Пример #8
0
void prefs_actions_write_config(void)
{
	gchar *rcpath;
	PrefFile *pfile;
	GSList *cur;

	debug_print("Writing actions configuration...\n");

	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
	if ((pfile= prefs_write_open(rcpath)) == NULL) {
		g_warning("Failed to write configuration to file\n");
		g_free(rcpath);
		return;
	}

	for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
		gchar *tmp = (gchar *)cur->data;
		const gchar *src_codeset = CS_UTF_8;
		const gchar *dest_codeset = conv_get_locale_charset_str();
		gchar *act;

		act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
		if (!act) {
			g_warning("Failed to convert character set of action configuration\n");
			act = g_strdup(act);
		}

		if (fputs(act, pfile->fp) == EOF ||
		    fputc('\n', pfile->fp) == EOF) {
			FILE_OP_ERROR(rcpath, "fputs || fputc");
			prefs_file_close_revert(pfile);
			g_free(act);
			g_free(rcpath);
			return;
		}
		g_free(act);
	}
	
	g_free(rcpath);

	if (prefs_file_close(pfile) < 0) {
		g_warning("failed to write configuration to file\n");
		return;
	}
}
Пример #9
0
/**
 * Builds a single regular expresion from an array of srings.
 *
 * @param strings The lines containing the different sub-regexp.
 *
 * @return The newly allocated regexp.
 */
static gchar *build_complete_regexp(gchar **strings)
{
	int i = 0;
	gchar *expr = NULL;
	while (strings && strings[i] && *strings[i]) {
		int old_len = expr ? strlen(expr):0;
		int new_len = 0;
		gchar *tmpstr = NULL;

		if (g_utf8_validate(strings[i], -1, NULL))
			tmpstr = g_strdup(strings[i]);
		else
			tmpstr = conv_codeset_strdup(strings[i], 
					conv_get_locale_charset_str_no_utf8(),
				 	CS_INTERNAL);

		if (strstr(tmpstr, "\n"))
			*(strstr(tmpstr, "\n")) = '\0';

		new_len = strlen(tmpstr);

		expr = g_realloc(expr, 
			expr ? (old_len + strlen("|()") + new_len + 1)
			     : (strlen("()") + new_len + 1));
		
		if (old_len) {
			strcpy(expr + old_len, "|(");
			strcpy(expr + old_len + 2, tmpstr);
			strcpy(expr + old_len + 2 + new_len, ")");
		} else {
			strcpy(expr+old_len, "(");
			strcpy(expr+old_len + 1, tmpstr);
			strcpy(expr+old_len + 1 + new_len, ")");
		}
		g_free(tmpstr);
		i++;
	}
	return expr;
}
Пример #10
0
static GtkWidget *about_create_child_page_authors(void)
{
	GtkWidget *scrolledwin;
	GtkWidget *text;
	GtkTextBuffer *buffer;
	GtkTextIter iter;
	gint i;

	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
				       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
					    GTK_SHADOW_IN);

	text = gtk_text_view_new();
	gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
	gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
	gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
	gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
	gtk_container_add(GTK_CONTAINER(scrolledwin), text);
	gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);

	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
	gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);

	/* init formatting tag: indentation for list items */
	gtk_text_buffer_create_tag(buffer, "indented-list-item",
				"indent", 8,
				NULL);
	gtk_text_buffer_create_tag(buffer, "underlined-list-title",
				"underline", PANGO_UNDERLINE_SINGLE,
				NULL);

	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("The Claws Mail Team")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; TEAM_LIST[i] != NULL; i++) {
		if (g_utf8_validate(TEAM_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, TEAM_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(TEAM_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("Previous team members")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; EX_TEAM_LIST[i] != NULL; i++) {
		if (g_utf8_validate(EX_TEAM_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, EX_TEAM_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(EX_TEAM_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("The translation team")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; TRANS_TEAM_LIST[i] != NULL; i++) {
		if (g_utf8_validate(TRANS_TEAM_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, TRANS_TEAM_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(TRANS_TEAM_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("Documentation team")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; DOC_TEAM_LIST[i] != NULL; i++) {
		if (g_utf8_validate(DOC_TEAM_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, DOC_TEAM_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(DOC_TEAM_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("Logo")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; LOGO_LIST[i] != NULL; i++) {
		if (g_utf8_validate(LOGO_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, LOGO_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(LOGO_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("Icons")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; ICONS_LIST[i] != NULL; i++) {
		if (g_utf8_validate(ICONS_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, ICONS_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(ICONS_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (_("Contributors")), -1,
			"underlined-list-title", NULL);
	gtk_text_buffer_insert(buffer, &iter, "\n", 1);

	for (i = 0; CONTRIBS_LIST[i] != NULL; i++) {
		if (g_utf8_validate(CONTRIBS_LIST[i], -1, NULL))
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, CONTRIBS_LIST[i], -1,
					"indented-list-item", NULL);
		else {
			gchar *conv = conv_codeset_strdup(CONTRIBS_LIST[i], CS_ISO_8859_1, CS_UTF_8);
			if (conv)
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, conv, -1,
						"indented-list-item", NULL);
			g_free(conv);
		}
		gtk_text_buffer_insert(buffer, &iter, "\n", 1);
	}

	return scrolledwin;
}
Пример #11
0
gchar *strconv_charset_convert(StringConverter *conv, gchar *srcstr)
{
	CharsetConverter *charsetconv = (CharsetConverter *) conv;

	return conv_codeset_strdup(srcstr, charsetconv->srccharset, charsetconv->dstcharset);
}
Пример #12
0
static gint pgpinline_check_signature(MimeInfo *mimeinfo)
{
    PrivacyDataPGP *data = NULL;
    gchar *textdata = NULL, *tmp = NULL;
    gpgme_data_t plain = NULL, cipher = NULL;
    gpgme_error_t err;

    cm_return_val_if_fail(mimeinfo != NULL, 0);

    if (procmime_mimeinfo_parent(mimeinfo) == NULL) {
        privacy_set_error(_("Incorrect part"));
        return 0; /* not parent */
    }
    if (mimeinfo->type != MIMETYPE_TEXT) {
        privacy_set_error(_("Not a text part"));
        debug_print("type %d\n", mimeinfo->type);
        return 0;
    }
    cm_return_val_if_fail(mimeinfo->privacy != NULL, 0);
    data = (PrivacyDataPGP *) mimeinfo->privacy;

    textdata = get_part_as_string(mimeinfo);

    if (!textdata) {
        g_free(textdata);
        privacy_set_error(_("Couldn't get text data."));
        return 0;
    }

    /* gtk2: convert back from utf8 */
    tmp = conv_codeset_strdup(textdata, CS_UTF_8,
                              procmime_mimeinfo_get_parameter(mimeinfo, "charset"));
    if (!tmp) {
        tmp = conv_codeset_strdup(textdata, CS_UTF_8,
                                  conv_get_locale_charset_str_no_utf8());
    }
    if (!tmp) {
        g_warning("Can't convert charset to anything sane");
        tmp = conv_codeset_strdup(textdata, CS_UTF_8, CS_US_ASCII);
    }
    g_free(textdata);

    if (!tmp) {
        privacy_set_error(_("Couldn't convert text data to any sane charset."));
        return 0;
    }
    textdata = g_strdup(tmp);
    g_free(tmp);

    if ((err = gpgme_new(&data->ctx)) != GPG_ERR_NO_ERROR) {
        debug_print(("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
        privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err));
        g_free(textdata);
        return 0;
    }
    gpgme_set_textmode(data->ctx, 1);
    gpgme_set_armor(data->ctx, 1);

    gpgme_data_new_from_mem(&plain, textdata, (size_t)strlen(textdata), 1);
    gpgme_data_new(&cipher);

    data->sigstatus = sgpgme_verify_signature(data->ctx, plain, NULL, cipher);

    gpgme_data_release(plain);
    gpgme_data_release(cipher);

    g_free(textdata);

    return 0;
}
Пример #13
0
gint xml_parse_next_tag(XMLFile *file)
{
	gchar buf[XMLBUFSIZE];
	gchar *bufp = buf;
	gchar *tag_str;
	XMLTag *tag;
	gint len;

next:
	if (file->is_empty_element == TRUE) {
		file->is_empty_element = FALSE;
		xml_pop_tag(file);
		return 0;
	}

	if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
		g_warning("xml_parse_next_tag(): Can't parse next tag  in %s", file->path);
		return -1;
	}

	len = strlen(buf);

	/* end-tag */
	if (buf[0] == '/') {
		if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
			g_warning("xml_parse_next_tag(): Tag name mismatch in %s : %s (%s)", file->path, buf, xml_get_current_tag(file)->tag);
			return -1;
		}
		xml_pop_tag(file);
		return 0;
	}

	if (len >= 7 && !strncmp(buf, "!-- ", 4) && !strncmp(buf+len-3, " --", 3)) {
		/* skip comment */
		goto next;
	}

	tag = xml_tag_new(NULL);
	xml_push_tag(file, tag);

	if (len > 0 && buf[len - 1] == '/') {
		file->is_empty_element = TRUE;
		buf[len - 1] = '\0';
		g_strchomp(buf);
	}
	
	if (strlen(buf) == 0) {
		g_warning("xml_parse_next_tag(): Tag name is empty in %s", file->path);
		return -1;
	}

	while (*bufp != '\0' && !g_ascii_isspace(*bufp)) bufp++;
	if (*bufp == '\0') {
		if (file->need_codeconv) {
			tag_str = conv_codeset_strdup(buf, file->encoding, CS_INTERNAL);
			if (tag_str) {
				tag->tag = XML_STRING_ADD(tag_str);
				g_free(tag_str);
			} else
				tag->tag = XML_STRING_ADD(buf);
		} else
			tag->tag = XML_STRING_ADD(buf);
		return 0;
	} else {
		*bufp++ = '\0';
		if (file->need_codeconv) {
			tag_str = conv_codeset_strdup(buf, file->encoding, CS_INTERNAL);
			if (tag_str) {
				tag->tag = XML_STRING_ADD(tag_str);
				g_free(tag_str);
			} else
				tag->tag = XML_STRING_ADD(buf);
		} else
			tag->tag = XML_STRING_ADD(buf);
	}

	/* parse attributes ( name=value ) */
	while (*bufp) {
		XMLAttr *attr;
		gchar *attr_name;
		gchar *attr_value;
		gchar *utf8_attr_name;
		gchar *utf8_attr_value;
		gchar *p;
		gchar quote;

		while (g_ascii_isspace(*bufp)) bufp++;
		attr_name = bufp;
		if ((p = strchr(attr_name, '=')) == NULL) {
			g_warning("xml_parse_next_tag(): Syntax error in %s, tag (a) %s", file->path, attr_name);
			return -1;
		}
		bufp = p;
		*bufp++ = '\0';
		while (g_ascii_isspace(*bufp)) bufp++;

		if (*bufp != '"' && *bufp != '\'') {
			g_warning("xml_parse_next_tag(): Syntax error in %s, tag (b) %s", file->path, bufp);
			return -1;
		}
		quote = *bufp;
		bufp++;
		attr_value = bufp;
		if ((p = strchr(attr_value, quote)) == NULL) {
			g_warning("xml_parse_next_tag(): Syntax error in %s, tag (c) %s", file->path, attr_value);
			return -1;
		}
		bufp = p;
		*bufp++ = '\0';

		g_strchomp(attr_name);
		xml_unescape_str(attr_value);
		if (file->need_codeconv) {
			utf8_attr_name = conv_codeset_strdup
				(attr_name, file->encoding, CS_INTERNAL);
			utf8_attr_value = conv_codeset_strdup
				(attr_value, file->encoding, CS_INTERNAL);
			if (!utf8_attr_name)
				utf8_attr_name = g_strdup(attr_name);
			if (!utf8_attr_value)
				utf8_attr_value = g_strdup(attr_value);

			attr = xml_attr_new(utf8_attr_name, utf8_attr_value);
			g_free(utf8_attr_value);
			g_free(utf8_attr_name);
		} else {
			attr = xml_attr_new(attr_name, attr_value);
		}
		xml_tag_add_attr(tag, attr);

	}
	tag->attr = g_list_reverse(tag->attr);

	return 0;
}
Пример #14
0
gchar *unmime_header(const gchar *encoded_str, gboolean addr_field)
{
	const gchar *p = encoded_str;
	const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
		    *eword_end_p;
	gchar charset[32];
	gchar encoding;
	gchar *conv_str;
	GString *outbuf;
	gchar *out_str;
	gsize out_len;
	int in_quote = FALSE;

	outbuf = g_string_sized_new(strlen(encoded_str) * 2);

	while (*p != '\0') {
		gchar *decoded_text = NULL;
		const gchar *quote_p;
		gint len;

		eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
		if (!eword_begin_p) {
			g_string_append(outbuf, p);
			break;
		}
		
		quote_p = p;
		while ((quote_p = strchr(quote_p, '"')) != NULL) {
			if (quote_p && quote_p < eword_begin_p) {
				/* Found a quote before the encoded word. */
				in_quote = !in_quote;
				quote_p++;
			}
			if (quote_p >= eword_begin_p)
				break;
		}

		encoding_begin_p = strchr(eword_begin_p + 2, '?');
		if (!encoding_begin_p) {
			g_string_append(outbuf, p);
			break;
		}
		text_begin_p = strchr(encoding_begin_p + 1, '?');
		if (!text_begin_p) {
			g_string_append(outbuf, p);
			break;
		}
		eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
		if (!eword_end_p) {
			g_string_append(outbuf, p);
			break;
		}

		if (p == encoded_str) {
			g_string_append_len(outbuf, p, eword_begin_p - p);
			p = eword_begin_p;
		} else {
			/* ignore spaces between encoded words */
			const gchar *sp;

			for (sp = p; sp < eword_begin_p; sp++) {
				if (!g_ascii_isspace(*sp)) {
					g_string_append_len
						(outbuf, p, eword_begin_p - p);
					p = eword_begin_p;
					break;
				}
			}
		}

		len = MIN(sizeof(charset) - 1,
			  encoding_begin_p - (eword_begin_p + 2));
		memcpy(charset, eword_begin_p + 2, len);
		charset[len] = '\0';
		encoding = g_ascii_toupper(*(encoding_begin_p + 1));

		if (encoding == 'B') {
			gchar *tmp;
			tmp = g_strndup(text_begin_p + 1, eword_end_p - (text_begin_p + 1) + 1);
			decoded_text = g_base64_decode(tmp, &out_len);
			g_free(tmp);
		} else if (encoding == 'Q') {
			decoded_text = g_malloc
				(eword_end_p - (text_begin_p + 1) + 1);
			len = qp_decode_q_encoding
				(decoded_text, text_begin_p + 1,
				 eword_end_p - (text_begin_p + 1));
		} else {
			g_string_append_len(outbuf, p, eword_end_p + 2 - p);
			p = eword_end_p + 2;
			continue;
		}

		/* An encoded word MUST not appear within a quoted string,
		 * so quoting that word after decoding should be safe.
		 * We check there are no quotes just to be sure. If there
		 * are, well, the comma won't pose a problem, probably.
		 */
		if (addr_field && strchr(decoded_text, ',') && !in_quote &&
		    !strchr(decoded_text, '"')) {
			gchar *tmp = g_strdup_printf("\"%s\"", decoded_text);
			g_free(decoded_text);
			decoded_text = tmp;
		}

		/* convert to UTF-8 */
		conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
		if (!conv_str || !g_utf8_validate(conv_str, -1, NULL)) {
			g_free(conv_str);
			conv_str = g_malloc(len + 1);
			conv_utf8todisp(conv_str, len + 1, decoded_text);
		}
		g_string_append(outbuf, conv_str);
		g_free(conv_str);

		g_free(decoded_text);

		p = eword_end_p + 2;
	}
	
	out_str = outbuf->str;
	out_len = outbuf->len;
	g_string_free(outbuf, FALSE);

	return g_realloc(out_str, out_len + 1);
}