User File #45899820641476932

Upload All User Files

#45899820641476932 - JPC-rr lua script to search for strings in memory

stringsearchmemory.lua
System: DOS
828 downloads
Uploaded 3/21/2018 2:15 AM by c-square (see all 10)
JPC-rr lua script to search for strings in memory
--inputFilename="outputfile.out" -- Uncomment for searches after the first one
outputFilename="outputfile.out" -- Rename output file for subseqent searches
value = "Francisco 49ers" -- String to search for

searchLevel="byte"
maxMemory = 2090000

function string:split( inSplitPattern, outResults )
  if not outResults then
    outResults = { }
  end
  local theStart = 1
  local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  while theSplitStart do
    table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
    theStart = theSplitEnd + 1
    theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  end
  table.insert( outResults, string.sub( self, theStart ) )
  return outResults
end

function searchFor(byteList, value, filename)
	print("Searching " .. #byteList .. " values for '" .. value .. "'");
	local input=1
	local output=0
	print("Opening Output File")
	outputBinary=io.open_write(filename)
	print("Output File Open")
	while byteList[input] do
		wordFound = 1
		for i=1, #value do
			if searchLevel=="word" then
				if jpcrr.read_word(byteList[input]+(i*2)-2) ~= string.byte(value,i) then
					wordFound=0
					break
				end
			elseif searchLevel=="byte" then		
				if jpcrr.read_byte(byteList[input]+i-1) ~= string.byte(value,i) then
					wordFound=0
					break
				end
			else 
				print("ERROR! Unknown searchLevel: " .. searchLevel)
			end
		end
		if wordFound == 1 then
			print("Word Found: " .. byteList[input])
			if output == 0 then
				outputBinary.write("?",byteList[input])
			else
				outputBinary.write("?",","..byteList[input])
			end
			output = output + 1
		end
		if input % 10000 == 0 then
			print(input)
		end
		input=input+1
	end
	print("Search Complete!")
	print("Found " .. output .. " Matches")
	outputBinary:close()
end

-- Main
if inputFilename then
	print("Opening input file")
	inputBinary, error=io.open_read(inputFilename)
	print("Spitting input")
	searchStr = inputBinary.read()
	inputBinary:close()
	searchList = searchStr:split(",")
	print("Input split into " .. #searchList .. " items")
end
if not searchList then
	searchList={}
	for i=1,maxMemory do
		searchList[i]=i
	end
end
searchFor(searchList, value, outputFilename)