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

Fix buffer size plus #9

Merged
merged 3 commits into from
Nov 4, 2021
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
82 changes: 51 additions & 31 deletions core/accelogic/src/ZipAccelogic.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@

static const int kHeaderSize = 10; // Regular ROOT header (9) plus one more for Blast.

union RealTypes {
float *f;
double *d;
char *c;
};

union IntegerTypes {
char *c;
short *s;
int *i;
long long *l;
unsigned char *uc;
unsigned short *us;
unsigned int *ui;
unsigned long long *ul;
};


void R__zipBLAST(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt, int *irep, EDataType datatype)
{
*irep = 0;
Expand Down Expand Up @@ -58,11 +76,13 @@ void R__zipBLAST(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt,
// Note: We need to check the source really start of a float boundary.
// Note: We need to upgrade blast to avoid the memcpy (which is IN ADDITION to an internal copy already!!!)
char *staging = nullptr;
RealTypes source;
source.c = src;

if (isfloat)
out_size = blast1_compress<true>(absSensLevel, (float*)src, float_number, staging);
out_size = blast1_compress<true>(absSensLevel, source.f, float_number, staging);
else
out_size = blast1_compress<true>(absSensLevel, (double*)src, float_number, staging);
out_size = blast1_compress<true>(absSensLevel, source.d, float_number, staging);

if ( (out_size + kHeaderSize) > (size_t)*tgtsize ) {
delete [] staging;
Expand All @@ -76,23 +96,25 @@ void R__zipBLAST(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt,
// Note: We need to check the source really start of a boundary.
// Note: We need to upgrade blast to avoid the memcpy (which is IN ADDITION to an internal copy already!!!)
char *staging = nullptr;
// out_size = blast2_compress<T,true>((T*)src, *srcsize, staging);
IntegerTypes source;
source.c = src;

if (cxlevel == 72) {
out_size = blast2_compress<char,true>(src, *srcsize, staging);
out_size = blast2_compress<true>(source.c, *srcsize, staging);
} else if (cxlevel == 73 && (*srcsize % sizeof(short) == 0)) {
out_size = blast2_compress<short,true>((short*)src, *srcsize, staging);
out_size = blast2_compress<true>(source.s, *srcsize, staging);
} else if (cxlevel == 74 && (*srcsize % sizeof(int) == 0)) {
out_size = blast2_compress<int,true>((int*)src, *srcsize, staging);
out_size = blast2_compress<true>(source.i, *srcsize, staging);
} else if (cxlevel == 75 && (*srcsize % sizeof(long long) == 0)) {
out_size = blast2_compress<long long,true>((long long*)src, *srcsize, staging);
out_size = blast2_compress<true>(source.l, *srcsize, staging);
} else if (cxlevel == 76) {
out_size = blast2_compress<unsigned char,true>((unsigned char*) src, *srcsize, staging);
out_size = blast2_compress<true>(source.uc, *srcsize, staging);
} else if (cxlevel == 77 && (*srcsize % sizeof(unsigned short) == 0)) {
out_size = blast2_compress<unsigned short,true>((unsigned short*)src, *srcsize, staging);
out_size = blast2_compress<true>(source.us, *srcsize, staging);
} else if (cxlevel == 78 && (*srcsize % sizeof(unsigned int) == 0)) {
out_size = blast2_compress<unsigned int,true>((unsigned int*)src, *srcsize, staging);
out_size = blast2_compress<true>(source.ui, *srcsize, staging);
} else if (cxlevel == 79 && (*srcsize % sizeof(unsigned long long) == 0)) {
out_size = blast2_compress<unsigned long long,true>((unsigned long long*)src, *srcsize, staging);
out_size = blast2_compress<true>(source.ul, *srcsize, staging);
} else {
// not proper length
return;
Expand Down Expand Up @@ -133,6 +155,8 @@ void R__unzipBLAST(int *srcsize, unsigned char *src, int *tgtsize, unsigned char
{
*irep = 0;

char* source = (char*)(&src[kHeaderSize]);
size_t in_size = (*srcsize) - kHeaderSize;
auto cxlevel = src[2];
auto datatype = src[9];

Expand All @@ -147,15 +171,11 @@ void R__unzipBLAST(int *srcsize, unsigned char *src, int *tgtsize, unsigned char
// Use "absSense". We shift the request config from [1,71] to [-60, 10]
auto absSensLevel = cxlevel - 61;
// Note: We need to check the destination really start of a float boundary.
union {
float *f;
double *d;
char *c;
} staging;
RealTypes staging;
staging.c = nullptr;

size_t float_size = isfloat ? blast1_decompress<true>(absSensLevel, (char*)(&src[kHeaderSize]), *srcsize, staging.f)
: blast1_decompress<true>(absSensLevel, (char*)(&src[kHeaderSize]), *srcsize, staging.d);
size_t float_size = isfloat ? blast1_decompress<true>(absSensLevel, source, in_size, staging.f)
: blast1_decompress<true>(absSensLevel, source, in_size, staging.d);

const size_t elsize = isfloat ? sizeof(float) : sizeof(double);
out_size = float_size * elsize;
Expand All @@ -170,25 +190,25 @@ void R__unzipBLAST(int *srcsize, unsigned char *src, int *tgtsize, unsigned char
} else if (cxlevel <= 79) {
// Use "RLE. cx level determines data type
// Note: We need to check the destination really start of a short boundary.
char *staging = nullptr;
char*& stagingPtr = staging;
IntegerTypes staging;
staging.c = nullptr;
switch (cxlevel) {
case (79) : out_size = blast2_decompress<unsigned long long,true>((char*)(&src[kHeaderSize]), *srcsize, (unsigned long long*&) stagingPtr); break;
case (78) : out_size = blast2_decompress<unsigned int,true>((char*)(&src[kHeaderSize]), *srcsize, (unsigned int*&) stagingPtr); break;
case (77) : out_size = blast2_decompress<unsigned short,true>((char*)(&src[kHeaderSize]), *srcsize, (unsigned short*&) stagingPtr); break;
case (76) : out_size = blast2_decompress<unsigned char,true>((char*)(&src[kHeaderSize]), *srcsize, (unsigned char*&) stagingPtr);
case (75) : out_size = blast2_decompress<long long,true>((char*)(&src[kHeaderSize]), *srcsize, (long long*&) stagingPtr); break;
case (74) : out_size = blast2_decompress<int,true>((char*)(&src[kHeaderSize]), *srcsize, (int*&) stagingPtr); break;
case (73) : out_size = blast2_decompress<short,true>((char*)(&src[kHeaderSize]), *srcsize, (short*&) stagingPtr); break;
default : out_size = blast2_decompress<char,true>((char*)(&src[kHeaderSize]), *srcsize, staging);
case (79) : out_size = blast2_decompress<true>(source, in_size, staging.ul); break;
case (78) : out_size = blast2_decompress<true>(source, in_size, staging.ui); break;
case (77) : out_size = blast2_decompress<true>(source, in_size, staging.us); break;
case (76) : out_size = blast2_decompress<true>(source, in_size, staging.uc); break;
case (75) : out_size = blast2_decompress<true>(source, in_size, staging.l); break;
case (74) : out_size = blast2_decompress<true>(source, in_size, staging.i); break;
case (73) : out_size = blast2_decompress<true>(source, in_size, staging.s); break;
default : out_size = blast2_decompress<true>(source, in_size, staging.c);
}
// Note: We need to upgrade blast to avoid the memcpy
if ( out_size > (size_t)*tgtsize ) {
delete [] staging;
delete [] staging.c;
return;
}
memcpy(tgt, staging, out_size);
delete [] staging;
memcpy(tgt, staging.c, out_size);
delete [] staging.c;
*irep = out_size;
} else {
// Need to handle the other engine
Expand Down