]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org_argeo_api_uuid_NativeUuidFactory.c
714abeb069ac7082e08c39381d1472b637190d3b
[lgpl/argeo-commons.git] / org_argeo_api_uuid_NativeUuidFactory.c
1 #include <jni.h>
2 #include <uuid.h>
3 #include "org_argeo_api_uuid_NativeUuidFactory.h"
4
5 /*
6 * UTILITIES
7 */
8
9 static inline jobject fromBytes(JNIEnv *env, uuid_t out) {
10 jlong msb = 0;
11 jlong lsb = 0;
12
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);
17
18 jclass uuidClass = (*env)->FindClass(env, "java/util/UUID");
19 jmethodID uuidConstructor = (*env)->GetMethodID(env, uuidClass, "<init>",
20 "(JJ)V");
21
22 jobject jUUID = (*env)->AllocObject(env, uuidClass);
23 (*env)->CallVoidMethod(env, jUUID, uuidConstructor, msb, lsb);
24
25 return jUUID;
26 }
27
28 static inline void toBytes(JNIEnv *env, jobject jUUID, uuid_t result) {
29
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");
35
36 jlong msb = (*env)->CallLongMethod(env, jUUID, getMostSignificantBits);
37 jlong lsb = (*env)->CallLongMethod(env, jUUID, getLeastSignificantBits);
38
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);
43 }
44
45 /*
46 * JNI IMPLEMENTATION
47 */
48
49 /*
50 * Class: org_argeo_api_uuid_NativeUuidFactory
51 * Method: timeUUID
52 * Signature: ()Ljava/util/UUID;
53 */
54 JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_timeUUID(
55 JNIEnv *env, jobject) {
56 uuid_t out;
57
58 uuid_generate_time(out);
59 return fromBytes(env, out);
60 }
61
62 /*
63 * Class: org_argeo_api_uuid_NativeUuidFactory
64 * Method: nameUUIDv5
65 * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID;
66 */
67 JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_nameUUIDv5(
68 JNIEnv *env, jobject, jobject namespaceUuid, jbyteArray name) {
69 uuid_t ns;
70 uuid_t out;
71
72 toBytes(env, namespaceUuid, ns);
73 jsize length = (*env)->GetArrayLength(env, name);
74 jbyte *bytes = (*env)->GetByteArrayElements(env, name, 0);
75
76 uuid_generate_sha1(out, ns, bytes, length);
77 return fromBytes(env, out);
78 }
79
80 /*
81 * Class: org_argeo_api_uuid_NativeUuidFactory
82 * Method: nameUUIDv3
83 * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID;
84 */
85 JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_nameUUIDv3(
86 JNIEnv *env, jobject, jobject namespaceUuid, jbyteArray name) {
87 uuid_t ns;
88 uuid_t out;
89
90 toBytes(env, namespaceUuid, ns);
91 jsize length = (*env)->GetArrayLength(env, name);
92 jbyte *bytes = (*env)->GetByteArrayElements(env, name, 0);
93
94 uuid_generate_md5(out, ns, bytes, length);
95 return fromBytes(env, out);
96 }
97
98 /*
99 * Class: org_argeo_api_uuid_NativeUuidFactory
100 * Method: randomUUIDStrong
101 * Signature: ()Ljava/util/UUID;
102 */
103 JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_randomUUIDStrong(
104 JNIEnv *env, jobject) {
105 uuid_t out;
106
107 uuid_generate_random(out);
108 return fromBytes(env, out);
109 }