示例#1
0
def ReadCharacteristicsFromData(f, limit, typeID, ndim):
    cStartPosition = f.tell()
    dataTypeName = bp4dbg_utils.GetTypeName(typeID)
    # 1 byte NCharacteristics
    nCharacteristics = np.fromfile(f, dtype=np.uint8, count=1)[0]
    print("      # of Characteristics    : {0}".format(nCharacteristics))
    # 4 bytes length
    charLen = np.fromfile(f, dtype=np.uint32, count=1)[0]
    print("      Characteristics Length  : {0}".format(charLen))

    for i in range(nCharacteristics):
        print("      Characteristics[{0}]".format(i))
        # 1 byte TYPE
        cID = np.fromfile(f, dtype=np.uint8, count=1)[0]
        cName = bp4dbg_utils.GetCharacteristicName(cID)
        print("          Type        : {0} ({1}) ".format(cName, cID))
        if cName == 'value' or cName == 'min' or cName == 'max':
            if dataTypeName == 'string':
                namelimit = limit - (f.tell() - cStartPosition)
                status, s = ReadEncodedString(f, "String Value", namelimit)
                if not status:
                    return False
                print("          Value       : '" + s + "'")
            else:
                data = readDataToNumpyArray(f, dataTypeName, 1)
                print("          Value       : {0}".format(data[0]))
        elif cName == 'offset' or cName == 'payload_offset':
            data = readDataToNumpyArray(f, 'unsigned_long', 1)
            print("          Value       : {0}".format(data[0]))
        elif cName == 'time_index' or cName == 'file_index':
            data = readDataToNumpyArray(f, 'unsigned_integer', 1)
            print("          Value       : {0}".format(data[0]))
        elif cName == 'minmax':
            nBlocks = np.fromfile(f, dtype=np.uint16, count=1)[0]
            print("          nBlocks     : {0}".format(nBlocks))
            bminmax = readDataToNumpyArray(f, dataTypeName, 2)
            print("          Min/max     : {0} / {1}".format(
                bminmax[0], bminmax[1]))
            if nBlocks > 1:
                method = np.fromfile(f, dtype=np.uint8, count=1)[0]
                print("          Division method: {0}".format(method))
                blockSize = np.fromfile(f, dtype=np.uint64, count=1)[0]
                print("          Block size     : {0}".format(blockSize))
                div = np.fromfile(f, dtype=np.uint16, count=ndim)
                print("          Division vector: (", end="")
                for d in range(ndim):
                    print("{0}".format(div[d]), end="")
                    if d < ndim - 1:
                        print(", ", end="")
                    else:
                        print(")")
                minmax = readDataToNumpyArray(f, dataTypeName, 2 * nBlocks)
                for i in range(nBlocks):
                    print("          Min/max        : {0} / {1}".format(
                        minmax[2 * i], minmax[2 * i + 1]))
        else:
            print("                ERROR: could not understand this "
                  "characteristics type '{0}' id {1}".format(cName, cID))
    return True
示例#2
0
def ReadVarData(f, nElements, typeID, ldims, expectedSize, varsStartPosition,
                varsTotalLength):
    if typeID == 9:  # string type
        return ReadStringVarData(f, expectedSize, varsStartPosition)
    typeSize = bp4dbg_utils.GetTypeSize(typeID)
    if (typeSize == 0):
        print("ERROR: Cannot process variable data block with "
              "unknown type size")
        return False

    currentPosition = f.tell()
    print("      Payload offset  : {0}".format(currentPosition))
    nBytes = np.ones(1, dtype=np.uint64)
    nBytes[0] = nElements * typeSize
    if (currentPosition + nBytes[0] > varsStartPosition + varsTotalLength):
        print("ERROR: Variable data block of size would reach beyond all "
              "variable blocks")
        print("VarsStartPosition = {0} varsTotalLength = {1}".format(
            varsStartPosition, varsTotalLength))
        print("current Position = {0} var block length = {1}".format(
            currentPosition, nBytes[0]))
        # return False
    if (nBytes[0] != expectedSize):
        print("ERROR: Variable data block size does not equal the size "
              "calculated from var block length")
        print("Expected size = {0}  calculated size from dimensions = {1}".
              format(expectedSize, nBytes[0]))

    if nElements == 1:
        # single value. read and print
        value = readDataToNumpyArray(f, bp4dbg_utils.GetTypeName(typeID),
                                     nElements)
        print("      Payload (value) : {0} ({1} bytes)".format(
            value[0], nBytes[0]))
    else:
        # seek instead of reading for now
        f.read(nBytes[0])
        # f.seek(nBytes[0], 1)
        # data = readDataToNumpyArray(f, bp4dbg_utils.GetTypeName(typeID),
        #                            nElements)
        print("      Payload (array) : {0} bytes".format(nBytes[0]))

    return True
