예제 #1
0
def expand(e):
    '''
        This will expand an indirect_object_stream and modify it in place
        It may delete the Filter, DecodeParams, JBIG2Globals keywords of 
        the dictionary
    '''
    dictionary_py = xmlToPy(e[0])
    if not 'Filter' in dictionary_py.keys():
        logger.info(
            'A filteres/compressed stream shall have the Filter key. obj %s already expanded?',
            payload(e))
        return True
    filters = dictionary_py['Filter']
    params = dictionary_py.get('DecodeParms', None)
    assert any([
        type(filters) == list and (type(params) == list or params == None),
        type(filters) != list and (type(params) == dict or params == None)
    ]), 'Filter/DecodeParms wrong type'

    if type(filters) != list:
        filters = [filters]
        params = params and [params] or [{}]

    if params == None:
        params = [{}] * len(filters)

    assert all([type(x) == str for x in filters]), 'Filter shall be a names'
    assert all([type(x) == dict
                for x in params]), 'Params shoulb be a dictionary.. or null?'
    assert len(filters) == len(
        params), 'Number of Decodeparams should match Filters'
    if len(
            set(['DCTDecode', 'CCITTFaxDecode', 'JPXDecode', 'JBIG2Decode'
                 ]).intersection(set(filters))) > 0:
        return False
    #Expand/defilter data
    data = payload(e[1])
    try:
        for filtername, param in zip(filters, params):
            data = defilterData(filtername, data, param)
        setpayload(e[1], data)

        #remove /Filter and /DecodeParms from stream dictionary
        for rem in e[0].xpath(
                './dictionary_entry/*[position()=1 and @payload=enc("Filter") or @payload=enc("DecodeParms")]/..'
        ):
            e[0].remove(rem)

        return True
    except Exception, ex:
        logger.error(
            'Error defiltering data with %s(%s). Exception: %s. Saving error stream on %s.error'
            % (filtername, params, ex, filtername))
        file('%s.error' % filtername, 'w').write(data)
예제 #2
0
    def popFilter(self):
        dictionary = self.dictionary
        assert dictionary.has_key('Filter'), 'Stream not Filtered!'

        selected_filter = None
        selected_params = None
        
        deletion_list = []
        
        
        if dictionary['Length'].value != len(self.data.value):
            logger.info("Length field of object %s does not match the actual data size (%d != %d)"%(str(self.get_numgen()),dictionary['Length'].value,len(self.data.value)))
        
        if type(dictionary['Filter']) == PDFArray:
            selected_filter = dictionary['Filter'][0]
            del dictionary['Filter'][0]
            if dictionary.has_key('DecodeParms'):
                assert dictionary['DecodeParms'] == PDFArray, 'Array of filters need array of decoding params'
                selected_params = dictionary['DecodeParms'][0]
                deletion_list.append((dictionary['DecodeParms'],0))
                #del dictionary['DecodeParms'][0]
        else:
            selected_filter = dictionary['Filter']
            del dictionary['Filter']
            if dictionary.has_key('DecodeParms'):
                selected_params = dictionary['DecodeParms']
                deletion_list.append((dictionary, 'DecodeParms'))
                #del dictionary['DecodeParms']
            
        if dictionary.has_key('Filter') and \
           type(dictionary['Filter']) == PDFArray and \
           len(dictionary['Filter']) == 0:
                deletion_list.append((dictionary, 'Filter'))
                #del dictionary['Filter']
        if dictionary.has_key('DecodeParms') and \
               type(dictionary['DecodeParms']) == PDFArray and \
               len(dictionary['DecodeParms']) == 0:
                deletion_list.append((dictionary, 'DecodeParms'))
                #del dictionary['DecodeParms']
        #FIX recode defilterData .. make it register/unregister able. 
        #(think /Crypt 7.4.10 Crypt Filter )
        self.data.value = defilterData(selected_filter.value,self.data.value, selected_params and selected_params.value or selected_params)
        for v,i in deletion_list:
            del v[i]
        dictionary['Length'].value = len(self.data.value)
예제 #3
0
파일: __init__.py 프로젝트: feliam/opaf
def expand(e):
    '''
        This will expand an indirect_object_stream and modify it in place
        It may delete the Filter, DecodeParams, JBIG2Globals keywords of 
        the dictionary
    '''
    dictionary_py = xmlToPy(e[0])
    if not 'Filter' in dictionary_py.keys():
        logger.info( 'A filteres/compressed stream shall have the Filter key. obj %s already expanded?',payload(e))
        return True
    filters = dictionary_py['Filter']
    params = dictionary_py.get('DecodeParms',None)
    assert any([type(filters) == list and (type(params) == list or params==None ),
                type(filters) != list and (type(params) == dict or params==None ) ]), 'Filter/DecodeParms wrong type'

    if type(filters) != list:
        filters=[filters]
        params=params and [params] or [{}]

    if params == None:
        params = [{}]*len(filters)
        
    assert all([type(x)==str for x in filters]), 'Filter shall be a names'
    assert all([type(x)==dict for x in params]), 'Params shoulb be a dictionary.. or null?'
    assert len(filters) == len(params),'Number of Decodeparams should match Filters'
    if len(set(['DCTDecode','CCITTFaxDecode','JPXDecode','JBIG2Decode']).intersection(set(filters)))>0:
        return False
    #Expand/defilter data
    data = payload(e[1])
    try:
        for filtername,param in zip(filters,params):
            data = defilterData(filtername,data, param)
        setpayload(e[1],data)   

        #remove /Filter and /DecodeParms from stream dictionary
        for rem in e[0].xpath('./dictionary_entry/*[position()=1 and @payload=enc("Filter") or @payload=enc("DecodeParms")]/..'):
            e[0].remove(rem)

        return True
    except Exception,ex:
        logger.error('Error defiltering data with %s(%s). Exception: %s. Saving error stream on %s.error'%(filtername, params, ex, filtername))
        file('%s.error'%filtername,'w').write(data)