Ejemplo n.º 1
0
def replaceBuffer_slow1(rfile:ReadBinaryFileWithABuffer, wfile:WriteFile, searchData:bytes, replaceData:bytes) :
    result=[]
    searchData_length= len(searchData)
    search_counter=0
    counter=0
    rfile.resetRead()
    while True:
        bufferData=rfile.read()
        if bufferData == b'':
            break
        for ii in range(0, len(bufferData)):
            if bufferData[ii:ii+1] == searchData[search_counter:search_counter+1]:
                if search_counter == searchData_length-1:
                    result.append( counter-search_counter )
                    search_counter = 0
                else:
                    search_counter += 1
            else:
                search_counter = 0
            counter += 1
    if len(result) > 0:
        rfile.resetRead()
        counter = 0
        add_bool = True
        add_bytes = b''
        pop_first = result.pop(0)
        pop_end  = pop_first + searchData_length
        while True:
            bufferData = rfile.read()
            if bufferData == b'':
                break
            #
            for jj in range(0, len(bufferData)):
                if counter == pop_first:
                    add_bool= False
                    add_bytes += replaceData
                elif counter == pop_end:
                    add_bool=True
                    if len(result) > 0:
                        pop_first = result.pop(0)
                        pop_end = pop_first + searchData_length
                        if counter == pop_first:
                            add_bool = False
                            add_bytes += replaceData
                if add_bool:
                    add_bytes += bufferData[jj:jj+1]
                counter += 1
            #
            if len( add_bytes ) > 0:
                wfile.appendByte(add_bytes)
            add_bytes = b''
    else:
        rfile.resetRead()
        while True:
            bufferData=rfile.read()
            if bufferData == b'':
                break
            wfile.writeByte(bufferData)
Ejemplo n.º 2
0
def deleteOneIndex(rfile:ReadBinaryFileWithABuffer, wfile:WriteFile, startIndex:int , stopIndex:int) :
    rfile.resetRead()
    while True:
        if rfile.getCurrent() >= rfile.file_seek_end:
            break
        if startIndex > rfile.getCurrent()+rfile.buffer or  rfile.getCurrent() >= stopIndex:
            wfile.appendByte( data=rfile.read() )
        else:
            if rfile.getCurrent() < startIndex:
                wfile.appendByte(data=readIndexBytes(filename=rfile.filename, startIndex=rfile.getCurrent(), stopIndex=startIndex))
            wfile.appendByte(data=rfile.readIndex(stopIndex))
Ejemplo n.º 3
0
def searchOneBuffer(file:ReadBinaryFileWithABuffer, searchData:bytes) -> int:
    searchData_length= len(searchData)
    search_counter=0
    counter=0
    file.resetRead()
    while True:
        bufferData=file.read()
        if bufferData == b'':
            break
        for ii in range(0, len(bufferData)):
            if bufferData[ii:ii+1] == searchData[search_counter:search_counter+1]:
                if search_counter == searchData_length-1:
                    return (counter-search_counter)
                else:
                    search_counter += 1
            else:
                search_counter = 0
            counter += 1
    return -1
Ejemplo n.º 4
0
def replaceBuffer_slow2(rfile:ReadBinaryFileWithABuffer, wfile:WriteFile, searchData:bytes, replaceData:bytes) :
    result=[]
    searchData_length= len(searchData)
    search_counter=0
    counter=0
    rfile.resetRead()
    while True:
        bufferData=rfile.read()
        if bufferData == b'':
            break
        for ii in range(0, len(bufferData)):
            if bufferData[ii:ii+1] == searchData[search_counter:search_counter+1]:
                if search_counter == searchData_length-1:
                    result.append( counter-search_counter )
                    search_counter = 0
                else:
                    search_counter += 1
            else:
                search_counter = 0
            counter += 1
    if len(result) > 0:
        pop_first = result.pop(0)
        pop_end = pop_first+searchData_length
        counter=0
        write_bool=True
        with open(file=rfile.filename, mode='rb') as readfile:
            with open(file=wfile.filename, mode='ab') as writefile:
                read_file_end = readfile.seek(0, 2)
                readfile.seek(0, 0)
                while True:
                    if counter >= read_file_end:
                        break
                    if counter == pop_first:
                        write_bool=False
                        writefile.write(replaceData)
                        writefile.flush()
                    elif counter == pop_end:
                        write_bool=True
                        if len(result) > 0:
                            pop_first = result.pop(0)
                            pop_end = pop_first + searchData_length
                            if counter == pop_first:
                                write_bool=True
                                writefile.write(replaceData)
                                writefile.flush()
                    bayt = readfile.read(1)
                    if bayt:
                        if write_bool:
                            writefile.write(bayt)
                            writefile.flush()
                    else:
                        break
                    counter += 1
    else:
        rfile.resetRead()
        while True:
            bufferData=rfile.read()
            if bufferData == b'':
                break
            wfile.appendByte(bufferData)