示例#3
0
def ReadVarData(f, nElements, typeID, ldims, varLen,
                varsStartPosition, varsTotalLength):
    if typeID == 9:  # string type
        return ReadStringVarData(f, varLen, varsStartPosition)
    typeSize = bp4dbg_utils.GetTypeSize(typeID)
    if (typeSize == 0):
        print("ERROR: Cannot process variable data block with "
              "unknown type size")
        return False

    currentPosition = f.tell()
    print("      Payload offset  : {0}".format(currentPosition))

    if (currentPosition + varLen > varsStartPosition + varsTotalLength):
        print("ERROR: Variable data block of size would reach beyond all "
              "variable blocks")
        print("VarsStartPosition = {0} varsTotalLength = {1}".format(
            varsStartPosition, varsTotalLength))
        print("current Position = {0} var block length = {1}".format(
            currentPosition, varLen))
        return False

    nBytes = int(varLen.item())

    if nElements == 1:
        # single value. read and print
        value = readDataToNumpyArray(f, bp4dbg_utils.GetTypeName(typeID),
                                     nElements)
        print("      Payload (value) : {0} ({1} bytes)".format(
            value[0], nBytes))
    else:
        # seek instead of reading for now
        # f.read(nBytes)
        f.seek(nBytes, 1)
        # data = readDataToNumpyArray(f, bp4dbg_utils.GetTypeName(typeID),
        #                            nElements)
        print("      Payload (array) : {0} bytes".format(nBytes))

    return True
示例#4
0
def ReadCharacteristicsFromData(f, limit, typeID):
    cStartPosition = f.tell()
    dataTypeName = bp4dbg_utils.GetTypeName(typeID)
    # 1 byte NCharacteristics
    nCharacteristics = np.fromfile(f, dtype=np.uint8, count=1)[0]
    print("      # of Characteristics    : {0}".format(nCharacteristics))
    # 4 bytes length
    charLen = np.fromfile(f, dtype=np.uint32, count=1)[0]
    print("      Characteristics Length  : {0}".format(charLen))

    for i in range(nCharacteristics):
        print("      Characteristics[{0}]".format(i))
        # 1 byte TYPE
        cID = np.fromfile(f, dtype=np.uint8, count=1)[0]
        cName = bp4dbg_utils.GetCharacteristicName(cID)
        print("          Type        : {0} ({1}) ".format(cName, cID))
        if cName == 'value' or cName == 'min' or cName == 'max':
            if dataTypeName == 'string':
                namelimit = limit - (f.tell() - cStartPosition)
                status, s = ReadEncodedString(f, "String Value", namelimit)
                if not status:
                    return False
                print("          Value       : '" + s + "'")
            else:
                data = readDataToNumpyArray(f, dataTypeName, 1)
                print("          Value       : {0}".format(data[0]))
        elif cName == 'offset' or cName == 'payload_offset':
            data = readDataToNumpyArray(f, 'unsigned_long', 1)
            print("          Value       : {0}".format(data[0]))
        elif cName == 'time_index' or cName == 'file_index':
            data = readDataToNumpyArray(f, 'unsigned_integer', 1)
            print("          Value       : {0}".format(data[0]))
        else:
            print("                ERROR: could not understand this "
                  "characteristics type '{0}' id {1}".format(cName, cID))
    return True
