15317bbafSopenharmony_ci/*
25317bbafSopenharmony_ci * Fast QR Code generator library
35317bbafSopenharmony_ci *
45317bbafSopenharmony_ci * Copyright (c) Project Nayuki. (MIT License)
55317bbafSopenharmony_ci * https://www.nayuki.io/page/fast-qr-code-generator-library
65317bbafSopenharmony_ci *
75317bbafSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy of
85317bbafSopenharmony_ci * this software and associated documentation files (the "Software"), to deal in
95317bbafSopenharmony_ci * the Software without restriction, including without limitation the rights to
105317bbafSopenharmony_ci * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
115317bbafSopenharmony_ci * the Software, and to permit persons to whom the Software is furnished to do so,
125317bbafSopenharmony_ci * subject to the following conditions:
135317bbafSopenharmony_ci * - The above copyright notice and this permission notice shall be included in
145317bbafSopenharmony_ci *   all copies or substantial portions of the Software.
155317bbafSopenharmony_ci * - The Software is provided "as is", without warranty of any kind, express or
165317bbafSopenharmony_ci *   implied, including but not limited to the warranties of merchantability,
175317bbafSopenharmony_ci *   fitness for a particular purpose and noninfringement. In no event shall the
185317bbafSopenharmony_ci *   authors or copyright holders be liable for any claim, damages or other
195317bbafSopenharmony_ci *   liability, whether in an action of contract, tort or otherwise, arising from,
205317bbafSopenharmony_ci *   out of or in connection with the Software or the use or other dealings in the
215317bbafSopenharmony_ci *   Software.
225317bbafSopenharmony_ci */
235317bbafSopenharmony_ci
245317bbafSopenharmony_cipackage io.nayuki.fastqrcodegen;
255317bbafSopenharmony_ci
265317bbafSopenharmony_ciimport java.util.Arrays;
275317bbafSopenharmony_ciimport java.util.Objects;
285317bbafSopenharmony_ci
295317bbafSopenharmony_ci
305317bbafSopenharmony_ci// An appendable sequence of bits (0s and 1s), mainly used by QrSegment.
315317bbafSopenharmony_cifinal class BitBuffer {
325317bbafSopenharmony_ci
335317bbafSopenharmony_ci	/*---- Fields ----*/
345317bbafSopenharmony_ci
355317bbafSopenharmony_ci	int[] data;  // In each 32-bit word, bits are filled from top down.
365317bbafSopenharmony_ci
375317bbafSopenharmony_ci	int bitLength;  // Always non-negative.
385317bbafSopenharmony_ci
395317bbafSopenharmony_ci
405317bbafSopenharmony_ci
415317bbafSopenharmony_ci	/*---- Constructor ----*/
425317bbafSopenharmony_ci
435317bbafSopenharmony_ci	// Creates an empty bit buffer.
445317bbafSopenharmony_ci	public BitBuffer() {
455317bbafSopenharmony_ci		data = new int[64];
465317bbafSopenharmony_ci		bitLength = 0;
475317bbafSopenharmony_ci	}
485317bbafSopenharmony_ci
495317bbafSopenharmony_ci
505317bbafSopenharmony_ci
515317bbafSopenharmony_ci	/*---- Methods ----*/
525317bbafSopenharmony_ci
535317bbafSopenharmony_ci	// Returns the bit at the given index, yielding 0 or 1.
545317bbafSopenharmony_ci	public int getBit(int index) {
555317bbafSopenharmony_ci		if (index < 0 || index >= bitLength)
565317bbafSopenharmony_ci			throw new IndexOutOfBoundsException();
575317bbafSopenharmony_ci		return (data[index >>> 5] >>> ~index) & 1;
585317bbafSopenharmony_ci	}
595317bbafSopenharmony_ci
605317bbafSopenharmony_ci
615317bbafSopenharmony_ci	// Returns a new array representing this buffer's bits packed into
625317bbafSopenharmony_ci	// bytes in big endian. The current bit length must be a multiple of 8.
635317bbafSopenharmony_ci	public byte[] getBytes() {
645317bbafSopenharmony_ci		if (bitLength % 8 != 0)
655317bbafSopenharmony_ci			throw new IllegalStateException("Data is not a whole number of bytes");
665317bbafSopenharmony_ci		byte[] result = new byte[bitLength / 8];
675317bbafSopenharmony_ci		for (int i = 0; i < result.length; i++)
685317bbafSopenharmony_ci			result[i] = (byte)(data[i >>> 2] >>> (~i << 3));
695317bbafSopenharmony_ci		return result;
705317bbafSopenharmony_ci	}
715317bbafSopenharmony_ci
725317bbafSopenharmony_ci
735317bbafSopenharmony_ci	// Appends the given number of low-order bits of the given value
745317bbafSopenharmony_ci	// to this buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
755317bbafSopenharmony_ci	public void appendBits(int val, int len) {
765317bbafSopenharmony_ci		if (len < 0 || len > 31 || val >>> len != 0)
775317bbafSopenharmony_ci			throw new IllegalArgumentException("Value out of range");
785317bbafSopenharmony_ci		if (len > Integer.MAX_VALUE - bitLength)
795317bbafSopenharmony_ci			throw new IllegalStateException("Maximum length reached");
805317bbafSopenharmony_ci
815317bbafSopenharmony_ci		if (bitLength + len + 1 > data.length << 5)
825317bbafSopenharmony_ci			data = Arrays.copyOf(data, data.length * 2);
835317bbafSopenharmony_ci		assert bitLength + len <= data.length << 5;
845317bbafSopenharmony_ci
855317bbafSopenharmony_ci		int remain = 32 - (bitLength & 0x1F);
865317bbafSopenharmony_ci		assert 1 <= remain && remain <= 32;
875317bbafSopenharmony_ci		if (remain < len) {
885317bbafSopenharmony_ci			data[bitLength >>> 5] |= val >>> (len - remain);
895317bbafSopenharmony_ci			bitLength += remain;
905317bbafSopenharmony_ci			assert (bitLength & 0x1F) == 0;
915317bbafSopenharmony_ci			len -= remain;
925317bbafSopenharmony_ci			val &= (1 << len) - 1;
935317bbafSopenharmony_ci			remain = 32;
945317bbafSopenharmony_ci		}
955317bbafSopenharmony_ci		data[bitLength >>> 5] |= val << (remain - len);
965317bbafSopenharmony_ci		bitLength += len;
975317bbafSopenharmony_ci	}
985317bbafSopenharmony_ci
995317bbafSopenharmony_ci
1005317bbafSopenharmony_ci	// Appends to this buffer the sequence of bits represented by the given
1015317bbafSopenharmony_ci	// word array and given bit length. Requires 0 <= len <= 32 * vals.length.
1025317bbafSopenharmony_ci	public void appendBits(int[] vals, int len) {
1035317bbafSopenharmony_ci		Objects.requireNonNull(vals);
1045317bbafSopenharmony_ci		if (len == 0)
1055317bbafSopenharmony_ci			return;
1065317bbafSopenharmony_ci		if (len < 0 || len > vals.length * 32L)
1075317bbafSopenharmony_ci			throw new IllegalArgumentException("Value out of range");
1085317bbafSopenharmony_ci		int wholeWords = len / 32;
1095317bbafSopenharmony_ci		int tailBits = len % 32;
1105317bbafSopenharmony_ci		if (tailBits > 0 && vals[wholeWords] << tailBits != 0)
1115317bbafSopenharmony_ci			throw new IllegalArgumentException("Last word must have low bits clear");
1125317bbafSopenharmony_ci		if (len > Integer.MAX_VALUE - bitLength)
1135317bbafSopenharmony_ci			throw new IllegalStateException("Maximum length reached");
1145317bbafSopenharmony_ci
1155317bbafSopenharmony_ci		while (bitLength + len > data.length * 32)
1165317bbafSopenharmony_ci			data = Arrays.copyOf(data, data.length * 2);
1175317bbafSopenharmony_ci
1185317bbafSopenharmony_ci		int shift = bitLength % 32;
1195317bbafSopenharmony_ci		if (shift == 0) {
1205317bbafSopenharmony_ci			System.arraycopy(vals, 0, data, bitLength / 32, (len + 31) / 32);
1215317bbafSopenharmony_ci			bitLength += len;
1225317bbafSopenharmony_ci		} else {
1235317bbafSopenharmony_ci			for (int i = 0; i < wholeWords; i++) {
1245317bbafSopenharmony_ci				int word = vals[i];
1255317bbafSopenharmony_ci				data[bitLength >>> 5] |= word >>> shift;
1265317bbafSopenharmony_ci				bitLength += 32;
1275317bbafSopenharmony_ci				data[bitLength >>> 5] = word << (32 - shift);
1285317bbafSopenharmony_ci			}
1295317bbafSopenharmony_ci			if (tailBits > 0)
1305317bbafSopenharmony_ci				appendBits(vals[wholeWords] >>> (32 - tailBits), tailBits);
1315317bbafSopenharmony_ci		}
1325317bbafSopenharmony_ci	}
1335317bbafSopenharmony_ci
1345317bbafSopenharmony_ci}
135