Page 1 of 1

API: History returns negative values for 'FileSizeLo'

Posted: 19 Apr 2020, 17:16
by ProXy
Hello,

I am writing a small app that reads history using the API. To my surprise I receive negative values for the field 'FileSizeLo' for some of the results. This only seems to happen for hidden items, normal ones are fine.

Am I doing something wrong, is this a bug or am I missing something else?

Re: API: History returns negative values for 'FileSizeLo'

Posted: 19 Apr 2020, 21:53
by hugbug
XML-Rpc or json-rpc?
Is the number negative in the raw response from nzbget?

Re: API: History returns negative values for 'FileSizeLo'

Posted: 20 Apr 2020, 02:12
by ProXy
I am using json-rpc. This is the result from

Code: Select all

curl --data-binary '{"jsonrpc":"2.0","id":0,"method":"history","params":true}' -H 'content-type:application/json;' http://127.0.0.1:6789/jsonrpc/ >> log.txt

Code: Select all

{
"ID" : 428,
"NZBID" : 428,
"Kind" : "DUP",
"Name" : "blablabla",
"HistoryTime" : 1584446011,
"FileSizeLo" : -1495418638,
"FileSizeHi" : 15,
"FileSizeMB" : 64109,
"DupeKey" : "",
"DupeScore" : 0,
"DupeMode" : "SCORE",
"DupStatus" : "SUCCESS",
"Status" : "SUCCESS/HIDDEN"
}

Re: API: History returns negative values for 'FileSizeLo'

Posted: 20 Apr 2020, 07:59
by sanderj
"FileSizeLo" : -1495418638,
"FileSizeHi" : 15,

A two's complement problem? What if you add 4.2G to that, would that make sense? You would get 2^32 -1495418638 = 2799548658

FileSizeLo (int) - Initial size of all files in group in bytes, Low 32-bits of 64-bit value.
FileSizeHi (int) - Initial size of all files in group in bytes, High 32-bits of 64-bit value.

Let's check the value "FileSizeHi" : 15, which would mean 15 * 4.2GB = 63GB. Could that be right at all in your case? This is History, so 63GB (plus 2.8 GB, so about 66GB), could very well be, right?

If so:
- quick workaround: on the client side, if negative, add 2^32. So force into unsigned int.
- with wireshark, you can see what NZBget API says (probably some around 2799548658), so the interpretation problem is in curl. I don't know if the JSON-RPC spec can make a difference between a signed int and unsigned int.

EDIT

Interesting quote on https://nzbget.net/api/
64 bit integers are returned in two separate fields ‘‘Hi’’ and ‘‘Lo’’ (for example ‘‘FileSizeHi’’ and ‘‘FileSizeLo’’). These fields are unsigned 32 bit integers. Although dynamic languages such as PHP or Python have no problems with these fields the XML-RPC specification does not allow unsigned integers. This may cause troubles in statically typed languages such as Java or C++ if XML-RPC-parser expects only signed integers in 32 bit range. As a solution use JSON-RPC instead (which does allow unsigned integers) instead of XML-RPC.
Quote: "As a solution use JSON-RPC instead (which does allow unsigned integers)"

Ah, now I understand why @hugbug asked "XML-Rpc or json-rpc?" ... :D

Re: API: History returns negative values for 'FileSizeLo'

Posted: 20 Apr 2020, 09:03
by ProXy
Yeah, I first started using XML-RPC, with no workaround for those uint32 fields, so I decided to switch to JSON-RPC.

I tried to add 2^32 bytes and the result seems to be fine. There are some more records with negative values, I will check some more.

Edit: Just used Wireshark (I don't get this user interface) and checked the raw data. The values are already negative in the received data, so it seems that this comes directly from NZBGet.

Re: API: History returns negative values for 'FileSizeLo'

Posted: 20 Apr 2020, 09:36
by hugbug
The numbers should not be negative.
Apparently you found a bug - file size for hidden history items isn't properly formatted when using json-rpc - https://github.com/nzbget/nzbget/blob/d ... .cpp#L2467.

The format specifier should be %u instead of %i.

The bug hasn't been noticed yet because in webui the size is printed using FileSizeMB if size is larger then 1024 MB. The field FileSizeLo is used only for files smaller than 1024 MB and in that case the FileSizeLo is always positive.

Re: API: History returns negative values for 'FileSizeLo'

Posted: 20 Apr 2020, 17:32
by hugbug

Re: API: History returns negative values for 'FileSizeLo'

Posted: 21 Apr 2020, 20:35
by ProXy
Thank you!