示例#5
0
def ReadAMD(f, attridx, attrsStartPosition, attrsTotalLength):
    startPosition = f.tell()
    print("  attr {0:5d}".format(attridx))
    print("      Starting offset : {0}".format(startPosition))
    # 4 bytes TAG
    tag = f.read(4)
    if (tag != b"[AMD"):
        print("  Tag: " + str(tag))
        print("ERROR: ATTR group does not start with [AMD")
        return False
    print("      Tag             : " + tag.decode('ascii'))

    # 8 bytes AMD Length
    amdlen = np.fromfile(f, dtype=np.uint32, count=1)[0]
    print("      Attr block size : {0} bytes (+4 for Tag)".format(amdlen))
    expectedAttrBlockLength = amdlen + 4  # [AMD is not included in amdlen
    if (startPosition + expectedAttrBlockLength >
            attrsStartPosition + attrsTotalLength):
        print("ERROR: There is not enough bytes inside this PG "
              "to read this Attr block")
        print("AttrsStartPosition = {0} attrsTotalLength = {1}".format(
            attrsStartPosition, attrsTotalLength))
        print("current attr's start position = {0} "
              "attr block length = {1}".format(
                  startPosition, expectedAttrBlockLength))
        return False

    # 4 bytes ATTR MEMBER ID
    memberID = np.fromfile(f, dtype=np.uint32, count=1)[0]
    print("      Member ID       : {0}".format(memberID))

    # ATTR NAME, 2 bytes length + string without \0
    sizeLimit = expectedAttrBlockLength - (f.tell() - startPosition)
    status, attrname = ReadEncodedString(f, "Attr Name", sizeLimit)
    if not status:
        return False
    print("      Attr Name       : " + attrname)

    # ATTR PATH, 2 bytes length + string without \0
    sizeLimit = expectedAttrBlockLength - (f.tell() - startPosition)
    status, attrpath = ReadEncodedString(f, "Attr Path", sizeLimit)
    if not status:
        return False
    print("      Attr Path       : " + attrpath)

    # isAttrAVar 1 byte, 'y' or 'n'
    isAttrAVar = f.read(1)
    if (isAttrAVar != b'y' and isAttrAVar != b'n'):
        print(
            "ERROR: Next byte for isAttrAVar must be 'y' or 'n' "
            "but it isn't = {0}".format(isAttrAVar))
        return False
    print("      Refers to Var?  : " + isAttrAVar.decode('ascii'))

    # 1 byte TYPE
    typeID = np.fromfile(f, dtype=np.uint8, count=1)[0]
    typeName = bp4dbg_utils.GetTypeName(typeID)
    print("      Type            : {0} ({1}) ".format(typeName, typeID))

    # Read Attribute data
    if typeName == 'string':
        sizeLimit = expectedAttrBlockLength - (f.tell() - startPosition)
        status, s = ReadEncodedString(
            f, "Attribute String Value", sizeLimit, 4)
        if not status:
            return False
        print("      Value           : '" + s + "'")

    elif typeName == 'string_array':
        nElems = np.fromfile(f, dtype=np.uint32, count=1)[0]
        sizeLimit = expectedAttrBlockLength - (f.tell() - startPosition)
        status, strList = ReadEncodedStringArray(
            f, "Attribute String Array", sizeLimit, nElems)
        if not status:
            return False
        print("      Value           : [", end="")
        for j in range(len(strList)):
            print("'" + strList[j] + "'", end="")
            if j < len(strList) - 1:
                print(", ", end="")
        print("]")
    else:
        nBytes = np.fromfile(f, dtype=np.uint32, count=1)[0]
        typeSize = bp4dbg_utils.GetTypeSize(typeID)
        nElems = int(nBytes / typeSize)
        data = readDataToNumpyArray(f, typeName, nElems)
        print("      Value           : [", end="")
        for j in range(nElems):
            print("{0}".format(data[j]), end="")
            if j < nElems - 1:
                print(", ", end="")
        print("]")

    # End TAG AMD]
    tag = f.read(4)
    if (tag != b"AMD]"):
        print("  Tag: " + str(tag))
        print("ERROR: PG group metadata does not end with AMD]")
        return False
    print("      Tag             : {0}".format(tag.decode('ascii')))

    return True
