|
| 1 | +package io.sentry.android.core.internal.tombstone; |
| 2 | + |
| 3 | +import androidx.annotation.Nullable; |
| 4 | + |
| 5 | +/** |
| 6 | + * Used to convert a build/code-id hex string into a little-endian GUID string (from OLE) which is |
| 7 | + * the expected format for the debug-id. |
| 8 | + * |
| 9 | + * <p>Each component used in a GUID uses little-endian representation. But the last two components |
| 10 | + * (`clock_seq` and `node`) were represented in memory as eight individual bytes (which makes them |
| 11 | + * look like big endian when formatted as a string). |
| 12 | + * |
| 13 | + * <p>Conversion example from the sentry development docs: |
| 14 | + * |
| 15 | + * <pre> |
| 16 | + * f1c3bcc0 2798 65fe 3058 404b2831d9e6 4135386c |
| 17 | + * 32-bit 16 16 2x8 6x8 Ignored |
| 18 | + * LE LE LE LE LE |
| 19 | + * = = = = = |
| 20 | + * c0bcc3f1-9827-fe65-3058-404b2831d9e6 |
| 21 | + * </pre> |
| 22 | + * |
| 23 | + * Note: Java bytes are signed. When promoted (e.g. during formatting or bit shifts), they |
| 24 | + * sign-extend to int, unlike uint8_t in C. We therefore mask with & 0xff to preserve the intended |
| 25 | + * unsigned byte values. |
| 26 | + */ |
| 27 | +public class OleGuidFormatter { |
| 28 | + public static String convert(final @Nullable String hex) { |
| 29 | + if (hex == null) { |
| 30 | + throw new NullPointerException("GUID conversion input hex string"); |
| 31 | + } |
| 32 | + if ((hex.length() % 2) != 0) { |
| 33 | + throw new IllegalArgumentException("The GUID conversion input hex string has odd length"); |
| 34 | + } |
| 35 | + if (hex.length() < 32) { |
| 36 | + throw new IllegalArgumentException( |
| 37 | + "Need at least 16 bytes (32 hex chars) to convert to GUID"); |
| 38 | + } |
| 39 | + |
| 40 | + final byte[] b = hexToBytes(hex); |
| 41 | + |
| 42 | + final long timeLow = u32le(b, 0); |
| 43 | + final int timeMid = u16le(b, 4); |
| 44 | + final int timeHiAndVersion = u16le(b, 6); |
| 45 | + |
| 46 | + return String.format( |
| 47 | + "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 48 | + timeLow, |
| 49 | + timeMid, |
| 50 | + timeHiAndVersion, |
| 51 | + // clock_seq_hi_and_reserved |
| 52 | + b[8] & 0xff, |
| 53 | + // clock_seq_low |
| 54 | + b[9] & 0xff, |
| 55 | + // node (6 MAC components) |
| 56 | + b[10] & 0xff, |
| 57 | + b[11] & 0xff, |
| 58 | + b[12] & 0xff, |
| 59 | + b[13] & 0xff, |
| 60 | + b[14] & 0xff, |
| 61 | + b[15] & 0xff); |
| 62 | + } |
| 63 | + |
| 64 | + private static int u16le(byte[] b, int offset) { |
| 65 | + return (b[offset] & 0xff) | ((b[offset + 1] & 0xff) << 8); |
| 66 | + } |
| 67 | + |
| 68 | + private static long u32le(byte[] b, int offset) { |
| 69 | + return (long) (b[offset] & 0xff) |
| 70 | + | ((long) (b[offset + 1] & 0xff) << 8) |
| 71 | + | ((long) (b[offset + 2] & 0xff) << 16) |
| 72 | + | ((long) (b[offset + 3] & 0xff) << 24); |
| 73 | + } |
| 74 | + |
| 75 | + private static byte[] hexToBytes(String hex) { |
| 76 | + int numBytes = 16; |
| 77 | + byte[] result = new byte[numBytes]; |
| 78 | + for (int byteIdx = 0; byteIdx < numBytes; byteIdx++) { |
| 79 | + int hi = Character.digit(hex.charAt(byteIdx * 2), numBytes); |
| 80 | + int lo = Character.digit(hex.charAt(byteIdx * 2 + 1), numBytes); |
| 81 | + result[byteIdx] = (byte) ((hi << 4) | lo); |
| 82 | + } |
| 83 | + return result; |
| 84 | + } |
| 85 | +} |
0 commit comments