[gradsusr] 2-byte integers in version 2.0.1.oga.1

Ming Pan fallspinach at gmail.com
Fri Jan 13 17:18:19 EST 2012


Hi,

I'm having trouble reading 2-byte integers correctly in the new
release of opengrads (version 2.0.1.oga.1). I built it from source on
my Linux x86_64 system.

What happens is that the 2-byte integers, e.g. unsigned, are read in
as 1-byte numbers (0-255) instead of 2-byte numbers (0-65535). I
compared the 2.0.1.oga.1 source code to the previous version
2.0.a9.oga.1, and part in subroutine in gaio.c for reading 2-byte
numbers is different:

2.0.1.oga.1, line 690: sval=(signed short)(*(cgr+cnt));
2.0.1.oga.1, line 696: usval=(unsigned short)(*(cgr+cnt));

and

2.0.a9.oga.1, line 697: ival=(gaint)(*(cgr+cnt)*256) +
(gaint)((*(cgr+cnt+1))) - 65536*(*(cgr+cnt)>>7);
2.0.a9.oga.1, line 703: ival=(gaint)(*(cgr+cnt)*256) + (gaint)((*(cgr+cnt+1)));

I think the problem in 2.0.1.oga.1 is that:

the pointer "cgr" is declared as "char *", so (*(cgr+cnt)) would
return a 1-byte data and the type conversion won't make up the missing
2nd byte.

I understand that the 2.0.a9.oga.1 approach has the implicit
assumption on byte order, and I think it could be fixed by filling the
sval and unsval byte by byte, for example:

svalp = (char *) &sval; *svalp = *(cgr+cnt); *(svalp+1) = *(cgr+cnt+1);
usvalp = (char *) &usval; *usvalp = *(cgr+cnt); *(usvalp+1) = *(cgr+cnt+1);


ming



More information about the gradsusr mailing list