Skip to main content
November 13, 2015
Question

how to parse the string with /Filter[/ASCII85Decode /LZWDecode]

  • November 13, 2015
  • 0 replies
  • 1566 views

In Linux, when developing a postscript printer driver, I get the postscript file, it contains some strings like this:

5 0 obj

<</Filter[/ASCII85Decode

/LZWDecode]/Length 726>>stream

J/grZ!H>tg",&r_d#e?fg'SZ`7Df.n`+CqG$q(7C4@1>.Maep],<.+2$60G.Ek,F1;_46Q@PHaA

#cJ(HPbQ##d88(!K#-S9!Gj4q3J`[7UN(88%TJ4La+FDCph2Ob<5p0ALsi79)-"UCkeQ8#9cV[&

A)V92Z7m1-Mkra>H42YB'b#VmOMGX4X:-RgCR*6\0c1BD(TZXkDE\rrC3TPu#[rCsh*rVo<(@gS

*"Rf,3rSoP3?J]POA.FG`[r$eXOPY"9S7l)f'BnO#f).UdZ#h>lXn;b3?!ikH4b'7C!*qoDPGO?

fPN_gNA!6&mSbEul!a*A^?/42QHWdB);a!qZp#rXJR+]ip@hK/r'"$9[Ps#p3R0sghmI-eKW2+*

a;!nrC#EOh0oD?Sr@Z3.+rEXbN%5/tiIHgn+tL:ZWJJooA@sf4*&.d]alGq07?4uBqo02oCrJgs

]JB:OYNU6Ma,uS>WC@I\Z5BW]:g1qpm_EHYa6O^?fnU`hI^Q0"/"lgK!PGf^Xt6Z'^TFT$@dq>&

;u455m#'Z?_,+rh)DUa1P8?%@'$13_ZR!KQ_,<Zu*sOO%j;LV[o8*n\Lt(S,ZYqsT_?HX7bg=HE

UM:XR&l<<_UJV`r(q/^OW3h:2r"Q&)%kEIA\F(992_r`Q$l7BL[%BI\N.\jo#,.;X":*KkNZ:[-

K64:SNU&qs`O(#Ar6^<_f$gaW]Y?]c_CBFOJQDmS~>

endstream

endobj

I want to parse the string with /Filter[/ASCII85Decode /LZWDecode], my decode programm is shown below, but the result is incorrect, I want to know is my program has problem?

And should I how to parse the string above?

int Ascii85DecodeBlock( unsigned char *ascii85_p, const unsigned char *raw_p, int raw_l )
{
    // ascii85_p = new unsigned char[raw_l];

    int len = 0;
    int count = 0;
    bool processChar = false;
    unsigned int _tuple = 0;
    const int _asciiOffset = 33;
    unsigned int pow85[] = { 85*85*85*85, 85*85*85, 85*85, 85, 1 };
    unsigned char _decodedBlock[4];

    for (int i = 0; i < raw_l; ++i) // char c in s)
    {
        unsigned char c = raw_p;
        switch (c)
        {
        case 'z':
            if (count != 0)
            {
                // throw new Exception("The character 'z' is invalid inside an ASCII85 block.");
            }
            _decodedBlock[0] = 0;
            _decodedBlock[1] = 0;
            _decodedBlock[2] = 0;
            _decodedBlock[3] = 0;
            // ms.Write(_decodedBlock, 0, _decodedBlock.Length);
            for (int j = 0; j < 4; ++j)
            {
                ascii85_p[len++] = _decodedBlock;
            }
            processChar = false;
            break;
        case '\n': case '\r': case '\t': case '\0': case '\f': case '\b':
            processChar = false;
            break;
        default:
            if (c < '!' || c > 'u')
            {
                // throw new Exception("Bad character '" + c + "' found. ASCII85 only allows characters '!' to 'u'.");
            }
            processChar = true;
            break;
        }

        if (processChar)
        {
            _tuple += ((uint)(c - _asciiOffset) * pow85[count]);
            count++;
            if (count == 5)
            {
                for (int j = 0; j < 4; j++)
                {
                    _decodedBlock = (unsigned char)(_tuple >> (24 - (j * 8)));
                }

                for (int j = 0; j < 4; ++j)
                {
                    ascii85_p[len++] = _decodedBlock;
                }
                _tuple = 0;
                count = 0;
            }
        }
    }

    // if we have some bytes left over at the end..
    if (count != 0)
    {
        if (count == 1)
        {
            // throw new Exception("The last block of ASCII85 data cannot be a single byte.");
        }
        count--;
        _tuple += pow85[count];
        for (int j = 0; j < count; j++)
        {
            _decodedBlock = (unsigned char)(_tuple >> (24 - (j * 8)));
        }
        for (int j = 0; j < count; j++)
        {
            ascii85_p[len++] = _decodedBlock;
        }
    }
    return len;
}

int lzwDecode(unsigned char *lzwdata, unsigned int *raw_p, int raw_l)// 译码;
{
    /*cout<<"Input num of decode:";
    cin>>len;
    cout<<endl;
    cout<<"Input string of decode:"<<endl;
    for(int j=0;j<len;j++)
    {
        cin>>a;
    }
    n=128;
    cout<<endl;
    cout<<"Result:"<<endl<<endl;*/

    unsigned char h[2];
    unsigned char p[100];
    int n = 256;
    cout<<Dictionary[raw_p[0]];
    strcat((char*)lzwdata, (char*)Dictionary[raw_p[0]]);
    strcpy((char*)p, (char*)Dictionary[raw_p[0]]);
    for(int i = 1; i < raw_l; i++)
    {
        if(Dictionary[raw_p][0])
        {
            cout<<Dictionary[raw_p];
            strcat((char*)lzwdata, (char*)Dictionary[raw_p]);
            h[0]=Dictionary[raw_p][0];
            strcpy((char*)Dictionary,strcat((char*)p,(char*)h));
            strcpy((char*)p,(char*)Dictionary[raw_p]);
            n++;
        }
        else
        {
            h[0]=p[0];
            strcpy((char*)Dictionary,strcat((char*)p,(char*)h));
            cout<<Dictionary;
            strcat((char*)lzwdata, (char*)Dictionary);
            strcpy((char*)p,(char*)Dictionary[raw_p]);
            n++;
        }
    }
    cout<<endl;
    return strlen((char*)lzwdata);
}

This topic has been closed for replies.