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 019import java.io.ByteArrayOutputStream; 020import java.io.DataOutputStream; 021import java.io.IOException; 022import java.io.UnsupportedEncodingException; 023 024import org.jivesoftware.smack.util.StringUtils; 025 026/** 027 * Base32 string encoding is useful for when filenames case-insensitive filesystems are encoded. 028 * Base32 representation takes roughly 20% more space then Base64. 029 * 030 * @author Florian Schmaus 031 * Based on code by Brian Wellington (bwelling@xbill.org) 032 * @see <a href="http://en.wikipedia.org/wiki/Base32">Base32 Wikipedia entry</a> 033 * 034 */ 035public class Base32 { 036 037 private static final StringEncoder base32Stringencoder = new StringEncoder() { 038 039 @Override 040 public String encode(String string) { 041 return Base32.encode(string); 042 } 043 044 @Override 045 public String decode(String string) { 046 return Base32.decode(string); 047 } 048 049 }; 050 private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678"; 051 052 public static StringEncoder getStringEncoder() { 053 return base32Stringencoder; 054 } 055 056 public static String decode(String str) { 057 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 058 byte[] raw; 059 try { 060 raw = str.getBytes(StringUtils.UTF8); 061 } 062 catch (UnsupportedEncodingException e) { 063 throw new AssertionError(e); 064 } 065 for (int i = 0; i < raw.length; i++) { 066 char c = (char) raw[i]; 067 if (!Character.isWhitespace(c)) { 068 c = Character.toUpperCase(c); 069 bs.write((byte) c); 070 } 071 } 072 073 while (bs.size() % 8 != 0) 074 bs.write('8'); 075 076 byte[] in = bs.toByteArray(); 077 078 bs.reset(); 079 DataOutputStream ds = new DataOutputStream(bs); 080 081 for (int i = 0; i < in.length / 8; i++) { 082 short[] s = new short[8]; 083 int[] t = new int[5]; 084 085 int padlen = 8; 086 for (int j = 0; j < 8; j++) { 087 char c = (char) in[i * 8 + j]; 088 if (c == '8') 089 break; 090 s[j] = (short) ALPHABET.indexOf(in[i * 8 + j]); 091 if (s[j] < 0) 092 return null; 093 padlen--; 094 } 095 int blocklen = paddingToLen(padlen); 096 if (blocklen < 0) 097 return null; 098 099 // all 5 bits of 1st, high 3 (of 5) of 2nd 100 t[0] = (s[0] << 3) | s[1] >> 2; 101 // lower 2 of 2nd, all 5 of 3rd, high 1 of 4th 102 t[1] = ((s[1] & 0x03) << 6) | (s[2] << 1) | (s[3] >> 4); 103 // lower 4 of 4th, high 4 of 5th 104 t[2] = ((s[3] & 0x0F) << 4) | ((s[4] >> 1) & 0x0F); 105 // lower 1 of 5th, all 5 of 6th, high 2 of 7th 106 t[3] = (s[4] << 7) | (s[5] << 2) | (s[6] >> 3); 107 // lower 3 of 7th, all of 8th 108 t[4] = ((s[6] & 0x07) << 5) | s[7]; 109 110 try { 111 for (int j = 0; j < blocklen; j++) 112 ds.writeByte((byte) (t[j] & 0xFF)); 113 } catch (IOException e) { 114 } 115 } 116 117 String res; 118 try { 119 res = new String(bs.toByteArray(), StringUtils.UTF8); 120 } 121 catch (UnsupportedEncodingException e) { 122 throw new AssertionError(e); 123 } 124 return res; 125 } 126 127 public static String encode(String str) { 128 byte[] b; 129 try { 130 b = str.getBytes(StringUtils.UTF8); 131 } 132 catch (UnsupportedEncodingException e) { 133 throw new AssertionError(e); 134 } 135 ByteArrayOutputStream os = new ByteArrayOutputStream(); 136 137 for (int i = 0; i < (b.length + 4) / 5; i++) { 138 short[] s = new short[5]; 139 int[] t = new int[8]; 140 141 int blocklen = 5; 142 for (int j = 0; j < 5; j++) { 143 if ((i * 5 + j) < b.length) 144 s[j] = (short) (b[i * 5 + j] & 0xFF); 145 else { 146 s[j] = 0; 147 blocklen--; 148 } 149 } 150 int padlen = lenToPadding(blocklen); 151 152 // convert the 5 byte block into 8 characters (values 0-31). 153 154 // upper 5 bits from first byte 155 t[0] = (byte) ((s[0] >> 3) & 0x1F); 156 // lower 3 bits from 1st byte, upper 2 bits from 2nd. 157 t[1] = (byte) (((s[0] & 0x07) << 2) | ((s[1] >> 6) & 0x03)); 158 // bits 5-1 from 2nd. 159 t[2] = (byte) ((s[1] >> 1) & 0x1F); 160 // lower 1 bit from 2nd, upper 4 from 3rd 161 t[3] = (byte) (((s[1] & 0x01) << 4) | ((s[2] >> 4) & 0x0F)); 162 // lower 4 from 3rd, upper 1 from 4th. 163 t[4] = (byte) (((s[2] & 0x0F) << 1) | ((s[3] >> 7) & 0x01)); 164 // bits 6-2 from 4th 165 t[5] = (byte) ((s[3] >> 2) & 0x1F); 166 // lower 2 from 4th, upper 3 from 5th; 167 t[6] = (byte) (((s[3] & 0x03) << 3) | ((s[4] >> 5) & 0x07)); 168 // lower 5 from 5th; 169 t[7] = (byte) (s[4] & 0x1F); 170 171 // write out the actual characters. 172 for (int j = 0; j < t.length - padlen; j++) { 173 char c = ALPHABET.charAt(t[j]); 174 os.write(c); 175 } 176 } 177 String res; 178 try { 179 res = new String(os.toByteArray(), StringUtils.UTF8); 180 } 181 catch (UnsupportedEncodingException e) { 182 throw new AssertionError(e); 183 } 184 return res; 185 } 186 187 private static int lenToPadding(int blocklen) { 188 switch (blocklen) { 189 case 1: 190 return 6; 191 case 2: 192 return 4; 193 case 3: 194 return 3; 195 case 4: 196 return 1; 197 case 5: 198 return 0; 199 default: 200 return -1; 201 } 202 } 203 204 private static int paddingToLen(int padlen) { 205 switch (padlen) { 206 case 6: 207 return 1; 208 case 4: 209 return 2; 210 case 3: 211 return 3; 212 case 1: 213 return 4; 214 case 0: 215 return 5; 216 default: 217 return -1; 218 } 219 } 220 221}