SOL9 2.0 Class: QPEncoder

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * QPEncoder.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#pragma once

// SOL9
// 2008/10/10
//

#include <sol\Object.h>
#include <sol\Bytes.h>
#include <sol\String.h>
#include <sol\StringT.h>

namespace SOL {

class QPEncoder :public Object {

public:
    /**
     * Constructor
     */
    QPEncoder() {

    }

public:
    /*
     * Convert byte array to QPEncoded string.
     * @param bytes        [in]    Byte array of data to be encoded.
     * @param string    [out]    QPEncoed string.
     */
    
    int encode(__in Bytes& bytes, __out String& string) {
        int rc = 0;
        char* encoded = NULL;
        int len = encode(bytes.getContents(), bytes.getSize(), &encoded);
        if (len>0) {
            rc = len;
            string = encoded;
            delete [] encoded;
        }
        return rc;
    }

public:
    int encode(__in Bytes& bytes, __out StringT<char>& string) {
        int rc = 0;
        char* encoded = NULL;
        int len = encode(bytes.getContents(), bytes.getSize(), &encoded);
        if (len>0) {
            rc = len;
            string.shallowCopy(encoded);
        }
        return rc;
    }

public:
    /**
     * Encode a byte-array in QuotedPritable format, and return the encoded string (NULL-terminated)
     * to the parameter *enc. Caller should delete the enc after use.
     *
     * @param in [in]    Byte array of data to be encoded.
     * @param enc        QPEncoded NULL-terminated string.
     */
    int encode(unsigned char* in, int len, char** enc)
    {
        int n = 0;

        *enc = NULL;
        if (in == NULL || len<=0) {
            return n;
        }

        static int hex[] = {
        '0','1','2','3','4','5','6','7','8','9',
        'A','B','C','D','E','F'};

        int slen = len*3 + 3;     
        char* out = new char[slen];
        //Make out to be a NULL-terminated string.
        memset(out, 0, slen);

        int ct = 0, prevc = 255;

        for (int i = 0; i<len; i++) {
            int c = in[i];

            if ((c < 32 && (c != '\n' && c != '\t'))
                || (c == '=')
                || (c >= 127)
                || (ct == 0 && c == '.')) {

                out[n++] = '=';
                out[n++] = hex[c >> 4];
                out[n++] = hex[c & 0xf];
                ct += 3;
                prevc = 'A'; 

            } else if (c == '\n') {
                //If c were a new line.
                if (prevc == ' ' || prevc == '\t') {
                    out[n++] = '=';
                    out[n++] = c;
                }
                out[n++] = c;
                ct = 0;
                prevc = c;
            } else {
                out[n++] = c;
                ++ct;
                prevc = c;
            }
            if (ct > 72) {
                out[n++] = '=';
                out[n++] = '\n';
                ct = 0;        
                prevc = '\n';
            }
        }

        if (ct != 0) {
            out[n++] = '=';
            out[n++] = '\n';
        }

        *enc = out;
        
        return n;
    }

};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.