示例#6
0
def ReadVMD(f, varidx, varsStartPosition, varsTotalLength):
    startPosition = f.tell()
    print("  Var {0:5d}".format(varidx))
    print("      Starting offset : {0}".format(startPosition))
    # 4 bytes TAG
    tag = f.read(4)
    if (tag != b"[VMD"):
        print("  Tag: " + str(tag))
        print("ERROR: VAR group does not start with [VMD")
        return False
    print("      Tag             : " + tag.decode('ascii'))

    # 8 bytes VMD Length
    vmdlen = np.fromfile(f, dtype=np.uint64, count=1)[0]
    print("      Var block size  : {0} bytes (+4 for Tag)".format(vmdlen))
    expectedVarBlockLength = vmdlen + 4  # [VMD is not included in vmdlen

    if (startPosition + expectedVarBlockLength >
            varsStartPosition + varsTotalLength):
        print("ERROR: There is not enough bytes inside this PG to read "
              "this Var block")
        print("VarsStartPosition = {0} varsTotalLength = {1}".format(
            varsStartPosition, varsTotalLength))
        print("current var's start position = {0} var block length = {1}".
              format(startPosition, expectedVarBlockLength))
        return False

    # 4 bytes VAR MEMBER ID
    memberID = np.fromfile(f, dtype=np.uint32, count=1)[0]
    print("      Member ID       : {0}".format(memberID))

    # VAR NAME, 2 bytes length + string without \0
    sizeLimit = expectedVarBlockLength - (f.tell() - startPosition)
    status, varname = ReadEncodedString(f, "Var Name", sizeLimit)
    if not status:
        return False
    print("      Var Name        : " + varname)

    # VAR PATH, 2 bytes length + string without \0
    sizeLimit = expectedVarBlockLength - (f.tell() - startPosition)
    status, varpath = ReadEncodedString(f, "Var Path", sizeLimit)
    if not status:
        return False
    print("      Var Path        : " + varpath)

    # 1 byte TYPE
    typeID = np.fromfile(f, dtype=np.uint8, count=1)[0]
    print("      Type            : {0} ({1}) ".format(
        bp4dbg_utils.GetTypeName(typeID), typeID))

    # ISDIMENSIONS 1 byte, 'y' or 'n'
    isDimensionVar = f.read(1)
    if (isDimensionVar != b'y' and isDimensionVar != b'n'):
        print(
            "ERROR: Next byte for isDimensionVar must be 'y' or 'n' "
            "but it isn't = {0}".format(isDimensionVar))
        return False
    print("      isDimensionVar  : " + isDimensionVar.decode('ascii'))

    # 1 byte NDIMENSIONS
    ndims = np.fromfile(f, dtype=np.uint8, count=1)[0]
    print("      # of Dimensions : {0}".format(
        ndims))

    # DIMLENGTH
    dimsLen = np.fromfile(f, dtype=np.uint16, count=1)[0]
    print("      Dims Length     : {0}".format(
        dimsLen))

    nElements = np.uint64(1)
    ldims = np.zeros(ndims, dtype=np.uint64)
    isLocalValueArray = False
    for i in range(ndims):
        print("      Dim[{0}]".format(i))
        # Read Local Dimensions (1 byte flag + 8 byte value)
        # Is Dimension a variable ID 1 byte, 'y' or 'n' or '\0'
        isDimensionVarID = f.read(1)
        if (isDimensionVarID != b'y' and isDimensionVarID != b'n' and
                isDimensionVarID != b'\0'):
            print(
                "ERROR: Next byte for isDimensionVarID must be 'y' or 'n' "
                "but it isn't = {0}".format(isDimensionVarID))
            return False
        if (isDimensionVarID == b'\0'):
            isDimensionVarID = b'n'
        ldims[i] = np.fromfile(f, dtype=np.uint64, count=1)[0]
        print("           local  dim : {0}".format(ldims[i]))
        nElements = nElements * ldims[i]
        # Read Global Dimensions (1 byte flag + 8 byte value)
        # Is Dimension a variable ID 1 byte, 'y' or 'n' or '\0'
        isDimensionVarID = f.read(1)
        if (isDimensionVarID != b'y' and isDimensionVarID != b'n' and
                isDimensionVarID != b'\0'):
            print(
                "ERROR: Next byte for isDimensionVarID must be 'y' or 'n' "
                "but it isn't = {0}".format(isDimensionVarID))
            return False
        if (isDimensionVarID == b'\0'):
            isDimensionVarID = b'n'
        gdim = np.fromfile(f, dtype=np.uint64, count=1)[0]
        if i == 0 and ldims[i] == 0 and gdim == bp4dbg_utils.LocalValueDim:
            print("           global dim : LocalValueDim ({0})".format(gdim))
            isLocalValueArray = True
        else:
            print("           global dim : {0}".format(gdim))

        # Read Offset Dimensions (1 byte flag + 8 byte value)
        # Is Dimension a variable ID 1 byte, 'y' or 'n' or '\0'
        isDimensionVarID = f.read(1)
        if (isDimensionVarID != b'y' and isDimensionVarID != b'n' and
                isDimensionVarID != b'\0'):
            print(
                "ERROR: Next byte for isDimensionVarID must be 'y' or 'n' "
                "but it isn't = {0}".format(isDimensionVarID))
            return False
        if (isDimensionVarID == b'\0'):
            isDimensionVarID = b'n'
        offset = np.fromfile(f, dtype=np.uint64, count=1)[0]
        print("           offset dim : {0}".format(offset))

    sizeLimit = expectedVarBlockLength - (f.tell() - startPosition)
    status = ReadCharacteristicsFromData(f, sizeLimit, typeID)
    if not status:
        return False

    # Padded end TAG
    # 1 byte length of tag
    endTagLen = np.fromfile(f, dtype=np.uint8, count=1)[0]
    tag = f.read(endTagLen)
    if (not tag.endswith(b"VMD]")):
        print("  Tag: " + str(tag))
        print("ERROR: VAR group metadata does not end with VMD]")
        return False
    print("      Tag (pad {0:2d})    : {1}".format(
        endTagLen - 4, tag.decode('ascii')))

    # special case: LocalValueDim: local values turned into 1D global array
    # but it seems there is no data block at all for these variables
    if isLocalValueArray:
        ldims[0] = 1
        nElements = np.uint64(1)
    else:
        expectedVarDataSize = expectedVarBlockLength - \
            (f.tell() - startPosition)
        status = ReadVarData(f, nElements, typeID, ldims, expectedVarDataSize,
                             varsStartPosition, varsTotalLength)
    if not status:
        return False

    return True
