Skip to content

Commit

Permalink
increase jasper max memory size to 100MB (#579)
Browse files Browse the repository at this point in the history
* more checking of input parameters

* fix threading in g2c_inq_msg()

* fix formatting

* fix formatting

* fix formatting

* more checking of input parameters

* more checking of input parameters

* more checking of input parameters

* more checking of input parameters

* more checking of input parameters

* more threading work

* more threading work

* more threading work

* more threading work

* more threading work

* fixed threading problem in g2c_inq_dim()

* fixed threading problem in g2c_inq_dim()

* added test

* added prototype

* increased jasper memory limit to 100MB

* improved comments
  • Loading branch information
edwardhartnett authored Dec 30, 2024
1 parent 1becc9e commit 80806b4
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 127 deletions.
3 changes: 2 additions & 1 deletion src/decenc_jpeg2000.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits,

/* Initialize Jasper. */
#ifdef JASPER3
#define HUNDRED_MB 100000000
jas_conf_clear();
/* static jas_std_allocator_t allocator; */
/* jas_std_allocator_init(&allocator); */
/* jas_conf_set_allocator(JAS_CAST(jas_std_allocator_t *, &allocator)); */
jas_conf_set_max_mem_usage(10000000);
jas_conf_set_max_mem_usage(HUNDRED_MB);
jas_conf_set_multithread(true);
if (jas_init_library())
return G2_JASPER_INIT;
Expand Down
226 changes: 157 additions & 69 deletions src/g2cinq.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ g2c_inq(int g2cid, int *num_msg)
{
int ret = G2C_NOERROR;

/* Is this an open GRIB2 file? */
/* Check input parameters. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;

/* If using threading, lock the mutex. */
MUTEX_LOCK(m);

/* Find the open file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;

Expand Down Expand Up @@ -88,33 +89,53 @@ g2c_inq_msg(int g2cid, int msg_num, unsigned char *discipline, int *num_fields,
unsigned char *local_version)
{
G2C_MESSAGE_INFO_T *msg;
/* Is this an open GRIB2 file? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES || g2c_file[g2cid].g2cid != g2cid)
int ret = G2C_NOERROR;

/* Check input parameters. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0)
return G2C_EINVAL;

/* If using threading, lock the mutex. */
MUTEX_LOCK(m);

/* Find the open file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;

/* Find the file and message. */
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
if (!ret)
{
if (msg->msg_num == msg_num)
ret = G2C_ENOMSG;
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
{
if (discipline)
*discipline = msg->discipline;
if (num_fields)
*num_fields = msg->num_fields;
if (num_local)
*num_local = msg->num_local;
if (center)
*center = msg->center;
if (subcenter)
*subcenter = msg->subcenter;
if (master_version)
*master_version = msg->master_version;
if (local_version)
*local_version = msg->local_version;
return G2C_NOERROR;
if (msg->msg_num == msg_num)
{
if (discipline)
*discipline = msg->discipline;
if (num_fields)
*num_fields = msg->num_fields;
if (num_local)
*num_local = msg->num_local;
if (center)
*center = msg->center;
if (subcenter)
*subcenter = msg->subcenter;
if (master_version)
*master_version = msg->master_version;
if (local_version)
*local_version = msg->local_version;
ret = G2C_NOERROR;
break;
}
}
}
return G2C_ENOMSG;

/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);

return ret;
}

/**
Expand Down Expand Up @@ -151,34 +172,53 @@ g2c_inq_msg_time(int g2cid, int msg_num, unsigned char *sig_ref_time, short *yea
unsigned char *minute, unsigned char *second)
{
G2C_MESSAGE_INFO_T *msg;
int ret = G2C_NOERROR;

/* Is this an open GRIB2 file? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES || g2c_file[g2cid].g2cid != g2cid)
/* Check input parameters. */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0)
return G2C_EINVAL;

/* Find the message. */
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
/* If using threading, lock the mutex. */
MUTEX_LOCK(m);

/* Find the open file. */
if (g2c_file[g2cid].g2cid != g2cid)
ret = G2C_EBADID;

/* Find the file and message. */
if (!ret)
{
if (msg->msg_num == msg_num)
ret = G2C_ENOMSG;
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
{
if (sig_ref_time)
*sig_ref_time = msg->sig_ref_time;
if (year)
*year = msg->year;
if (month)
*month = msg->month;
if (day)
*day = msg->day;
if (hour)
*hour = msg->hour;
if (minute)
*minute = msg->minute;
if (second)
*second = msg->second;
return G2C_NOERROR;
if (msg->msg_num == msg_num)
{
if (sig_ref_time)
*sig_ref_time = msg->sig_ref_time;
if (year)
*year = msg->year;
if (month)
*month = msg->month;
if (day)
*day = msg->day;
if (hour)
*hour = msg->hour;
if (minute)
*minute = msg->minute;
if (second)
*second = msg->second;
ret = G2C_NOERROR;
break;
}
}
}
return G2C_ENOMSG;

