Hello, I'm new to python and have a requirement to decompress various NRV2B compressed files. I have the python functions for performing this decompression, however I am unsure how to get it working.

I have tried making this a module, importing it and then feeding my compressed files as a parameter (recordDataEncoded). I think I am going about this in the wrong way.

So my question is, how would one implement this?

def getBit(pos, recordDataEncoded, fourBytes, count):
#Get the bit at position count. If count == 0, reinitialize count and move to #next decompression.

    if count == 0:
        count = 31
        fourBytes = struct.unpack('<L', recordDataEncoded[pos:pos+4])[0]
        print 'Read Four Bytes: 0x%.8X at Pos: %d'%(fourBytes, pos)
        pos += 4

    else:
        count -= 1
    bit = ((fourBytes >> count ) & 1)

    return (bit, pos, fourBytes, count)


def secondDecode(recordDataEncoded):
    recordDataDecoded = ''
    sPos = 0
    dPos = 0
    lastMOff = 1
    shift = 0
    fourBytes = 0

    #Main Loop
    while True:
        if sPos >= len(recordDataEncoded):
            return recordDataDecoded
        print 'first shift is: 0x%x'%(shift)
        (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
        while(gb != 0):
            recordDataDecoded += recordDataEncoded[sPos]
            sPos += 1
            if sPos > len(recordDataEncoded):
                'Record Data Len Exceeded 1'
                return recordDataDecoded
            dPos += 1
            (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)

        #mOff calculation
        if sPos >= len(recordDataEncoded):
            return recordDataDecoded
        (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
        mOff = 2+gb
        if sPos >= len(recordDataEncoded):
            return recordDataDecoded
        (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
        while(gb == 0):
            if sPos >= len(recordDataEncoded):
                return recordDataDecoded
            (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
            mOff = 2*mOff + gb
            if sPos >= len(recordDataEncoded):
                return recordDataDecoded
            (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)


        if mOff == 2:
            mOff = lastMOff
        else:
            mOff = (mOff - 3) * 256 + ord(recordDataEncoded[sPos])
            sPos += 1
            if sPos > len(recordDataEncoded):
                'Record Data Len Exceeded 2'
                return recordDataDecoded
        
            if int(mOff) == -1:
                break;
            else:
                mOff += 1
                lastMOff = mOff


        #mLen calculation
        if sPos >= len(recordDataEncoded):
            return recordDataDecoded
        (mLen, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
        if sPos >= len(recordDataEncoded):
            return recordDataDecoded
        (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
        mLen = mLen*2 + gb
        if mLen == 0:
            mLen += 1
            if sPos >= len(recordDataEncoded):
                return recordDataDecoded
            (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
            mLen = 2*mLen + gb
            if sPos >= len(recordDataEncoded):
                return recordDataDecoded
            (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
            while (gb == 0):
                if sPos >= len(recordDataEncoded):
                    return recordDataDecoded
                (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
                mLen = 2*mLen + gb
                (gb, sPos, fourBytes, shift) = getBit(sPos, recordDataEncoded, fourBytes, shift)
            mLen += 2
        print 'mLen after loop is: 0x%.8X'%mLen

        mPos = dPos - mOff
        if mPos < 0:
            print 'mPos is negative'
            t = input()
            return recordDataDecoded
        if mPos > dPos:
            print 'Oops mPos exceeds dPos'
            t = input()
            return recordDataDecoded

        #Copy uncompressed data
        recordDataDecoded += recordDataDecoded[mPos]
        mPos += 1
        dPos += 1

        while mLen > 0:
            mLen -= 1
            recordDataDecoded += recordDataDecoded[mPos]
            dPos += 1
            mPos += 1

    return recordDataDecoded

Shouldn't you open the file, read the data and send the data as the input to secondDecode? say you save the module as nvr2b.py, then to decompress a file you should,

import nvr2b
# Open the Compressed File
cf = open( <compressedfilename>, 'rb' )
extractedData = nvr2b.secondDecode( cf.read() )
cf.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.