unqlite binary read test is inconsistent

May 20, 2008
Here are two BTMs, create.btm to create the binary key and read.btm to read it.

TCC  23.00.21 x64   Windows 10 [Version 10.0.17134.112]

@echo off
set key=btest

unqlite /db:"test.db" /rwc

set size=%@random[10,100]

REM Handle off by one poke error with @inc
set bhandle=%@balloc[%@inc[%size]]

set result=%@bpokestr[%bhandle,0,a,%@repeat[x,%size]]
unqlite /kvb %key %bhandle %size

set result=%@bfree[%bhandle]

unqlite /c
echo Binary key '%key' created of size %size

@echo off
set db=test.db
set key=btest

set result=%@unqopen[rwc,%db]

REM Size unknown - let unqreadb expand buffer
set bhandle=%@balloc[1]
set result=%@unqreadb[%db,%key,%bhandle]
set size=%@bsize[%bhandle]
set value=%@bpeekstr[%bhandle,0,a,%size]
set result=%@bfree[%bhandle]
set result=%@unqclose[%db]

echo Binary key '%key' read of size %size
echo Value: %value
The create seems to always work okay. Every once in a while, the read will have the same size. But, usually, the read will either be of a different/larger size with some junk chars at the end or TCC will close. When I put pauses between the steps, it seems the issue may be with bsize, though the handle is valid.


Staff member
May 14, 2008
There are a couple of things wrong with this example.

First, don't use @BPEEKSTR to read binary values. For example, it will not work as you expect if there are any embedded nulls in the binary blob. And if you try read 20 bytes from a 10 byte "string", you're going to get 10 bytes of garbage tacked onto the end of the string.

Second, the reason it fails (most of the time) is because of your @balloc. Allocating 1 byte guarantees that not only will the memory buffer have to be reallocated, it will also almost always be moved -- and then your bhandle value is going to be wrong for all subsequent functions.

Dealing with the pointer change is a bit tricky. What I've done for 23.0.22 is check if the pointer changed, then look through the environment for the old value and change it. But that only works if you've saved he @balloc result to an environment variable.

If you had allocated a larger value to @balloc, it probably would have been able to reallocate it (if necessary) without having to move it. If you don't have ANY idea how big the binary record is going to be, just allocate a few K. You've got terabytes of virtual memory; quibbling over 1 byte vs. 16K bytes is utterly irrelevant.
May 20, 2008
It's difficult to know undocumented intentions. The help for @BPEEKSTR says it is to 'Read a string from a binary buffer' and now you say don't. Thanks for adding some clarification here in the forum. I wish more of that were documented in the help. When I work with TCC, I don't default to thinking in C/C++. Maybe I need to change that.
Similar threads
Thread starter Title Forum Replies Date
Joe Caverly UNQLITE and :mem: Support 1
Joe Caverly UNQLITE and %_unq_db Support 2
cgunhouse Dumping an UNQLITE Database Support 1
samintz How to? swap endianness in a binary file Support 12
S Documentation @SMPEEK refers to binary buffer Support 0
vefatica Fixed Binary buffers broken? Support 0
nikbackm Use cases for binary buffers Support 1
T eval[] crashes with negative binary output Support 1
dcantor Comments and suggestions on functions for binary buffers Support 4
S Accessing binary clipboard Support 1
K "copy /z /w" command not deleting read-only files in destination Support 10
A TCC failing to read recursive symlinks Support 25
Alpengreis [Forum] Mark forumS (all!) read Support 2
D Custom ini-file is not read, by tcmd.exe cli Support 6
T read snmp values Support 4
WinLanEm Read Cyrillic text from a file Support 12
C How to? Read Win7's Computer Libraries Support 19
T [TCC 18] Can't read nor write history Support 1
Charles Dye Read-only environment variables wiped by SETLOCAL / ENDLOCAL Support 5
MickeyF Trying to read XML Support 3
R attrib -C on a read only file? Support 1
samintz How to? read while ignoring whitespace Support 3
S WAD "Mark Forums Read" fails Support 4
vefatica Can DIRHISTORY read from clip:? Support 2
vefatica Mark a forum or thread as "read"? Support 5
M How to? How do I read a Unicode file through standard-input? Support 4
daniel347x How to get Take Command to read in all current system environment variables that CMD.EXE sees? Support 13
C Attaching PowerShell changes its colors and makes it hard to read Support 15

Similar threads