rename other to that
[liba.git] / quickjs / src / crc8.c
blob8061ba3697cf8ec2225eb4f68fd50e05305ce59c
1 #include "a.h"
2 #include "a/crc.h"
4 struct crc8
6 a_u8 table[0x100];
7 };
9 static JSClassID liba_crc8_class_id;
11 static void liba_crc8_finalizer(JSRuntime *rt, JSValue val)
13 js_free_rt(rt, JS_GetOpaque(val, liba_crc8_class_id));
16 static JSClassDef liba_crc8_class = {"crc8", .finalizer = liba_crc8_finalizer};
18 static JSValue liba_crc8_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
20 JSValue proto, clazz = JS_UNDEFINED;
21 struct crc8 *const self = (struct crc8 *)js_mallocz(ctx, sizeof(struct crc8));
22 if (!self) { return JS_EXCEPTION; }
23 a_u32 poly = 0;
24 if (JS_ToUint32(ctx, &poly, argv[0])) { goto fail; }
25 int reversed = 0;
26 if (argc > 1)
28 reversed = JS_ToBool(ctx, argv[1]);
29 if (reversed < 0) { goto fail; }
31 reversed
32 ? a_crc8l_init(self->table, (a_u8)poly)
33 : a_crc8m_init(self->table, (a_u8)poly);
34 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
35 if (JS_IsException(proto)) { goto fail; }
36 clazz = JS_NewObjectProtoClass(ctx, proto, liba_crc8_class_id);
37 JS_FreeValue(ctx, proto);
38 if (JS_IsException(clazz)) { goto fail; }
39 JS_SetOpaque(clazz, self);
40 return clazz;
41 fail:
42 js_free(ctx, self);
43 JS_FreeValue(ctx, clazz);
44 return JS_UNDEFINED;
47 static JSValue liba_crc8_get(JSContext *ctx, JSValueConst this_val, int magic)
49 struct crc8 *const self = (struct crc8 *)JS_GetOpaque2(ctx, this_val, liba_crc8_class_id);
50 if (!self) { return JS_EXCEPTION; }
51 if (magic == 0)
53 return js_array_u8_new(ctx, self->table, 0x100);
55 return JS_UNDEFINED;
58 static JSValue liba_crc8_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
60 struct crc8 *const self = (struct crc8 *)JS_GetOpaque2(ctx, this_val, liba_crc8_class_id);
61 if (!self) { return JS_EXCEPTION; }
62 a_u32 poly = 0;
63 if (JS_ToUint32(ctx, &poly, argv[0])) { return JS_EXCEPTION; }
64 int reversed = 0;
65 if (argc > 1)
67 reversed = JS_ToBool(ctx, argv[1]);
68 if (reversed < 0) { return JS_EXCEPTION; }
70 reversed
71 ? a_crc8l_init(self->table, (a_u8)poly)
72 : a_crc8m_init(self->table, (a_u8)poly);
73 return JS_UNDEFINED;
76 static JSValue liba_crc8_eval(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
78 struct crc8 *const self = (struct crc8 *)JS_GetOpaque2(ctx, this_val, liba_crc8_class_id);
79 if (!self) { return JS_EXCEPTION; }
80 a_u8 value = 0;
81 if (argc > 1)
83 a_u32 x = 0;
84 if (JS_ToUint32(ctx, &x, argv[1])) { return JS_EXCEPTION; }
85 value = (a_u8)x;
87 size_t n = 0;
88 if (JS_IsArray(ctx, argv[0]))
90 a_byte *p = JS_GetArrayBuffer(ctx, &n, argv[0]);
91 if (p) { value = a_crc8(self->table, p, n, value); }
93 else
95 char const *const p = JS_ToCStringLen(ctx, &n, argv[0]);
96 value = a_crc8(self->table, p, n, value);
97 JS_FreeCString(ctx, p);
99 return JS_NewUint32(ctx, value);
102 static JSValue liba_crc8_pack(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
104 JSValue val = JS_UNDEFINED;
105 struct crc8 *const self = (struct crc8 *)JS_GetOpaque2(ctx, this_val, liba_crc8_class_id);
106 if (!self) { return JS_EXCEPTION; }
107 a_u8 value = 0;
108 if (argc > 1)
110 a_u32 x = 0;
111 if (JS_ToUint32(ctx, &x, argv[1])) { return JS_EXCEPTION; }
112 value = (a_u8)x;
114 size_t n = 0;
115 char const *const s = JS_ToCStringLen(ctx, &n, argv[0]);
116 value = a_crc8(self->table, s, n, value);
117 a_byte *p = (a_byte *)js_malloc(ctx, n + 1);
118 if (p) { a_copy(p, s, n); }
119 else { goto fail; }
120 p[n] = value;
121 val = js_array_u8_new(ctx, p, (uint32_t)n + 1);
122 fail:
123 JS_FreeCString(ctx, s);
124 js_free(ctx, p);
125 return val;
128 static JSCFunctionListEntry const liba_crc8_proto[] = {
129 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.crc8", 0),
130 JS_CGETSET_MAGIC_DEF("table", liba_crc8_get, NULL, 0),
131 JS_CFUNC_DEF("gen", 2, liba_crc8_gen),
132 JS_CFUNC_DEF("eval", 2, liba_crc8_eval),
133 JS_CFUNC_DEF("pack", 2, liba_crc8_pack),
136 int js_liba_crc8_init(JSContext *ctx, JSModuleDef *m)
138 JS_NewClassID(&liba_crc8_class_id);
139 JS_NewClass(JS_GetRuntime(ctx), liba_crc8_class_id, &liba_crc8_class);
141 JSValue const proto = JS_NewObject(ctx);
142 JS_SetPropertyFunctionList(ctx, proto, liba_crc8_proto, A_LEN(liba_crc8_proto));
144 JSValue const clazz = JS_NewCFunction2(ctx, liba_crc8_ctor, "crc8", 2, JS_CFUNC_constructor, 0);
145 JS_SetConstructor(ctx, clazz, proto);
146 JS_SetClassProto(ctx, liba_crc8_class_id, proto);
148 return JS_SetModuleExport(ctx, m, "crc8", clazz);