1

For a class assignment, I'm analyzing how Amazon's Kindle digital rights management implementation works as well as how to defeat it. In my research, I came across a set of Python scripts that extract out the book data from the encryption. It fits my needs for explaining the encryption-cracking part of my paper.

Problem is, I'm not fluent in Python or have any experience other than print 'Hello World'.

When working my way through the source code, I came across this snippet

def __init__(self, infile):
    # initial sanity check on file
    self.data_file = file(infile, 'rb').read()
    self.mobi_data = ''
    self.header = self.data_file[0:78]

    if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
        raise DrmException("invalid file format")

    self.magic = self.header[0x3C:0x3C+8]
    self.crypto_type = -1

My interpretation of the code goes like this:

  1. self.data_file is a byte array that is returned by read() on the file(infile, 'rb') call.
  2. self.header is the value of the first 79 bytes of the data file

The problem I'm having is just what does self.header[0x3C:0x3C+8] mean?

2
  • The length of [0:78] is 78, not 79. A slice doesn't include the ending value, so it's 0..77. Commented May 2, 2012 at 22:45
  • @MarkRansom, most code I work with has the last value inclusive, not exclusive. Thanks for the tip. Commented May 2, 2012 at 22:50

2 Answers 2

8

It's a normal slicing, like self.data_file[0:78], except uses hex literal as the offset. 0x3C is 60 in base10, so it's the same as self.header[60:60+8].

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

Comments

2

self.header[0x3C:0x3C+8] will get a string of 8 bytes from the header, starting at offset 0x3C.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.