FileDocCategorySizeDatePackage
Base32.javaAPI DocAzureus 3.0.3.44467Thu Feb 09 19:42:44 GMT 2006org.gudy.azureus2.core3.util

Base32

public class Base32 extends Object
author
parg Derived from com.bitzi.util.Base32.java (PD) 2001 The Bitzi Corporation Please see http://bitzi.com/publicdomain for more info.

Fields Summary
private static final String
base32Chars
private static final int[]
base32Lookup
Constructors Summary
Methods Summary
public static byte[]decode(java.lang.String base32)

	    int    i, index, lookup, offset, digit;
	    byte[] bytes = new byte[base32.length()*5/8];
	
	    for(i = 0, index = 0, offset = 0; i < base32.length(); i++)
	    {
	        lookup = base32.charAt(i) - '0";
	        
	        /* Skip chars outside the lookup table */
	        if ( lookup < 0 || lookup >= base32Lookup.length)
	            continue;
	        
	        digit = base32Lookup[lookup];
	
	        /* If this digit is not in the table, ignore it */
	        if (digit == 0xFF)
	            continue;
	
	        if (index <= 3)
	        {
	            index = (index + 5) % 8;
	            if (index == 0)
	            {
	               bytes[offset] |= digit;
	               offset++;
	               if(offset>=bytes.length) break;
	            }
	            else
	               bytes[offset] |= digit << (8 - index);
	        }
	        else
	        {
	            index = (index + 5) % 8;
	            bytes[offset] |= (digit >>> index);
	            offset++;
	            
	            if(offset>=bytes.length) break;
	            bytes[offset] |= digit << (8 - index);
	        }
	    }
	    return bytes;
	
public static java.lang.Stringencode(byte[] bytes)

	
	   
	
		   
	
	    int i =0, index = 0, digit = 0;
	    int currByte, nextByte;
	    StringBuffer base32 = new StringBuffer((bytes.length+7)*8/5); 
	
	    while(i < bytes.length)
	    {
	        currByte = (bytes[i]>=0) ? bytes[i] : (bytes[i]+256); // unsign
	         
	        /* Is the current digit going to span a byte boundary? */
	        if (index > 3)
	        {                
	            if ((i+1)<bytes.length) 
	                nextByte = (bytes[i+1]>=0) ? bytes[i+1] : (bytes[i+1]+256);
	            else
	                nextByte = 0;
	            
	            digit = currByte & (0xFF >> index);
	            index = (index + 5) % 8;
	            digit <<= index;
	            digit |= nextByte >> (8 - index);
	            i++;
	        }
	        else
	        {
	            digit = (currByte >> (8 - (index + 5))) & 0x1F;
	            index = (index + 5) % 8;
	            if (index == 0)
	                i++;
	        }
	        base32.append(base32Chars.charAt(digit));
	    }
	
	    return base32.toString();