skip to content
==> src/id.c <==
1/* jobs.rs's job_word, parse_job, hex and unhex. */
2#include "mesh/jobs/id.h"
3#include "mesh/rs_parse.h"
4
5#include <stdio.h>
6
7void mj_job_word(mj_job_id id, char out[MJ_JOB_WORD_MAX]) {
8    snprintf(out, MJ_JOB_WORD_MAX, "%llu.%llu", (unsigned long long)(id >> 32), (unsigned long long)(id & 0xffffffffu));
9}
10
11bool mj_parse_job(const char *s, mj_job_id *out) {
12    const char *dot = strchr(s, '.');
13    if (!dot) return false;
14    uint64_t t;
15    uint32_t n;
16    if (!rs_u64(s, (size_t)(dot - s), &t) || !rs_u32(dot + 1, strlen(dot + 1), &n)) return false;
17    *out = t << 32 | n;
18    return true;
19}
20
21void mj_hex(const mj_hash *h, char out[65]) {
22    static const char d[] = "0123456789abcdef";
23    for (int i = 0; i < 32; i++) {
24        out[2 * i] = d[h->b[i] >> 4];
25        out[2 * i + 1] = d[h->b[i] & 15];
26    }
27    out[64] = 0;
28}
29
30bool mj_unhex(const char *s, mj_hash *out) {
31    if (strlen(s) != 64) return false;
32    mj_hash h;
33    for (int i = 0; i < 32; i++) {
34        uint64_t v;
35        if (!rs_parse_uint(s + 2 * i, 2, 16, 255, &v)) return false;
36        h.b[i] = (uint8_t)v;
37    }
38    *out = h;
39    return true;
40}