Page 1 of 1

Bug in readfile()/writefile() binary

Posted: 12 Jun 2022 23:28
by PeterH
It took me "some" time (and much joy) to find this...

As I see it, there's a :bug: in readfile(.., b', ...) (i.e. binary)
"In general" it works as it should.
*BUT* if one of the bytes in the file is 0x80 it returns 0xac20 = '€'.
This is seen: STEP-mode / Variables / doubleclick the return-var: correct hex chars return double-byte-chars 0x??00, while for 0x80 0xAC20 is returned.
This interpreted by hexdump($bin, 0, 'b') returns '3f', i.e. '?'. (While '80' is to expect)

OK: to verify for illegal chars I just created a file with "writefile (..., ..., 'a', 'b')" [append, binary],
with data = chr(0) . chr(1) . ... . chr(255);
The written file is correct up to 7f, but x'80' and *many* following fom x'80' to x'9f' are replaced by x'3f'. The rest x'a0' to x'ff' seems to be ok.
Just verified: the data string to be written by writefile correctly contains x'80', so the bad boy *is* writefile.binary!

I'd call this a :bug: - as a binary write that can't write all binary combinations renders a bit of useless.

A test with readfile.bin for x'80818283' shows the same wrong pattern as writefile.bin: x'3f813f3f'.

The result: *any* script using readfile/writefile binary *can* fail.
I used BitnessDetector.xys - und suddenly it returned garbage! Reason: the 1st pointer "usually" it x'b8' (OK!), while I happened to test a program with x'80'. Bad bad bad. Had to start searching...

In the end: what's to do? Can this be repaired?

Regards, Peter

Re: Bug in readfile()/writefile() binary

Posted: 13 Jun 2022 09:22
by admin
Interesting! I'm a busy man, do you mind sharing the script that does the faulty writing?

Re: Bug in readfile()/writefile() binary

Posted: 13 Jun 2022 10:29
by admin
well, "b" was simply the wrong choice. It should be "ru":

Code: Select all

writefile("text.txt", Chr(0x80),, "ru");

Re: Bug in readfile()/writefile() binary

Posted: 13 Jun 2022 13:16
by PeterH
Appended: the script to create a binary file. Tell me if there's more I can do!
Dirty: the file is written to \test subdir of <xyscripts>. (For me the script is there, too.)
(In real it's only part of another script. With very dirty coding, so deleted...)

By changing comments near the end you can change BINARY to RAW.

Tested with raw: instead of 1 hex byte 2 are written. So for x80 x8000 will be written (I assume in reality it's x0080) - every 2nd byte is 00! The file will have double length.
(But 80 will be 8000! Right value, wrong length!) I think it's documented this way?
So RAW would be correct (for writefile!) if all the 00 would not be there...

Also tested for ReadFile "r": only every 2nd byte was read, but either as "strange characters", or converted (by hextodec() to "?".

In the end: I have no idea what could help.

Re: Bug in readfile()/writefile() binary

Posted: 13 Jun 2022 14:11
by admin
Thanks. As I said in my previous post, use "ru":

Code: Select all

RIGHT:	$rcwr	= WriteFile($ofilenm, $string, "o", "ru");
WRONG:	$rcwr	= WriteFile($ofilenm, $string, "o", "b");

Re: Bug in readfile()/writefile() binary

Posted: 13 Jun 2022 18:08
by PeterH
You are right: "ru" is it. For writefile as well as for readfile!
So no (real) :bug:

My problem: being puzzled by wording etc. in help! In the end missed the real solution, neither noticed "ru" in readfile nor in Don's answer.

In my understanding:
- binary: use a byte (= 8 bit) as it is. (i.e. like 'ru' does)
. . No conversion etc.
. . 1 byte: don't think of unicode (as in 'ru')
- DBCS, Unicode or whatever: represent a char as 2 byte, as 'r' does
. . might think of 'raw dbcs char'

I only would have use for 'real binary' (now named 'tu').
Maybe can imagine someone to have use for 't'
Can't imagine use for the current 'b'=binary

(Just my 2 cent)

PS: Will do some work in BitnessDetector.
Seems I can take some time, as nobody seems to have problems with the wrong ReadFile 'b'.

Re: Bug in readfile()/writefile() binary

Posted: 13 Jun 2022 18:22
by admin
Yes, it's a bit confusing. But it's too late to change it. :)