示例#1
0
/** @short Produce a parameter for a MIME message header */
QByteArray encodeRfc2231Parameter(const QByteArray &key, const QString &value)
{
    if (value.isEmpty())
        return key + "=\"\"";

    bool safeAscii = true;

    // Find "dangerous" characters
    for (int i = 0; i < value.size(); ++i) {
        if (rfc2311NeedsEscaping(value[i].unicode())) {
            safeAscii = false;
            break;
        }
    }

    if (safeAscii)
        return key + '=' + value.toUtf8();

    QByteArray res = key + "*=\"utf-8''";
    QByteArray encoded = value.toUtf8();
    for (int i = 0; i < encoded.size(); ++i) {
        char unicode = encoded[i];
        if (rfc2311NeedsEscaping(unicode))
            res += toHexChar(unicode, '%');
        else
            res += unicode;
    }
    return res + '"';
}
示例#2
0
文件: otDebug.cpp 项目: GYGit/oneteam
void
otDebugDumpData(const unsigned char *data, PRInt32 len)
{
  PRInt32 i;
  char formatedString[72], *hexPart = formatedString, *charPart = formatedString+55;

  memset(formatedString, ' ', sizeof(formatedString));
  formatedString[71] = '\0';
  formatedString[12] = formatedString[26] = formatedString[40] = '|';

  for (i = 0; ; i++, data++, charPart++, hexPart+=3) {
    if ((i % 4) == 0)
      hexPart+=2;

    if ((i % 16) == 0) {
      if (i > 0)
        PR_LogPrint("%06x  %s\n", i-16, formatedString);
      hexPart = formatedString;
      charPart = formatedString+55;
      if (i >= len)
        return;
    }

    if (i < len) {
      hexPart[0] = toHexChar((data[0] >> 4));
      hexPart[1] = toHexChar(data[0] & 0xf);
      charPart[0] = (data[0] < 0x1f || data[0] > 0x7f) ? '.' : data[0];
    } else {
char * fromDec(int n, char form) {
    int base = DEC_BASE;
    if (form == 'x')
        base = HEX_BASE;
    else if (form == 'o')
        base = OCT_BASE;
    else if (form == 'b')
        base = BIN_BASE;
    
    char *result = (char *) calloc(1, sizeof(char));
    char *digit = (char *) calloc(1, sizeof(char));
    int remainder;

    char *neg = "";

    if (n < 0) {
        n = (~n)+1;
        neg = "-";
    }

    while (n) {
        remainder = n % base;
        if (base == HEX_BASE && remainder >= 10) {
            digit[0] = toHexChar(remainder);
        }
        else {
            digit[0] = tochar(remainder);
        }

        strcat(result, digit);
        n /= base;
    }
    printf("%s%c", neg, form);
    return strrev(result);
}
示例#4
0
QByteArray encodeRFC2047String(const QString &text, const Rfc2047StringCharacterSetType charset)
{
    // We can't allow more than 75 chars per encoded-word, including the boiler plate (7 chars and the size of the encoding spec)
    // -- this is defined by RFC2047.
    int maximumEncoded = 75 - 7;
    QByteArray encoding;
    if (charset == RFC2047_STRING_UTF8)
        encoding = "utf-8";
    else
        encoding = "iso-8859-1";
    maximumEncoded -= encoding.size();

    // If this is an encodedWord, we need to include any whitespace that we don't want to lose
    if (charset == RFC2047_STRING_UTF8) {
        QByteArray res;
        int start = 0;

        while (start < text.size()) {
            // as long as we have something to work on...
            int size = maximumEncoded;
            QByteArray candidate;

            // Find the character boundary at which we have to split the input.
            // Remember that we're iterating on Unicode codepoints now, not on raw bytes.
            while (true) {
                candidate = text.mid(start, size).toUtf8();
                int utf8Size = candidate.size();
                int base64Size = utf8Size * 4 / 3 + utf8Size % 3;
                if (base64Size <= maximumEncoded) {
                    // if this chunk's size is small enough, great
                    QByteArray encoded = candidate.toBase64();
                    if (!res.isEmpty())
                        res.append("\r\n ");
                    res.append("=?utf-8?B?" + encoded + "?=");
                    start += size;
                    break;
                } else {
                    // otherwise, try with something smaller
                    --size;
                    Q_ASSERT(size >= 1);
                }
            }
        }
        return res;
    } else {
        QByteArray buf = "=?" + encoding + "?Q?";
        int i = 0;
        int currentLineLength = 0;
        while (i < text.size()) {
            QByteArray symbol;
            const ushort unicode = text[i].unicode();
            if (unicode == 0x20) {
                symbol = "_";
            } else if (!rfc2047QPNeedsEscpaing(unicode)) {
                symbol += text[i].toLatin1();
            } else {
                symbol = toHexChar(unicode, '=');
            }
            currentLineLength += symbol.size();
            if (currentLineLength > maximumEncoded) {
                buf += "?=\r\n =?" + encoding + "?Q?";
                currentLineLength = 0;
            }
            buf += symbol;
            ++i;
        }
        buf += "?=";
        return buf;
    }
}