/* * BIP39 library, a Java implementation of BIP39 * Copyright (C) 2017-2019 Alan Evans, NovaCrypto * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Original source: https://github.com/NovaCrypto/BIP39 * You can contact the authors via github issues. */ package io.github.novacrypto.bip39; final class ByteUtils { static int next11Bits(byte[] bytes, int offset) { final int skip = offset / 8; final int lowerBitsToRemove = (3 * 8 - 11) - (offset % 8); return (((int) bytes[skip] & 0xff) << 16 | ((int) bytes[skip + 1] & 0xff) << 8 | (lowerBitsToRemove < 8 ? ((int) bytes[skip + 2] & 0xff) : 0)) >> lowerBitsToRemove & (1 << 11) - 1; } static void writeNext11(byte[] bytes, int value, int offset) { int skip = offset / 8; int bitSkip = offset % 8; {//byte 0 byte firstValue = bytes[skip]; byte toWrite = (byte) (value >> (3 + bitSkip)); bytes[skip] = (byte) (firstValue | toWrite); } {//byte 1 byte valueInByte = bytes[skip + 1]; final int i = 5 - bitSkip; byte toWrite = (byte) (i > 0 ? (value << i) : (value >> -i)); bytes[skip + 1] = (byte) (valueInByte | toWrite); } if (bitSkip >= 6) {//byte 2 byte valueInByte = bytes[skip + 2]; byte toWrite = (byte) (value << 13 - bitSkip); bytes[skip + 2] = (byte) (valueInByte | toWrite); } } }