PDFConsole.py 207 KB
Newer Older
1 2 3 4 5
#
#    peepdf is a tool to analyse and modify PDF files
#    http://peepdf.eternal-todo.com
#    By Jose Miguel Esparza <jesparza AT eternal-todo.com>
#
6
#    Copyright (C) 2011-2014 Jose Miguel Esparza
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#
#    This file is part of peepdf.
#
#        peepdf is free software: you can redistribute it and/or modify
#        it under the terms of the GNU General Public License as published by
#        the Free Software Foundation, either version 3 of the License, or
#        (at your option) any later version.
#
#        peepdf is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
#        GNU General Public License for more details.
#
#        You should have received a copy of the GNU General Public License
#        along with peepdf.    If not, see <http://www.gnu.org/licenses/>.
#

'''
    Implementation of the interactive console of peepdf
'''

28
import cmd, sys, os, re, subprocess, optparse, hashlib, jsbeautifier, traceback
29 30 31 32 33 34
from PDFUtils import *
from PDFCrypto import *
from JSAnalysis import *
from PDFCore import *
from base64 import b64encode,b64decode
from PDFFilters import decodeStream,encodeStream
35 36
from jjdecode import JJDecoder

37 38 39 40 41 42
try:
    from colorama import init, Fore, Back, Style
    COLORIZED_OUTPUT = True
except:
    COLORIZED_OUTPUT = False
try:
43
    import PyV8
44 45 46 47 48 49 50 51 52
    JS_MODULE = True
except ImportError, e:
    JS_MODULE = False
try:
    import pylibemu
    EMU_MODULE = True
except:
    EMU_MODULE = False
    
53 54 55 56 57 58 59 60 61
# The GNU readline function does not handle correctly the colorized (ANSI) prompts, so this is a dirty fix
try:
    import readline
    RL_PROMPT_START_IGNORE = '\001'
    RL_PROMPT_END_IGNORE = '\002'
except:
    RL_PROMPT_START_IGNORE = RL_PROMPT_END_IGNORE = ''
    
# File and variable redirections 
62 63 64 65 66
FILE_WRITE = 1
FILE_ADD = 2
VAR_WRITE = 3
VAR_ADD = 4
newLine = os.linesep
67
errorsFile = 'errors.txt'
68 69 70 71 72 73
filter2RealFilterDict = {'b64':'base64','base64':'base64','asciihex':'/ASCIIHexDecode','ahx':'/ASCIIHexDecode','ascii85':'/ASCII85Decode','a85':'/ASCII85Decode','lzw':'/LZWDecode','flatedecode':'/FlateDecode','fl':'/FlateDecode','runlength':'/RunLengthDecode','rl':'/RunLengthDecode','ccittfax':'/CCITTFaxDecode','ccf':'/CCITTFaxDecode','jbig2':'/JBIG2Decode','dct':'/DCTDecode','jpx':'/JPXDecode'}

class PDFConsole(cmd.Cmd):
    '''
        Class of the peepdf interactive console. To see details about commands: http://code.google.com/p/peepdf/wiki/Commands
    '''
74

75 76
    def __init__(self, pdfFile, vtKey, avoidOutputColors = False, stdin = None):
        global COLORIZED_OUTPUT
77
        cmd.Cmd.__init__(self, stdin = stdin)
78 79 80 81 82 83 84 85
        errorColorizedInit = False
        self.warningColor = ''
        self.errorColor = ''
        self.alertColor = ''
        self.staticColor = ''
        self.resetColor = ''
        if not COLORIZED_OUTPUT or avoidOutputColors:
            self.avoidOutputColors = True
86
        else:
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            try:
                init()
                self.warningColor = Fore.YELLOW
                self.errorColor = Fore.RED
                self.alertColor = Fore.RED
                self.staticColor = Fore.BLUE
                self.promptColor = RL_PROMPT_START_IGNORE + Fore.GREEN + RL_PROMPT_END_IGNORE
                self.resetColor = Style.RESET_ALL
                self.avoidOutputColors = False
            except:
                self.avoidOutputColors = True
                COLORIZED_OUTPUT = False

        if not self.avoidOutputColors:
            self.prompt = self.promptColor + 'PPDF> ' + RL_PROMPT_START_IGNORE + self.resetColor + RL_PROMPT_END_IGNORE
        else:
            self.prompt = 'PPDF> '       
104 105 106 107 108
        self.use_rawinput = True
        if stdin != None:
            self.use_rawinput = False
            self.prompt = '' 
        self.pdfFile = pdfFile
109
        self.variables = {'output_limit':[1000,1000],
110
                          'malformed_options':[[],[]],
111 112 113
                          'header_file':[None,None],
                          'vt_key':[vtKey,vtKey]}
        self.javaScriptContexts = {'global': None}
114 115 116 117
        self.readOnlyVariables = ['malformed_options','header_file']
        self.loggingFile = None
        self.output = None
        self.redirect = None
118
        self.leaving = False
119 120 121 122 123 124 125 126 127 128 129 130
        self.outputVarName = None
        self.outputFileName = None
        
    def emptyline(self):
        return
        
    def precmd(self, line):
        if line == 'EOF':
            return 'exit'
        else:
            return line

131
    def postloop(self):
132 133
    	if self.use_rawinput:
        	print newLine + 'Leaving the Peepdf interactive console...Bye! ;)' + newLine
134 135
        self.leaving = True

