Left here for archival purposes.

User avatar
By Markus Gritsch
#6637 The recently added file.read() function has a bug which prevents it from reading binary data which contains a 0xFF byte.

The problem seems to be caused by some EOF checking using an int value of -1 in combination with (signed char) casting of the read character: If the read byte is 255 and then casted to (signed char) this equals to -1 and the function wrongly assumes EOF is reached.

The following test program
Code: Select allfname = 'test.txt'

file.remove(fname)
file.open(fname, 'w')
file.write(string.char(65, 66, 67, 255, 68, 69, 70))
file.close()

print(file.list()[fname] .. ' bytes written')

file.open(fname, 'r')
cont = file.read()
file.close()
print(#cont .. ' bytes read with file.read()')
print(string.byte(cont,1,-1))

gives this output:
Code: Select all7 bytes written
3 bytes read with file.read()
65  66  67
User avatar
By zeroday
#6647
Markus Gritsch wrote:The recently added file.read() function has a bug which prevents it from reading binary data which contains a 0xFF byte.

The problem seems to be caused by some EOF checking using an int value of -1 in combination with (signed char) casting of the read character: If the read byte is 255 and then casted to (signed char) this equals to -1 and the function wrongly assumes EOF is reached.

The following test program
Code: Select allfname = 'test.txt'

file.remove(fname)
file.open(fname, 'w')
file.write(string.char(65, 66, 67, 255, 68, 69, 70))
file.close()

print(file.list()[fname] .. ' bytes written')

file.open(fname, 'r')
cont = file.read()
file.close()
print(#cont .. ' bytes read with file.read()')
print(string.byte(cont,1,-1))

gives this output:
Code: Select all7 bytes written
3 bytes read with file.read()
65  66  67


build 20150108.
User avatar
By alonewolfx2
#6708
Markus Gritsch wrote:Thanks zeroday, you are the best! It works perfectly now, and for the first time my webserver can now deliver small images! Nice.

can you share example? i am so excited :)