示例#7
0
def ReadCharacteristicsFromMetaData(buf, idx, pos, limit, typeID, fileOffset,
                                    isVarCharacteristics):
    cStartPosition = pos
    dataTypeName = bp4dbg_utils.GetTypeName(typeID)
    print("        Block {0}: ".format(idx))
    print("            Starting offset : {0}".format(fileOffset))
    # 1 byte NCharacteristics
    nChars = np.frombuffer(buf, dtype=np.uint8, count=1, offset=pos)[0]
    pos = pos + 1
    print("            # of Characteristics    : {0}".format(nChars))
    # 4 bytes length
    charLen = np.frombuffer(buf, dtype=np.uint8, count=32, offset=pos)[0]
    pos = pos + 4
    print("            Characteristics Length  : {0}".format(charLen))

    # For attributes, we need to remember the dimensions and size
    # when reading the value
    ndim = 0
    nElems = 1

    for i in range(nChars):
        print("            Characteristics[{0}]".format(i))
        # 1 byte TYPE
        cID = np.frombuffer(buf, dtype=np.uint8, count=1, offset=pos)[0]
        pos = pos + 1
        cName = bp4dbg_utils.GetCharacteristicName(cID)
        print("                Type        : {0} ({1}) ".format(cName, cID))
        cLen = bp4dbg_utils.GetCharacteristicDataLength(cID, typeID)

        if cName == 'dimensions':
            status, pos, ndim, lgo = ReadDimensionCharacteristics(buf, pos)
            if not status:
                return status, pos
            if ndim > 0:
                print("                Dims (lgo)  : (", end="")
                for d in range(ndim):
                    p = 3 * d
                    nElems = int(nElems * lgo[p])  # need for value later
                    print("{0}:{1}:{2}".format(lgo[p], lgo[p + 1], lgo[p + 2]),
                          end="")
                    if d < ndim - 1:
                        print(", ", end="")
                    else:
                        print(")")

        elif cName == 'value' or cName == 'min' or cName == 'max':
            if dataTypeName == 'string':
                namelimit = limit - (pos - cStartPosition)
                status, s, sLen, pos = ReadEncodedStringFromBuffer(
                    buf, pos, "String Value", namelimit)
                if not status:
                    return False, pos
                print("                Value       : '" + s +
                      "' ({0} bytes)".format(sLen))
            elif dataTypeName == 'string_array':
                namelimit = limit - (pos - cStartPosition)
                status, strList, pos = ReadEncodedStringArrayFromBuffer(
                    buf, pos, "String Array", namelimit, lgo[0])
                if not status:
                    return False, pos
                print("                Value       : [", end="")
                for j in range(len(strList)):
                    print("'" + strList[j] + "'", end="")
                    if j < len(strList) - 1:
                        print(", ", end="")
                print("]")

            else:
                if (isVarCharacteristics):
                    cData = buf[pos:pos + cLen]
                    pos = pos + cLen
                    data = bDataToNumpyArray(cData, dataTypeName, 1)
                    print("                Value       : {0}"
                          "  ({1} bytes)".format(data[0], cLen))
                else:  # attribute value characteristics are different
                    dataTypeSize = bp4dbg_utils.GetTypeSize(typeID)
                    nBytes = int(nElems * dataTypeSize)
                    cData = buf[pos:pos + nBytes]
                    pos = pos + nBytes
                    data = bDataToNumpyArray(cData, dataTypeName, nElems)
                    print("                Value       : [", end="")
                    for j in range(nElems):
                        print("{0}".format(data[j]), end="")
                        if j < nElems - 1:
                            print(", ", end="")
                    print("]")

        elif cName == 'offset' or cName == 'payload_offset':
            cData = buf[pos:pos + cLen]
            pos = pos + cLen
            data = bDataToNumpyArray(cData, 'unsigned_long', 1)
            print("                Value       : {0}  ({1} bytes)".format(
                data[0], cLen))
        elif cName == 'time_index' or cName == 'file_index':
            cData = buf[pos:pos + cLen]
            pos = pos + cLen
            data = bDataToNumpyArray(cData, 'unsigned_integer', 1)
            print("                Value       : {0}  ({1} bytes)".format(
                data[0], cLen))
        else:
            print("                ERROR: could not understand this "
                  "characteristics type '{0}' id {1}".format(cName, cID))
    return True, pos