136 137 138 139 140 141 142 143
    def do_bytes(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('bytes ' + argv, message)
            return False
        bytes = ''
        args = self.parseArgs(argv)
        if args == None:
144
            message = '*** Error: The command line arguments have not been parsed successfully!!'
145 146 147 148 149 150 151 152
            self.log_output('bytes ' + argv, message)
            return False
        numArgs = len(args)
        if numArgs == 2 or numArgs == 3:
            offset = int(args[0])
            size = int(args[1])
            ret = getBytesFromFile(self.pdfFile.getPath(),offset,size)
            if ret[0] == -1:
153
                message = '*** Error: The file does not exist!!'
154 155 156 157
                self.log_output('bytes ' + argv, message)
                return False
            bytes = ret[1]
            if numArgs == 2:
158
                self.log_output('bytes ' + argv, bytes, [bytes], bytesOutput = True)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            else:
                outputFile = args[2]
                open(outputFile,'wb').write(bytes)
        else:
            self.help_bytes()
                
    def help_bytes(self):
        print newLine + 'Usage: bytes $offset $num_bytes [$file]'
        print newLine + 'Shows or stores in the specified file $num_bytes of the file beginning from $offset' + newLine    

    def do_changelog(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('changelog ' + argv, message)
            return False
        output = ''
        args = self.parseArgs(argv)
        if args == None:
177
            message = '*** Error: The command line arguments have not been parsed successfully!!'
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
            self.log_output('changelog ' + argv, message)
            return False
        if len(args) == 0:
            version = None
        elif len(args) == 1:
            version = args[0]
        else:
            self.help_changelog()
            return False
        if version != None and not version.isdigit():
            self.help_changelog()
            return False
        if version != None:
            version = int(version)
            if version > self.pdfFile.getNumUpdates():
193
                message = '*** Error: The version number is not valid!!'
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                self.log_output('changelog ' + argv, message)
                return False
        if version == 0 or (version == None and self.pdfFile.getNumUpdates() == 0):
            message = '*** No changes!!'
            self.log_output('changelog ' + argv, message)
            return False
        # Getting information about original document
        data = self.pdfFile.getBasicMetadata(0)
        if data.has_key('author'):
            output += '\tAuthor: ' + data['author'] + newLine
        if data.has_key('creator'):
            output += '\tCreator: ' + data['creator'] + newLine
        if data.has_key('producer'):
            output += '\tProducer: ' + data['producer'] + newLine
        if data.has_key('creation'):
            output += '\tCreation date: ' + data['creation'] + newLine
        if output != '':
            output = 'Original document information:' + newLine + output + newLine
        
        # Getting changes for versions
        changes = self.pdfFile.getChangeLog(version)
        for i in range(len(changes)):
            changelog = changes[i]
            if changelog == [[],[],[],[]]:
                output += 'No changes in version ' + str(i+1) + newLine
            else:
                output += 'Changes in version ' + str(i+1) + ':' + newLine
            # Getting modification information
            data = self.pdfFile.getBasicMetadata(i+1)
            if data.has_key('author'):
                output += '\tAuthor: ' + data['author'] + newLine
            if data.has_key('creator'):
                output += '\tCreator: ' + data['creator'] + newLine
            if data.has_key('producer'):
                output += '\tProducer: ' + data['producer'] + newLine
            if data.has_key('modification'):
                output += '\tModification date: ' + data['modification'] + newLine
            addedObjects = changelog[0]
            modifiedObjects = changelog[1]
            removedObjects = changelog[2]
            notMatchingObjects = changelog[3]
            if addedObjects != []:
                output += '\tAdded objects: ' + str(addedObjects) + newLine
            if modifiedObjects != []:
                output += '\tModified objects: ' + str(modifiedObjects) + newLine
            if removedObjects != []:
                output += '\tRemoved objects: ' + str(removedObjects) + newLine
            if notMatchingObjects != []:
                output += '\tIncoherent objects: ' + str(notMatchingObjects) + newLine
            output += newLine
244
        self.log_output('changelog ' + argv, output)
245 246 247 248 249 250 251 252 253 254
        
    def help_changelog(self):
        print newLine + 'Usage: changelog [$version]'
        print newLine + 'Shows the changelog of the document or version of the document' + newLine

    def do_create(self, argv):
        message = ''
        validCreateTypes = ['pdf','object_stream']
        args = self.parseArgs(argv)
        if args == None:
255
            message = '*** Error: The command line arguments have not been parsed successfully!!'
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
            self.log_output('create ' + argv, message)
            return False
        numArgs = len(args)
        if numArgs == 0:
            self.help_create()
            return False
        elementType = args[0]
        if elementType not in validCreateTypes:
            self.help_create()
            return False
        if elementType == 'pdf':
            content = ''
            validPDFTypes = ['simple','open_action_js']
            pdfType = 'simple'
            if numArgs > 1:
                pdfType = args[1]
                if pdfType not in validPDFTypes:
                    self.help_create()
                    return False
                if pdfType == 'open_action_js':
                    if numArgs > 3:
                        self.help_create()
                        return False
                    elif numArgs == 3:
                        jsFile = args[2]
                        if not os.path.exists(jsFile):
282
                            message = '*** Error: The file "'+jsFile+'" does not exist!!'
283 284 285 286 287 288 289
                            self.log_output('create ' + argv, message)
                            return False
                        content = open(jsFile,'rb').read()
                    else:
                        if self.use_rawinput:
                            content = raw_input(newLine+'Please, specify the Javascript code you want to include in the file (if the code includes EOL characters use a js_file instead):' + newLine*2)
                        else:
290
                            message = '*** Error: You must specify a Javascript file in batch mode!!'
291 292 293 294 295 296 297 298 299 300 301
                            self.log_output('create ' + argv, message)
                            return False
                elif pdfType == 'simple':
                    if numArgs > 2:
                        self.help_create()
                        return False
            self.pdfFile = PDFFile()
            ret = self.pdfFile.makePDF(pdfType,content)
            if ret[0] == 0:
                message = 'PDF structure created successfully!!'
            else:
302
                message = '*** Error: An error occurred while creating the PDF structure!!'
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
            self.log_output('create ' + argv, message)
        elif elementType == 'object_stream':
            if self.pdfFile == None:
                message = '*** Error: You must open a file!!'
                self.log_output('create ' + argv, message)
                return False
            objectsToCompress = []
            streamContent = None
            version = None
            if numArgs == 2:
                version = args[1]
            elif numArgs > 2:
                self.help_create()
                return False
            if version != None and not version.isdigit():
                self.help_create()
                return False
            if version != None:
                version = int(version)
                if version > self.pdfFile.getNumUpdates():
323
                    message = '*** Error: The version number is not valid!!'
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                    self.log_output('create ' + argv, message)
                    return False
            warning = 'Warning: stream objects cannot be compressed. If the Catalog object is compressed could lead to corrupted files for Adobe Reader!!'
            if self.use_rawinput:
                res = raw_input(warning+newLine+'Which objects do you want to compress? (Valid respones: all | 1-5 | 1,2,5,7,8) ')
            else:
                res = 'all'
            if res == 'all':
                objects = []
            elif res.count('-') == 1:
                limits = res.split('-')
                objects = range(int(limits[0]),int(limits[1])+1)
            elif res.find(',') != -1:
                objects = [int(id) for id in res.split(',')]
            elif res.isdigit():
                objects = [int(res)]
            else:
341
                message = '*** Error: The response format is not valid. It should be: all | 1-13 | 1,3,5,8!!'
342 343 344 345 346 347 348 349 350 351 352 353 354
                self.log_output('create ' + argv, message)
                return False
            ret = self.pdfFile.createObjectStream(version, objectIds = objects)
            if ret[0] == -1:
                error = ret[1]
                if error.find('Error') != -1:
                    message = '*** Error: '+ret[1]+'!!'
                    self.log_output('create ' + argv, message)
                    return False
                else:
                    message = '*** Warning: '+ret[1]+'!!'
            id = ret[1]
            if id == None:
355
                message = '*** Error: The object stream has NOT been created!!'
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
                self.log_output('create ' + argv, message)
                return False
            else:
                if message != '':
                    message += newLine*2
                message += 'The object stream has been created successfully'
            self.log_output('create ' + argv, message)        
                            
    def help_create(self):
        print newLine + 'Usage: create pdf simple|(open_action_js [$js_file])'
        print newLine + 'Creates a new simple PDF file or one with Javascript code to be executed when opening the file. It\'s possible to specify the file where the Javascript code is stored or do it manually.' + newLine*2
        print 'Usage: create object_stream [$version]' + newLine
        print 'Creates an object stream choosing the objects to be compressed.' + newLine
        
    def do_decode(self, argv):
        decodedContent = ''
        src = ''
        offset = 0
        size = 0
        validTypes = ['variable','file','raw']
        notImplementedFilters = ['ccittfax''ccf','jbig2','dct','jpx']
        filters = []
        args = self.parseArgs(argv)
        if args == None:
380
            message = '*** Error: The command line arguments have not been parsed successfully!!'
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
            self.log_output('decode ' + argv, message)
            return False
        if len(args) > 2:
            type = args[0]
            iniFilterArgs = 2
            if type not in validTypes:
                self.help_decode()
                return False
            if type == 'variable' or type == 'file':
                src = args[1]
            else:
                if self.pdfFile == None:
                    message = '*** Error: You must open a file!!'
                    self.log_output('decode ' + argv, message)
                    return False
                if len(args) < 3:
                    self.help_decode()
                    return False
                iniFilterArgs = 3
                offset = args[1]
                size = args[2]
                if not offset.isdigit() or not size.isdigit():
                    message = '*** Error: "offset" and "num_bytes" must be integers!!'
                    self.log_output('decode ' + argv, message)
                    return False
                offset = int(args[1])
                size = int(args[1])
            for i in range(iniFilterArgs,len(args)):
                filter = args[i].lower()
                if filter not in filter2RealFilterDict.keys():
                    self.help_decode()
                    return False
                if filter in notImplementedFilters:
414
                    message = '*** Error: Filter "'+filter+'" not implemented yet!!'
415 416 417 418 419 420 421 422 423
                    self.log_output('decode ' + argv, message)
                    return False
                filters.append(filter)
        else:
            self.help_decode()
            return False
        
        if type == 'variable':
            if not self.variables.has_key(src):
424
                message = '*** Error: The variable does not exist!!'
425 426 427 428 429 430
                self.log_output('decode ' + argv, message)
                return False
            else:
                decodedContent = self.variables[src][0]
        elif type == 'file':
            if not os.path.exists(src):
431
                message = '*** Error: The file does not exist!!'
432 433 434 435 436 437 438
                self.log_output('decode ' + argv, message)
                return False
            else:
                decodedContent = open(src,'rb').read()                
        else:
            ret = getBytesFromFile(self.pdfFile.getPath(),offset,size)
            if ret[0] == -1:
439
                message = '*** Error: The file does not exist!!'
440 441 442 443
                self.log_output('decode ' + argv, message)
                return False
            decodedContent = ret[1]
        if decodedContent == '':
444
            message = '*** Error: The content is empty!!'
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
            self.log_output('decode ' + argv, message)
            return False
        for filter in filters:
            realFilter = filter2RealFilterDict[filter]
            if realFilter == 'base64':
                try:
                    decodedContent = b64decode(decodedContent)
                except:
                    message = '*** Error: '+str(sys.exc_info()[1])+'!!'
                    self.log_output('decode ' + argv, message)
                    return False
            else:
                ret = decodeStream(decodedContent, realFilter)
                if ret[0] == -1:
                    message = '*** Error: '+ret[1]+'!!'
                    self.log_output('decode ' + argv, message)
                    return False
                decodedContent = ret[1]
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        self.log_output('decode ' + argv, decodedContent, [decodedContent], bytesOutput = True)
              
    def help_decode(self):
        print newLine + 'Usage: decode variable $var_name $filter1 [$filter2 ...]'
        print 'Usage: decode file $file_name $filter1 [$filter2 ...]'
        print 'Usage: decode raw $offset $num_bytes $filter1 [$filter2 ...]' + newLine
        print 'Decodes the content of the specified variable, file or raw bytes using the following filters or algorithms:'
        print '\tbase64,b64: Base64'
        print '\tasciihex,ahx: /ASCIIHexDecode'
        print '\tascii85,a85: /ASCII85Decode'
        print '\tlzw: /LZWDecode'
        print '\tflatedecode,fl: /FlateDecode'
        print '\trunlength,rl: /RunLengthDecode'
        print '\tccittfax,ccf: /CCITTFaxDecode'
        print '\tjbig2: /JBIG2Decode (Not implemented)'
        print '\tdct: /DCTDecode (Not implemented)'
        print '\tjpx: /JPXDecode (Not implemented)' + newLine
480 481 482 483 484 485 486 487

    def do_decrypt(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('decrypt ' + argv, message)
            return False
        args = self.parseArgs(argv)
        if args == None:
488
            message = '*** Error: The command line arguments have not been parsed successfully!!'
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
            self.log_output('decrypt ' + argv, message)
            return False
        numArgs = len(args)
        if numArgs == 1:
            password = args[0]
        else:
            self.help_decrypt()
            return False
        ret = self.pdfFile.decrypt(password)
        if ret[0] == -1:
            message = '*** Error: '+ret[1]+'!!'
        else:
            message = 'File decrypted successfully!!'
        self.log_output('decrypt ' + argv, message)                    
        
    def help_decrypt(self):
        print newLine + 'Usage: decrypt $password'
        print newLine + 'Decrypts the file with the specified password' + newLine

    def do_embed(self, argv):
        fileType = 'application#2Fpdf'
        option = None
        version = None
        fileContent = None
        execute = False
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('embed ' + argv, message)
            return False
        args = self.parseArgs(argv)
        if args == None:
520
            message = '*** Error: The command line arguments have not been parsed successfully!!'
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
            self.log_output('embed ' + argv, message)
            return False
        numArgs = len(args)
        if numArgs == 1:
            fileName = args[0]
        elif numArgs == 2:
            if args[0] == '-x':
                fileName = args[1]
                execute = True
            else:
                fileName = args[0]
                fileType = args[1]
                if not os.path.exists(fileName):
                    self.help_embed()
                    return False
        elif numArgs == 3:
            option = args[0]
            fileName = args[1]
            fileType = args[2]
            if option != '-x':
541
                message = '*** Error: Option not valid!!'
542 543 544 545 546 547 548 549
                self.log_output('embed ' + argv, message)
                return False
            execute = True    
        else:
            self.help_embed()
            return False
        
        if not os.path.exists(fileName):
550
            message = '*** Error: The file does not exist!!'
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
            self.log_output('embed ' + argv, message)
            return False
        fileContent = open(fileName,'rb').read()
        fileType = fileType.replace('/','#2F')
        
        # Check existent /Names in Catalog
        namesDict = None
        namesDictId = None
        namesToFilesDict = None
        namesToFilesDictId = None
        catalogObject = None
        catalogObjectId = None
        catalogIndirectObjects = self.pdfFile.getCatalogObject(indirect = True)
        for i in range(len(catalogIndirectObjects)-1,-1,-1):
            catalogIndirectObject = catalogIndirectObjects[i]
            if catalogIndirectObject != None:
                catalogObject = catalogIndirectObject.getObject()
                if catalogObject != None:
                    catalogObjectId = catalogIndirectObject.getId()
                    catalogObject = catalogIndirectObject.getObject()
                    version = i
                    if catalogObject.hasElement('/Names'):
                        namesDict = catalogObject.getElement('/Names')
                        namesDictType = namesDict.getType()
                        if namesDictType == 'reference':
                            namesDictId = namesDict.getId()
                            namesDict = self.pdfFile.getObject(namesDictId,version)
                        elif namesObjectType != 'dictionary':
579
                            message = '*** Error: Bad type for /Names in Catalog!!'
580 581 582 583 584 585 586 587 588
                            self.log_output('embed ' + argv, message)
                            return False
                        if namesDict != None and namesDict.hasElement('/EmbeddedFiles'):
                            namesToFilesDict = namesDict.getElement('/EmbeddedFiles')
                            namesToFilesDictType = namesToFilesDict.getType()
                            if namesToFilesDictType == 'reference':
                                namesToFilesDictId = namesToFilesDict.getId()
                                namesToFilesDict = self.pdfFile.getObject(namesToFilesDictId,version)
                            elif namesToFilesDictType != 'dictionary':
589
                                message = '*** Error: Bad type for /EmbeddedFiles element!!'
590 591 592 593
                                self.log_output('embed ' + argv, message)
                                return False
                    break
        if version == None:
594
            message = '*** Error: Missing Catalog object!!'
595 596 597 598 599 600 601 602 603 604 605 606
            self.log_output('embed ' + argv, message)
            return False
        
        hexFileNameObject = PDFHexString(fileName.encode('hex'))
        md5Hash = hashlib.md5(fileContent).hexdigest()
        fileSize = len(fileContent)
        paramsDic = PDFDictionary(elements = {'/Size':PDFNum(str(fileSize)),'/Checksum':PDFHexString(md5Hash)})
        embeddedFileElements = {'/Type':PDFName('EmbeddedFile'),'/Subtype':PDFName(fileType),'/Params':paramsDic,'/Length':PDFNum(str(fileSize))}
        embeddedFileStream = PDFStream(rawStream = fileContent,elements = embeddedFileElements)
        embeddedFileStream.setElement('/Filter',PDFName('FlateDecode'))
        ret = self.pdfFile.setObject(None,embeddedFileStream,version)
        if ret[0] == -1:
607
            message = '*** Error: The embedded stream has not been created!!'
608 609 610 611 612 613 614
            self.log_output('embed ' + argv, message)
            return False
        embeddedFileStreamId = ret[1][0]
        embeddedListDict = PDFDictionary(elements = {'/F':PDFReference(str(embeddedFileStreamId))})
        fileSpecDict = PDFDictionary(elements = {'/Type':PDFName('Filespec'),'/F':PDFString(fileName),'/EF':embeddedListDict})
        ret = self.pdfFile.setObject(None,fileSpecDict,version)
        if ret[0] == -1:
615
            message = '*** Error: The Filespec dictionary has not been created!!'
616 617 618 619 620 621 622 623 624 625 626 627
            self.log_output('embed ' + argv, message)
            return False
        fileSpecDictId = ret[1][0]
        
        if namesToFilesDict != None:
            if namesToFilesDict.hasElement('/Names'):
                namesToFileArray = namesToFilesDict.getElement('/Names')
                namesToFileArrayType = namesToFileArray.getType()
                if namesToFileArrayType == 'reference':
                    namesToFileArrayId = namesToFileArray.getId()
                    namesToFileArray = self.pdfFile.getObject(namesToFileArrayId,version)
                elif namesToFileArrayType != 'array':
628
                    message = '*** Error: Bad type for /Names in /EmbeddedFiles element!!'
629 630 631 632 633 634 635 636 637 638 639
                    self.log_output('embed ' + argv, message)
                    return False
                namesToFileArray.addElement(hexFileNameObject)
                namesToFileArray.addElement(PDFReference(str(fileSpecDictId)))
                if namesToFileArrayType == 'reference':
                    self.pdfFile.setObject(namesToFileArrayId,namesToFileArray,version)
                else:
                    namesToFilesDict.setElement('/Names',namesToFileArray)
                    if namesToFilesDictId != None:
                        ret = self.pdfFile.setObject(namesToFilesDictId,namesToFilesDict,version)
                        if ret[0] == -1:
640
                            message = '*** Error: The /EmbeddedFiles dictionary has not been modified!!'
641 642 643
                            self.log_output('embed ' + argv, message)
                            return False
            elif namesToFilesDict.hasElement('/Kids'):
644
                message = '*** Error: Children nodes in the /EmbeddedFiles element not supported!!'
645 646 647 648 649 650 651 652 653 654 655 656 657 658
                self.log_output('embed ' + argv, message)
                return False
            else:
                namesToFilesDict.setElement('/Names',PDFArray(elements = [hexFileNameObject,PDFReference(str(fileSpecDictId))]))
        else:
            namesToFilesDict = PDFDictionary(elements = {'/Names':PDFArray(elements = [hexFileNameObject,PDFReference(str(fileSpecDictId))])})
            

        if namesDict != None:
            if namesToFilesDictId == None:
                namesDict.setElement('/EmbeddedFiles',namesToFilesDict)
                if namesDictId != None:
                    ret = self.pdfFile.setObject(namesDictId,namesDict,version)
                    if ret[0] == -1:
659
                        message = '*** Error: The /Names dictionary has not been modified!!'
660 661 662 663 664 665 666 667
                        self.log_output('embed ' + argv, message)
                        return False    
        else:
            namesDict = PDFDictionary(elements = {'/EmbeddedFiles':namesToFilesDict})
        if namesDictId == None:
            catalogObject.setElement('/Names',namesDict)
            ret = self.pdfFile.setObject(catalogObjectId,catalogObject,version)
            if ret[0] == -1:
668
                message = '*** Error: The Catalog has not been modified!!'
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
                self.log_output('embed ' + argv, message)
                return False
            
        # Checking that the /Contents element is present
        if catalogObject.hasElement('/Pages'):
            pagesObject = catalogObject.getElement('/Pages')
            if pagesObject.getType() == 'reference':
                pagesObjectId = pagesObject.getId()
                pagesObject = self.pdfFile.getObject(pagesObjectId,version)
                if pagesObject != None:
                    if pagesObject.hasElement('/Kids'):
                        kidsObject = pagesObject.getElement('/Kids')
                        if kidsObject != None:
                            kidsObjectType = kidsObject.getType()
                            if kidsObjectType == 'reference':
                                kidsObjectId = kidsObject.getId()
                                kidsObject = self.pdfFile.getObject(kidsObjectId,version)
                            elif kidsObjectType != 'array':
687
                                message = '*** Error: Bad type for /Kids element!!'
688 689 690 691 692 693 694 695 696 697
                                self.log_output('embed ' + argv, message)
                                return False
                            pageObjects = kidsObject.getElements()
                            if len(pageObjects) > 0:
                                firstPageObjectId = None
                                firstPageObject = pageObjects[0]
                                if firstPageObject != None and firstPageObject.getType() == 'reference':
                                    firstPageObjectId = firstPageObject.getId()
                                    firstPageObject = self.pdfFile.getObject(firstPageObjectId,version)
                                else:
698
                                    message = '*** Error: Bad type for /Page reference!!'
699 700 701 702 703 704 705
                                    self.log_output('embed ' + argv, message)
                                    return False
                                if firstPageObject.getType() == 'dictionary':
                                    if not firstPageObject.hasElement('/Contents'):
                                        contentsStream = PDFStream(rawStream = '',elements = {'/Length':PDFNum('0')})
                                        ret = self.pdfFile.setObject(None,contentsStream,version)
                                        if ret[0] == -1:
706
                                            message = '*** Error: The /Contents stream has not been created!!'
707 708 709 710 711 712 713 714 715 716
                                            self.log_output('embed ' + argv, message)
                                            return False
                                        contentsStreamId = ret[1][0]
                                        firstPageObject.setElement('/Contents',PDFReference(str(contentsStreamId)))
                                    # Adding GoToE action
                                    if execute:
                                        targetDict = PDFDictionary(elements = {'/N': hexFileNameObject, '/R': PDFName('C')})
                                        actionGoToEDict = PDFDictionary(elements = {'/S':PDFName('GoToE'),'/NewWindow':PDFBool('false'),'/T':targetDict})
                                        ret = self.pdfFile.setObject(None,actionGoToEDict,version)
                                        if ret[0] == -1:
717
                                            message = '*** Error: The /GoToE element has not been created!!'
718 719 720 721 722 723 724
                                            self.log_output('embed ' + argv, message)
                                            return False
                                        actionGoToEDictId = ret[1][0]
                                        aaDict = PDFDictionary(elements = {'/O':PDFReference(str(actionGoToEDictId))})
                                        firstPageObject.setElement('/AA',aaDict)
                                        ret = self.pdfFile.setObject(firstPageObjectId,firstPageObject,version)
                                        if ret[0] == -1:
725
                                            message = '*** Error: The /Page element has not been modified!!'
726 727 728
                                            self.log_output('embed ' + argv, message)
                                            return False
                                else:
729
                                    message = '*** Error: Bad type for /Page element!!'
730 731 732
                                    self.log_output('embed ' + argv, message)
                                    return False
                            else:
733
                                message = '*** Error: Missing /Page element!!'
734 735 736 737 738 739 740
                                self.log_output('embed ' + argv, message)
                                return False
                        else:
                            message = '*** Error: /Kids element corrupted!!'
                            self.log_output('embed ' + argv, message)
                            return False
                    else:
741
                        message = '*** Error: Missing /Kids element!!'
742 743 744 745 746 747 748
                        self.log_output('embed ' + argv, message)
                        return False
                else:
                    message = '*** Error: /Pages element corrupted!!'
                    self.log_output('embed ' + argv, message)
                    return False
            else:
749
                message = '*** Error: Bad type for /Pages element!!'
750 751 752
                self.log_output('embed ' + argv, message)
                return False
        else:
753
            message = '*** Error: Missing /Pages element!!'
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
            self.log_output('embed ' + argv, message)
            return False
            
        message = 'File embedded successfully!!'
        self.log_output('open ' + argv, message)

    def help_embed(self):
        print newLine + 'Usage: embed [-x] $filename [$file_type]'
        print newLine + 'Embeds the specified file in the actual PDF file. The default type is "application/pdf".' + newLine
        print 'Options:'
        print '\t-x: The file is executed when the actual PDF file is opened' + newLine

    def do_encode(self, argv):
        encodedContent = ''
        src = ''
        offset = 0
        size = 0
        validTypes = ['variable','file','raw']
        notImplementedFilters = ['ascii85','a85','runlength','rl','jbig2','jpx','ccittfax','ccf','dct']
        filters = []
        args = self.parseArgs(argv)
        if args == None:
776
            message = '*** Error: The command line arguments have not been parsed successfully!!'
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
            self.log_output('encode ' + argv, message)
            return False
        if len(args) > 2:
            type = args[0]
            iniFilterArgs = 2
            if type not in validTypes:
                self.help_encode()
                return False
            if type == 'variable' or type == 'file':
                src = args[1]
            else:
                if self.pdfFile == None:
                    message = '*** Error: You must open a file!!'
                    self.log_output('decode ' + argv, message)
                    return False
                if len(args) < 3:
                    self.help_encode()
                    return False
                iniFilterArgs = 3
                offset = args[1]
                size = args[2]
                if not offset.isdigit() or not size.isdigit():
                    message = '*** Error: "offset" and "num_bytes" must be integers!!'
                    self.log_output('encode ' + argv, message)
                    return False
                offset = int(args[1])
                size = int(args[1])
            for i in range(iniFilterArgs,len(args)):
                filter = args[i].lower()
                if filter not in filter2RealFilterDict.keys():
                    self.help_encode()
                    return False
                if filter in notImplementedFilters:
810
                    message = '*** Error: Filter "'+filter+'" not implemented yet!!'
811 812 813 814 815 816 817 818 819
                    self.log_output('encode ' + argv, message)
                    return False
                filters.append(filter)
        else:
            self.help_encode()
            return False
        
        if type == 'variable':
            if not self.variables.has_key(src):
820
                message = '*** Error: The variable does not exist!!'
821 822 823 824 825 826
                self.log_output('encode ' + argv, message)
                return False
            else:
                encodedContent = self.variables[src][0]
        elif type == 'file':
            if not os.path.exists(src):
827
                message = '*** Error: The file does not exist!!'
828 829 830 831 832 833 834
                self.log_output('encode ' + argv, message)
                return False
            else:
                encodedContent = open(src,'rb').read()                
        else:
            ret = getBytesFromFile(self.pdfFile.getPath(),offset,size)
            if ret[0] == -1:
835
                message = '*** Error: The file does not exist!!'
836 837 838 839
                self.log_output('encode ' + argv, message)
                return False
            encodedContent = ret[1]
        if encodedContent == '':
840
            message = '*** Error: The content is empty!!'
841 842 843 844 845 846 847 848 849 850 851 852 853
            self.log_output('encode ' + argv, message)
            return False
        for filter in filters:
            realFilter = filter2RealFilterDict[filter]
            if realFilter == 'base64':
                encodedContent = b64encode(encodedContent)
            else:
                ret = encodeStream(encodedContent, realFilter)
                if ret[0] == -1:
                    message = '*** Error: '+ret[1]+'!!'
                    self.log_output('encode ' + argv, message)
                    return False
                encodedContent = ret[1]
854
        self.log_output('encode ' + argv, encodedContent, [encodedContent], bytesOutput = True)
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
                                
    def help_encode(self):
        print newLine + 'Usage: encode variable $var_name $filter1 [$filter2 ...]'
        print 'Usage: encode file $file_name $filter1 [$filter2 ...]'
        print 'Usage: encode raw $offset $num_bytes $filter1 [$filter2 ...]' + newLine
        print 'Encodes the content of the specified variable, file or raw bytes using the following filters or algorithms:'
        print '\tbase64,b64: Base64'
        print '\tasciihex,ahx: /ASCIIHexDecode'
        print '\tascii85,a85: /ASCII85Decode (Not implemented)'
        print '\tlzw: /LZWDecode'
        print '\tflatedecode,fl: /FlateDecode'
        print '\trunlength,rl: /RunLengthDecode (Not implemented)'
        print '\tccittfax,ccf: /CCITTFaxDecode (Not implemented)'
        print '\tjbig2: /JBIG2Decode (Not implemented)'
        print '\tdct: /DCTDecode (Not implemented)'
        print '\tjpx: /JPXDecode (Not implemented)' + newLine

    def do_encode_strings(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('encode_strings ' + argv, message)
            return False
        args = self.parseArgs(argv)
        if args == None:
879
            message = '*** Error: The command line arguments have not been parsed successfully!!'
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
            self.log_output('encode_strings ' + argv, message)
            return False
        if len(args) == 0:
            ret = self.pdfFile.encodeChars()
            if ret[0] == -1:
                message = '*** Error: '+ret[1]+'!!'
                self.log_output('encode_strings ' + argv, message)
                return False
            message = 'File encoded successfully'
        elif len(args) == 1 or len(args) == 2:
            if len(args) == 1:
                version = None
            else:
                version = args[1]
            id = args[0]
            if (not id.isdigit() and id != 'trailer') or (version != None and not version.isdigit()):
                self.help_encode_strings()
                return False
            if version != None:
                version = int(version)
                if version > self.pdfFile.getNumUpdates():
901
                    message = '*** Error: The version number is not valid!!'
902 903 904 905 906
                    self.log_output('encode_strings ' + argv, message)
                    return False
            if id == 'trailer':
                ret = self.pdfFile.getTrailer(version)
                if ret == None or ret[1] == [] or ret[1] == None or ret[1] == [None,None]:
907
                    message = '*** Error: Trailer not found!!'
908 909 910 911 912 913 914 915 916
                    self.log_output('encode_strings ' + argv, message)
                    return False
                else:
                    trailerArray = ret[1]
                    version = ret[0]
                if trailerArray[0] != None:
                    trailerArray[0].encodeChars()
                    ret = self.pdfFile.setTrailer(trailerArray,version)
                    if ret[0] == -1:
917
                        message = '*** Error: There were some problems in the modification process!!'
918 919 920 921 922 923 924
                        self.log_output('encode_strings ' + argv, message)
                        return False
                    message = 'Trailer encoded successfully'
            else:
                id = int(id)
                object = self.pdfFile.getObject(id, version)
                if object == None:
925
                    message = '*** Error: Object not found!!'
926 927 928 929
                    self.log_output('encode_strings ' + argv, message)
                    return False
                objectType = object.getType()
                if objectType not in ['string','name','array','dictionary','stream']:
930
                    message = '*** Error: This type of object cannot be encoded!!'
931 932 933 934 935 936 937 938 939
                    self.log_output('encode_strings ' + argv, message)
                    return False
                ret = object.encodeChars()
                if ret[0] == -1:
                    message = '*** Error: '+ret[1]+'!!'
                    self.log_output('encode_strings ' + argv, message)
                    return False
                ret = self.pdfFile.setObject(id, object, version, True)
                if ret[0] == -1:
940
                    message = '*** Error: There were some problems in the modification process!!'
941 942 943 944 945 946
                    self.log_output('encode_strings ' + argv, message)
                    return False
                message = 'Object encoded successfully'
        else:
            self.help_encode_strings()
            return False
947
        self.log_output('encode_strings ' + argv, message)
948 949 950 951 952 953 954 955 956 957 958 959
                    
    def help_encode_strings(self):
        print newLine + 'Usage: encode_strings [$object_id|trailer [$version]]'
        print newLine + 'Encodes the strings and names included in the file, object or trailer' + newLine

    def do_encrypt(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('encrypt ' + argv, message)
            return False
        args = self.parseArgs(argv)
        if args == None:
960
            message = '*** Error: The command line arguments have not been parsed successfully!!'
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
            self.log_output('encrypt ' + argv, message)
            return False
        numArgs = len(args)
        if numArgs == 0:
            password = ''
        elif numArgs == 1:
            password = args[0]
        else:
            self.help_encrypt()
            return False
        ret = self.pdfFile.encrypt(password)
        if ret[0] == -1:
            message = '*** Error: '+ret[1]+'!!'
        else:
            message = 'File encrypted successfully!!'
        self.log_output('encrypt ' + argv, message)                    
        
    def help_encrypt(self):
        print newLine + 'Usage: encrypt [$password]'
        print newLine + 'Encrypts the file with the default or specified password' + newLine

    def do_errors(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('errors ' + argv, message)
            return False
        errors = ''
        errorsArray = []
        args = self.parseArgs(argv)
        if args == None:
991
            message = '*** Error: The command line arguments have not been parsed successfully!!'
992 993 994 995 996 997 998 999 1000 1001 1002
            self.log_output('errors ' + argv, message)
            return False
        if len(args) == 0:
            errorsArray = self.pdfFile.getErrors()
            for error in errorsArray:
                errors += error
                if error != errorsArray[-1]:
                    errors += newLine
            if errors == '':
                errors = 'No errors!!'
            else:
1003
                errors = self.errorColor + errors + self.resetColor
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
            self.log_output('errors ' + argv, errors)
            return False
        elif len(args) == 1:
            version = None
        elif len(args) == 2:
            version = args[1]
        else:
            self.help_errors()
            return False
        id = args[0]
        if (not id.isdigit() and id != 'trailer' and id != 'xref') or (version != None and not version.isdigit()):
            self.help_errors()
            return False
        if version != None:
            version = int(version)
            if version > self.pdfFile.getNumUpdates():
1020
                message = '*** Error: The version number is not valid!!'
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
                self.log_output('errors ' + argv, message)
                return False
        if id == 'xref':
            ret = self.pdfFile.getXrefSection(version)
            if ret == None or ret[1] == None or ret[1] == [] or ret[1] == [None,None]:
                message = '*** Error: xref section not found!!'
                self.log_output('errors ' + argv, message)
                return False
            else:
                xrefArray = ret[1]
            if xrefArray[0] != None:
                errorsArray = xrefArray[0].getErrors()
            if xrefArray[1] != None:    
                errorsArray += xrefArray[1].getErrors()
        elif id == 'trailer':
            ret = self.pdfFile.getTrailer(version)
            if ret == None or ret[1] == None or ret[1] == [] or ret[1] == [None,None]:
1038
                message = '*** Error: Trailer not found!!'
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
                self.log_output('errors ' + argv, message)
                return False
            else:
                trailerArray = ret[1]
            if trailerArray[0] != None:
                errorsArray = trailerArray[0].getErrors()
            if trailerArray[1] != None:    
                errorsArray += trailerArray[1].getErrors()
        else:
            id = int(id)
            object = self.pdfFile.getObject(id, version)
            if object == None:
1051
                message = '*** Error: Object not found!!'
1052 1053 1054 1055 1056 1057 1058 1059 1060
                self.log_output('errors ' + argv, message)
                return False
            errorsArray = object.getErrors()
        messages,counters = countArrayElements(errorsArray)
        for i in range(len(messages)):
            errors += messages[i] + ' ('+ str(counters[i]) +') ' + newLine
        if errors == '':
            errors = 'No errors!!'
        else:
1061
            errors = self.errorColor + errors + self.resetColor
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
        self.log_output('errors ' + argv, errors)            
        
    def help_errors(self):
        print newLine + 'Usage: errors [$object_id|xref|trailer [$version]]'
        print newLine + 'Shows the errors of the file or object (object_id, xref, trailer)' + newLine
                
    def do_exit(self, argv):
        return True
    
    def help_exit(self):
        print newLine + 'Usage: exit'
        print newLine + 'Exits from the console' + newLine

    def do_filters(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('errors ' + argv, message)
            return False
        message = ''
        value = ''
        filtersArray = []
        notImplementedFilters = ['ascii85','a85','runlength','rl','jbig2','jpx','ccittfax','ccf','dct']
        iniFilterArgs = 1
        filters = []
        args = self.parseArgs(argv)
        if args == None:
1088
            message = '*** Error: The command line arguments have not been parsed successfully!!'
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
            self.log_output('filters ' + argv, message)
            return False
        if len(args) == 0:
            self.help_filters()
            return False
        elif len(args) == 1:
            version = None
        else:
            if args[1].isdigit():
                version = args[1]
                iniFilterArgs = 2
            else:
                version = None
            validFilters = filter2RealFilterDict.keys() + ['none']
            validFilters.remove('b64')
            validFilters.remove('base64')
            for i in range(iniFilterArgs,len(args)):
                filter = args[i].lower()
                if filter not in validFilters:
                    self.help_filters()
                    return False
                if filter in notImplementedFilters:
1111
                    message = '*** Error: Filter "'+filter+'" not implemented yet!!'
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
                    self.log_output('filters ' + argv, message)
                    return False
                filters.append(filter)
                
        id = args[0]
        if not id.isdigit() or (version != None and not version.isdigit()):
            self.help_filters()
            return False
        id = int(id)
        if version != None:
            version = int(version)
            if version > self.pdfFile.getNumUpdates():
1124
                message = '*** Error: The version number is not valid!!'
1125 1126 1127 1128 1129
                self.log_output('filters ' + argv, message)
                return False
            
        object = self.pdfFile.getObject(id, version)
        if object == None:
1130
            message = '*** Error: Object not found!!'
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
            self.log_output('filters ' + argv, message)
            return False
        if object.getType() != 'stream':
            message = '*** Error: The object doesn\'t contain any streams!!'
            self.log_output('filters ' + argv, message)
            return False
        errors = object.getErrors()
        if filters == []:
            if object.hasElement('/Filter'):
                value = object.getElementByName('/Filter').getValue()
                if object.hasElement('/DecodeParms'):
                    parameters = object.getElementByName('/DecodeParms').getValue()
                    value += " " + parameters
            else:
                message = '*** Warning: No filters found in the object!!'
                self.log_output('filters ' + argv, message)
                return False
        else:
            value = object.getStream()
            if value == -1 or value == '':
                message = '*** Error: The stream cannot be decoded!!'
                self.log_output('filters ' + argv, message)
                return False
            if len(filters) == 1:
                if filters[0] == 'none':
                    ret = object.delElement('/Filter')
                else:
                    filtersPDFName = PDFName(filter2RealFilterDict[filters[0]])
                    ret = object.setElement('/Filter',filtersPDFName)
                if ret[0] == -1:
                    message = '*** Error: '+ret[1]+'!!'
                    self.log_output('filters ' + argv, message)
                    return False
            else:
                while True:
                    if 'none' in filters:
                        filters.remove('none')
                    else:
                        break
                filters.reverse()
                for filter in filters:
                    filtersArray.append(PDFName(filter2RealFilterDict[filter]))
                if filtersArray != []: 
                    filtersPDFArray = PDFArray('',filtersArray)
                    ret = object.setElement('/Filter',filtersPDFArray)
                    if ret[0] == -1:
                        message = '*** Error: '+ret[1]+'!!'
                        self.log_output('filters ' + argv, message)
                        return False
            ret = self.pdfFile.setObject(id, object, version)
            if ret[0] == -1:
                message = '*** Error: '+ret[1]+'!!'
                self.log_output('filters ' + argv, message)
                return False
            value = str(object.getRawValue())
            newErrors = object.getErrors()
            if newErrors != errors:
                message = 'Warning: Some errors found in the modification process!!' + newLine
1189
        self.log_output('filters ' + argv, message+value, value)
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
            
    def help_filters(self):
        print newLine + 'Usage: filters $object_id [$version] [$filter1 [$filter2 ...]]'
        print newLine + 'Shows the filters found in the stream object or set the filters in the object (first filter is used first). The valid values for filters are the following:'
        print '\tnone: No filters'
        print '\tasciihex,ahx: /ASCIIHexDecode'
        print '\tascii85,a85: /ASCII85Decode (Not implemented)'
        print '\tlzw: /LZWDecode'
        print '\tflatedecode,fl: /FlateDecode'
        print '\trunlength,rl: /RunLengthDecode (Not implemented)'
        print '\tccittfax,ccf: /CCITTFaxDecode (Not implemented)'
        print '\tjbig2: /JBIG2Decode (Not implemented)'
        print '\tdct: /DCTDecode (Not implemented)'
        print '\tjpx: /JPXDecode (Not implemented)' + newLine

    def do_hash(self, argv):
        content = ''
        validTypes = ['variable','file','raw','object','rawobject','stream','rawstream']
        args = self.parseArgs(argv)
        if args == None:
1210
            message = '*** Error: The command line arguments have not been parsed successfully!!'
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
            self.log_output('hash ' + argv, message)
            return False
        
        if len(args) == 2:
            if args[0] in ['object','rawobject','stream','rawstream']:
                id = args[1]
                version = None
            elif args[0] == 'file' or args[0] == 'variable':
                srcName = args[1]
            else:
                self.help_hash()
                return False
        elif len(args) == 3:
            if args[0] in ['object','rawobject','stream','rawstream']:
                id = args[1]
                version = args[2]
            elif args[0] == 'raw':
                offset = args[1]
                size = args[2]
            else:
                self.help_hash()
                return False
        else:
            self.help_hash()
            return False
        
        type = args[0]
        if type not in validTypes:
            self.help_hash()
            return False
        if type == 'variable':
            if not self.variables.has_key(srcName):
1243
                message = '*** Error: The variable does not exist!!'
1244 1245 1246 1247 1248 1249
                self.log_output('hash ' + argv, message)
                return False
            else:
                content = self.variables[srcName][0]
        elif type == 'file':
            if not os.path.exists(srcName):
1250
                message = '*** Error: The file does not exist!!'
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
                self.log_output('hash ' + argv, message)
                return False
            else:
                content = open(srcName,'rb').read()
        else:
            if self.pdfFile == None:
                message = '*** Error: You must open a file!!'
                self.log_output('hash ' + argv, message)
                return False
            if type == 'raw':
                if not offset.isdigit() or not size.isdigit():
                    self.help_hash()
                    return False
                offset = int(offset)
                size = int(size)
                ret = getBytesFromFile(self.pdfFile.getPath(),offset,size)
                if ret[0] == -1:
1268
                    message = '*** Error: The file does not exist!!'
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
                    self.log_output('hash ' + argv, message)
                    return False
                content = ret[1]
            else:
                if not id.isdigit() or (version != None and not version.isdigit()):
                    self.help_hash()
                    return False
                id = int(id)
                if version != None:
                    version = int(version)
                    if version > self.pdfFile.getNumUpdates():
1280
                        message = '*** Error: The version number is not valid!!'
1281 1282 1283 1284
                        self.log_output('hash ' + argv, message)
                        return False
                object = self.pdfFile.getObject(id, version)
                if object == None:
1285
                    message = '*** Error: Object not found!!'
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
                    self.log_output('hash ' + argv, message)
                    return False
                if type == 'stream' or type == 'rawstream':
                    if object.getType() != 'stream':
                        message = '*** Error: The object doesn\'t contain any stream!!'
                        self.log_output('hash ' + argv, message)
                        return False
                    if type == 'stream':
                        content = object.getStream()
                    else:
                        content = object.getRawStream()
                elif type == 'object':
                    content = object.getValue()
                else:
                    content = object.getRawValue()
        content = str(content)
        md5Hash = hashlib.md5(content).hexdigest()
        sha1Hash = hashlib.sha1(content).hexdigest()
        sha256Hash = hashlib.sha256(content).hexdigest()
        output = 'MD5: ' + md5Hash + newLine + 'SHA1: ' + sha1Hash + newLine + 'SHA256: ' + sha256Hash + newLine
1306
        self.log_output('hash ' + argv, output)
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

    def help_hash(self):
        print newLine + 'Usage: hash object|rawobject|stream|rawstream $object_id [$version]'
        print 'Usage: hash raw $offset $num_bytes'
        print 'Usage: hash file $file_name'
        print 'Usage: hash variable $var_name'
        print newLine + 'Generates the hash (MD5/SHA1/SHA256) of the specified source: raw bytes of the file, objects and streams, and the content of files or variables' + newLine
            
    def help_help(self):
        print newLine + 'Usage: help [$command]'
        print newLine + 'Shows the available commands or the usage of the specified command' + newLine
        
    def do_info(self, argv):
        if self.pdfFile == None:
            message = '*** Error: You must open a file!!'
            self.log_output('info ' + argv, message)
            return False
        stats = ''
        args = self.parseArgs(argv)
        if args == None:
1327
            message = '*** Error: The command line arguments have not been parsed successfully!!'
1328 1329
            self.log_output('info ' + argv, message)
            return False
1330 1331
        if not self.avoidOutputColors:
            beforeStaticLabel = self.staticColor
1332
        else:
1333
            beforeStaticLabel = ''            
1334 1335
        if len(args) == 0:
            statsDict = self.pdfFile.getStats()            
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
            stats += beforeStaticLabel + 'File: ' + self.resetColor + statsDict['File'] + newLine
            stats += beforeStaticLabel + 'MD5: ' + self.resetColor + statsDict['MD5'] + newLine
            stats += beforeStaticLabel + 'SHA1: ' + self.resetColor + statsDict['SHA1'] + newLine
            #stats += beforeStaticLabel + 'SHA256: ' + self.resetColor + statsDict['SHA256'] + newLine
            stats += beforeStaticLabel + 'Size: ' + self.resetColor + statsDict['Size'] + ' bytes' + newLine
            if statsDict['Detection'] != []:
                detectionReportInfo = ''
                if statsDict['Detection'] != None:
                     detectionLevel = statsDict['Detection'][0]/(statsDict['Detection'][1]/3)
                     if detectionLevel == 0:
                          detectionColor = self.alertColor
                     elif detectionLevel == 1:
                          detectionColor = self.warningColor
                     else:
                          detectionColor = ''
                     detectionRate = '%s%d%s/%d' % (detectionColor, statsDict['Detection'][0], self.resetColor, statsDict['Detection'][1])
                     if statsDict['Detection report'] != '':
                         detectionReportInfo = beforeStaticLabel + 'Detection report: ' + self.resetColor + statsDict['Detection report'] + newLine
                     else:
                         detectionRate = 'File not found on VirusTotal'
                     stats += beforeStaticLabel + 'Detection: ' + self.resetColor + detectionRate + newLine
                     stats += detectionReportInfo
            stats += beforeStaticLabel + 'Version: ' + self.resetColor + statsDict['Version'] + newLine
            stats += beforeStaticLabel + 'Binary: ' + self.resetColor + statsDict['Binary'] + newLine
            stats += beforeStaticLabel + 'Linearized: ' + self.resetColor + statsDict['Linearized'] + newLine
            stats += beforeStaticLabel + 'Encrypted: ' + self.resetColor + statsDict['Encrypted']
1362 1363 1364 1365 1366 1367
            if statsDict['Encryption Algorithms'] != []:
                stats += ' ('
                for algorithmInfo in statsDict['Encryption Algorithms']:
                    stats += algorithmInfo[0] + ' ' + str(algorithmInfo[1]) + ' bits, '
                stats = stats[:-2] + ')'
            stats += newLine
1368 1369 1370 1371 1372
            stats += beforeStaticLabel + 'Updates: ' + self.resetColor + statsDict['Updates'] + newLine
            stats += beforeStaticLabel + 'Objects: ' + self.resetColor + statsDict['Objects'] + newLine
            stats += beforeStaticLabel + 'Streams: ' + self.resetColor + statsDict['Streams'] + newLine
            stats += beforeStaticLabel + 'Comments: ' + self.resetColor + statsDict['Comments'] + newLine
            stats += beforeStaticLabel + 'Errors: ' + self.resetColor + str(len(statsDict['Errors'])) + newLine*2                    
1373 1374
            for version in range(len(statsDict['Versions'])):
                statsVersion = statsDict['Versions'][version]
1375
                stats += beforeStaticLabel + 'Version ' + self.resetColor + str(version) + ':' + newLine
1376
                if statsVersion['Catalog'] != None:
1377
                    stats += beforeStaticLabel + '\tCatalog: ' + self.resetColor + statsVersion['Catalog'] + newLine
1378
                else:
1379
                    stats += beforeStaticLabel + '\tCatalog: ' + self.resetColor + 'No' + newLine
1380
                if statsVersion['Info'] != None:
1381
                    stats += beforeStaticLabel + '\tInfo: ' + self.resetColor + statsVersion['Info'] + newLine
1382
                else:
1383 1384
                    stats += beforeStaticLabel + '\tInfo: ' + self.resetColor + 'No' + newLine
                stats += beforeStaticLabel + '\tObjects ('+statsVersion['Objects'][0]+'): ' + self.resetColor + str(statsVersion['Objects'][1]) + newLine
1385
                if statsVersion['Compressed Objects'] != None:
1386
                    stats += beforeStaticLabel + '\tCompressed objects ('+statsVersion['Compressed Objects'][0]+'): ' + self.resetColor + str(statsVersion['Compressed Objects'][1]) + newLine
1387
                if statsVersion['Errors'] != None:
1388 1389
                    stats += beforeStaticLabel + '\t\tErrors ('+statsVersion['Errors'][0]+'): ' + self.resetColor + str(statsVersion['Errors'][1]) + newLine
                stats += beforeStaticLabel + '\tStreams ('+statsVersion['Streams'][0]+'): ' + self.resetColor + str(statsVersion['Streams'][1])
1390
                if statsVersion['Xref Streams'] != None:
1391
                    stats += newLine + beforeStaticLabel + '\t\tXref streams ('+statsVersion['Xref Streams'][0]+'): ' + self.resetColor + str(statsVersion['Xref Streams'][1])
1392
                if statsVersion['Object Streams'] != None:
1393
                    stats += newLine + beforeStaticLabel + '\t\tObject streams ('+statsVersion['Object Streams'][0]+'): ' + self.resetColor + str(statsVersion['Object Streams'][1])
1394
                if int(statsVersion['Streams'][0]) > 0:
1395
                    stats += newLine + beforeStaticLabel + '\t\tEncoded ('+statsVersion['Encoded'][0]+'): ' + self.resetColor + str(statsVersion['Encoded'][1])
1396
                    if statsVersion['Decoding Errors'] != None:
1397 1398 1399
                        stats += newLine + beforeStaticLabel + '\t\tDecoding errors ('+statsVersion['Decoding Errors'][0]+'): ' + self.resetColor + str(statsVersion['Decoding Errors'][1])
                if not self.avoidOutputColors:
                    beforeStaticLabel = self.warningColor
1400
                if statsVersion['Objects with JS code'] != None:
1401
                    stats += newLine + beforeStaticLabel + '\tObjects with JS code ('+statsVersion['Objects with JS code'][0]+'): ' + self.resetColor + str(statsVersion['Objects with JS code'][1])
1402 1403 1404 1405 1406
                actions = statsVersion['Actions']
                events = statsVersion['Events']
                vulns = statsVersion['Vulns']
                elements = statsVersion['Elements']
                if events != None or actions != None or vulns != None or elements != None:
1407
                    stats += newLine + beforeStaticLabel + '\tSuspicious elements:' + self.resetColor + newLine
1408 1409
                    if events != None:
                        for event in events:
1410
                            stats += '\t\t' + beforeStaticLabel + event + ': ' + self.resetColor + str(events[event]) + newLine
1411 1412
                    if actions != None:
                        for action in actions:
1413
                            stats += '\t\t' + beforeStaticLabel + action + ': ' + self.resetColor + str(actions[action]) + newLine
1414 1415 1416
                    if vulns != None:
                        for vuln in vulns:
                            if vulnsDict.has_key(vuln):
1417 1418 1419 1420
                                vulnName = vulnsDict[vuln][0]
                                vulnCVEList = vulnsDict[vuln][1]
                                stats += '\t\t' + beforeStaticLabel + vulnName + ' ('
                                for vulnCVE in vulnCVEList: 
1421
                                    stats += vulnCVE + ',' 
1422
                                stats = stats[:-1] + '): ' + self.resetColor + str(vulns[vuln]) + newLine
1423
                            else:
1424
                                stats += '\t\t' + beforeStaticLabel + vuln + ': ' + self.resetColor + str(vulns[vuln]) + newLine
1425 1426 1427
                    if elements != None:
                        for element in elements:
                            if vulnsDict.has_key(element):
1428 1429 1430 1431
                                vulnName = vulnsDict[element][0]
                                vulnCVEList = vulnsDict[element][1]
                                stats += '\t\t' + beforeStaticLabel + vulnName + ' ('
                                for vulnCVE in vulnCVEList: 
1432
                                    stats += vulnCVE + ',' 
1433
                                stats = stats[:-1] + '): ' + self.resetColor + str(elements[element]) + newLine
1434
                            else:
1435 1436 1437
                                stats += '\t\t' + beforeStaticLabel + element + ': ' + self.resetColor + str(elements[element]) + newLine
                if not self.avoidOutputColors:
                    beforeStaticLabel = self.staticColor
1438 1439
                urls = statsVersion['URLs']
                if urls != None:
1440
                    stats += newLine + beforeStaticLabel + '\tFound URLs:' + self.resetColor + newLine
1441 1442 1443
                    for url in urls:
                        stats += '\t\t' + url + newLine
                stats += newLine * 2           
1444
            self.log_output('info ' + argv, stats)
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
            return False
        elif len(args) == 1:
            version = None
        elif len(args) == 2:
            version = args[1]
        else:
            self.help_info()
            return False
        id = args[0]
        if (not id.isdigit() and id != 'trailer' and id != 'xref') or (version != None and not version.isdigit()):
            self.help_info()
            return False
        if version != None:
            version = int(version)
            if version > self.pdfFile.getNumUpdates():
1460
                message = '*** Error: The version number is not valid!!'
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
                self.log_output('info ' + argv, message)
                return False
        if id == 'xref':
            statsDict = {}
            ret = self.pdfFile.getXrefSection(version)
            if ret == None or ret[1] == None or ret[1] == [] or ret[1] == [None,None]:
                message = '*** Error: xref section not found!!'
                self.log_output('info ' + argv, message)
                return False
            else:
                xrefArray = ret[1]
            if xrefArray[0] != None:
                statsDict = xrefArray[0].getStats()
            if xrefArray[1] != None:    
                statsStream = xrefArray[1].getStats()
                for key in statsStream:
                    if not statsDict.has_key(key):
                        statsDict[key] = statsStream[key]
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
            if statsDict['Offset'] != None:
                stats += beforeStaticLabel + 'Offset: ' + self.resetColor + statsDict['Offset'] + newLine
            stats += beforeStaticLabel + 'Size: ' + self.resetColor + statsDict['Size'] + newLine
            if statsDict['Stream'] != None:
                stats += beforeStaticLabel + 'Stream: ' + self.resetColor + statsDict['Stream'] + newLine
            else:
                stats += beforeStaticLabel + 'Stream: ' + self.resetColor + 'No' + newLine
            numSubSections = len(statsDict['Subsections'])
            stats += beforeStaticLabel + 'Subsections: ' + self.resetColor + str(numSubSections) + newLine
            for i in range(numSubSections):
                subStats = statsDict['Subsections'][i]
                stats += beforeStaticLabel + '\tSubsection ' + self.resetColor + str(i+1) + ':' + newLine
                stats += beforeStaticLabel + '\t\tEntries: ' + self.resetColor + subStats['Entries'] + newLine
                if subStats['Errors'] != None:
                    stats += beforeStaticLabel + '\t\tErrors: ' + self.resetColor + subStats['Errors'] + newLine
            if statsDict['Errors'] != None:
                stats += beforeStaticLabel + 'Errors: ' + self.resetColor + statsDict['Errors'] + newLine
1496 1497 1498 1499
        elif id == 'trailer':
            statsDict = {}
            ret = self.pdfFile.getTrailer(version)
            if ret == None or ret[1] == None or ret[1] == [] or ret[1] == [None,None]:
1500
                message = '*** Error: Trailer not found!!'
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
                self.log_output('info ' + argv, message)
                return False
            else:
                trailerArray = ret[1]
            if trailerArray[0] != None:
                statsDict = trailerArray[0].getStats()
            if trailerArray[1] != None:    
                statsStream = trailerArray[1].getStats()
                for key in statsStream:
                    if not statsDict.has_key(key):
                        statsDict[key] = statsStream[key]
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
            if statsDict['Offset'] != None:
                stats += beforeStaticLabel + 'Offset: ' + self.resetColor + statsDict['Offset'] + newLine
            stats += beforeStaticLabel + 'Size: ' + self.resetColor + statsDict['Size'] + newLine
            if statsDict['Stream'] != None:
                stats += beforeStaticLabel + 'Stream: ' + self.resetColor + statsDict['Stream'] + newLine
            else:
                stats += beforeStaticLabel + 'Stream: ' + self.resetColor + 'No' + newLine
            stats += beforeStaticLabel + 'Objects: ' + statsDict['Objects'] + newLine
            if statsDict['Root Object'] != None:
                stats += beforeStaticLabel + 'Root Object: ' + self.resetColor + statsDict['Root Object'] + newLine
            else:
                stats += beforeStaticLabel + 'Root Object: ' + self.resetColor + 'No' + newLine
            if statsDict['Info Object'] != None:
                stats += beforeStaticLabel + 'Info Object: ' + self.resetColor + statsDict['Info Object'] + newLine
            else:
                stats += beforeStaticLabel + 'Info Object: ' + self.resetColor + 'No' + newLine
            if statsDict['ID'] != None:
                stats += beforeStaticLabel + 'ID: ' + self.resetColor + statsDict['ID'] + newLine
            if statsDict['Encrypted']:
                stats += beforeStaticLabel + 'Encrypted: ' + self.resetColor + 'Yes' + newLine
            else:
                stats += beforeStaticLabel + 'Encrypted: ' + self.resetColor + 'No' + newLine
            if statsDict['Errors'] != None:
                stats += beforeStaticLabel + 'Errors: ' + self.resetColor + statsDict['Errors'] + newLine            
1536 1537 1538 1539
        else:
            id = int(id)
            indirectObject = self.pdfFile.getObject(id, version, indirect = True)
            if indirectObject == None:
1540
                message = '*** Error: Object not found!!'
1541 1542 1543 1544
                self.log_output('info ' + argv, message)
                return False
            statsDict = indirectObject.getStats()
            if statsDict['Offset'] != None:
1545 1546 1547 1548
                stats += beforeStaticLabel + 'Offset: ' + self.resetColor  + statsDict['Offset'] + newLine
            stats += beforeStaticLabel + 'Size: ' + self.resetColor  + statsDict['Size'] + newLine
            stats += beforeStaticLabel + 'MD5: ' + self.resetColor  + statsDict['MD5'] + newLine
            stats += beforeStaticLabel + 'Object: ' + self.resetColor  + statsDict['Object'] + newLine
1549 1550
            if statsDict['Object'] in ['dictionary','stream']:
                if statsDict['Type'] != None:
1551
                    stats += beforeStaticLabel + 'Type: ' + self.resetColor  + statsDict['Type'] + newLine
1552
                if statsDict['Subtype'] != None:
1553
                    stats += beforeStaticLabel + 'Subtype: ' + self.resetColor  + statsDict['Subtype'] + newLine
1554
                if statsDict['Object'] == 'stream':
1555
                    stats += beforeStaticLabel + 'Stream MD5: ' + self.resetColor  + statsDict['Stream MD5'] + newLine
1556
                    if statsDict['Stream MD5'] != statsDict['Raw Stream MD5']:
1557 1558
                        stats += beforeStaticLabel + 'Raw Stream MD5: ' + self.resetColor  + statsDict['Raw Stream MD5'] + newLine
                    stats += beforeStaticLabel + 'Length: ' + self.resetColor  + statsDict['Length'] + newLine
1559
                    if statsDict['Real Length'] != None:
1560
                        stats += beforeStaticLabel + 'Real length: ' + self.resetColor  + statsDict['Real Length'] + newLine
1561
                    if statsDict['Encoded']:
1562
                        stats += beforeStaticLabel + 'Encoded: ' + self.resetColor + 'Yes' + newLine
1563
                        if statsDict['Stream File'] != None:
1564 1565
                            stats += beforeStaticLabel + 'Stream File: ' + self.resetColor  + statsDict['Stream File'] + newLine
                        stats += beforeStaticLabel + 'Filters: ' + self.resetColor  + statsDict['Filters'] + newLine
1566
                        if statsDict['Filter Parameters']:
1567
                            stats += beforeStaticLabel + 'Filter Parameters: ' + self.resetColor + 'Yes' + newLine
1568
                        else:
1569
                            stats += beforeStaticLabel + 'Filter Parameters: ' + self.resetColor + 'No' + newLine
1570
                        if statsDict['Decoding Errors']:
1571
                            stats += beforeStaticLabel + 'Decoding errors: ' + self.resetColor + 'Yes' + newLine
1572
                        else:
1573
                            stats += beforeStaticLabel + 'Decoding errors: ' + self.resetColor + 'No' + newLine
1574
                    else:
1575
                        stats += beforeStaticLabel + 'Encoded: ' + self.resetColor + 'No' + newLine
1576 1577
            if statsDict['Object'] != 'stream':
                if statsDict['Compressed in'] != None:
1578
                    stats += beforeStaticLabel + 'Compressed in: ' + self.resetColor  + statsDict['Compressed in'] + newLine
1579 1580
            if statsDict['Object'] == 'dictionary':
                if statsDict['Action type'] != None:
1581 1582
                    stats += beforeStaticLabel + 'Action type: ' + self.resetColor  + statsDict['Action type'] + newLine
            stats += beforeStaticLabel + 'References: ' + self.resetColor  + statsDict['References'] + newLine
1583
            if statsDict['JSCode']:
1584
                stats += beforeStaticLabel + 'JSCode: ' + self.resetColor + 'Yes' + newLine
1585
                if statsDict['Escaped Bytes']:
1586
                    stats += beforeStaticLabel + 'Escaped bytes: ' + self.resetColor + 'Yes' + newLine
1587
                if statsDict['URLs']:
1588
                    stats += beforeStaticLabel + 'URLs: ' + self.resetColor + 'Yes' + newLine
1589 1590
            if statsDict['Errors']:
                if statsDict['Object'] == 'stream':
1591
                    stats += beforeStaticLabel + 'Parsing Errors: ' + self.resetColor  + statsDict['Errors'] + newLine
1592
                else:
1593 1594
                    stats += beforeStaticLabel + 'Errors: ' + self.resetColor  + statsDict['Errors'] + newLine
        self.log_output('info ' + argv, stats)        
1595 1596 1597 1598 1599 1600 1601
        
    def help_info(self):
        print newLine + 'Usage: info [$object_id|xref|trailer [$version]]'
        print newLine + 'Shows information of the file or object ($object_id, xref, trailer)' + newLine

    def do_js_analyse(self, argv):
        content = ''
1602
        validTypes = ['variable','file','object','code']
1603
        if not JS_MODULE:
1604
            message = '*** Error: PyV8 is not installed!!'
1605 1606 1607 1608
            self.log_output('js_analyse ' + argv, message)
            return False
        args = self.parseArgs(argv)
        if args == None:
1609
            message = '*** Error: The command line arguments have not been parsed successfully!!'
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
            self.log_output('js_analyse ' + argv, message)
            return False
        if len(args) == 2:
            version = None
        elif len(args) == 3 and args[0] == 'object':
            version = args[2]
        else:
            self.help_js_analyse()
            return False
        type = args[0]
        src = args[1]
        if type not in validTypes:
            self.help_js_analyse()
            return False
        if type == 'variable':
            if not self.variables.has_key(src):
1626
                message = '*** Error: The variable does not exist!!'
1627 1628 1629 1630 1631 1632 1633 1634
                self.log_output('js_analyse ' + argv, message)
                return False
            else:
                content = self.variables[src][0]
                if not isJavascript(content):
                    if self.use_rawinput:
                        res = raw_input('The variable may not contain Javascript code, do you want to continue? (y/n) ')
                        if res.lower() == 'n':
1635
                            message = '*** Error: The variable does not contain Javascript code!!'
1636 1637 1638 1639 1640 1641
                            self.log_output('js_analyse ' + argv, message)
                            return False
                    else:
                        print 'Warning: the object may not contain Javascript code...' + newLine
        elif type == 'file':
            if not os.path.exists(src):
1642
                message = '*** Error: The file does not exist!!'
1643 1644 1645 1646 1647 1648 1649 1650
                self.log_output('js_analyse ' + argv, message)
                return False
            else:
                content = open(src,'rb').read()
                if not isJavascript(content):
                    if self.use_rawinput:
                        res = raw_input('The file may not contain Javascript code, do you want to continue? (y/n) ')
                        if res.lower() == 'n':
1651
                            message = '*** Error: The file does not contain Javascript code!!'
1652 1653 1654 1655
                            self.log_output('js_analyse ' + argv, message)
                            return False                
                    else:
                        print 'Warning: the object may not contain Javascript code...' + newLine
1656
        elif type == 'object':
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
            if self.pdfFile == None:
                message =