Guest User

Big Endian Lua Functions Example (untested)

a guest
Apr 17th, 2017
257
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- main functions
  2.  
  3. -- https://gist.github.com/balaam/3122129
  4. function reverseTable(tbl)
  5.   for i=1, math.floor(#tbl / 2) do
  6.     tbl[i], tbl[#tbl - i + 1] = tbl[#tbl - i + 1], tbl[i]
  7.   end
  8. end
  9.  
  10. function readBigEndianBytes(address,n,ReturnAsTable)
  11.   local bytes = readBytes(address,n,true)
  12.   if not bytes then return nil end
  13.   reverseTable(bytes)
  14.   return ReturnAsTable and bytes or unpack(bytes)
  15. end
  16.  
  17. function WriteBytesBigEndian(address,bytes)
  18.   reverseTable(bytes)
  19.   return writeBytes(address,bytes)
  20. end
  21.  
  22. -- utility functions for certain sizes/types
  23.  
  24. function readWordBigEndian(address)
  25.     return byteTableToWord(readBytesBigEndian(address,2,true))
  26. end
  27.  
  28. function readIntegerBigEndian(address)
  29.     return byteTableToDword(readBytesBigEndian(address,4,true))
  30. end
  31.  
  32. function readDwordBigEndian(address) return readIntegerBigEndian(address) end
  33.  
  34. function readQwordBigEndian(address)
  35.   bytes = readBytes(address,8,true)
  36.   reverseTable(bytes)
  37.   return byteTableToQword(bytes)
  38. end
  39.  
  40. function readPointerBigEndian(address)
  41.   return Is64Bit and readQwordBigEndian(address) or readIntegerBigEndian(address)
  42. end
  43.  
  44. function readFloatBigEndian(address)
  45.   bytes = readBytes(address,4,true)
  46.   reverse(bytes)
  47.   return byteTableToFloat(bytes)
  48. end
  49.  
  50. function readDoubleBigEndian(address)
  51.   bytes = readBytes(address,8,true)
  52.   reverse(bytes)
  53.   return byteTableToDouble(bytes)
  54. end
  55.  
  56. function writeIntegerBigEndian(address,value)
  57.   return WriteBytesBigEndian(address,dwordToByteTable(value))
  58. end
  59.  
  60. function writeIntegerBigEndian(address,value)
  61.   return WriteBytesBigEndian(address,dwordToByteTable(value))
  62. end
  63.  
  64. function writeQwordBigEndian(address,value)
  65.   return WriteBytesBigEndian(address,qwordToByteTable(value))
  66. end
  67.  
  68. function writePointerBigEndian(address,value)
  69.   byteFunction = Is64Bit and qwordToByteTable or dwordToByteTable
  70.   return WriteBytesBigEndian(address,byteFunction(value))
  71. end
  72.  
  73. function writeFloatBigEndian(address,value)
  74.   return WriteBytesBigEndian(address,floatToByteTable(value))
  75. end
  76.  
  77. function writeDoubleBigEndian(address,value)
  78.   return WriteBytesBigEndian(address,doubleToByteTable(value))
  79. end
RAW Paste Data