001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.serialization.jvm; 018 019 import org.jetbrains.annotations.NotNull; 020 021 import java.util.ArrayList; 022 import java.util.List; 023 024 import static org.jetbrains.kotlin.serialization.jvm.UtfEncodingKt.MAX_UTF8_INFO_LENGTH; 025 026 public class BitEncoding { 027 private static final boolean FORCE_8TO7_ENCODING = "true".equals(System.getProperty("kotlin.jvm.serialization.use8to7")); 028 029 private static final char _8TO7_MODE_MARKER = (char) -1; 030 031 private BitEncoding() { 032 } 033 034 /** 035 * Converts a byte array of serialized data to an array of {@code String} satisfying JVM annotation value argument restrictions: 036 * <ol> 037 * <li>Each string's length should be no more than 65535</li> 038 * <li>UTF-8 representation of each string cannot contain bytes in the range 0xf0..0xff</li> 039 * </ol> 040 */ 041 @NotNull 042 public static String[] encodeBytes(@NotNull byte[] data) { 043 // TODO: try both encodings here and choose the best one (with the smallest size) 044 if (!FORCE_8TO7_ENCODING) { 045 return UtfEncodingKt.bytesToStrings(data); 046 } 047 byte[] bytes = encode8to7(data); 048 // Since 0x0 byte is encoded as two bytes in the Modified UTF-8 (0xc0 0x80) and zero is rather common to byte arrays, we increment 049 // every byte by one modulo max byte value, so that the less common value 0x7f will be represented as two bytes instead. 050 addModuloByte(bytes, 1); 051 return splitBytesToStringArray(bytes); 052 } 053 054 /** 055 * Converts a byte array to another byte array, every element of which is in the range 0x0..0x7f. 056 * 057 * The conversion is equivalent to the following: input bytes are combined into one long bit string. This big string is then split into 058 * groups of 7 bits. Each resulting 7-bit chunk is then converted to a byte (with a leading bit = 0). The last chunk may have less than 059 * 7 bits, it's prepended with zeros to form a byte. The result is then the array of these bytes, each of which is obviously in the 060 * range 0x0..0x7f. 061 * 062 * Suppose the input of 4 bytes is given (bytes are listed from the beginning to the end, each byte from the least significant bit to 063 * the most significant bit, bits within each byte are numbered): 064 * 065 * 01234567 01234567 01234567 01234567 066 * 067 * The output for this kind of input will be of the following form ('#' represents a zero bit): 068 * 069 * 0123456# 7012345# 6701234# 5670123# 4567#### 070 */ 071 @NotNull 072 private static byte[] encode8to7(@NotNull byte[] data) { 073 // ceil(data.length * 8 / 7) 074 int resultLength = (data.length * 8 + 6) / 7; 075 byte[] result = new byte[resultLength]; 076 077 // We maintain a pointer to the bit in the input, which is represented by two numbers: index of the current byte in the input and 078 // the index of a bit inside this byte (0 is least significant, 7 is most significant) 079 int byteIndex = 0; 080 int bit = 0; 081 082 // Write all resulting bytes except the last one. To do this we need to collect exactly 7 bits, starting from the current, into a 083 // byte. In almost all cases these 7 bits can be collected from two parts: the first is several (at least one) most significant bits 084 // from the current byte, the second is several (maybe zero) least significant bits from the next byte. The special case is when the 085 // current bit is the first (least significant) bit in its byte (bit == 0): then the 7 needed bits are just the 7 least significant 086 // of the current byte. 087 for (int i = 0; i < resultLength - 1; i++) { 088 if (bit == 0) { 089 result[i] = (byte) (data[byteIndex] & 0x7f); 090 bit = 7; 091 continue; 092 } 093 094 int firstPart = (data[byteIndex] & 0xff) >>> bit; 095 int newBit = (bit + 7) & 7; 096 int secondPart = (data[++byteIndex] & ((1 << newBit) - 1)) << 8 - bit; 097 result[i] = (byte) (firstPart + secondPart); 098 bit = newBit; 099 } 100 101 // Write the last byte, which is just several most significant bits of the last byte in the input, padded with zeros 102 if (resultLength > 0) { 103 assert bit != 0 : "The last chunk cannot start from the input byte since otherwise at least one bit will remain unprocessed"; 104 assert byteIndex == data.length - 1 : "The last 7-bit chunk should be encoded from the last input byte: " + 105 byteIndex + " != " + (data.length - 1); 106 result[resultLength - 1] = (byte) ((data[byteIndex] & 0xff) >>> bit); 107 } 108 109 return result; 110 } 111 112 private static void addModuloByte(@NotNull byte[] data, int increment) { 113 for (int i = 0, n = data.length; i < n; i++) { 114 data[i] = (byte) ((data[i] + increment) & 0x7f); 115 } 116 } 117 118 /** 119 * Converts a big byte array into the array of strings, where each string, when written to the constant pool table in bytecode, produces 120 * a byte array of not more than MAX_UTF8_INFO_LENGTH. Each byte, except those which are 0x0, occupies exactly one byte in the constant 121 * pool table. Zero bytes occupy two bytes in the table each. 122 * 123 * When strings are constructed from the array of bytes here, they are encoded in the platform's default encoding. This is fine: the 124 * conversion to the Modified UTF-8 (which here would be equivalent to replacing each 0x0 with 0xc0 0x80) will happen later by ASM, when 125 * it writes these strings to the bytecode 126 */ 127 @NotNull 128 private static String[] splitBytesToStringArray(@NotNull byte[] data) { 129 List<String> result = new ArrayList<String>(); 130 131 // The offset where the currently processed string starts 132 int off = 0; 133 134 // The effective length the bytes of the current string would occupy in the constant pool table. 135 // 2 because the first char is -1 which denotes the encoding mode and occupies two bytes in Modified UTF-8 136 int len = 2; 137 138 boolean encodingModeAdded = false; 139 140 for (int i = 0, n = data.length; i < n; i++) { 141 // When the effective length reaches at least MAX - 1, we add the current string to the result. Note that the effective length 142 // is at most MAX here: non-zero bytes occupy 1 byte and zero bytes occupy 2 bytes, so we couldn't jump over more than one byte 143 if (len >= MAX_UTF8_INFO_LENGTH - 1) { 144 assert len <= MAX_UTF8_INFO_LENGTH : "Produced strings cannot contain more than " + MAX_UTF8_INFO_LENGTH + " bytes: " + len; 145 String string = new String(data, off, i - off); 146 if (!encodingModeAdded) { 147 encodingModeAdded = true; 148 result.add(_8TO7_MODE_MARKER + string); 149 } 150 else { 151 result.add(string); 152 } 153 off = i; 154 len = 0; 155 } 156 157 if (data[i] == 0) { 158 len += 2; 159 } 160 else { 161 len++; 162 } 163 } 164 165 if (len >= 0) { 166 result.add(new String(data, off, data.length - off)); 167 } 168 169 return result.toArray(new String[result.size()]); 170 } 171 172 /** 173 * Converts encoded array of {@code String} obtained by {@link BitEncoding#encodeBytes(byte[])} back to a byte array. 174 */ 175 @NotNull 176 public static byte[] decodeBytes(@NotNull String[] data) { 177 if (data.length > 0 && !data[0].isEmpty()) { 178 char possibleMarker = data[0].charAt(0); 179 if (possibleMarker == UtfEncodingKt.UTF8_MODE_MARKER) { 180 return UtfEncodingKt.stringsToBytes(dropMarker(data)); 181 } 182 if (possibleMarker == _8TO7_MODE_MARKER) { 183 data = dropMarker(data); 184 } 185 } 186 187 byte[] bytes = combineStringArrayIntoBytes(data); 188 // Adding 0x7f modulo max byte value is equivalent to subtracting 1 the same modulo, which is inverse to what happens in encodeBytes 189 addModuloByte(bytes, 0x7f); 190 return decode7to8(bytes); 191 } 192 193 @NotNull 194 private static String[] dropMarker(@NotNull String[] data) { 195 // Clone because the clients should be able to use the passed array for their own purposes. 196 // This is cheap because the size of the array is 1 or 2 almost always. 197 String[] result = data.clone(); 198 result[0] = result[0].substring(1); 199 return result; 200 } 201 202 /** 203 * Combines the array of strings resulted from encodeBytes() into one long byte array 204 */ 205 @NotNull 206 private static byte[] combineStringArrayIntoBytes(@NotNull String[] data) { 207 int resultLength = 0; 208 for (String s : data) { 209 assert s.length() <= MAX_UTF8_INFO_LENGTH : "String is too long: " + s.length(); 210 resultLength += s.length(); 211 } 212 213 byte[] result = new byte[resultLength]; 214 int p = 0; 215 for (String s : data) { 216 for (int i = 0, n = s.length(); i < n; i++) { 217 result[p++] = (byte) s.charAt(i); 218 } 219 } 220 221 return result; 222 } 223 224 /** 225 * Decodes the byte array resulted from encode8to7(). 226 * 227 * Each byte of the input array has at most 7 valuable bits of information. So the decoding is equivalent to the following: least 228 * significant 7 bits of all input bytes are combined into one long bit string. This bit string is then split into groups of 8 bits, 229 * each of which forms a byte in the output. If there are any leftovers, they are ignored, since they were added just as a padding and 230 * do not comprise a full byte. 231 * 232 * Suppose the following encoded byte array is given (bits are numbered the same way as in encode8to7() doc): 233 * 234 * 01234567 01234567 01234567 01234567 235 * 236 * The output of the following form would be produced: 237 * 238 * 01234560 12345601 23456012 239 * 240 * Note how all most significant bits and leftovers are dropped, since they don't contain any useful information 241 */ 242 @NotNull 243 private static byte[] decode7to8(@NotNull byte[] data) { 244 // floor(7 * data.length / 8) 245 int resultLength = 7 * data.length / 8; 246 247 byte[] result = new byte[resultLength]; 248 249 // We maintain a pointer to an input bit in the same fashion as in encode8to7(): it's represented as two numbers: index of the 250 // current byte in the input and index of the bit in the byte 251 int byteIndex = 0; 252 int bit = 0; 253 254 // A resulting byte is comprised of 8 bits, starting from the current bit. Since each input byte only "contains 7 bytes", a 255 // resulting byte always consists of two parts: several most significant bits of the current byte and several least significant bits 256 // of the next byte 257 for (int i = 0; i < resultLength; i++) { 258 int firstPart = (data[byteIndex] & 0xff) >>> bit; 259 byteIndex++; 260 int secondPart = (data[byteIndex] & ((1 << (bit + 1)) - 1)) << 7 - bit; 261 result[i] = (byte) (firstPart + secondPart); 262 263 if (bit == 6) { 264 byteIndex++; 265 bit = 0; 266 } 267 else { 268 bit++; 269 } 270 } 271 272 return result; 273 } 274 }