From 2f1acb1e6f9429bc9c09ad69bb862cf59235648e Mon Sep 17 00:00:00 2001 From: Tim203 Date: Sat, 19 Dec 2020 22:45:34 +0100 Subject: [PATCH] Separate method for Base64 length calc. Added offset method in RawSkin --- .../geysermc/floodgate/util/Base64Utils.java | 35 +++++++++++++++++++ .../org/geysermc/floodgate/util/RawSkin.java | 11 ++++-- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/org/geysermc/floodgate/util/Base64Utils.java diff --git a/common/src/main/java/org/geysermc/floodgate/util/Base64Utils.java b/common/src/main/java/org/geysermc/floodgate/util/Base64Utils.java new file mode 100644 index 000000000..388b54838 --- /dev/null +++ b/common/src/main/java/org/geysermc/floodgate/util/Base64Utils.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019-2020 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.floodgate.util; + +public class Base64Utils { + public static int getEncodedLength(int length) { + if (length <= 0) { + return -1; + } + return 4 * ((length + 2) / 3); + } +} diff --git a/common/src/main/java/org/geysermc/floodgate/util/RawSkin.java b/common/src/main/java/org/geysermc/floodgate/util/RawSkin.java index 152cbec5a..6759ffa3c 100644 --- a/common/src/main/java/org/geysermc/floodgate/util/RawSkin.java +++ b/common/src/main/java/org/geysermc/floodgate/util/RawSkin.java @@ -45,13 +45,18 @@ public final class RawSkin { } public static RawSkin decode(byte[] data) throws InvalidFormatException { + return decode(data, 0); + } + + public static RawSkin decode(byte[] data, int offset) throws InvalidFormatException { + // offset is an amount of bytes before the Base64 starts if (data == null) { return null; } - int maxEncodedLength = 4 * (((64 * 64 * 4 + 9) + 2) / 3); + int maxEncodedLength = Base64Utils.getEncodedLength(64 * 64 * 4 + 9); // if the RawSkin is longer then the max Java Edition skin length - if (data.length > maxEncodedLength) { + if ((data.length - offset) > maxEncodedLength) { throw new InvalidFormatException(format( "Encoded data cannot be longer then %s bytes! Got %s", maxEncodedLength, data.length @@ -59,7 +64,7 @@ public final class RawSkin { } // if the encoded data doesn't even contain the width, height (8 bytes, 2 ints) and isAlex - if (data.length < 4 * ((9 + 2) / 3)) { + if ((data.length - offset) < Base64Utils.getEncodedLength(9)) { throw new InvalidFormatException("Encoded data must be at least 16 bytes long!"); }