1
/*******************************************************************************
2
* Copyright (c) 2004, 2010 Intel Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Intel Corporation - Initial API and implementation
10
* Mark Mitchell, CodeSourcery - Bug 136896: View variables in binary format
11
*******************************************************************************/
12
package org.eclipse.cdt.utils;
14
import java.io.Serializable;
15
import java.math.BigInteger;
17
import org.eclipse.cdt.core.IAddress;
18
import org.eclipse.cdt.internal.core.Messages;
20
public class Addr64 implements IAddress, Serializable {
22
public static final Addr64 ZERO = new Addr64("0"); //$NON-NLS-1$
23
public static final Addr64 MAX = new Addr64("ffffffffffffffff", 16); //$NON-NLS-1$
25
public static final BigInteger MAX_OFFSET = new BigInteger("ffffffffffffffff", 16); //$NON-NLS-1$
27
private static final int BYTES_NUM = 8;
28
private static final int DIGITS_NUM = BYTES_NUM * 2;
29
private static final int CHARS_NUM = DIGITS_NUM + 2;
30
private static final int BINARY_DIGITS_NUM = BYTES_NUM * 8;
31
private static final int BINARY_CHARS_NUM = BINARY_DIGITS_NUM + 2;
33
private final BigInteger address;
35
public Addr64(byte[] addrBytes) {
36
address = checkAddress(new BigInteger(1, addrBytes), true);
39
public Addr64(BigInteger rawaddress) {
40
this(rawaddress, true);
43
public Addr64(BigInteger rawaddress, boolean truncate) {
44
address = checkAddress(rawaddress, truncate);
47
public Addr64(String addr) {
51
public Addr64(String addr, boolean truncate) {
52
addr = addr.toLowerCase();
53
if (addr.startsWith("0x")) { //$NON-NLS-1$
54
address = checkAddress(new BigInteger(addr.substring(2), 16), truncate);
56
address = checkAddress(new BigInteger(addr, 10), truncate);
60
public Addr64(String addr, int radix) {
61
this(addr, radix, true);
64
public Addr64(String addr, int radix, boolean truncate) {
65
this(new BigInteger(addr, radix), truncate);
68
private BigInteger checkAddress(BigInteger addr, boolean truncate) {
69
if (addr.signum() == -1) {
70
throw new IllegalArgumentException("Invalid Address, must be positive value"); //$NON-NLS-1$
72
if (addr.bitLength() > 64 ) {
74
return addr.and(MAX.getValue()); // truncate
77
throw (new NumberFormatException(Messages.Addr_valueOutOfRange));
84
public IAddress add(BigInteger offset) {
85
return new Addr64(this.address.add(offset));
88
public IAddress add(long offset) {
89
return new Addr64(this.address.add(BigInteger.valueOf(offset)));
92
public BigInteger getMaxOffset() {
96
public BigInteger distanceTo(IAddress other) {
97
return other.getValue().subtract(getValue());
100
public boolean isMax() {
101
return address.equals(MAX.getValue());
104
public boolean isZero() {
105
return address.equals(ZERO.getValue());
108
public BigInteger getValue() {
112
public int compareTo(Object other) {
113
if (!(other instanceof IAddress)) {
114
throw new IllegalArgumentException();
117
return getValue().compareTo(((IAddress)other).getValue());
121
public boolean equals(Object x) {
124
if (!(x instanceof IAddress))
126
return getValue().equals(((IAddress)x).getValue());
130
public int hashCode() {
131
return address.hashCode();
135
public String toString() {
139
public String toString(int radix) {
140
return address.toString(radix);
143
public String toHexAddressString() {
144
String addressString = address.toString(16);
145
StringBuffer sb = new StringBuffer(CHARS_NUM);
146
int count = DIGITS_NUM - addressString.length();
147
sb.append("0x"); //$NON-NLS-1$
148
for (int i = 0; i < count; ++i) {
151
sb.append(addressString);
152
return sb.toString();
155
public String toBinaryAddressString() {
156
String addressString = address.toString(2);
157
StringBuffer sb = new StringBuffer(BINARY_CHARS_NUM);
158
int count = BINARY_DIGITS_NUM - addressString.length();
159
sb.append("0b"); //$NON-NLS-1$
160
for (int i = 0; i < count; ++i) {
163
sb.append(addressString);
164
return sb.toString();
167
public int getCharsNum() {
171
public int getSize() {