/*************************************************************************************

This file is part of FragMend.

Written by Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.

Copyright (c) 2006, Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.     
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.       
    * Neither the name of Florian Buchholz, Glenn Henderson, David Horvath, Jeff
      Jones, nor the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*************************************************************************************/

import java.lang.String;

import java.nio.*;
import java.nio.charset.*;

public class DataUtilities {

	public static String getHexView(byte[] data) {
	
		String ret = new String();
		for (int i = 0; i < data.length; i += 16) {
			int max;
			if (data.length - i < 16)
				max = data.length - i;
			else
				max = 16;
			String hexline = "";
			String asciiline = "";
			for (int j = 0; j < max; j++) {
				byte ascii_char = data[i+j];
				hexline += String.format("%02x ", ascii_char);
				if (isPrintable(ascii_char)) {
					if ((ascii_char == 9) || (ascii_char == 10))
						asciiline += ".";
					else
						asciiline += String.format("%c", ascii_char);
				}
				else {
					asciiline += ".";
				}
			}
			ret = ret + String.format("%06x ", i) + hexline + " " + asciiline + "\n";
			
		}
		
		return ret;
	}

	public static String getAsciiView(byte[] data, int separator) {
	
//		System.out.println("Constructing string");
		/*
		String ret = "";
		for (int i = 0; i < data.length; i++) {
			byte ascii_char = data[i];
			if (isPrintable(ascii_char)) {
				ret += String.format("%c", ascii_char);
			}
			else {
				ret += ".";
			}
			if (separator > 0 && i > 0 && (i % separator == 0))
				ret += "\n++++++++++++++++++++++++++++++++++++++++++\n";
		}
		
		*/
		/*
		Charset charset = Charset.forName("US-ASCII");
		CharsetDecoder decoder = charset.newDecoder();
		decoder = decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
		decoder = decoder.replaceWith(".");
		ByteBuffer buf = ByteBuffer.allocate(data.length);
		buf.put(data);
		CharBuffer chars = CharBuffer.allocate(data.length);		

		decoder.reset();
		decoder.decode(buf, chars, true);
		decoder.flush(chars);
		
		String ret = chars.toString();
		*/

		String ret = new String(data);
		
//		System.out.println("... done");

		return ret;
	}

	
	public static boolean isPrintable(byte ascii_char) {
		return (((31 < ascii_char) && (ascii_char < 127)) || 
			(ascii_char == 9) || (ascii_char == 10) || (ascii_char == 13));	
	}
	
	public static int printableAsciiCount(byte[] data) {
		int ret = 0;
		for (int i = 0; i < data.length; i++) {
			if (isPrintable(data[i]))
				ret++;
		}
		return ret;
	}
	
	public static boolean isZeroPadded(byte[] data) {
	
		return ((data[data.length-1] == 0) &&
			(data[data.length-2] == 0) &&
			(data[data.length-3] == 0) &&
			(data[data.length-4] == 0));
		
	}

	public static int getShortLittle(byte[] data, int pos) {
		return (((int)data[pos+1]&0xff) << 8) + ((int)data[pos]&0xff);
	}
	
	public static long getWordLittle(byte[] data, int pos) {	
		return (((long)data[pos+3]&0xff) << 24) + (((long)data[pos+2]&0xff) << 16) + 
				(((long)data[pos+1]&0xff) << 8) + ((long)data[pos]&0xff);
	}
		
	public static int getShortBig(byte[] data, int pos) {
		return (((int)data[pos]&0xff) << 8) + ((int)data[pos+1]&0xff);
	}
	
	public static long getWordBig(byte[] data, int pos) {	
		return (((long)data[pos]&0xff) << 24) + (((long)data[pos+1]&0xff) << 16) + 
				(((long)data[pos+2]&0xff) << 8) + ((long)data[pos+3]&0xff);
	}
		
	
}

