3 #include "org_argeo_api_uuid_libuuid_LibuuidFactory.h"
9 static inline jobject
fromBytes(JNIEnv
*env
, uuid_t out
) {
13 for (int i
= 0; i
< 8; i
++)
14 msb
= (msb
<< 8) | (out
[i
] & 0xff);
15 for (int i
= 8; i
< 16; i
++)
16 lsb
= (lsb
<< 8) | (out
[i
] & 0xff);
18 jclass uuidClass
= (*env
)->FindClass(env
, "java/util/UUID");
19 jmethodID uuidConstructor
= (*env
)->GetMethodID(env
, uuidClass
, "<init>",
22 jobject jUUID
= (*env
)->AllocObject(env
, uuidClass
);
23 (*env
)->CallVoidMethod(env
, jUUID
, uuidConstructor
, msb
, lsb
);
28 static inline void toBytes(JNIEnv
*env
, jobject jUUID
, uuid_t result
) {
30 jclass uuidClass
= (*env
)->FindClass(env
, "java/util/UUID");
31 jmethodID getMostSignificantBits
= (*env
)->GetMethodID(env
, uuidClass
,
32 "getMostSignificantBits", "()J");
33 jmethodID getLeastSignificantBits
= (*env
)->GetMethodID(env
, uuidClass
,
34 "getLeastSignificantBits", "()J");
36 jlong msb
= (*env
)->CallLongMethod(env
, jUUID
, getMostSignificantBits
);
37 jlong lsb
= (*env
)->CallLongMethod(env
, jUUID
, getLeastSignificantBits
);
39 for (int i
= 0; i
< 8; i
++)
40 result
[i
] = (unsigned char) ((msb
>> ((7 - i
) * 8)) & 0xff);
41 for (int i
= 8; i
< 16; i
++)
42 result
[i
] = (unsigned char) ((lsb
>> ((15 - i
) * 8)) & 0xff);
49 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_libuuid_LibuuidFactory_timeUUID(
50 JNIEnv
*env
, jobject uuidFactory
) {
53 uuid_generate_time(out
);
54 return fromBytes(env
, out
);
57 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_libuuid_LibuuidFactory_nameUUIDv5(
58 JNIEnv
*env
, jobject uuidFactory
, jobject namespaceUuid
,
63 toBytes(env
, namespaceUuid
, ns
);
64 jsize length
= (*env
)->GetArrayLength(env
, name
);
65 jbyte
*bytes
= (*env
)->GetByteArrayElements(env
, name
, 0);
67 uuid_generate_sha1(out
, ns
, bytes
, length
);
68 return fromBytes(env
, out
);
71 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_libuuid_LibuuidFactory_nameUUIDv3(
72 JNIEnv
*env
, jobject uuidFactory
, jobject namespaceUuid
,
77 toBytes(env
, namespaceUuid
, ns
);
78 jsize length
= (*env
)->GetArrayLength(env
, name
);
79 jbyte
*bytes
= (*env
)->GetByteArrayElements(env
, name
, 0);
81 uuid_generate_md5(out
, ns
, bytes
, length
);
82 return fromBytes(env
, out
);
85 JNIEXPORT jobject JNICALL
Java_org_argeo_api_uuid_libuuid_LibuuidFactory_randomUUIDStrong(
86 JNIEnv
*env
, jobject uuidFactory
) {
89 uuid_generate_random(out
);
90 return fromBytes(env
, out
);