示例#8
0
def ReadAttrMD(buf, idx, pos, limit, attrStartOffset):
    # Read one ATTR index group
    attrStartPosition = pos
    print("    Attr {0}: ".format(idx))
    print("        Starting offset : {0}".format(attrStartOffset))

    # 4 bytes ATTR Index Length
    attrLength = np.frombuffer(buf, dtype=np.uint32, count=1, offset=pos)[0]
    pos = pos + 4
    print("        Attr idx length : {0} bytes (+4 for idx length)".format(
        attrLength))
    if (attrStartPosition + attrLength + 4 > limit):
        print("ERROR: There is not enough bytes in Attr index block "
              "to read this single Attr index")
        return False, pos

    memberID = np.frombuffer(buf, dtype=np.uint32, count=1, offset=pos)[0]
    pos = pos + 4
    print("        MemberID        : {0}".format(memberID))

    namelimit = limit - (pos - attrStartPosition)
    status, grpName, grpNameLen, pos = ReadEncodedStringFromBuffer(
        buf, pos, "Group Name", namelimit)
    if not status:
        return False, pos
    print("        Group Name      : '" + grpName +
          "' ({0} bytes)".format(grpNameLen))

    namelimit = limit - (pos - attrStartPosition)
    status, attrName, attrNameLen, pos = ReadEncodedStringFromBuffer(
        buf, pos, "Attr Name", namelimit)
    if not status:
        return False, pos
    print("        Attr Name       : '" + attrName +
          "' ({0} bytes)".format(attrNameLen))

    # print("        Current offset : {0}".format(attrStartOffset + pos))
    namelimit = limit - (pos - attrStartPosition)
    status, attrPath, attrPathLen, pos = ReadEncodedStringFromBuffer(
        buf, pos, "Attr Path", namelimit)
    if not status:
        return False, pos
    print("        Attr Path       : '" + attrPath +
          "' ({0} bytes)".format(attrPathLen))

    # 1 byte TYPE
    typeID = buf[pos]
    pos = pos + 1
    print("        Type            : {0} ({1}) ".format(
        bp4dbg_utils.GetTypeName(typeID), typeID))

    # 8 byte Number of Characteristics Sets
    cSets = np.frombuffer(buf, dtype=np.uint64, count=1, offset=pos)[0]
    pos = pos + 8
    print("        # of blocks     : {0}".format(cSets))

    for i in range(cSets):
        # one characteristics block
        newlimit = limit - (pos - attrStartPosition)
        fileOffset = attrStartOffset + (pos - attrStartPosition)
        status, pos = ReadCharacteristicsFromMetaData(buf, i, pos, newlimit,
                                                      typeID, fileOffset,
                                                      False)
        if not status:
            return False

    return True, pos
