0

I am working on a school assignment that requires me to use the following code:

#!/usr/bin/python3

import re
jfif = re.compile(b'\xff\xd8\xff\xe0..JFIF\x00',
              flags = re.DOTALL)
exif = re.compile(b'\xff\xd8\xff\xe1..EXIF\x00',
              flags = re.DOTALL)
spiff = re.compile(b'\xff\xd8\xff\xe8..SPIFF\x00',
              flags = re.DOTALL)

f = open('manson.dd', 'rb')
sector = f.read(512)

count = 0

while len(sector) != 0:
     if jfif.match(sector):
         count += 1
         print('found JFIF at offset {:X}, sector {}'.format(
             f.tell()-512, (f.tell()  - 512) // 512))

     elif exif.match(sector):
         count += 1
         print('found EXIF at offset {:X}, sector {}'.format(
             f.tell()-512, (f.tell()  - 512) // 512))

     elif pdf.match(sector):
         pdfCount += 1
         print('found PDF at offset {:X}, sector {}'.format(
             f.tell()-512, (f.tell()  - 512) // 512))
     sector = f.read(512)

print("Found", count, "JPEG Files")

The assignment requires me to make a new script that finds GIF files and imports the actual search function into a script that prompts for the file and prints the result

This is what I have so far

Main Script

#!/usr/bin/python3

import search

fileName = input("Enter the file you would like to search: ")
f = open(fileName, 'rb')
sector = f.read(512)

search.crawl()

print("Found", count, "GIF Files")

Imported Code

#!/usr/bin/python3
import re

gif = re.compile(b'\x47\x49\x46\x38..GIF\x61',
              flags = re.DOTALL)
def crawl():
    count = 0
    while len(sector) != 0:
             if gif.match(sector):
                 count += 1
                 print('found GIF at offset {:X}, sector {}'.format(
                     f.tell()-512, (f.tell()  - 512) // 512))
                 sector = f.read(512) 

When I run the main script, I am told that "the local variable 'sector' is referenced before assignment'. I haven't the slightest idea how to rectify this.

1
  • put a sector = f.read(512) line before your while loop in crawl function Commented Nov 20, 2015 at 20:49

2 Answers 2

1

Instead of def crawl(), use def crawl(sector). And then instead of calling search.crawl(), call search.crawl(sector).

Also, you will encounter a similar error with count. So you should modify search.crawl(sector) to return count and then when you call search.crawl(sector) in main(), assign to variable count.

In main:

count = search.crawl(sector)

In search:

def crawl(sector):
    count = 0
    while len(sector) != 0:
             if gif.match(sector):
                 count += 1
                 print('found GIF at offset {:X}, sector {}'.format(
                     f.tell()-512, (f.tell()  - 512) // 512))
                 sector = f.read(512) 
    return count

The reason for this is because when you import the module search, it has no notion of the sector variable. The only way for main's sector to be sent into search.crawl() is for it to be passed in as a function parameter a la search.crawl(sector).

Sign up to request clarification or add additional context in comments.

1 Comment

I understand why that should work, but when I made those changes, instead of the error coming up, it just hangs until I kill it. It's like the while loop is going infinitely for some reason. Any thoughts?
1

sector variable isn't global, you can pass it to crawl function

def crawl(sector):

and then:

search.crawl(sector)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.