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 invalid checksum error #1380

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
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
40 changes: 23 additions & 17 deletions cdm/core/src/main/java/ucar/nc2/filter/Shuffle.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int getId() {

@Override
public byte[] encode(byte[] dataIn) {
if (dataIn.length % elemSize != 0 || elemSize <= 1) {
if (elemSize <= 1) {
return dataIn;
}

Expand All @@ -56,29 +56,35 @@ public byte[] encode(byte[] dataIn) {
}
}

int leftoverBytes = dataIn.length % this.elemSize;
System.arraycopy(dataIn, dataIn.length - leftoverBytes, result, result.length - leftoverBytes, leftoverBytes);

return result;
}

@Override
public byte[] decode(byte[] dataIn) {
if (dataIn.length % this.elemSize == 0 && this.elemSize > 1) {
int nElems = dataIn.length / this.elemSize;
byte[] result = new byte[dataIn.length];

for (int j = 0; j < this.elemSize; ++j) {
int sourceIndex = j * nElems;
int destIndex = j;
for (int i = 0; i < nElems; ++i) {
result[destIndex] = dataIn[sourceIndex];
sourceIndex++;
destIndex += this.elemSize;
}
}

return result;
} else {
if (elemSize <= 1) {
return dataIn;
}

int nElems = dataIn.length / this.elemSize;
byte[] result = new byte[dataIn.length];

for (int j = 0; j < this.elemSize; ++j) {
int sourceIndex = j * nElems;
int destIndex = j;
for (int i = 0; i < nElems; ++i) {
result[destIndex] = dataIn[sourceIndex];
sourceIndex++;
destIndex += this.elemSize;
}
}

int leftoverBytes = dataIn.length % this.elemSize;
System.arraycopy(dataIn, dataIn.length - leftoverBytes, result, result.length - leftoverBytes, leftoverBytes);

return result;
}

public static class Provider implements FilterProvider {
Expand Down
22 changes: 22 additions & 0 deletions cdm/core/src/test/java/ucar/nc2/filter/TestFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ucar.unidata.io.RandomAccessFile;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -59,6 +60,27 @@ public void testShuffle() throws IOException {
testEncodeDecode(filter, "shuffle");
}

@Test
public void shouldShuffleDoublesAndLeaveIntAtEnd() throws IOException {
ByteBuffer bb = ByteBuffer.allocate(2 * 8 + 4);
bb.putDouble(-1.0);
bb.putDouble(1.0);
// to represent a checksum at the end of a chunk of doubles
bb.putInt(12345);
byte[] bytes = bb.array();

Map<String, Object> props = new HashMap<>();
props.put("id", "shuffle");
props.put("elementsize", 8);
Filter filter = new Shuffle(props);

byte[] expected = new byte[] {-65, 63, -16, -16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 57};
byte[] encoded = filter.encode(bytes);
assertThat(encoded).isEqualTo(expected);
byte[] decoded = filter.decode(encoded);
assertThat(decoded).isEqualTo(bytes);
}

@Test
public void testChecksum32() throws IOException {
// test Adler32
Expand Down
Loading