示例#9
0
def ReadVarMD(buf, idx, pos, limit, varStartOffset):
    # Read one VAR index group
    varStartPosition = pos
    print("    Var {0}: ".format(idx))
    print("        Starting offset : {0}".format(varStartOffset))

    # 4 bytes VAR Index Length
    varLength = np.frombuffer(buf, dtype=np.uint32, count=1, offset=pos)[0]
    pos = pos + 4
    print("        Var idx length  : {0} bytes (+4 for idx length)".format(
        varLength))
    if (varStartPosition + varLength + 4 > limit):
        print("ERROR: There is not enough bytes in Var index block "
              "to read this single Var index")
        return False, pos

    memberID = np.frombuffer(buf, dtype=np.uint32, count=1, offset=pos)[0]
    pos = pos + 4
    print("        MemberID        : {0}".format(memberID))

    namelimit = limit - (pos - varStartPosition)
    status, grpName, grpNameLen, pos = ReadEncodedStringFromBuffer(
        buf, pos, "Group Name", namelimit)
    if not status:
        return False, pos
    print("        Group Name      : '" + grpName +
          "' ({0} bytes)".format(grpNameLen))

    namelimit = limit - (pos - varStartPosition)
    status, varName, varNameLen, pos = ReadEncodedStringFromBuffer(
        buf, pos, "Var Name", namelimit)
    if not status:
        return False, pos
    print("        Var Name        : '" + varName +
          "' ({0} bytes)".format(varNameLen))

    # print("        Current offset : {0}".format(varStartOffset + pos))
    # namelimit = limit - (pos - varStartPosition)
    # status, varPath, varPathLen, pos = ReadEncodedStringFromBuffer(
    #     buf, pos, "Var Path", namelimit)
    # if not status:
    #     return False, pos
    # print("        Var Path        : '" + varPath +
    #       "' ({0} bytes)".format(varPathLen))

    # 1 byte ORDER (K, C, F)
    order = buf[pos]  # this is an integer value
    pos = pos + 1
    if (order != ord('K') and order != ord('C') and order != ord('F')):
        print("ERROR: Next byte for Order must be 'K', 'C', or 'F' "
              "but it isn't = {0} (={1})".format(chr(order), order))
        return False
    print("        Order           : " + chr(order))

    # 1 byte UNUSED
    unused = buf[pos]  # this is an integer value
    pos = pos + 1
    print("        Unused byte     : {0}".format(unused))

    # 1 byte TYPE
    typeID = buf[pos]
    pos = pos + 1
    print("        Type            : {0} ({1}) ".format(
        bp4dbg_utils.GetTypeName(typeID), typeID))

    # 8 byte Number of Characteristics Sets
    cSets = np.frombuffer(buf, dtype=np.uint64, count=1, offset=pos)[0]
    pos = pos + 8
    print("        # of blocks     : {0}".format(cSets))

    for i in range(cSets):
        # one characteristics block
        newlimit = limit - (pos - varStartPosition)
        fileOffset = varStartOffset + (pos - varStartPosition)
        status, pos = ReadCharacteristicsFromMetaData(buf, i, pos, newlimit,
                                                      typeID, fileOffset, True)
        if not status:
            return False

    return True, pos
