-
Notifications
You must be signed in to change notification settings - Fork 7
/
id_BM.c
232 lines (212 loc) · 5.34 KB
/
id_BM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// file id_BM.c
// SPDX-License-Identifier: GPL-3.0-or-later
/***
BISMON
Copyright © 2018 - 2022 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdint.h>
#include <stdbool.h>
#include <glib.h>
#include "id_BM.h"
serial63_tyBM
randomserial63_BM (void)
{
serial63_tyBM s = 0;
do
{
// notice that g_random_int is seeded by /dev/random
s = (((serial63_tyBM) g_random_int ()) << 32) | g_random_int ();
}
while (!validserial63_BM (s));
return s;
} /* end randomserial63_BM */
// return the number of bytes written into cbuf
int
serial63tocbuf16_BM (serial63_tyBM s, char cbuf[static 16])
{
memset (cbuf, 0, 16);
if (s == 0)
{
cbuf[0] = '_';
cbuf[1] = '_';
return 2;
};
cbuf[0] = '_';
uint64_t n = s;
char *pc = cbuf + SERIALDIGITS_BM;
const char *b62digits = B62DIGITS_BM;
while (n != 0)
{
unsigned d = n % SERIALBASE_BM;
n = n / SERIALBASE_BM;
*pc = b62digits[d];
pc--;
}
while (pc > cbuf)
*(pc--) = '0';
cbuf[16 - 1] = (char) 0;
return SERIALDIGITS_BM + 1;
} /* end serial63tocbuf16_BM */
serial63_tyBM
parse_serial63_BM (const char *buf, const char **pend)
{
if (buf && buf[0] == '_' && buf[1] == '_')
{
if (pend)
*pend = buf + 2;
return 0;
};
if (!buf || buf[0] != '_' || buf[1] < '0' || buf[1] > '9')
{
if (pend)
*pend = buf;
return 0;
};
uint64_t n = 0;
const char *b62digits = B62DIGITS_BM;
for (unsigned i = 0; i < SERIALDIGITS_BM; i++)
{
if (!buf[i + 1])
{
if (pend)
*pend = buf;
return 0;
};
const char *str = strchr (b62digits, buf[i + 1]);
if (!str)
{
if (pend)
*pend = buf;
return 0;
};
n = n * SERIALBASE_BM + (str - b62digits);
};
if (!validserial63_BM (n))
{
if (pend)
*pend = buf;
return 0;
};
if (pend)
*pend = buf + SERIALDIGITS_BM + 1;
return n;
} /* end parse_serial63_BM */
rawid_tyBM
randomid_BM (void)
{
rawid_tyBM r;
r.id_hi = randomserial63_BM ();
r.id_lo = randomserial63_BM ();
return r;
} /* end randomid_BM */
// return the number of bytes written into cbuf
int
idtocbuf32_BM (rawid_tyBM id, char cbuf[static 32])
{
memset (cbuf, 0, 32);
if (!validid_BM (id))
{
cbuf[0] = cbuf[1] = '_';
return 2;
};
int llo = serial63tocbuf16_BM (id.id_hi, cbuf);
int lhi = serial63tocbuf16_BM (id.id_lo, cbuf + llo);
return llo + lhi;
} /* end idtocbuf32_BM */
rawid_tyBM
parse_rawid_BM (const char *buf, const char **pend)
{
if (buf && buf[0] == '_' && buf[1] == '_')
{
if (pend)
*pend = buf + 2;
return (rawid_tyBM)
{
0, 0};
}
if (!buf || buf[0] != '_' || buf[1] < '0' || buf[1] > '9')
{
if (pend)
*pend = buf;
return (rawid_tyBM)
{
0, 0};
}
const char *endhi = NULL;
const char *endlo = NULL;
serial63_tyBM idhi = 0, idlo = 0;
idhi = parse_serial63_BM (buf, &endhi);
if (endhi && endhi > buf + SERIALDIGITS_BM)
idlo = parse_serial63_BM (endhi, &endlo);
if (endlo && endlo > endhi + SERIALDIGITS_BM)
{
if (pend)
*pend = endlo;
return (rawid_tyBM)
{
idhi, idlo};
};
if (pend)
*pend = buf;
return (rawid_tyBM)
{
0, 0};
} /* end parse_rawid_BM */
const char *
iddbg_BM (rawid_tyBM id)
{
if (id.id_hi == 0 && id.id_lo == 0)
return "__";
else
{
static char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (id, idbuf);
return idbuf;
}
} /* end iddbg_BM */
const char *
iddbg1_BM (rawid_tyBM id)
{
if (id.id_hi == 0 && id.id_lo == 0)
return "__";
else
{
static char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (id, idbuf);
return idbuf;
}
} /* end iddbg1_BM */
const char *
iddbg2_BM (rawid_tyBM id)
{
if (id.id_hi == 0 && id.id_lo == 0)
return "__";
else
{
static char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (id, idbuf);
return idbuf;
}
} /* end iddbg2_BM */
/****************
** for Emacs...
** Local Variables: ;;
** compile-command: "./Build" ;;
** End: ;;
****************/
/**** end of file id_BM.c ***/