Ejemplo n.º 5
0
def replaceDelBuffer(rfile:ReadBinaryFileWithABuffer, wfile:WriteFile, searchData:bytes):
    result=[]
    searchData_length= len(searchData)
    search_counter=0
    counter=0
    rfile.resetRead()
    while True:
        bufferData=rfile.read()
        if bufferData == b'':
            break
        for ii in range(0, len(bufferData)):
            if bufferData[ii:ii+1] == searchData[search_counter:search_counter+1]:
                if search_counter == searchData_length-1:
                    result.append( counter-search_counter )
                    search_counter = 0
                else:
                    search_counter += 1
            else:
                search_counter = 0
            counter += 1
    if len(result) > 0:
        rfile.resetRead()
        startIndex  = result.pop(0)
        stopIndex = startIndex + searchData_length
        while True:
            if rfile.getCurrent() >= rfile.file_seek_end:
                break
            if startIndex > rfile.getCurrent() + rfile.buffer or rfile.getCurrent() >= stopIndex:
                wfile.appendByte(data=rfile.read())
            else:
                if rfile.getCurrent() < startIndex:
                    wfile.appendByte(data=readIndexBytes(filename=rfile.filename, startIndex=rfile.getCurrent(),
                                                         stopIndex=startIndex))
                rfile.setCurrent(seek=stopIndex)
                if len(result)>0:
                    startIndex = result.pop(0)
                    stopIndex = startIndex + searchData_length
                else:
                    wfile.appendByte(data=rfile.readIndex(stopIndex))
    else:
        rfile.resetRead()
        while True:
            bufferData=rfile.read()
            if bufferData == b'':
                break
            wfile.writeByte(bufferData)
Ejemplo n.º 6
0
def delBuffer(rfile:ReadBinaryFileWithABuffer, wfile:WriteFile, delList:list) :
    rfile.resetRead()
    if len(delList) > 0:
        rfile.resetRead()
        startStopIndex  = delList.pop(0)
        startIndex = startStopIndex[0]
        stopIndex = startStopIndex[1]
        while True:
            if rfile.getCurrent() >= rfile.file_seek_end:
                break
            if startIndex > rfile.getCurrent() + rfile.buffer or rfile.getCurrent() >= stopIndex:
                wfile.appendByte(data=rfile.read())
            else:
                if rfile.getCurrent() < startIndex:
                    wfile.appendByte(data=readIndexBytes(filename=rfile.filename, startIndex=rfile.getCurrent(),
                                                         stopIndex=startIndex))
                rfile.setCurrent(seek=stopIndex)
                if len(delList) > 0:
                    startStopIndex = delList.pop(0)
                    startIndex = startStopIndex[0]
                    stopIndex = startStopIndex[1]
                else:
                    wfile.appendByte(data=rfile.readIndex(stopIndex))
    else:
        rfile.resetRead()
        while True:
            bufferData=rfile.read()
            if bufferData == b'':
                break
            wfile.writeByte(bufferData)
def showIndexHexFile(readFile: ReadBinaryFileWithABuffer,
                     columnSize: int = 10,
                     linesHalt: int = -1,
                     index=0):
    readFile.resetRead()
    line_format = '\tNo:{:0>6d}-{:0>6d}\t <Ascii>||{:<}||\t <Hex>||{:<}||'
    line_index = 0
    line_old_index = 0
    if index > 0:
        index_start = (index - 1)
        counter = index_start
        counter_old = counter
        check_rest = counter % columnSize
    else:
        index_start = 0
        counter = 0
        counter_old = 0
        check_rest = 0
    turn = True
    while turn:
        if readFile.getCurrent() < readFile.file_seek_end:
            if index_start == counter:
                read_bytes = readFile.readIndex(index=index_start)
            else:
                read_bytes = readFile.read()
        else:
            break
        read_bytes_length = len(read_bytes)
        if read_bytes_length < 1:
            break
        for r_index in range(0, read_bytes_length):
            if counter % columnSize == check_rest:
                if counter != index_start:
                    print(
                        line_format.format(counter_old, counter, ascii_line,
                                           hex_line))
                    line_index += 1
                else:
                    nameTmp = "\t|| Filename: '{}' - Size: {} (bayt) ||".format(
                        readFile.filename, readFile.file_seek_end)
                    dashTmp1 = '\t' + '-' * ((len(nameTmp)) - 1)
                    dashTmp2 = dashTmp1 + '\n'
                    print(dashTmp1)
                    print(nameTmp)
                    print(dashTmp2)
                counter_old = counter + 1
                bayt = read_bytes[r_index:r_index + 1]
                hex_line = bayt.hex()
                char = bayt.decode(encoding="ascii", errors='replace')
                if char.isprintable() and char != '�':
                    ascii_line = char
                else:
                    ascii_line = "."
            else:
                bayt = read_bytes[r_index:r_index + 1]
                hex_line += ' ' + bayt.hex()
                char = bayt.decode(encoding="ascii", errors='replace')
                if char.isprintable() and char != '�':
                    ascii_line += char
                else:
                    ascii_line += "."
            if counter == readFile.file_seek_end - 1:
                if line_index == 0:
                    print(
                        line_format.format(counter_old, (counter + 1),
                                           ascii_line, hex_line))
                else:
                    addSpace = columnSize - len(ascii_line)
                    ascii_line += ' ' * addSpace
                    addSpace = (columnSize * 2) + (columnSize -
                                                   1) - len(hex_line)
                    hex_line += ' ' * addSpace
                    print(
                        line_format.format(counter_old, (counter + 1),
                                           ascii_line, hex_line))
            if linesHalt != -1 and counter != readFile.file_seek_end - 1:
                if line_index != line_old_index and line_index % linesHalt == 0:
                    calculate = int((counter / readFile.file_seek_end) * 10000)
                    percent = int((counter / readFile.file_seek_end) * 100)
                    percent_point = calculate - (percent * 100)
                    print()
                    print("\t%{:0>2d}.{:0>2d} has completed".format(
                        percent, percent_point))
                    input_char = input(
                        "\t[Hex-Editor] Continue <<enter>> or quit <<q>> :")
                    print()
                    if input_char.lower() == 'q':
                        turn = False
                        break
                    line_old_index = line_index
            counter += 1