/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);

return ret;
}

/**
Expand Down Expand Up @@ -216,6 +256,8 @@ g2c_inq_prod(int g2cid, int msg_num, int prod_num, int *pds_template_len,
/* Is this an open GRIB2 file? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0 || prod_num < 0)
return G2C_EINVAL;

/* If using threading, lock the mutex. */
MUTEX_LOCK(m);
Expand Down Expand Up @@ -302,7 +344,9 @@ g2c_inq_prod(int g2cid, int msg_num, int prod_num, int *pds_template_len,
}

/**
* Learn about the one of the dimensions of a GRIB2 product.
* Learn about the one of the dimensions of a GRIB2 product. This
* function will return the size, name, and values along the
* dimension.
*
* @param g2cid ID of the opened file, as from g2c_open().
* @param msg_num Number of the message in the file, starting with the
Expand Down Expand Up @@ -334,51 +378,95 @@ g2c_inq_dim(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
int d;
int ret = G2C_NOERROR;

/* Is this an open GRIB2 file? */
/* Are these valid IDs? */
if (g2cid < 0 || g2cid > G2C_MAX_FILES)
return G2C_EBADID;
if (msg_num < 0 || prod_num < 0 || dim_num < 0)
return G2C_EINVAL;

/* If using threading, lock the mutex. */
MUTEX_LOCK(m);

/* Find the file. */
if (g2c_file[g2cid].g2cid != g2cid)
return G2C_EBADID;
ret = G2C_EBADID;

/* Find the message. */
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
if (msg->msg_num == msg_num)
break;
if (!msg)
return G2C_ENOMSG;
if (!ret)
{
for (msg = g2c_file[g2cid].msg; msg; msg = msg->next)
if (msg->msg_num == msg_num)
break;
if (!msg)
ret = G2C_ENOMSG;
}

/* Find the product. After this, sec4 will point to the
* appropropriate section 4 G2C_SECTION_INFO_T. */
for (sec4 = msg->sec; sec4; sec4 = sec4->next)
if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
break;
if (!sec4)
return G2C_ENOPRODUCT;
/* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
if (!ret)
{
for (sec4 = msg->sec; sec4; sec4 = sec4->next)
if (sec4->sec_num == 4 && ((G2C_SECTION4_INFO_T *)sec4->sec_info)->field_num == prod_num)
break;
if (!sec4)
ret = G2C_ENOPRODUCT;
/* sec4_info = (G2C_SECTION4_INFO_T *)sec4->sec_info; */
}

/* Find the GDS. */
for (sec3 = sec4->prev; sec3; sec3 = sec3->prev)
if (sec3->sec_num == 3)
break;
if (!sec3)
return G2C_ENOSECTION;
dim = &((G2C_SECTION3_INFO_T *)sec3->sec_info)->dim[dim_num];
if (!ret)
{
for (sec3 = sec4->prev; sec3; sec3 = sec3->prev)
if (sec3->sec_num == 3)
break;
if (!sec3)
ret = G2C_ENOSECTION;
dim = &((G2C_SECTION3_INFO_T *)sec3->sec_info)->dim[dim_num];
}

/* Give the caller the info they want. */
if (len)
*len = dim->len;
if (name)
strncpy(name, dim->name, G2C_MAX_NAME);
if (val)
for (d = 0; d < dim->len; d++)
val[d] = dim->value[d];
if (!ret)
{
if (len)
*len = dim->len;
if (name)
strncpy(name, dim->name, G2C_MAX_NAME);
if (val)
for (d = 0; d < dim->len; d++)
val[d] = dim->value[d];
}

/* If using threading, unlock the mutex. */
MUTEX_UNLOCK(m);

return ret;
}

/**
* Learn about the one of the dimensions of a GRIB2 product. This
* function will return the size and name of the dimension.
*
* @param g2cid ID of the opened file, as from g2c_open().
* @param msg_num Number of the message in the file, starting with the
* first message as 0.
* @param prod_num Product number.
* @param dim_num Dimension number, with the first dimension as 0.
* @param len Pointer that gets the length of this dimension. Ignored if NULL.
* @param name Pointer that gets the name of this dimension. Must have
* memory of size G2C_MAX_NAME. Ignored if NULL.
*
* @return
* - ::G2C_NOERROR No error.
* - ::G2C_EBADID File ID not found.
* - ::G2C_ENOMSG Message not found.
* - ::G2C_ENOPRODUCT Product not found.
* - ::G2C_ENOSECTION GDS not found.
*
* @author Ed Hartnett @date 10/21/22
*/
int
g2c_inq_dim_info(int g2cid, int msg_num, int prod_num, int dim_num, size_t *len,
char *name)
{
return g2c_inq_dim(g2cid, msg_num, prod_num, dim_num, len, name, NULL);
}
Loading

0 comments on commit 80806b4

Please sign in to comment.