How to decode q-encoding in C?

倖福魔咒の 提交于 2019-12-12 11:31:38

问题


Is there any library for q-encoding? I need to decode some q-encoded text, such as:

**Subject: =?iso-8859-1?Q?=A1Hola,_se=F1or!?=**

回答1:


GNU Mailutils libmailutils is one example of such library.

"Q"-encoding is specified by RFC 2047 so using it as a search term gives you other relevant results.




回答2:


Email subject is encoded according to RFC 2047. We can decode it by using function mu_rfc2047_decode() provided by GNU mailutils. Example:

#include <stdio.h>
#include <stdlib.h>
#include <mailutils/mailutils.h>
#include <mailutils/mime.h>
...
char cipher[] = "=?GB2312?B?UmWjujEy1MK8xruuse0=?=";
char *plaintext;
int rc = mu_rfc2047_decode("utf-8", cipher, &plaintext);
if (rc) {
    fprintf(stderr, "Fail to decode '%s'\n", cipher);
} else {
    puts(plaintext); 
    free(plaintext);
}

To download GNU mailutils, visit https://mailutils.org/

To understand RFC 2047, read https://www.ietf.org/rfc/rfc2047.txt

Test Result:

Cipher:
**Subject: =?iso-8859-1?Q?=A1Hola,_se=F1or!?=**
Plaintext:
**Subject: ¡Hola, señor!**

Cipher:
=?Big5?Q?=AE=F8=B6O=BA=A18=A4d=BFW=AEa?=
Plaintext:
消費滿8千獨家

Cipher:
=?GB2312?B?UmWjujEy1MK8xruuse0=?=
Plaintext:
Re:12月计划表



回答3:


Not sure about Q-encoding libraries, couldn't find any.

Do note, though, that your latter example does not look like Q encoding, notice that the character after the charset ("UTF-8") is not a 'Q' but a 'B'. This means it's base64 encoding, for which there are plenty of libraries, glib is one example.

See MIME on Wikipedia for details on how to detect which encoding is being used.



来源:https://stackoverflow.com/questions/8701269/how-to-decode-q-encoding-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!