001/** 002 * 003 * Copyright the original author or authors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util.stringencoder; 018 019 020import java.io.ByteArrayOutputStream; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024/** 025 * Base32 string encoding is useful for when filenames case-insensitive filesystems are encoded. 026 * Base32 representation takes roughly 20% more space then Base64. 027 * 028 * @author Florian Schmaus 029 * Based on code by Brian Wellington (bwelling@xbill.org) 030 * @see <a href="http://en.wikipedia.org/wiki/Base32">Base32 Wikipedia entry</a> 031 * 032 */ 033public class Base32 { 034 035 private static final StringEncoder base32Stringencoder = new StringEncoder() { 036 037 @Override 038 public String encode(String string) { 039 return Base32.encode(string); 040 } 041 042 @Override 043 public String decode(String string) { 044 return Base32.decode(string); 045 } 046 047 }; 048 private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678"; 049 050 public static StringEncoder getStringEncoder() { 051 return base32Stringencoder; 052 } 053 054 public static String decode(String str) { 055 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 056 byte[] raw = str.getBytes(); 057 for (int i = 0; i < raw.length; i++) { 058 char c = (char) raw[i]; 059 if (!Character.isWhitespace(c)) { 060 c = Character.toUpperCase(c); 061 bs.write((byte) c); 062 } 063 } 064 065 while (bs.size() % 8 != 0) 066 bs.write('8'); 067 068 byte[] in = bs.toByteArray(); 069 070 bs.reset(); 071 DataOutputStream ds = new DataOutputStream(bs); 072 073 for (int i = 0; i < in.length / 8; i++) { 074 short[] s = new short[8]; 075 int[] t = new int[5]; 076 077 int padlen = 8; 078 for (int j = 0; j < 8; j++) { 079 char c = (char) in[i * 8 + j]; 080 if (c == '8') 081 break; 082 s[j] = (short) ALPHABET.indexOf(in[i * 8 + j]); 083 if (s[j] < 0) 084 return null; 085 padlen--; 086 } 087 int blocklen = paddingToLen(padlen); 088 if (blocklen < 0) 089 return null; 090 091 // all 5 bits of 1st, high 3 (of 5) of 2nd 092 t[0] = (s[0] << 3) | s[1] >> 2; 093 // lower 2 of 2nd, all 5 of 3rd, high 1 of 4th 094 t[1] = ((s[1] & 0x03) << 6) | (s[2] << 1) | (s[3] >> 4); 095 // lower 4 of 4th, high 4 of 5th 096 t[2] = ((s[3] & 0x0F) << 4) | ((s[4] >> 1) & 0x0F); 097 // lower 1 of 5th, all 5 of 6th, high 2 of 7th 098 t[3] = (s[4] << 7) | (s[5] << 2) | (s[6] >> 3); 099 // lower 3 of 7th, all of 8th 100 t[4] = ((s[6] & 0x07) << 5) | s[7]; 101 102 try { 103 for (int j = 0; j < blocklen; j++) 104 ds.writeByte((byte) (t[j] & 0xFF)); 105 } catch (IOException e) { 106 } 107 } 108 109 return new String(bs.toByteArray()); 110 } 111 112 public static String encode(String str) { 113 byte[] b = str.getBytes(); 114 ByteArrayOutputStream os = new ByteArrayOutputStream(); 115 116 for (int i = 0; i < (b.length + 4) / 5; i++) { 117 short[] s = new short[5]; 118 int[] t = new int[8]; 119 120 int blocklen = 5; 121 for (int j = 0; j < 5; j++) { 122 if ((i * 5 + j) < b.length) 123 s[j] = (short) (b[i * 5 + j] & 0xFF); 124 else { 125 s[j] = 0; 126 blocklen--; 127 } 128 } 129 int padlen = lenToPadding(blocklen); 130 131 // convert the 5 byte block into 8 characters (values 0-31). 132 133 // upper 5 bits from first byte 134 t[0] = (byte) ((s[0] >> 3) & 0x1F); 135 // lower 3 bits from 1st byte, upper 2 bits from 2nd. 136 t[1] = (byte) (((s[0] & 0x07) << 2) | ((s[1] >> 6) & 0x03)); 137 // bits 5-1 from 2nd. 138 t[2] = (byte) ((s[1] >> 1) & 0x1F); 139 // lower 1 bit from 2nd, upper 4 from 3rd 140 t[3] = (byte) (((s[1] & 0x01) << 4) | ((s[2] >> 4) & 0x0F)); 141 // lower 4 from 3rd, upper 1 from 4th. 142 t[4] = (byte) (((s[2] & 0x0F) << 1) | ((s[3] >> 7) & 0x01)); 143 // bits 6-2 from 4th 144 t[5] = (byte) ((s[3] >> 2) & 0x1F); 145 // lower 2 from 4th, upper 3 from 5th; 146 t[6] = (byte) (((s[3] & 0x03) << 3) | ((s[4] >> 5) & 0x07)); 147 // lower 5 from 5th; 148 t[7] = (byte) (s[4] & 0x1F); 149 150 // write out the actual characters. 151 for (int j = 0; j < t.length - padlen; j++) { 152 char c = ALPHABET.charAt(t[j]); 153 os.write(c); 154 } 155 } 156 return new String(os.toByteArray()); 157 } 158 159 private static int lenToPadding(int blocklen) { 160 switch (blocklen) { 161 case 1: 162 return 6; 163 case 2: 164 return 4; 165 case 3: 166 return 3; 167 case 4: 168 return 1; 169 case 5: 170 return 0; 171 default: 172 return -1; 173 } 174 } 175 176 private static int paddingToLen(int padlen) { 177 switch (padlen) { 178 case 6: 179 return 1; 180 case 4: 181 return 2; 182 case 3: 183 return 3; 184 case 1: 185 return 4; 186 case 0: 187 return 5; 188 default: 189 return -1; 190 } 191 } 192 193}