Skip to content

Commit

Permalink
fix coding styles
Browse files Browse the repository at this point in the history
  • Loading branch information
torusrxxx committed Oct 6, 2024
1 parent e92a7c3 commit 3a4d385
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 77 deletions.
3 changes: 0 additions & 3 deletions src/freenet/client/DefaultMIMETypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,6 @@ public synchronized static short byName(String s) {
addMIMEType((short)620, "audio/ogg", "oga");
addMIMEType((short)621, "audio/flac", "flac");
addMIMEType((short)622, "image/webp", "webp");
addMIMEType((short)623, "image/avif", "avif");
addMIMEType((short)624, "image/heic", "heic");
addMIMEType((short)625, "image/heif", "heif");
}

/** Guess a MIME type from a filename.
Expand Down
42 changes: 21 additions & 21 deletions src/freenet/client/filter/ContentFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ false, false, new M3UFilter(), false, false, false, false, false, false,
l10n("audioMP3ReadAdvice"), false, null, null, false));

// WAV - has a filter
register(new FilterMIMEType("audio/vnd.wave", "mp3", new String[] {"audio/vnd.wave", "audio/x-wav", "audio/wav", "audio/wave"},
register(new FilterMIMEType("audio/vnd.wave", "wav", new String[] {"audio/x-wav", "audio/wav", "audio/wave"},
new String[0], true, false, new WAVFilter(), true, true, false, true, false, false,
l10n("audioWAVReadAdvice"), false, null, null, false));

Expand Down Expand Up @@ -538,26 +538,26 @@ public static boolean startsWith(byte[] data, byte[] cmp, int length) {
}

public static String mimeTypeForSrc(String uriold) {
String uriPath = uriold.contains("?")
? uriold.split("\\?")[0]
: uriold;
String subMimetype;
if (uriPath.endsWith(".m3u") || uriPath.endsWith(".m3u8")) {
subMimetype = "audio/mpegurl";
} else if (uriPath.endsWith(".flac")) {
subMimetype = "audio/flac";
} else if (uriPath.endsWith(".oga")) {
subMimetype = "audio/ogg";
} else if (uriPath.endsWith(".ogv")) {
subMimetype = "video/ogg";
} else if (uriPath.endsWith(".ogg")) {
subMimetype = "application/ogg";
} else if (uriPath.endsWith(".wav")) {
subMimetype = "audio/vnd.wave";
} else { // force mp3 for anything we do not know
subMimetype = "audio/mpeg";
}
return subMimetype;
String uriPath = uriold.contains("?")
? uriold.split("\\?")[0]
: uriold;
String subMimetype;
if (uriPath.endsWith(".m3u") || uriPath.endsWith(".m3u8")) {
subMimetype = "audio/mpegurl";
} else if (uriPath.endsWith(".flac")) {
subMimetype = "audio/flac";
} else if (uriPath.endsWith(".oga")) {
subMimetype = "audio/ogg";
} else if (uriPath.endsWith(".ogv")) {
subMimetype = "video/ogg";
} else if (uriPath.endsWith(".ogg")) {
subMimetype = "application/ogg";
} else if (uriPath.endsWith(".wav")) {
subMimetype = "audio/vnd.wave";
} else { // force mp3 for anything we do not know
subMimetype = "audio/mpeg";
}
return subMimetype;
}

public static class FilterStatus {
Expand Down
57 changes: 32 additions & 25 deletions src/freenet/client/filter/WAVFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ public class WAVFilter extends RIFFFilter {
private final int WAVE_FORMAT_IEEE_FLOAT = 3;
private final int WAVE_FORMAT_ALAW = 6;
private final int WAVE_FORMAT_MULAW = 7;
// Header sizes (https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html)
// fmt header without cbSize field
private final int FMT_SIZE_BASIC = 16;
// fmt header with cbSize = 0
private final int FMT_SIZE_cbSize = 18;
// fmt header with cbSize and extensions
private final int FMT_SIZE_cbSize_extension = 40;

@Override
protected byte[] getChunkMagicNumber() {
return new byte[] {'W', 'A', 'V', 'E'};
}

class WAVFilterContext {
public boolean hasfmt = false;
public boolean hasdata = false;
public int nSamplesPerSec = 0;
public int nChannels = 0;
public int nBlockAlign = 0;
public int wBitsPerSample = 0;
public int format = 0;
private static final class WAVFilterContext {
boolean hasfmt = false;
boolean hasdata = false;
int nSamplesPerSec = 0;
int nChannels = 0;
int nBlockAlign = 0;
int wBitsPerSample = 0;
int format = 0;
}

@Override
Expand All @@ -44,7 +51,7 @@ protected void readFilterChunk(byte[] ID, int size, Object context, DataInputStr
if(ctx.hasfmt) {
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "Unexpected fmt chunk was encountered");
}
if(size != 16 && size != 18 && size != 40) {
if(size != FMT_SIZE_BASIC && size != FMT_SIZE_cbSize && size != FMT_SIZE_cbSize_extension) {
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "fmt chunk size is invalid");
}
ctx.format = Short.reverseBytes(input.readShort());
Expand All @@ -63,37 +70,36 @@ protected void readFilterChunk(byte[] ID, int size, Object context, DataInputStr
ctx.wBitsPerSample = Short.reverseBytes(input.readShort());
output.writeInt((Short.reverseBytes((short) ctx.nBlockAlign) << 16) | Short.reverseBytes((short) ctx.wBitsPerSample));
ctx.hasfmt = true;
if(size > 16) {
if(size > FMT_SIZE_BASIC) {
short cbSize = Short.reverseBytes(input.readShort());
if(cbSize + 18 != size) {
if(cbSize + FMT_SIZE_cbSize != size) {
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "fmt chunk size is invalid");
}
output.writeShort(Short.reverseBytes(cbSize));
}
if(size > 18) {
if(size > FMT_SIZE_cbSize) {
// wValidBitsPerSample, dwChannelMask, and SubFormat GUID
passthroughBytes(input, output, 22);
passthroughBytes(input, output, FMT_SIZE_cbSize_extension - FMT_SIZE_cbSize);
}
// Further checks
if((ctx.format == WAVE_FORMAT_ALAW || ctx.format == WAVE_FORMAT_MULAW) && ctx.wBitsPerSample != 8) {
// These formats are 8-bit
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "Unexpected bits per sample value");
}
return;
} else if(!ctx.hasfmt) {
}
if(!ctx.hasfmt) {
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "Unexpected header chunk was encountered, instead of fmt chunk");
}
if(ID[0] == 'd' && ID[1] == 'a' && ID[2] == 't' && ID[3] == 'a') {
if(ctx.format == WAVE_FORMAT_PCM || ctx.format == WAVE_FORMAT_IEEE_FLOAT || ctx.format == WAVE_FORMAT_ALAW || ctx.format == WAVE_FORMAT_MULAW) {
// Safe format, pass through
output.write(ID);
writeLittleEndianInt(output, size);
passthroughBytes(input, output, size);
if((size & 1) != 0) // Add padding if necessary
output.writeByte(input.readByte());
ctx.hasdata = true;
} else {
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "Data format is not yet supported");
// audio data
output.write(ID);
writeLittleEndianInt(output, size);
passthroughBytes(input, output, size);
if((size & 1) != 0) { // Add padding if necessary
output.writeByte(input.readByte());
}
ctx.hasdata = true;
} else if(ID[0] == 'f' && ID[1] == 'a' && ID[2] == 'c' && ID[3] == 't') {
if(size < 4) {
throw new DataFilterException(l10n("invalidTitle"), l10n("invalidTitle"), "fact chunk must contain at least 4 bytes");
Expand All @@ -102,8 +108,9 @@ protected void readFilterChunk(byte[] ID, int size, Object context, DataInputStr
output.write(ID);
writeLittleEndianInt(output, size);
passthroughBytes(input, output, size);
if((size & 1) != 0) // Add padding if necessary
if((size & 1) != 0) { // Add padding if necessary
output.writeByte(input.readByte());
}
} else {
// Unknown block
writeJunkChunk(input, output, size);
Expand Down
56 changes: 28 additions & 28 deletions test/freenet/client/filter/WAVFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,36 @@ public void testAnotherFile() throws IOException {
// There is just a JUNK chunk in the file
@Test
public void testFileJustJUNK() throws IOException {
ByteBuffer buf = ByteBuffer.allocate(28)
.order(ByteOrder.LITTLE_ENDIAN)
.put(new byte[]{'R', 'I', 'F', 'F'})
.putInt(20 /* file size */)
.put(new byte[]{'W', 'A', 'V', 'E'})
.put(new byte[]{'J', 'U', 'N', 'K'})
.putInt(7 /* chunk size */)
.putLong(0);
ByteBuffer buf = ByteBuffer.allocate(28)
.order(ByteOrder.LITTLE_ENDIAN)
.put(new byte[]{'R', 'I', 'F', 'F'})
.putInt(20 /* file size */)
.put(new byte[]{'W', 'A', 'V', 'E'})
.put(new byte[]{'J', 'U', 'N', 'K'})
.putInt(7 /* chunk size */)
.putLong(0);

