2016-02-24 21:11:14 +00:00
|
|
|
package com.bytezone.diskbrowser.utilities;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.MathContext;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public class HexFormatter
|
2019-11-03 05:49:01 +00:00
|
|
|
// -----------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-02-24 02:13:52 +00:00
|
|
|
private static String[] hex =
|
|
|
|
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
|
2015-06-01 09:35:51 +00:00
|
|
|
private static MathContext mathContext = new MathContext (9);
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format (byte[] buffer)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return format (buffer, 0, buffer.length);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-24 08:59:40 +00:00
|
|
|
public static String formatNoHeader (byte[] buffer)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-24 08:59:40 +00:00
|
|
|
{
|
|
|
|
return formatNoHeader (buffer, 0, buffer.length);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format (byte[] buffer, int offset, int length)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return format (buffer, offset, length, true, 0);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format (byte[] buffer, int offset, int length, int startingAddress)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return format (buffer, offset, length, true, startingAddress);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String formatNoHeader (byte[] buffer, int offset, int length)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return format (buffer, offset, length, false, 0);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String formatNoHeader (byte[] buffer, int offset, int length,
|
2016-02-24 02:13:52 +00:00
|
|
|
int startingAddress)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return format (buffer, offset, length, false, startingAddress);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format (byte[] buffer, int offset, int length, boolean header,
|
2016-02-24 02:13:52 +00:00
|
|
|
int startingAddress)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2017-04-02 05:02:29 +00:00
|
|
|
StringBuilder line = new StringBuilder ();
|
2015-06-01 09:35:51 +00:00
|
|
|
int[] freq = new int[256];
|
2016-08-22 03:41:43 +00:00
|
|
|
boolean startedOnBoundary = offset % 0x100 == 0;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
line.append (" ");
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
line.append (" " + hex[i]);
|
|
|
|
if (offset == 0)
|
|
|
|
line.append ("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = offset; i < offset + length; i += 16)
|
|
|
|
{
|
|
|
|
if (line.length () > 0 && i > 0)
|
|
|
|
line.append ("\n");
|
2016-08-22 03:41:43 +00:00
|
|
|
if (i > offset && startedOnBoundary && (i % 0x200) == 0)
|
2016-08-05 08:40:32 +00:00
|
|
|
line.append ("\n");
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
// print offset
|
2017-01-17 00:00:51 +00:00
|
|
|
line.append (String.format ("%05X : ", (startingAddress + i - offset)));
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
// print hex values
|
|
|
|
StringBuffer trans = new StringBuffer ();
|
|
|
|
StringBuffer hexLine = new StringBuffer ();
|
|
|
|
|
2017-01-25 04:40:15 +00:00
|
|
|
int max = Math.min (i + 16, offset + length);
|
|
|
|
max = Math.min (max, buffer.length);
|
|
|
|
for (int j = i; j < max; j++)
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
int c = buffer[j] & 0xFF;
|
|
|
|
freq[c]++;
|
|
|
|
hexLine.append (String.format ("%02X ", c));
|
|
|
|
|
|
|
|
if (c > 127)
|
|
|
|
{
|
|
|
|
if (c < 160)
|
|
|
|
c -= 64;
|
|
|
|
else
|
|
|
|
c -= 128;
|
|
|
|
}
|
2016-12-17 23:58:23 +00:00
|
|
|
if (c < 32 || c == 127) // non-printable
|
2015-06-01 09:35:51 +00:00
|
|
|
trans.append (".");
|
2016-12-17 23:58:23 +00:00
|
|
|
else // standard ascii
|
2015-06-01 09:35:51 +00:00
|
|
|
trans.append ((char) c);
|
|
|
|
}
|
|
|
|
while (hexLine.length () < 48)
|
|
|
|
hexLine.append (" ");
|
|
|
|
|
|
|
|
line.append (hexLine.toString () + ": " + trans.toString ());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false)
|
|
|
|
{
|
|
|
|
line.append ("\n\n");
|
|
|
|
int totalBits = 0;
|
|
|
|
for (int i = 0; i < freq.length; i++)
|
|
|
|
if (freq[i] > 0)
|
|
|
|
{
|
|
|
|
totalBits += (Integer.bitCount (i) * freq[i]);
|
2016-08-05 08:40:32 +00:00
|
|
|
line.append (
|
|
|
|
String.format ("%02X %3d %d%n", i, freq[i], Integer.bitCount (i)));
|
2015-06-01 09:35:51 +00:00
|
|
|
}
|
|
|
|
line.append (String.format ("%nTotal bits : %d%n", totalBits));
|
|
|
|
}
|
|
|
|
return line.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String sanitiseString (byte[] buffer, int offset, int length)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
StringBuilder trans = new StringBuilder ();
|
|
|
|
for (int j = offset; j < offset + length; j++)
|
|
|
|
{
|
|
|
|
int c = buffer[j] & 0xFF;
|
|
|
|
|
|
|
|
if (c > 127)
|
|
|
|
{
|
|
|
|
if (c < 160)
|
|
|
|
c -= 64;
|
|
|
|
else
|
|
|
|
c -= 128;
|
|
|
|
}
|
|
|
|
|
2016-12-17 23:58:23 +00:00
|
|
|
if (c < 32 || c == 127) // non-printable
|
2015-06-01 09:35:51 +00:00
|
|
|
trans.append (".");
|
|
|
|
else
|
|
|
|
trans.append ((char) c); // standard ascii
|
|
|
|
}
|
|
|
|
return trans.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getString (byte[] buffer)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return getString (buffer, 0, buffer.length);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getString (byte[] buffer, int offset, int length)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2017-04-02 05:02:29 +00:00
|
|
|
StringBuilder text = new StringBuilder ();
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
for (int i = offset; i < offset + length; i++)
|
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
int c = buffer[i] & 0xFF;
|
2015-06-01 09:35:51 +00:00
|
|
|
if (c > 127)
|
|
|
|
{
|
|
|
|
if (c < 160)
|
|
|
|
c -= 64;
|
|
|
|
else
|
|
|
|
c -= 128;
|
|
|
|
}
|
|
|
|
if (c == 13)
|
|
|
|
text.append ("\n");
|
2016-12-17 23:58:23 +00:00
|
|
|
else if (c < 32) // non-printable
|
2015-06-01 09:35:51 +00:00
|
|
|
text.append (".");
|
2016-12-17 23:58:23 +00:00
|
|
|
else // standard ascii
|
2015-06-01 09:35:51 +00:00
|
|
|
text.append ((char) c);
|
|
|
|
}
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getString2 (byte[] buffer, int offset, int length)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2017-04-02 05:02:29 +00:00
|
|
|
StringBuilder text = new StringBuilder ();
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
for (int i = offset; i < offset + length; i++)
|
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
int c = buffer[i] & 0xFF;
|
2017-04-02 05:02:29 +00:00
|
|
|
if (c == 136)
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2017-04-02 05:02:29 +00:00
|
|
|
if (text.length () > 0)
|
|
|
|
text.deleteCharAt (text.length () - 1);
|
2015-06-01 09:35:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c > 127)
|
|
|
|
{
|
|
|
|
if (c < 160)
|
|
|
|
c -= 64;
|
|
|
|
else
|
|
|
|
c -= 128;
|
|
|
|
}
|
2016-12-17 23:58:23 +00:00
|
|
|
if (c < 32) // non-printable
|
2015-06-01 09:35:51 +00:00
|
|
|
text.append ((char) (c + 64));
|
2016-12-17 23:58:23 +00:00
|
|
|
else // standard ascii
|
2015-06-01 09:35:51 +00:00
|
|
|
text.append ((char) c);
|
|
|
|
}
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getHexString (byte[] buffer, int offset, int length)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return getHexString (buffer, offset, length, true);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getHexString (byte[] buffer)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return getHexString (buffer, 0, buffer.length);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getHexString (byte[] buffer, int offset, int length, boolean space)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
StringBuilder hex = new StringBuilder ();
|
2017-01-24 21:53:34 +00:00
|
|
|
int max = Math.min (offset + length, buffer.length);
|
|
|
|
for (int i = offset; i < max; i++)
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2017-01-24 21:53:34 +00:00
|
|
|
hex.append (String.format ("%02X", buffer[i]));
|
2015-06-01 09:35:51 +00:00
|
|
|
if (space)
|
|
|
|
hex.append (' ');
|
|
|
|
}
|
|
|
|
if (length > 0 && space)
|
|
|
|
hex.deleteCharAt (hex.length () - 1);
|
|
|
|
return hex.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getHexStringReversed (byte[] buffer, int offset, int length,
|
2016-02-24 02:13:52 +00:00
|
|
|
boolean space)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
StringBuilder hex = new StringBuilder ();
|
|
|
|
for (int i = length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
hex.append (String.format ("%02X", buffer[offset + i]));
|
|
|
|
if (space)
|
|
|
|
hex.append (' ');
|
|
|
|
}
|
|
|
|
if (length > 0 && space)
|
|
|
|
hex.deleteCharAt (hex.length () - 1);
|
|
|
|
return hex.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-17 00:00:51 +00:00
|
|
|
public static String getBitString (byte b)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-17 00:00:51 +00:00
|
|
|
{
|
|
|
|
String s = "0000000" + Integer.toBinaryString (b & 0xFF);
|
|
|
|
s = s.replaceAll ("0", ".");
|
|
|
|
return s.substring (s.length () - 8);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static char byteValue (byte b)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
int c = b & 0xFF;
|
2015-06-01 09:35:51 +00:00
|
|
|
if (c > 127)
|
|
|
|
c -= 128;
|
|
|
|
if (c > 95)
|
|
|
|
c -= 64;
|
2016-12-17 23:58:23 +00:00
|
|
|
if (c < 32) // non-printable
|
2015-06-01 09:35:51 +00:00
|
|
|
return '.';
|
2016-12-17 23:58:23 +00:00
|
|
|
return (char) c; // standard ascii
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format4 (int value)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
if (value < 0)
|
|
|
|
return "***err**";
|
|
|
|
StringBuffer text = new StringBuffer ();
|
|
|
|
for (int i = 0, weight = 4096; i < 4; i++)
|
|
|
|
{
|
|
|
|
int digit = value / weight;
|
|
|
|
if (digit < 0 || digit > 15)
|
|
|
|
return "***err**";
|
|
|
|
text.append (hex[digit]);
|
|
|
|
value %= weight;
|
|
|
|
weight /= 16;
|
|
|
|
}
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format3 (int value)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
return format4 (value).substring (1);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format2 (int value)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
if (value < 0)
|
|
|
|
value += 256;
|
|
|
|
String text = hex[value / 16] + hex[value % 16];
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String format1 (int value)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
String text = hex[value];
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static int intValue (byte b1, byte b2)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
return (b1 & 0xFF) + (b2 & 0xFF) * 256;
|
2015-06-01 09:35:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static int intValue (byte b1, byte b2, byte b3)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
return (b1 & 0xFF) + (b2 & 0xFF) * 256 + (b3 & 0xFF) * 65536;
|
2015-06-01 09:35:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-22 00:01:15 +00:00
|
|
|
public static int unsignedLong (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
for (int i = 3; i >= 0; i--)
|
|
|
|
{
|
|
|
|
val <<= 8;
|
|
|
|
val += buffer[ptr + i] & 0xFF;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-22 00:01:15 +00:00
|
|
|
public static int signedLong (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-22 00:01:15 +00:00
|
|
|
{
|
|
|
|
return (((buffer[ptr] & 0xFF) << 24) | ((buffer[ptr] & 0xFF) << 16)
|
|
|
|
| ((buffer[ptr] & 0xFF) << 8) | (buffer[ptr + 1] & 0xFF));
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2016-11-28 00:45:17 +00:00
|
|
|
public static int getLongBigEndian (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2016-11-28 00:45:17 +00:00
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
val <<= 8;
|
|
|
|
val += buffer[ptr + i] & 0xFF;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-20 04:07:08 +00:00
|
|
|
public static int unsignedShort (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2016-12-17 22:07:55 +00:00
|
|
|
{
|
2018-07-25 05:48:14 +00:00
|
|
|
if (ptr >= buffer.length)
|
|
|
|
{
|
|
|
|
System.out.println ("index out of range: " + ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-22 06:31:25 +00:00
|
|
|
return (buffer[ptr] & 0xFF) | ((buffer[ptr + 1] & 0xFF) << 8);
|
2016-12-17 22:07:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-20 04:07:08 +00:00
|
|
|
public static int signedShort (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2017-01-20 04:07:08 +00:00
|
|
|
{
|
2018-07-25 05:48:14 +00:00
|
|
|
if (ptr >= buffer.length)
|
|
|
|
{
|
|
|
|
System.out.println ("index out of range: " + ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-24 08:59:40 +00:00
|
|
|
return (short) ((buffer[ptr] & 0xFF) | ((buffer[ptr + 1] & 0xFF) << 8));
|
2017-01-20 04:07:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2016-11-28 00:45:17 +00:00
|
|
|
public static int getShortBigEndian (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2016-11-28 00:45:17 +00:00
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
val <<= 8;
|
2016-12-17 23:58:23 +00:00
|
|
|
val |= buffer[ptr + i] & 0xFF;
|
2016-11-28 00:45:17 +00:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static double getSANEDouble (byte[] buffer, int offset)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
long bits = 0;
|
|
|
|
for (int i = 7; i >= 0; i--)
|
|
|
|
{
|
|
|
|
bits <<= 8;
|
|
|
|
bits |= buffer[offset + i] & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Double.longBitsToDouble (bits);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static double floatValueOld (byte[] buffer, int offset)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
double val = 0;
|
|
|
|
|
2016-12-17 22:07:55 +00:00
|
|
|
int exponent = (buffer[offset] & 0xFF) - 0x80;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
int mantissa =
|
2016-12-17 22:07:55 +00:00
|
|
|
(buffer[offset + 1] & 0x7F) * 0x1000000 + (buffer[offset + 2] & 0xFF) * 0x10000
|
|
|
|
+ (buffer[offset + 3] & 0xFF) * 0x100 + (buffer[offset + 4] & 0xFF);
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
int weight1 = 1;
|
|
|
|
long weight2 = 2147483648L;
|
|
|
|
double multiplier = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
if ((mantissa & weight2) > 0)
|
|
|
|
multiplier += (1.0 / weight1);
|
|
|
|
weight2 /= 2;
|
|
|
|
weight1 *= 2;
|
|
|
|
}
|
|
|
|
val = Math.pow (2, exponent - 1) * (multiplier + 1);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static double floatValue (byte[] buffer, int ptr)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-12-17 23:58:23 +00:00
|
|
|
int exponent = buffer[ptr] & 0x7F; // biased 128
|
2015-06-01 09:35:51 +00:00
|
|
|
if (exponent == 0)
|
|
|
|
return 0.0;
|
|
|
|
|
2016-02-24 02:13:52 +00:00
|
|
|
int mantissa = (buffer[ptr + 1] & 0x7F) << 24 | (buffer[ptr + 2] & 0xFF) << 16
|
|
|
|
| (buffer[ptr + 3] & 0xFF) << 8 | (buffer[ptr + 4] & 0xFF);
|
2015-06-01 09:35:51 +00:00
|
|
|
boolean negative = (buffer[ptr + 1] & 0x80) > 0;
|
|
|
|
double value = 0.5;
|
|
|
|
for (int i = 2, weight = 0x40000000; i <= 32; i++, weight >>>= 1)
|
|
|
|
if ((mantissa & weight) > 0)
|
|
|
|
value += Math.pow (0.5, i);
|
|
|
|
value *= Math.pow (2, exponent);
|
|
|
|
BigDecimal bd = new BigDecimal (value);
|
|
|
|
double rounded = bd.round (mathContext).doubleValue ();
|
|
|
|
return negative ? rounded * -1 : rounded;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static GregorianCalendar getAppleDate (byte[] buffer, int offset)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
int date = HexFormatter.intValue (buffer[offset], buffer[offset + 1]);
|
|
|
|
if (date > 0)
|
|
|
|
{
|
|
|
|
int year = (date & 0xFE00) >> 9;
|
|
|
|
int month = (date & 0x01E0) >> 5;
|
|
|
|
int day = date & 0x001F;
|
2016-12-17 22:07:55 +00:00
|
|
|
int hour = buffer[offset + 3] & 0x1F;
|
|
|
|
int minute = buffer[offset + 2] & 0x3F;
|
2015-06-01 09:35:51 +00:00
|
|
|
if (year < 70)
|
|
|
|
year += 2000;
|
|
|
|
else
|
|
|
|
year += 1900;
|
|
|
|
return new GregorianCalendar (year, month - 1, day, hour, minute);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static GregorianCalendar getPascalDate (byte[] buffer, int offset)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
int year = (buffer[offset + 1] & 0xFF);
|
2015-06-01 09:35:51 +00:00
|
|
|
int day = (buffer[offset] & 0xF0) >> 4;
|
|
|
|
int month = buffer[offset] & 0x0F;
|
|
|
|
if (day == 0 || month == 0)
|
|
|
|
return null;
|
|
|
|
if (year % 2 > 0)
|
|
|
|
day += 16;
|
|
|
|
year /= 2;
|
|
|
|
if (year < 70)
|
|
|
|
year += 2000;
|
|
|
|
else
|
|
|
|
year += 1900;
|
|
|
|
return new GregorianCalendar (year, month - 1, day);
|
|
|
|
}
|
|
|
|
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
public static String getPascalString (byte[] buffer, int offset)
|
2019-11-03 05:49:01 +00:00
|
|
|
// ---------------------------------------------------------------------------------//
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
2016-12-17 22:07:55 +00:00
|
|
|
int length = buffer[offset] & 0xFF;
|
2015-06-01 09:35:51 +00:00
|
|
|
return HexFormatter.getString (buffer, offset + 1, length);
|
|
|
|
}
|
2019-11-04 08:30:13 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
public static String getCString (byte[] buffer, int offset)
|
|
|
|
// ---------------------------------------------------------------------------------//
|
|
|
|
{
|
|
|
|
int end = offset;
|
|
|
|
while (buffer[end] != 0)
|
|
|
|
end++;
|
|
|
|
|
|
|
|
return HexFormatter.getString (buffer, offset, end - offset);
|
|
|
|
}
|
2015-06-01 09:35:51 +00:00
|
|
|
}
|