0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.jgss;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.EOFException;
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * Utilities for processing GSS Tokens.
0N/A *
0N/A */
0N/A
0N/Apublic abstract class GSSToken {
0N/A
0N/A /**
0N/A * Copies an integer value to a byte array in little endian form.
0N/A * @param value the integer value to write
0N/A * @param array the byte array into which the integer must be copied. It
0N/A * is assumed that the array will be large enough to hold the 4 bytes of
0N/A * the integer.
0N/A */
0N/A public static final void writeLittleEndian(int value, byte[] array) {
0N/A writeLittleEndian(value, array, 0);
0N/A }
0N/A
0N/A /**
0N/A * Copies an integer value to a byte array in little endian form.
0N/A * @param value the integer value to write
0N/A * @param array the byte array into which the integer must be copied. It
0N/A * is assumed that the array will be large enough to hold the 4 bytes of
0N/A * the integer.
0N/A * @param pos the position at which to start writing
0N/A */
0N/A public static final void writeLittleEndian(int value, byte[] array,
0N/A int pos) {
0N/A array[pos++] = (byte)(value);
0N/A array[pos++] = (byte)((value>>>8));
0N/A array[pos++] = (byte)((value>>>16));
0N/A array[pos++] = (byte)((value>>>24));
0N/A }
0N/A
0N/A public static final void writeBigEndian(int value, byte[] array) {
0N/A writeBigEndian(value, array, 0);
0N/A }
0N/A
0N/A public static final void writeBigEndian(int value, byte[] array,
0N/A int pos) {
0N/A array[pos++] = (byte)((value>>>24));
0N/A array[pos++] = (byte)((value>>>16));
0N/A array[pos++] = (byte)((value>>>8));
0N/A array[pos++] = (byte)(value);
0N/A }
0N/A
0N/A /**
0N/A * Reads an integer value from a byte array in little endian form. This
0N/A * method allows the reading of two byte values as well as four bytes
0N/A * values both of which are needed in the Kerberos v5 GSS-API mechanism.
0N/A *
0N/A * @param data the array containing the bytes of the integer value
0N/A * @param pos the offset in the array
0N/A * @size the number of bytes to read from the array.
0N/A * @return the integer value
0N/A */
0N/A public static final int readLittleEndian(byte[] data, int pos, int size) {
0N/A int retVal = 0;
0N/A int shifter = 0;
0N/A while (size > 0) {
0N/A retVal += (data[pos] & 0xff) << shifter;
0N/A shifter += 8;
0N/A pos++;
0N/A size--;
0N/A }
0N/A return retVal;
0N/A }
0N/A
0N/A public static final int readBigEndian(byte[] data, int pos, int size) {
0N/A int retVal = 0;
0N/A int shifter = (size-1)*8;
0N/A while (size > 0) {
0N/A retVal += (data[pos] & 0xff) << shifter;
0N/A shifter -= 8;
0N/A pos++;
0N/A size--;
0N/A }
0N/A return retVal;
0N/A }
0N/A
0N/A /**
0N/A * Writes a two byte integer value to a OutputStream.
0N/A *
0N/A * @param val the integer value. It will lose the high-order two bytes.
0N/A * @param os the OutputStream to write to
0N/A * @throws IOException if an error occurs while writing to the OutputStream
0N/A */
0N/A public static final void writeInt(int val, OutputStream os)
0N/A throws IOException {
0N/A os.write(val>>>8);
0N/A os.write(val);
0N/A }
0N/A
0N/A /**
0N/A * Writes a two byte integer value to a byte array.
0N/A *
0N/A * @param val the integer value. It will lose the high-order two bytes.
0N/A * @param dest the byte array to write to
0N/A * @param pos the offset to start writing to
0N/A */
0N/A public static final int writeInt(int val, byte[] dest, int pos) {
0N/A dest[pos++] = (byte)(val>>>8);
0N/A dest[pos++] = (byte)val;
0N/A return pos;
0N/A }
0N/A
0N/A /**
0N/A * Reads a two byte integer value from an InputStream.
0N/A *
0N/A * @param is the InputStream to read from
0N/A * @returns the integer value
0N/A * @throws IOException if some errors occurs while reading the integer
0N/A * bytes.
0N/A */
0N/A public static final int readInt(InputStream is) throws IOException {
0N/A return (((0xFF & is.read()) << 8)
0N/A | (0xFF & is.read()));
0N/A }
0N/A
0N/A /**
0N/A * Reads a two byte integer value from a byte array.
0N/A *
0N/A * @param src the byte arra to read from
0N/A * @param pos the offset to start reading from
0N/A * @returns the integer value
0N/A */
0N/A public static final int readInt(byte[] src, int pos) {
0N/A return ((0xFF & src[pos])<<8 | (0xFF & src[pos+1]));
0N/A }
0N/A
0N/A /**
0N/A * Blocks till the required number of bytes have been read from the
0N/A * input stream.
0N/A *
0N/A * @param is the InputStream to read from
0N/A * @param buffer the buffer to store the bytes into
0N/A * @param throws EOFException if EOF is reached before all bytes are
0N/A * read.
0N/A * @throws IOException is an error occurs while reading
0N/A */
0N/A public static final void readFully(InputStream is, byte[] buffer)
0N/A throws IOException {
0N/A readFully(is, buffer, 0, buffer.length);
0N/A }
0N/A
0N/A /**
0N/A * Blocks till the required number of bytes have been read from the
0N/A * input stream.
0N/A *
0N/A * @param is the InputStream to read from
0N/A * @param buffer the buffer to store the bytes into
0N/A * @param offset the offset to start storing at
0N/A * @param len the number of bytes to read
0N/A * @param throws EOFException if EOF is reached before all bytes are
0N/A * read.
0N/A * @throws IOException is an error occurs while reading
0N/A */
0N/A public static final void readFully(InputStream is,
0N/A byte[] buffer, int offset, int len)
0N/A throws IOException {
0N/A int temp;
0N/A while (len > 0) {
0N/A temp = is.read(buffer, offset, len);
0N/A if (temp == -1)
0N/A throw new EOFException("Cannot read all "
0N/A + len
0N/A + " bytes needed to form this token!");
0N/A offset += temp;
0N/A len -= temp;
0N/A }
0N/A }
0N/A
0N/A public static final void debug(String str) {
0N/A System.err.print(str);
0N/A }
0N/A
0N/A public static final String getHexBytes(byte[] bytes) {
0N/A return getHexBytes(bytes, 0, bytes.length);
0N/A }
0N/A
0N/A public static final String getHexBytes(byte[] bytes, int len) {
0N/A return getHexBytes(bytes, 0, len);
0N/A }
0N/A
0N/A public static final String getHexBytes(byte[] bytes, int pos, int len) {
0N/A StringBuffer sb = new StringBuffer();
0N/A for (int i = pos; i < (pos+len); i++) {
0N/A int b1 = (bytes[i]>>4) & 0x0f;
0N/A int b2 = bytes[i] & 0x0f;
0N/A
0N/A sb.append(Integer.toHexString(b1));
0N/A sb.append(Integer.toHexString(b2));
0N/A sb.append(' ');
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A}