1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
15 import java
.util
.UUID
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.CompoundDeclaration
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
27 * @author Matthew Khouzam
28 * @author Simon Marchi
30 public final class Utils
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
40 * CTF magic number. (sort of looks like CTF CTF CT)
42 public static final int CTF_MAGIC
= 0xC1FC1FC1;
45 * TSDL magic number. (sort of looks like TSDL LSDT)
47 public static final int TSDL_MAGIC
= 0x75D11D57;
50 * TSDL magic number length in bytes.
52 public static final int TSDL_MAGIC_LEN
= 4;
55 * Directory separator on the current platform.
57 public static final String SEPARATOR
= System
.getProperty("file.separator"); //$NON-NLS-1$
60 * Length in bytes of a UUID value.
62 public static final int UUID_LEN
= 16;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Performs an unsigned long comparison on two unsigned long numbers.
71 * <strong> As Java does not support unsigned types and arithmetic,
72 * parameters are received encoded as a signed long (two-complement) but the
73 * operation is an unsigned comparator.</strong>
76 * Left operand of the comparator.
78 * Right operand of the comparator.
79 * @return -1 if left < right, 1 if left > right, 0 if left == right.
81 public static int unsignedCompare(long left
, long right
) {
83 * This method assumes that the arithmetic overflow on signed integer
84 * wrap on a circular domain (modulo arithmetic in two-complement),
85 * which is the defined behavior in Java.
87 * This idea is to rotate the domain by the length of the negative
88 * space, and then use the signed operator.
90 final long a
= left
+ Long
.MIN_VALUE
;
91 final long b
= right
+ Long
.MIN_VALUE
;
101 * Gets a UUID from an array defintion
104 * the array defintions, must contain integer bytes
106 * @throws CTFReaderException
107 * if the definition contains less than 16 elements
110 public static UUID
getUUIDfromDefinition(AbstractArrayDefinition uuidDef
) throws CTFReaderException
{
111 byte[] uuidArray
= new byte[16];
112 IDeclaration declaration
= uuidDef
.getDeclaration();
113 if (!(declaration
instanceof CompoundDeclaration
)) {
114 throw new CTFReaderException("UUID must be a sequence of unsigned bytes"); //$NON-NLS-1$
116 CompoundDeclaration uuidDec
= (CompoundDeclaration
) declaration
;
118 IDeclaration uuidElem
= uuidDec
.getElementType();
119 if (!(uuidElem
instanceof IntegerDeclaration
)) {
120 throw new CTFReaderException("UUID must be a sequence of unsigned bytes"); //$NON-NLS-1$
122 IntegerDeclaration intUuidElem
= (IntegerDeclaration
) uuidElem
;
123 if (!intUuidElem
.isUnsignedByte()) {
124 throw new CTFReaderException("UUID must be a sequence of unsigned bytes"); //$NON-NLS-1$
126 return getUUID(uuidDef
, uuidArray
);
129 private static UUID
getUUID(AbstractArrayDefinition uuidDef
, byte[] uuidArray
) throws CTFReaderException
{
130 for (int i
= 0; i
< uuidArray
.length
; i
++) {
131 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
.getDefinitions().get(i
);
132 if (uuidByteDef
== null) {
133 throw new CTFReaderException("UUID incomplete, only " + i
+ " bytes available"); //$NON-NLS-1$ //$NON-NLS-2$
135 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
138 UUID uuid
= Utils
.makeUUID(uuidArray
);
143 * Gets a UUID from an array defintion
146 * the array defintions, must contain integer bytes
148 * @throws CTFReaderException
149 * if the definition contains less than 16 elements
152 * {@link Utils#getUUIDfromDefinition(AbstractArrayDefinition uuidDef)}
155 public static UUID
getUUIDfromDefinition(org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition uuidDef
) throws CTFReaderException
{
156 byte[] uuidArray
= new byte[16];
157 return getUUID(uuidDef
, uuidArray
);
161 * Creates a UUID object from an array of 16 bytes.
165 * @return A UUID object.
167 public static UUID
makeUUID(byte bytes
[]) {
171 assert (bytes
.length
== Utils
.UUID_LEN
);
173 for (int i
= 0; i
< 8; i
++) {
174 low
= (low
<< 8) | (bytes
[i
+ 8] & 0xFF);
175 high
= (high
<< 8) | (bytes
[i
] & 0xFF);
178 UUID uuid
= new UUID(high
, low
);