/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class Mangle {
 public static final int BASE_CHAR = 65;

 public static final int CHAR_SIZE = 2;

 public static int read_byte(InputStream is) throws Throwable {
  int val,val2;
 
  val=is.read();

  if (val<0) return -1;

  val2=is.read();

  if (val2<0) return -1;

  val-=BASE_CHAR;
  val2-=BASE_CHAR;

  val&=0x0f;
  val2&=0x0f;

  return ((val<<4)|val2);
 }

 public static void write_byte(OutputStream os,int val) throws Throwable {
  int val1,val2;
 
  val1=(val>>4)&0x0f;
  val2=(val)&0x0f;

  val1+=BASE_CHAR;
  val2+=BASE_CHAR;

  os.write(val1);
  os.write(val2);
 }

 public static int read_byte(ByteBuffer bb) throws Throwable {
  int val,val2;
 
  val=((int)bb.get())&0xff;

  if (val<0) return -1;

  val2=((int)bb.get())&0xff;

  if (val2<0) return -1;

  val-=BASE_CHAR;
  val2-=BASE_CHAR;

  val&=0x0f;
  val2&=0x0f;

  return ((val<<4)|val2);
 }

 public static void write_byte(ByteBuffer bb,int val) throws Throwable {
  int val1,val2;
 
  val1=(val>>4)&0x0f;
  val2=(val)&0x0f;

  val1+=BASE_CHAR;
  val2+=BASE_CHAR;

  bb.put((byte)val1);
  bb.put((byte)val2);
 }

}
