rename other to that
[liba.git] / quickjs / src / crc64.c
blobc8692c49fd73030e29fb187b8e0560c669ee3e12
1 #include "a.h"
2 #include "a/crc.h"
4 struct crc64
6 a_u64 table[0x100];
7 a_u64 (*eval)(a_u64 const table[0x100], void const *pdata, a_size nbyte, a_u64 value);
8 };
10 static JSClassID liba_crc64_class_id;
12 static void liba_crc64_finalizer(JSRuntime *rt, JSValue val)
14 js_free_rt(rt, JS_GetOpaque(val, liba_crc64_class_id));
17 static JSClassDef liba_crc64_class = {"crc64", .finalizer = liba_crc64_finalizer};
19 static JSValue liba_crc64_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
21 JSValue proto, clazz = JS_UNDEFINED;
22 struct crc64 *const self = (struct crc64 *)js_mallocz(ctx, sizeof(struct crc64));
23 if (!self) { return JS_EXCEPTION; }
24 a_i64 poly = 0;
25 if (JS_ToInt64Ext(ctx, &poly, argv[0])) { goto fail; }
26 int reversed = 0;
27 if (argc > 1)
29 reversed = JS_ToBool(ctx, argv[1]);
30 if (reversed < 0) { goto fail; }
32 if (reversed)
34 a_crc64l_init(self->table, (a_u64)poly);
35 self->eval = a_crc64l;
37 else
39 a_crc64m_init(self->table, (a_u64)poly);
40 self->eval = a_crc64m;
42 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
43 if (JS_IsException(proto)) { goto fail; }
44 clazz = JS_NewObjectProtoClass(ctx, proto, liba_crc64_class_id);
45 JS_FreeValue(ctx, proto);
46 if (JS_IsException(clazz)) { goto fail; }
47 JS_SetOpaque(clazz, self);
48 return clazz;
49 fail:
50 js_free(ctx, self);
51 JS_FreeValue(ctx, clazz);
52 return JS_UNDEFINED;
55 static JSValue liba_crc64_get(JSContext *ctx, JSValueConst this_val, int magic)
57 struct crc64 *const self = (struct crc64 *)JS_GetOpaque2(ctx, this_val, liba_crc64_class_id);
58 if (!self) { return JS_EXCEPTION; }
59 if (magic == 0)
61 return js_array_u64_new(ctx, self->table, 0x100);
63 return JS_UNDEFINED;
66 static JSValue liba_crc64_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
68 struct crc64 *const self = (struct crc64 *)JS_GetOpaque2(ctx, this_val, liba_crc64_class_id);
69 if (!self) { return JS_EXCEPTION; }
70 a_i64 poly = 0;
71 if (JS_ToInt64Ext(ctx, &poly, argv[0])) { return JS_EXCEPTION; }
72 int reversed = 0;
73 if (argc > 1)
75 reversed = JS_ToBool(ctx, argv[1]);
76 if (reversed < 0) { return JS_EXCEPTION; }
78 if (reversed)
80 a_crc64l_init(self->table, (a_u64)poly);
81 self->eval = a_crc64l;
83 else
85 a_crc64m_init(self->table, (a_u64)poly);
86 self->eval = a_crc64m;
88 return JS_UNDEFINED;
91 static JSValue liba_crc64_eval(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
93 struct crc64 *const self = (struct crc64 *)JS_GetOpaque2(ctx, this_val, liba_crc64_class_id);
94 if (!self) { return JS_EXCEPTION; }
95 a_u64 value = 0;
96 if (argc > 1)
98 a_i64 x = 0;
99 if (JS_ToInt64Ext(ctx, &x, argv[1])) { return JS_EXCEPTION; }
100 value = (a_u64)x;
102 size_t n = 0;
103 if (JS_IsArray(ctx, argv[0]))
105 a_byte *p = JS_GetArrayBuffer(ctx, &n, argv[0]);
106 if (p) { value = self->eval(self->table, p, n, value); }
108 else
110 char const *const p = JS_ToCStringLen(ctx, &n, argv[0]);
111 value = self->eval(self->table, p, n, value);
112 JS_FreeCString(ctx, p);
114 return JS_NewBigUint64(ctx, value);
117 static JSValue liba_crc64_pack(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
119 JSValue val = JS_UNDEFINED;
120 struct crc64 *const self = (struct crc64 *)JS_GetOpaque2(ctx, this_val, liba_crc64_class_id);
121 if (!self) { return JS_EXCEPTION; }
122 a_u64 value = 0;
123 if (argc > 1)
125 a_i64 x = 0;
126 if (JS_ToInt64Ext(ctx, &x, argv[1])) { return JS_EXCEPTION; }
127 value = (a_u64)x;
129 size_t n = 0;
130 char const *const s = JS_ToCStringLen(ctx, &n, argv[0]);
131 value = self->eval(self->table, s, n, value);
132 a_byte *p = (a_byte *)js_malloc(ctx, n + 8);
133 if (p) { a_copy(p, s, n); }
134 else { goto fail; }
135 self->eval == a_crc64m
136 ? a_u64_setb(p + n, value)
137 : a_u64_setl(p + n, value);
138 val = js_array_u8_new(ctx, p, (uint32_t)n + 8);
139 fail:
140 JS_FreeCString(ctx, s);
141 js_free(ctx, p);
142 return val;
145 static JSCFunctionListEntry const liba_crc64_proto[] = {
146 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.crc64", 0),
147 JS_CGETSET_MAGIC_DEF("table", liba_crc64_get, NULL, 0),
148 JS_CFUNC_DEF("gen", 2, liba_crc64_gen),
149 JS_CFUNC_DEF("eval", 2, liba_crc64_eval),
150 JS_CFUNC_DEF("pack", 2, liba_crc64_pack),
153 int js_liba_crc64_init(JSContext *ctx, JSModuleDef *m)
155 JS_NewClassID(&liba_crc64_class_id);
156 JS_NewClass(JS_GetRuntime(ctx), liba_crc64_class_id, &liba_crc64_class);
158 JSValue const proto = JS_NewObject(ctx);
159 JS_SetPropertyFunctionList(ctx, proto, liba_crc64_proto, A_LEN(liba_crc64_proto));
161 JSValue const clazz = JS_NewCFunction2(ctx, liba_crc64_ctor, "crc64", 2, JS_CFUNC_constructor, 0);
162 JS_SetConstructor(ctx, clazz, proto);
163 JS_SetClassProto(ctx, liba_crc64_class_id, proto);
165 return JS_SetModuleExport(ctx, m, "crc64", clazz);