Commit 2f164aa8 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

cgribexlib update.

parent bd35c78d
......@@ -226,7 +226,7 @@ void gribPrintBDS(int nrec, long recpos, long recsize, unsigned char *gribbuffe
void gribCheck1(int nrec, long recpos, long recsize, unsigned char *gribbuffer);
void gribRepair1(int nrec, long recsize, unsigned char *gribbuffer);
int gribGetZip(long recsize, unsigned char *gribbuffer, long *urecsize);
int gribGetZip(size_t recsize, unsigned char *gribbuffer, size_t *urecsize);
int gribBzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize);
int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize);
......
/* Automatically generated by m214003 at 2016-11-04, do not edit */
/* Automatically generated by m214003 at 2016-11-05, do not edit */
/* CGRIBEXLIB_VERSION="1.7.6" */
......@@ -139,8 +139,8 @@ int correct_bdslen(int bdslen, long recsize, long gribpos);
/* param format: DDDCCCNNN */
void cdiDecodeParam(int param, int *dis, int *cat, int *num);
int cdiEncodeParam(int dis, int cat, int num);
void cdiDecodeParam(int param, int *pnum, int *pcat, int *pdis);
int cdiEncodeParam(int pnum, int pcat, int pdis);
/* date format: YYYYMMDD */
/* time format: hhmmss */
......@@ -6051,34 +6051,25 @@ void gribRepair1(int nrec, long recsize, unsigned char *gribbuffer)
# include "config.h"
#endif
#if defined (HAVE_LIBSZ) || defined (HAVE_LIBAEC)
#if defined (HAVE_LIBSZ)
#if defined(__cplusplus)
extern "C" {
#endif
#if defined (HAVE_LIBAEC)
# include <libaec.h>
#else
# include <szlib.h>
#endif
#include <szlib.h>
#if defined (__cplusplus)
}
#endif
#if defined (HAVE_LIBAEC)
# define AEC_FLAGS (AEC_DATA_MSB | AEC_DATA_PREPROCESS)
#else
# define OPTIONS_MASK (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK)
#endif
#define OPTIONS_MASK (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK)
# define PIXELS_PER_BLOCK (8)
# define PIXELS_PER_SCANLINE (PIXELS_PER_BLOCK*128)
#define PIXELS_PER_BLOCK (8)
#define PIXELS_PER_SCANLINE (PIXELS_PER_BLOCK*128)
# define MIN_COMPRESS (0.95)
# define MIN_SIZE (256)
#define MIN_COMPRESS (0.95)
#define MIN_SIZE (256)
#endif
#define Z_SZIP 128
#define Z_AEC 130
#define SetLen3(var, offset, value) ((var[offset+0] = 0xFF & (value >> 16)), \
......@@ -6090,120 +6081,93 @@ extern "C" {
(var[offset+3] = 0xFF & (value )))
int gribGetZip(long recsize, unsigned char *gribbuffer, long *urecsize)
int gribGetZip(size_t recsize, unsigned char *gribbuffer, size_t *urecsize)
{
/* urecsize : uncompressed record size */
int compress = 0;
int nerr;
/* int bds_len, bds_nbits, lspherc, lcomplex; */
int bds_flag, lcompress;
long gribsize = 0;
unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
int gribversion = gribVersion(gribbuffer, (size_t)recsize);
int gribversion = gribVersion(gribbuffer, recsize);
if ( gribversion == 2 ) return (compress);
if ( gribversion == 2 ) return compress;
long gribrecsize;
nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
int nerr = grib1Sections(gribbuffer, (long)recsize, &pds, &gds, &bms, &bds, &gribrecsize);
if ( nerr < 0 )
{
fprintf(stdout, "GRIB message error\n");
return (compress);
return compress;
}
if ( nerr > 0 )
{
fprintf(stdout, "GRIB data corrupted!\n");
return (compress);
return compress;
}
/* bds_len = BDS_Len; */
/* bds_nbits = BDS_NumBits; */
bds_flag = BDS_Flag;
int bds_flag = BDS_Flag;
/* lspherc = bds_flag >> 7; */
/* lcomplex = (bds_flag >> 6)&1; */
lcompress = (bds_flag >> 4)&1;
int lcompress = (bds_flag >> 4)&1;
*urecsize = 0;
size_t gribsize = 0;
if ( lcompress )
{
compress = BDS_Z;
if ( compress == Z_SZIP || compress == Z_AEC )
{
gribsize = gribrec_len(bds[14], bds[15], bds[16]);
}
if ( compress == Z_SZIP ) gribsize = (size_t) gribrec_len(bds[14], bds[15], bds[16]);
}
*urecsize = gribsize;
return (compress);
return compress;
}
int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize)
{
int nerr;
int gribLen;
int rec_len;
int llarge = FALSE;
#if ! (defined (HAVE_LIBSZ) || defined (HAVE_LIBAEC))
#if ! defined(HAVE_LIBSZ)
static int libszwarn = 1;
#endif
unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
gribLen = gribrec_len(dbuf[4], dbuf[5], dbuf[6]);
if ( gribLen > JP23SET ) llarge = TRUE;
int gribLen = gribrec_len(dbuf[4], dbuf[5], dbuf[6]);
int llarge = (gribLen > JP23SET);
rec_len = gribLen;
int rec_len = gribLen;
long gribrecsize;
nerr = grib1Sections(dbuf, dbufsize, &pds, &gds, &bms, &bds, &gribrecsize);
int nerr = grib1Sections(dbuf, dbufsize, &pds, &gds, &bms, &bds, &gribrecsize);
if ( nerr < 0 )
{
fprintf(stdout, "GRIB message error\n");
return (rec_len);
return rec_len;
}
if ( nerr > 0 )
{
fprintf(stdout, "GRIB data corrupted!\n");
return (rec_len);
return rec_len;
}
#if defined (HAVE_LIBSZ) || defined (HAVE_LIBAEC)
#if defined(HAVE_LIBSZ)
{
long i;
int bdsLen;
int gribLenOld = 0;
int status;
size_t datstart, datsize;
#if defined (HAVE_LIBAEC)
struct aec_stream strm;
#else
SZ_com_t sz_param; /* szip parameter block */
#endif
unsigned char *dest, *source;
size_t destLen, sourceLen;
int bits_per_sample;
int bds_len, bds_nbits, bds_flag, lspherc, lcomplex,/* lcompress,*/ bds_ubits;
int bds_head = 11;
int bds_ext = 0;
int bds_zoffset, bds_zstart;
unsigned char *pbuf = NULL;
bds_zstart = 14;
bds_zoffset = 12;
int bds_zstart = 14;
int bds_zoffset = 12;
if ( llarge ) bds_zoffset += 2;
bds_len = BDS_Len;
int bds_len = BDS_Len;
bds_len = correct_bdslen(bds_len, gribLen, bds-dbuf);
bds_nbits = BDS_NumBits;
bds_flag = BDS_Flag;
bds_ubits = bds_flag & 15;
lspherc = bds_flag >> 7;
lcomplex = (bds_flag >> 6)&1;
int bds_nbits = BDS_NumBits;
int bds_flag = BDS_Flag;
int bds_ubits = bds_flag & 15;
int lspherc = bds_flag >> 7;
int lcomplex = (bds_flag >> 6)&1;
/* lcompress = (bds_flag >> 4)&1; */
if ( bds_nbits != 8 && bds_nbits != 16 && bds_nbits != 24 && bds_nbits != 32 )
......@@ -6214,36 +6178,23 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
linfo = 0;
fprintf(stderr, "GRIB szip only supports 8, 16, 24 and 32 bit data!\n");
}
return (rec_len);
return rec_len;
}
#if defined (HAVE_LIBSZ)
if ( bds_nbits == 24 )
bits_per_sample = 8;
else
#endif
bits_per_sample = bds_nbits;
int bits_per_sample = (bds_nbits == 24) ? 8 : bds_nbits;
#if defined (HAVE_LIBAEC)
strm.bits_per_sample = bits_per_sample;
strm.block_size = PIXELS_PER_BLOCK;
strm.rsi = PIXELS_PER_SCANLINE / PIXELS_PER_BLOCK;
strm.flags = AEC_FLAGS;
if ( bds_nbits == 24 ) strm.flags |= AEC_DATA_3BYTE;
#else
SZ_com_t sz_param; /* szip parameter block */
sz_param.options_mask = OPTIONS_MASK;
sz_param.bits_per_pixel = bits_per_sample;
sz_param.pixels_per_block = PIXELS_PER_BLOCK;
sz_param.pixels_per_scanline = PIXELS_PER_SCANLINE;
#endif
if ( lspherc )
{
if ( lcomplex )
{
int jup, ioff;
jup = bds[15];
ioff = (jup+1)*(jup+2);
int jup = bds[15];
int ioff = (jup+1)*(jup+2);
bds_ext = 4 + 3 + 4*ioff;
}
else
......@@ -6252,27 +6203,25 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
}
}
datstart = bds_head + bds_ext;
size_t datstart = bds_head + bds_ext;
datsize = ((((bds_len - datstart)*8-bds_ubits)/bds_nbits)*bds_nbits)/8;
size_t datsize = ((((bds_len - datstart)*8-bds_ubits)/bds_nbits)*bds_nbits)/8;
if ( datsize < MIN_SIZE ) return (rec_len);
if ( datsize < MIN_SIZE ) return rec_len;
/*
fprintf(stderr, "%d %d %d %d\n", bds_len, datstart, bds_len - datstart, datsize);
*/
sourceLen = datsize;
destLen = sbufsize;
size_t sourceLen = datsize;
size_t destLen = sbufsize;
source = bds + datstart;
dest = sbuf;
unsigned char *source = bds + datstart;
unsigned char *dest = sbuf;
#if defined (HAVE_LIBSZ)
if ( bds_nbits == 24 )
{
long nelem;
nelem = sourceLen/3;
long nelem = sourceLen/3;
pbuf = (unsigned char*) Malloc(sourceLen);
for ( i = 0; i < nelem; i++ )
for ( long i = 0; i < nelem; i++ )
{
pbuf[ i] = source[3*i ];
pbuf[ nelem+i] = source[3*i+1];
......@@ -6280,24 +6229,8 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
}
source = pbuf;
}
#endif
#if defined (HAVE_LIBAEC)
strm.next_in = source;
strm.avail_in = sourceLen;
strm.next_out = dest;
strm.avail_out = destLen;
status = aec_buffer_encode(&strm);
if ( status != AEC_OK )
{
if ( status != AEC_DATA_ERROR )
Warning("AEC ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
}
destLen = strm.total_out;
#else
status = SZ_BufftoBuffCompress(dest, &destLen, source, sourceLen, &sz_param);
int status = SZ_BufftoBuffCompress(dest, &destLen, source, sourceLen, &sz_param);
if ( status != SZ_OK )
{
if ( status == SZ_NO_ENCODER_ERROR )
......@@ -6311,7 +6244,6 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
else
Warning("SZ ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
}
#endif
if ( pbuf ) Free(pbuf);
/*
......@@ -6329,7 +6261,7 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
gribLenOld = gribLen;
if ( bds_ext )
for ( i = bds_ext-1; i >= 0; --i )
for ( long i = bds_ext-1; i >= 0; --i )
bds[bds_zoffset+bds_head+i] = bds[bds_head+i];
/*
......@@ -6361,15 +6293,12 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
SetLen3(bds, bds_zstart+6, destLen);
}
bdsLen = datstart + bds_zoffset + destLen;
int bdsLen = datstart + bds_zoffset + destLen;
bds[11] = 0;
bds[12] = 0;
#if defined (HAVE_LIBAEC)
BDS_Z = Z_AEC;
#else
BDS_Z = Z_SZIP;
#endif
BDS_Flag += 16;
if ( (bdsLen%2) == 1 )
......@@ -6389,7 +6318,6 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
if ( llarge )
{
long itemp;
long bdslen = gribLen - 4;
/*
......@@ -6402,7 +6330,7 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
*/
while ( gribLen%120 ) dbuf[gribLen++] = 0;
itemp = gribLen / (-120);
long itemp = gribLen / (-120);
itemp = JP23SET - itemp + 1;
SetLen3(dbuf, 4, itemp);
......@@ -6434,7 +6362,7 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
if ( libszwarn )
{
Warning("Compression disabled, szlib or libaec not available!");
Warning("Compression disabled, szlib not available!");
libszwarn = 0;
}
#endif
......@@ -6446,23 +6374,20 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufs
rec_len = gribLen;
return (rec_len);
return rec_len;
}
int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize)
{
#if ! (defined (HAVE_LIBSZ) || defined (HAVE_LIBAEC))
#if ! defined(HAVE_LIBSZ)
static int libszwarn = 1;
#endif
unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
size_t gribLen = 0;
unsigned char *dest, *source;
size_t destLen, sourceLen;
int /* bds_len, */ bds_nbits, bds_flag, lspherc, lcomplex /*, lcompress*/;
enum { bds_head = 11 };
int bds_ext = 0;
int bds_zoffset, bds_zstart;
int llarge = FALSE;
UNUSED(dbufsize);
......@@ -6472,37 +6397,36 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
if ( nerr < 0 )
{
fprintf(stdout, "GRIB message error\n");
return (0);
return 0;
}
if ( nerr > 0 )
{
fprintf(stdout, "GRIB data corrupted!\n");
return (0);
return 0;
}
bds_zstart = 14;
int bds_zstart = 14;
int recLen = gribrec_len(bds[bds_zstart], bds[bds_zstart+1], bds[bds_zstart+2]);
if ( recLen > JP23SET ) llarge = TRUE;
bds_zoffset = 12;
int bds_zoffset = 12;
if ( llarge ) bds_zoffset += 2;
/* bds_len = BDS_Len; */
bds_nbits = BDS_NumBits;
bds_flag = BDS_Flag;
lspherc = bds_flag >> 7;
lcomplex = (bds_flag >> 6)&1;
int bds_nbits = BDS_NumBits;
int bds_flag = BDS_Flag;
int lspherc = bds_flag >> 7;
int lcomplex = (bds_flag >> 6)&1;
/* lcompress = (bds_flag >> 4)&1; */
if ( lspherc )
{
if ( lcomplex )
{
int jup, ioff;
jup = bds[bds_zoffset+15];
ioff = (jup+1)*(jup+2);
int jup = bds[bds_zoffset+15];
int ioff = (jup+1)*(jup+2);
bds_ext = 4 + 3 + 4*ioff;
}
else
......@@ -6513,7 +6437,7 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
size_t datstart = bds_head + (size_t)bds_ext;
source = bds + datstart + bds_zoffset;
unsigned char *source = bds + datstart + bds_zoffset;
if ( llarge )
sourceLen = ((size_t) ((bds[21]<<24)+(bds[22]<<16)+(bds[23]<<8)+bds[24]));
else
......@@ -6523,16 +6447,16 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
if ( nerr < 0 )
{
fprintf(stdout, "GRIB message error\n");
return (0);
return 0;
}
if ( nerr > 0 )
{
fprintf(stdout, "GRIB data corrupted!\n");
return (0);
return 0;
}
dest = bds + datstart;
unsigned char *dest = bds + datstart;
if ( llarge )
destLen = ((size_t) ((bds[17]<<24)+(bds[18]<<16)+(bds[19]<<8)+bds[20]));
else
......@@ -6542,63 +6466,28 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
size_t bdsLen = datstart + destLen;
#if defined (HAVE_LIBSZ) || defined (HAVE_LIBAEC)
#if defined(HAVE_LIBSZ)
{
int status;
long i;
size_t tmpLen;
int bds_ubits;
int bits_per_sample;
#if defined (HAVE_LIBAEC)
struct aec_stream strm;
#else
SZ_com_t sz_param; /* szip parameter block */
#endif
int bits_per_sample = (bds_nbits == 24) ? 8 : bds_nbits;
#if defined (HAVE_LIBSZ)
if ( bds_nbits == 24 )
bits_per_sample = 8;
else
#endif
bits_per_sample = bds_nbits;
#if defined (HAVE_LIBAEC)
strm.bits_per_sample = bits_per_sample;
strm.block_size = PIXELS_PER_BLOCK;
strm.rsi = PIXELS_PER_SCANLINE / PIXELS_PER_BLOCK;
strm.flags = AEC_FLAGS;
if ( bds_nbits == 24 ) strm.flags |= AEC_DATA_3BYTE;
#else
SZ_com_t sz_param; /* szip parameter block */
sz_param.options_mask = OPTIONS_MASK;
sz_param.bits_per_pixel = bits_per_sample;
sz_param.pixels_per_block = PIXELS_PER_BLOCK;
sz_param.pixels_per_scanline = PIXELS_PER_SCANLINE;
#endif
if ( bds_ext )
for ( i = 0; i < bds_ext; ++i )
for ( long i = 0; i < bds_ext; ++i )
bds[bds_head+i] = bds[bds_zoffset+bds_head+i];
/*
fprintf(stderr, "gribUnzip: sourceLen %ld; destLen %ld\n", (long)sourceLen, (long)destLen);
/* fprintf(stderr, "gribUnzip: sourceLen %ld; destLen %ld\n", (long)sourceLen, (long)destLen);
fprintf(stderr, "gribUnzip: sourceOff %d; destOff %d\n", bds[12], bds[11]);
fprintf(stderr, "gribUnzip: reclen %d; bdslen %d\n", recLen, bdsLen);
*/
tmpLen = destLen;
#if defined (HAVE_LIBAEC)
strm.next_in = source;
strm.avail_in = sourceLen;
strm.next_out = dest;
strm.avail_out = tmpLen;
status = aec_buffer_decode(&strm);
if ( status != AEC_OK )
Warning("AEC ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
size_t tmpLen = destLen;
tmpLen = strm.total_out;
#else
status = SZ_BufftoBuffDecompress(dest, &tmpLen, source, sourceLen, &sz_param);
int status = SZ_BufftoBuffDecompress(dest, &tmpLen, source, sourceLen, &sz_param);
if ( status != SZ_OK )
{
if ( status == SZ_NO_ENCODER_ERROR )
......@@ -6612,7 +6501,6 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
else
Warning("SZ ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
}
#endif
/*
fprintf(stderr, "gribUnzip: sl = %ld dl = %ld tl = %ld\n",
(long)sourceLen, (long)destLen,(long) tmpLen);
......@@ -6621,14 +6509,11 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
Warning("unzip size differ: code %3d level %3d ibuflen %ld ubuflen %ld",
PDS_Parameter, PDS_Level2, (long) destLen, (long) tmpLen);
#if defined (HAVE_LIBSZ)
if ( bds_nbits == 24 )
{
long nelem;
unsigned char *pbuf;
nelem = tmpLen/3;
pbuf = (unsigned char*) Malloc(tmpLen);
for ( i = 0; i < nelem; i++ )
long nelem = tmpLen/3;
unsigned char *pbuf = (unsigned char*) Malloc(tmpLen);
for ( long i = 0; i < nelem; i++ )
{
pbuf[3*i ] = dest[ i];
pbuf[3*i+1] = dest[ nelem+i];
......@@ -6637,9 +6522,8 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
memcpy(dest, pbuf, tmpLen);
Free(pbuf);
}
#endif
bds_ubits = BDS_Flag & 15;
int bds_ubits = BDS_Flag & 15;
BDS_Flag -= bds_ubits;
if ( (bdsLen%2) == 1 )
......@@ -6707,7 +6591,7 @@ int gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
if ( libszwarn )
{
Warning("Decompression disabled, szlib or libaec not available!");
Warning("Decompression disabled, szlib not available!");
libszwarn = 0;
}
#endif
......@@ -13825,7 +13709,7 @@ void encode_dummy(void)
(void) encode_array_unrolled_double(0, 0, 0, NULL, NULL, 0, 0, NULL);
(void) encode_array_unrolled_float(0, 0, 0, NULL, NULL, 0, 0, NULL);
}
static const char grb_libvers[] = "1.7.6" " of ""Nov 4 2016"" ""19:12:51";
static const char grb_libvers[] = "1.7.6" " of ""Nov 5 2016"" ""11:59:02";
const char *
cgribexLibraryVersion(void)
{
......
......@@ -65,12 +65,12 @@ int grbUnzipRecord(void *gribbuffer, size_t *gribsize)
{
int zip = 0;
int izip;
long unzipsize;
size_t unzipsize;
size_t igribsize = *gribsize;
size_t ogribsize = *gribsize;
if ( (izip = gribGetZip((long)igribsize, (unsigned char *)gribbuffer, &unzipsize)) > 0 )
if ( (izip = gribGetZip(igribsize, (unsigned char *)gribbuffer, &unzipsize)) > 0 )
{
zip = izip;
if ( izip == 128 ) /* szip */
......@@ -78,9 +78,9 @@ int grbUnzipRecord(void *gribbuffer, size_t *gribsize)
unsigned char *itmpbuffer = NULL;
size_t itmpbuffersize = 0;