Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for aecunpack.c #462

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/aecunpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
void *fld, int fld_is_double, int verbose)
{
g2int *ifld;
g2int j, ctemplen, nbits;
g2int j, ctemplen = 0 , nbits;
g2int ccsds_flags, ccsds_block_size, ccsds_rsi;
//g2int ifld1 = 0;
int ret = 0;
float ref, bscale, dscale;
float *ffld = fld;
double *dfld = fld;
unsigned char *ctemp;

ctemplen = 0;
size_t nbytes = 0;

LOG((2, "aecunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));

/* Get compression parameters from data representation template array. */
rdieee(idrstmpl, &ref, 1);
bscale = int_power(2.0, idrstmpl[1]);
dscale = int_power(10.0, -idrstmpl[2]);
Expand All @@ -75,17 +76,36 @@ aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
return G2C_ENOMEM;
}

ctemplen = ((nbits + 7)/8) * (size_t) ndpts;
/* Determine the number of bytes needed for each value, then allocate the
* buffer for the decoded AEC stream. */
nbytes = (nbits + 7)/8;
if (nbytes == 3)
nbytes = 4;
ctemplen = nbytes * ndpts;
if ((ctemp = (unsigned char *) malloc(ctemplen)) == NULL)
{
if (verbose)
fprintf(stderr, "Allocation error.\n");
return G2C_ENOMEM;
}

/* Decode the AEC stream. */
ret = dec_aec(cpack, len, nbits, ccsds_flags, ccsds_block_size, ccsds_rsi, ctemp, ctemplen);
if (ret < 0)
return ret;
gbits(ctemp, ifld, 0, nbits, 0, ndpts);

/* IMPORTANT: The decoded AEC stream is byte-aligned (not bit), so when extracting the data
* values from the buffer via gbits, we need to pass the byte size in bits, not nbits. */
gbits(ctemp, ifld, 0, nbytes*8, 0, ndpts);

/* Zero out all higher-order bits, preserving nbits. NOTE: This might be unecessary. */
//for (j = 0; j < ndpts; j++)
// ifld1 = ifld[j];
// ifld1 = ifld1 << (sizeof(ifld1)*8-nbits);
// ifld1 = ifld1 >> (sizeof(ifld1)*8-nbits);
// ifld[j] = ifld1;

/* Unscale data. */
if (fld_is_double)
{
for (j = 0; j < ndpts; j++)
Expand All @@ -96,6 +116,8 @@ aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
for (j = 0; j < ndpts; j++)
ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}

/* Clean up. */
free(ctemp);
free(ifld);
}
Expand Down
Loading