[2019-02-23 15:38:33 最后更新]
[golang]quoted-printable 解码函数
我手头的 golang 1.7.3 的 mime 有很多错误,一般都是对各种编码特例考虑不周。刚好我又是老牌的 email 开发人员 ... 咳咳,低调低调。
所以由我提供一个可用的 [golang]quoted-printable 解码函数吧
--------------------------------------------------
很忙,家里的事也很糟糕,所以改天再传到 github 了。
--------------------------------------------------
// qDecode decodes a Q encoded string.
func qDecode(s string) ([]byte, error) {
dec := make([]byte, len(s))
n := 0
for i := 0; i < len(s); i++ {
switch c := s[i]; {
case c == '_':
dec[n] = ' '
case c == '=':
if i+2 >= len(s) {
fmt.Println("quoted-printable 解码失败3!"); //clq add//这个应该是长度不足够
return nil, errInvalidWord
}
//--------------------------------------------------
//clq 根据我 eemail 中修改的 mail2000.pas 的经验,这里应该忽略掉三种软件回车的情况
//function DecodeQuoted(pSrc:pchar):string;
//if (strncmp(pSrc, "=\r\n", 3) == 0) //软件回车,跳过
//if (StrLComp(pSrc, '='#13#10, 3) = 0) then
//if (StrLComp(pSrc, '='#13, 2) = 0) then
//if (StrLComp(pSrc, '='#10, 2) = 0) then
if (s[i+1] == '\r' && s[i+2] == '\n') {
i += 2;
continue;
}
if (s[i+1] == '\r') {
i += 1;
continue;
}
if (s[i+2] == '\n') {
i += 1;
continue;
}
//--------------------------------------------------
b, err := readHexByte(s[i+1], s[i+2])
if err != nil {
fmt.Println("quoted-printable 解码失败1!"); //clq add //等号后面两个刚好是回车的时候就会出错,这个函数的设计明显有问题
//或者是没有考虑回车和其他空白字符的问题
fmt.Println("s[i+1] ", s[i+1]);
fmt.Println("s[i+2] ", s[i+2]);
return nil, err
}
dec[n] = b
i += 2
case (c <= '~' && c >= ' ') || c == '\n' || c == '\r' || c == '\t':
dec[n] = c
default:
fmt.Println("quoted-printable 解码失败2!"); //clq add
return nil, errInvalidWord
}
n++
}
return dec[:n], nil
}