Bug in readfile()/writefile() binary

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Bug in readfile()/writefile() binary

Post 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

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

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

Post by admin »

Interesting! I'm a busy man, do you mind sharing the script that does the faulty writing?

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

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

Post by admin »

well, "b" was simply the wrong choice. It should be "ru":

Code: Select all

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

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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.
To see the attached files, you need to log into the forum.

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

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

Post 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");

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

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

Post 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'.

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

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

Post by admin »

Yes, it's a bit confusing. But it's too late to change it. :)

Post Reply