Subversion Repositories Scribus

Rev

Rev 22521 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11551 jghali 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
8
#include "scstreamfilter_ascii85.h"
9
#include "util.h"
10
 
11
ScASCII85EncodeFilter::ScASCII85EncodeFilter(QDataStream* stream)
12
                                         : ScStreamFilter(stream)
13
{
14
}
15
 
16
ScASCII85EncodeFilter::ScASCII85EncodeFilter(ScStreamFilter* filter)
17
                                         : ScStreamFilter(filter)
18
{
19
}
20
 
22521 craig 21
bool ScASCII85EncodeFilter::openFilter ()
11551 jghali 22
{
23
        m_buffer_pending     = 0;
24
        m_four_tuple_pending = 0;
25
        m_buffer.resize(65536);
26
        if (m_buffer.size() <= 0)
27
                return false;
28
        return ScStreamFilter::openFilter();
29
}
30
 
22521 craig 31
bool ScASCII85EncodeFilter::closeFilter()
11551 jghali 32
{
33
        bool success = true;
34
        if (m_buffer_pending)
35
        {
36
                char* writeBuffer = m_buffer.data();
37
                writeBuffer[m_buffer_pending] = 0;
38
                success &= writeDataInternal((const char*) writeBuffer, m_buffer_pending);
39
        }
40
        if (m_four_tuple_pending)
41
        {
42
                quint32 value;
43
                const char* ascii85;
44
                unsigned char five_tuple[6];
45
                bool allZero;
46
 
47
                memset (m_four_tuple + m_four_tuple_pending, 0, 4 - m_four_tuple_pending);
48
                value   = m_four_tuple[0] << 24 | m_four_tuple[1] << 16 | m_four_tuple[2] << 8 | m_four_tuple[3];
49
                ascii85 = toAscii85(value, allZero);
50
                memcpy (five_tuple, ascii85, 5);
51
                five_tuple[m_four_tuple_pending + 1] = 0;
52
                int five_tuple_len  = strlen((const char* ) five_tuple);
53
                success &= writeDataInternal((const char* ) five_tuple, five_tuple_len);
54
        }
55
        success &= writeDataInternal("~>\n", 3);
56
        success &= ScStreamFilter::closeFilter();
57
        return success;
58
}
59
 
60
bool ScASCII85EncodeFilter::writeData(const char* data, int dataLen)
61
{
62
        bool writeSuccess = true;
63
        int  lineLength = 0;
64
        bool allZero  = true;
65
        const char* ascii85;
66
        quint32 value;
67
 
68
        int maxWrite = m_buffer.size() - (m_buffer.size() % 5) - 5;
69
        if (maxWrite <= 5) return 0;
70
 
71
        const unsigned char *ptr   = (const unsigned char*) data;
72
        unsigned char* writeBuffer = (unsigned char*) m_buffer.data();
73
        unsigned char* ptrw = writeBuffer + m_buffer_pending;
74
 
75
        while (dataLen)
76
        {
77
                m_four_tuple[m_four_tuple_pending++] = *ptr++;
78
                dataLen--;
79
                if (m_four_tuple_pending == 4)
80
                {
81
                        value   = m_four_tuple[0] << 24 | m_four_tuple[1] << 16 | m_four_tuple[2] << 8 | m_four_tuple[3];
82
                        ascii85 = toAscii85(value, allZero);
83
                        if (allZero)
84
                                *ptrw++ = 'z';
85
                        else
86
                        {
87
                                *ptrw++ = ascii85[0];
88
                                *ptrw++ = ascii85[1];
89
                                *ptrw++ = ascii85[2];
90
                                *ptrw++ = ascii85[3];
91
                                *ptrw++ = ascii85[4];
92
                        }
93
                        lineLength       += ((allZero) ? 1 : 5);
94
                        m_buffer_pending += ((allZero) ? 1 : 5);
95
                        if (lineLength > 75)
96
                        {
97
                                *ptrw++ = '\n';
98
                                ++m_buffer_pending;
99
                                lineLength = 0;
100
                        }
101
                        if (m_buffer_pending > maxWrite)
102
                        {
103
                                writeSuccess &= writeDataInternal((const char*) writeBuffer, m_buffer_pending);
104
                                ptrw = writeBuffer;
105
                                m_buffer_pending = 0;
106
                        }
107
                        m_four_tuple_pending = 0;
108
                }
109
        }
110
        return writeSuccess;
111
}