From 3a4d3853e8506fb109472dac2cb7d5a37ea5dee7 Mon Sep 17 00:00:00 2001 From: torusrxxx Date: Sun, 6 Oct 2024 21:15:01 +0800 Subject: [PATCH] fix coding styles --- src/freenet/client/DefaultMIMETypes.java | 3 - src/freenet/client/filter/ContentFilter.java | 42 +++++++------- src/freenet/client/filter/WAVFilter.java | 57 +++++++++++-------- test/freenet/client/filter/WAVFilterTest.java | 56 +++++++++--------- 4 files changed, 81 insertions(+), 77 deletions(-) diff --git a/src/freenet/client/DefaultMIMETypes.java b/src/freenet/client/DefaultMIMETypes.java index b40cf39c50..1c5f767343 100644 --- a/src/freenet/client/DefaultMIMETypes.java +++ b/src/freenet/client/DefaultMIMETypes.java @@ -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. diff --git a/src/freenet/client/filter/ContentFilter.java b/src/freenet/client/filter/ContentFilter.java index 590ed83cb1..d41c165679 100644 --- a/src/freenet/client/filter/ContentFilter.java +++ b/src/freenet/client/filter/ContentFilter.java @@ -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)); @@ -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 { diff --git a/src/freenet/client/filter/WAVFilter.java b/src/freenet/client/filter/WAVFilter.java index 998a62852c..c5d45dc21c 100644 --- a/src/freenet/client/filter/WAVFilter.java +++ b/src/freenet/client/filter/WAVFilter.java @@ -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 @@ -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()); @@ -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"); @@ -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); diff --git a/test/freenet/client/filter/WAVFilterTest.java b/test/freenet/client/filter/WAVFilterTest.java index 4ba51355d4..4399ec058c 100644 --- a/test/freenet/client/filter/WAVFilterTest.java +++ b/test/freenet/client/filter/WAVFilterTest.java @@ -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 expected) throws IOException { @@ -78,11 +78,11 @@ private Bucket filterWAV(Bucket input, Class 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; }