Changeset 702

Show
Ignore:
Timestamp:
01/24/06 13:30:47 (3 years ago)
Author:
laotzu
Message:

Add ASCII85 decode

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pypdf/trunk/pyPdf/filters.py

    r701 r702  
    9797    decode = staticmethod(decode) 
    9898 
     99class ASCII85Decode(object): 
     100    def decode(data, decodeParms=None): 
     101        retval = "" 
     102        group = [] 
     103        x = 0 
     104        hitEod = False 
     105        while not hitEod: 
     106            c = data[x] 
     107            if len(retval) == 0 and c == "<" and data[x+1] == "~": 
     108                x += 2 
     109                continue 
     110            elif c.isspace(): 
     111                x += 1 
     112                continue 
     113            elif c == 'z': 
     114                assert len(group) == 0 
     115                retval += '\x00\x00\x00\x00' 
     116                continue 
     117            elif c == "~" and data[x+1] == ">": 
     118                if len(group) != 0: 
     119                    # cannot have a final group of just 1 char 
     120                    assert len(group) > 1 
     121                    cnt = len(group) - 1 
     122                    group += [ 85, 85, 85 ] 
     123                    hitEod = cnt 
     124                else: 
     125                    break 
     126            else: 
     127                c = ord(c) - 33 
     128                assert c >= 0 and c < 85 
     129                group += [ c ] 
     130            if len(group) >= 5: 
     131                b = group[0] * (85**4) + \ 
     132                    group[1] * (85**3) + \ 
     133                    group[2] * (85**2) + \ 
     134                    group[3] * 85 + \ 
     135                    group[4] 
     136                assert b < (2**32 - 1) 
     137                c4 = chr((b >> 0) % 256) 
     138                c3 = chr((b >> 8) % 256) 
     139                c2 = chr((b >> 16) % 256) 
     140                c1 = chr(b >> 24) 
     141                retval += (c1 + c2 + c3 + c4) 
     142                if hitEod: 
     143                    retval = retval[:-4+hitEod] 
     144                group = [] 
     145            x += 1 
     146        return retval 
     147    decode = staticmethod(decode) 
     148 
    99149def decodeStreamData(stream): 
    100150    filters = stream.get("/Filter", ()) 
     
    115165if __name__ == "__main__": 
    116166    assert "abc" == ASCIIHexDecode.decode('61\n626\n3>') 
     167 
     168    ascii85Test = """ 
     169     <~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!, 
     170     O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY 
     171     i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa 
     172     l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G 
     173     >uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c~> 
     174    """ 
     175    ascii85_originalText="Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure." 
     176    assert ASCII85Decode.decode(ascii85Test) == ascii85_originalText