3 #include "liba_crc16.h"
6 #define L Java_liba_crc16
13 JNIEXPORT
void JNICALL
Java_liba_crc16_clinit(JNIEnv
*Env
, jclass Cls
)
15 jclass Nbb
= (*Env
)->FindClass(Env
, "Ljava/nio/ByteBuffer;");
16 L
.New
= (*Env
)->GetStaticMethodID(Env
, Nbb
, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
17 L
.ctx
= (*Env
)->GetFieldID(Env
, Cls
, "ctx", "Ljava/nio/ByteBuffer;");
23 a_u16 (*eval
)(a_u16
const table
[0x100], void const *pdata
, a_size nbyte
, a_u16 value
);
26 JNIEXPORT
void JNICALL
Java_liba_crc16_init(JNIEnv
*Env
, jobject Obj
, jshort poly
, jboolean reversed
)
28 jobject Ctx
= (*Env
)->CallObjectMethod(Env
, Obj
, L
.New
, (jint
)sizeof(struct crc16
));
29 struct crc16
*ctx
= (struct crc16
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
30 (*Env
)->SetObjectField(Env
, Obj
, L
.ctx
, Ctx
);
33 a_crc16l_init(ctx
->table
, (a_u16
)poly
);
38 a_crc16m_init(ctx
->table
, (a_u16
)poly
);
43 JNIEXPORT jshortArray JNICALL
Java_liba_crc16_table(JNIEnv
*Env
, jobject Obj
)
45 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
46 struct crc16
*ctx
= (struct crc16
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
47 jshortArray table
= (*Env
)->NewShortArray(Env
, A_LEN(ctx
->table
));
48 (*Env
)->SetShortArrayRegion(Env
, table
, 0, A_LEN(ctx
->table
), (jshort
*)ctx
->table
);
52 JNIEXPORT jobject JNICALL
Java_liba_crc16_gen(JNIEnv
*Env
, jobject Obj
, jshort poly
, jboolean reversed
)
54 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
55 struct crc16
*ctx
= (struct crc16
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
58 a_crc16l_init(ctx
->table
, (a_u16
)poly
);
63 a_crc16m_init(ctx
->table
, (a_u16
)poly
);
69 JNIEXPORT jshort JNICALL
Java_liba_crc16_eval(JNIEnv
*Env
, jobject Obj
, jbyteArray block
, jshort value
)
71 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
72 struct crc16
*ctx
= (struct crc16
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
73 jsize n
= (*Env
)->GetArrayLength(Env
, block
);
74 jbyte
*p
= (*Env
)->GetByteArrayElements(Env
, block
, NULL
);
75 value
= (jshort
)ctx
->eval(ctx
->table
, p
, (a_size
)n
, (a_u16
)value
);
76 (*Env
)->ReleaseByteArrayElements(Env
, block
, p
, JNI_ABORT
);
80 JNIEXPORT jbyteArray JNICALL
Java_liba_crc16_pack(JNIEnv
*Env
, jobject Obj
, jbyteArray block
, jshort value
)
82 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
83 struct crc16
*ctx
= (struct crc16
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
84 jsize block_n
= (*Env
)->GetArrayLength(Env
, block
);
85 jbyteArray res
= (*Env
)->NewByteArray(Env
, block_n
+ 2);
86 jbyte
*block_p
= (*Env
)->GetByteArrayElements(Env
, block
, NULL
);
87 value
= (jshort
)ctx
->eval(ctx
->table
, block_p
, (a_size
)block_n
, (a_u16
)value
);
88 ctx
->eval
== a_crc16m
? a_u16_setb(&value
, (a_u16
)value
) : a_u16_setl(&value
, (a_u16
)value
);
89 (*Env
)->SetByteArrayRegion(Env
, res
, 0, block_n
, block_p
);
90 (*Env
)->ReleaseByteArrayElements(Env
, block
, block_p
, JNI_ABORT
);
91 (*Env
)->SetByteArrayRegion(Env
, res
, block_n
, 2, (jbyte
*)&value
);