Bucket input = new ArrayBucket(buf.array());
filterWAV(input, DataFilterException.class);
Bucket input = new ArrayBucket(buf.array());
filterWAV(input, DataFilterException.class);
}

// There is just a fmt chunk in the file, but no audio data
@Test
public void testFileNoData() throws IOException {
ByteBuffer buf = ByteBuffer.allocate(36)
.order(ByteOrder.LITTLE_ENDIAN)
.put(new byte[]{'R', 'I', 'F', 'F'})
.putInt(28 /* file size */)
.put(new byte[]{'W', 'A', 'V', 'E'})
.put(new byte[]{'f', 'm', 't', ' '})
.putInt(16 /* chunk size */)
.put(new byte[]{1, 0, 2, 0}) //format, nChannels
.putInt(44100) // nSamplesPerSec
.putInt(44100 * 4) // nAvgBytesPerSec
.put(new byte[]{4, 0, 16, 0}); // nBlockAlign, wBitsPerSample
ByteBuffer buf = ByteBuffer.allocate(36)
.order(ByteOrder.LITTLE_ENDIAN)
.put(new byte[]{'R', 'I', 'F', 'F'})
.putInt(28 /* file size */)
.put(new byte[]{'W', 'A', 'V', 'E'})
.put(new byte[]{'f', 'm', 't', ' '})
.putInt(16 /* chunk size */)
.put(new byte[]{1, 0, 2, 0}) //format, nChannels
.putInt(44100) // nSamplesPerSec
.putInt(44100 * 4) // nAvgBytesPerSec
.put(new byte[]{4, 0, 16, 0}); // nBlockAlign, wBitsPerSample

Bucket input = new ArrayBucket(buf.array());
filterWAV(input, DataFilterException.class);
Bucket input = new ArrayBucket(buf.array());
filterWAV(input, DataFilterException.class);
}

private Bucket filterWAV(Bucket input, Class<? extends Exception> expected) throws IOException {
Expand All @@ -78,11 +78,11 @@ private Bucket filterWAV(Bucket input, Class<? extends Exception> expected) thro
InputStream inStream = input.getInputStream();
OutputStream outStream = output.getOutputStream()
) {
if (expected != null) {
assertThrows(expected, () -> objWAVFilter.readFilter(inStream, outStream, "", null, null, null));
} else {
objWAVFilter.readFilter(inStream, outStream, "", null, null, null);
}
if (expected != null) {
assertThrows(expected, () -> objWAVFilter.readFilter(inStream, outStream, "", null, null, null));
} else {
objWAVFilter.readFilter(inStream, outStream, "", null, null, null);
}
}
return output;
}
Expand Down

0 comments on commit 3a4d385

Please sign in to comment.