示例#10
0
def ReadCharacteristicsFromMetaData(buf, idx, pos, limit, typeID, fileOffset,
                                    isVarCharacteristics):
    cStartPosition = pos
    dataTypeName = bp4dbg_utils.GetTypeName(typeID)
    print("        Block {0}: ".format(idx))
    print("            Starting offset : {0}".format(fileOffset))
    # 1 byte NCharacteristics
    nChars = np.frombuffer(buf, dtype=np.uint8, count=1, offset=pos)[0]
    pos = pos + 1
    print("            # of Characteristics    : {0}".format(nChars))
    # 4 bytes length
    charLen = np.frombuffer(buf, dtype=np.uint8, count=32, offset=pos)[0]
    pos = pos + 4
    print("            Characteristics Length  : {0}".format(charLen))

    # For attributes, we need to remember the dimensions and size
    # when reading the value
    ndim = 0
    nElems = 1

    for i in range(nChars):
        print("            Characteristics[{0}]".format(i))
        # 1 byte TYPE
        cID = np.frombuffer(buf, dtype=np.uint8, count=1, offset=pos)[0]
        pos = pos + 1
        cName = bp4dbg_utils.GetCharacteristicName(cID)
        print("                Type           : {0} ({1}) ".format(cName, cID))
        cLen = bp4dbg_utils.GetCharacteristicDataLength(cID, typeID)

        if cName == 'dimensions':
            status, pos, ndim, lgo = ReadDimensionCharacteristics(buf, pos)
            if not status:
                return status, pos
            print("                # of Dims      : {0}".format(ndim))
            if ndim > 0:
                print("                Dims (lgo)     : (", end="")
                for d in range(ndim):
                    p = 3 * d
                    nElems = int(nElems * lgo[p])  # need for value later
                    print("{0}:{1}:{2}".format(lgo[p], lgo[p + 1], lgo[p + 2]),
                          end="")
                    if d < ndim - 1:
                        print(", ", end="")
                    else:
                        print(")")

        elif cName == 'value' or cName == 'min' or cName == 'max':
            if dataTypeName == 'string':
                namelimit = limit - (pos - cStartPosition)
                status, s, sLen, pos = ReadEncodedStringFromBuffer(
                    buf, pos, "String Value", namelimit)
                if not status:
                    return False, pos
                print("                Value          : '" + s +
                      "' ({0} bytes)".format(sLen))
            elif dataTypeName == 'string_array':
                namelimit = limit - (pos - cStartPosition)
                status, strList, pos = ReadEncodedStringArrayFromBuffer(
                    buf, pos, "String Array", namelimit, lgo[0])
                if not status:
                    return False, pos
                print("                Value          : [", end="")
                for j in range(len(strList)):
                    print("'" + strList[j] + "'", end="")
                    if j < len(strList) - 1:
                        print(", ", end="")
                print("]")

            else:
                if (isVarCharacteristics):
                    cData = buf[pos:pos + cLen]
                    pos = pos + cLen
                    data = bDataToNumpyArray(cData, dataTypeName, 1)
                    print("                Value          : {0}"
                          "  ({1} bytes)".format(data[0], cLen))
                else:  # attribute value characteristics are different
                    dataTypeSize = bp4dbg_utils.GetTypeSize(typeID)
                    nBytes = int(nElems * dataTypeSize)
                    cData = buf[pos:pos + nBytes]
                    pos = pos + nBytes
                    data = bDataToNumpyArray(cData, dataTypeName, nElems)
                    print("                Value          : [", end="")
                    for j in range(nElems):
                        print("{0}".format(data[j]), end="")
                        if j < nElems - 1:
                            print(", ", end="")
                    print("]")

        elif cName == 'offset' or cName == 'payload_offset':
            cData = buf[pos:pos + cLen]
            pos = pos + cLen
            data = bDataToNumpyArray(cData, 'unsigned_long', 1)
            print("                Value          : {0}  ({1} bytes)".format(
                data[0], cLen))
        elif cName == 'time_index' or cName == 'file_index':
            cData = buf[pos:pos + cLen]
            pos = pos + cLen
            data = bDataToNumpyArray(cData, 'unsigned_integer', 1)
            print("                Value          : {0}  ({1} bytes)".format(
                data[0], cLen))
        elif cName == 'minmax':
            nBlocks = np.frombuffer(buf, dtype=np.uint16, count=1,
                                    offset=pos)[0]
            print("                nBlocks        : {0}".format(nBlocks))
            pos = pos + 2
            bminmax = bDataToNumpyArray(buf, dataTypeName, 2, pos)
            pos = pos + 2 * cLen
            print("                Min/max        : {0} / {1}".format(
                bminmax[0], bminmax[1]))
            if nBlocks > 1:
                method = np.frombuffer(buf,
                                       dtype=np.uint8,
                                       count=1,
                                       offset=pos)[0]
                pos = pos + 1
                print("                Division method: {0}".format(method))
                blockSize = np.frombuffer(buf,
                                          dtype=np.uint64,
                                          count=1,
                                          offset=pos)[0]
                pos = pos + 8
                print("                Block size     : {0}".format(blockSize))
                div = np.frombuffer(buf,
                                    dtype=np.uint16,
                                    count=ndim,
                                    offset=pos)
                pos = pos + 2 * ndim
                print("                Division vector: (", end="")
                for d in range(ndim):
                    print("{0}".format(div[d]), end="")
                    if d < ndim - 1:
                        print(", ", end="")
                    else:
                        print(")")
                minmax = bDataToNumpyArray(buf, dataTypeName, 2 * nBlocks, pos)
                pos = pos + 2 * nBlocks * cLen
                for i in range(nBlocks):
                    print("                Min/max        : {0} / {1}".format(
                        minmax[2 * i], minmax[2 * i + 1]))
        elif cName == "transform_type":
            # Operator name (8 bit length)
            namelimit = limit - (pos - cStartPosition)
            status, s, sLen, pos = ReadEncodedStringFromBuffer(buf,
                                                               pos,
                                                               "Operator Name",
                                                               namelimit,
                                                               lenbytes=1)
            if not status:
                return False, pos
            print("                Operator       : '" + s +
                  "' ({0} bytes)".format(sLen))

            # 1 byte TYPE
            typeID = buf[pos]
            pos = pos + 1
            print("                Pre-type       : {0} ({1}) ".format(
                bp4dbg_utils.GetTypeName(typeID), typeID))

            # Pre-transform dimenstions
            status, pos, ndim, lgo = ReadDimensionCharacteristics(buf, pos)
            if not status:
                return status, pos
            print("                Pre-# of dims  : {0}".format(ndim))
            if ndim > 0:
                print("                Pre-Dims (lgo) : (", end="")
                for d in range(ndim):
                    p = 3 * d
                    nElems = int(nElems * lgo[p])  # need for value later
                    print("{0}:{1}:{2}".format(lgo[p], lgo[p + 1], lgo[p + 2]),
                          end="")
                    if d < ndim - 1:
                        print(", ", end="")
                    else:
                        print(")")

            # Operator specific metadata
            omdlen = np.frombuffer(buf, dtype=np.uint16, count=1,
                                   offset=pos)[0]
            pos = pos + 2
            print("                Op. data length: {0}".format(omdlen))
            pos = pos + omdlen
        else:
            print("                ERROR: could not understand this "
                  "characteristics type '{0}' id {1}".format(cName, cID))
    return True, pos