Debian Codegraph @

ulib/parse.c

raw
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4
5 #include "ulib/parse.h"
6
7 #ifdef __cplusplus
8 }
9 #endif
10
11 IV uu_parse(const char *in, struct_uu_t *out) {
12 int i;
13 const char *cp;
14 char buf[3];
15
16 if (strlen(in) != 36)
17 return -1;
18 for (i=0, cp = in; i <= 36; i++,cp++) {
19 if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
20 if (*cp == '-')
21 continue;
22 return -1;
23 }
24 if (i == 36 && *cp == 0)
25 continue;
26 if (!isxdigit((int)*cp))
27 return -1;
28 }
29 out->v1.time_low = strtoul(in, NULL, 16);
30 out->v1.time_mid = (U16)strtoul(in+9, NULL, 16);
31 out->v1.time_high_and_version = (U16)strtoul(in+14, NULL, 16);
32 out->v1.clock_seq_and_variant = (U16)strtoul(in+19, NULL, 16);
33 cp = in+24;
34 buf[2] = 0;
35 for (i=0; i < 6; i++) {
36 buf[0] = *cp++;
37 buf[1] = *cp++;
38 out->v1.node[i] = (U8)strtoul(buf, NULL, 16);
39 }
40
41 return 0;
42 }
43
44
45 const char *uu_parse_unparse_fmt_lower = "0123456789abcdef";
46 const char *uu_parse_unparse_fmt_upper = "0123456789ABCDEF";
47
48 /* convert U64 to hex chars. */
49 static void uu_u64_2hex(const U64 in, char *out, const int len, const char *fmt) {
50 U64 n = in;
51 int i = len;
52
53 do {
54 out[--i] = fmt[n % 16];
55 n >>= 4;
56 } while (n > 0);
57
58 while (i > 0)
59 out[--i] = '0';
60 }
61
62 void uu_parse_unparse_x0(const struct_uu_t *in, char *out, const char *fmt) {
63 char *dst = out;
64
65 uu_u64_2hex(in->v1.time_low, dst, 8, fmt); dst += 8; *dst++ = '-';
66 uu_u64_2hex(in->v1.time_mid, dst, 4, fmt); dst += 4; *dst++ = '-';
67 uu_u64_2hex(in->v1.time_high_and_version, dst, 4, fmt); dst += 4; *dst++ = '-';
68 uu_u64_2hex(in->v1.clock_seq_and_variant, dst, 4, fmt); dst += 4; *dst++ = '-';
69 uu_u64_2hex(in->v1.node[0], dst, 2, fmt); dst += 2;
70 uu_u64_2hex(in->v1.node[1], dst, 2, fmt); dst += 2;
71 uu_u64_2hex(in->v1.node[2], dst, 2, fmt); dst += 2;
72 uu_u64_2hex(in->v1.node[3], dst, 2, fmt); dst += 2;
73 uu_u64_2hex(in->v1.node[4], dst, 2, fmt); dst += 2;
74 uu_u64_2hex(in->v1.node[5], dst, 2, fmt); dst += 2;
75 *dst = 0;
76 }
77
78 void uu_parse_unparse_x1(const struct_uu_t *in, char *out, const char *fmt) {
79 char *dst = out;
80
81 uu_u64_2hex(in->v1.time_low, dst, 8, fmt); dst += 8; *dst++ = '-';
82 uu_u64_2hex(in->v1.time_mid, dst, 4, fmt); dst += 4; *dst++ = '-';
83 uu_u64_2hex(in->v1.time_high_and_version, dst, 4, fmt); dst += 4; *dst++ = '-';
84 uu_u64_2hex(in->v1.clock_seq_and_variant, dst, 4, fmt); dst += 4; *dst++ = '-';
85 uu_u64_2hex(in->v1.node[0], dst, 2, fmt); dst += 2;
86 uu_u64_2hex(in->v1.node[1], dst, 2, fmt); dst += 2;
87 uu_u64_2hex(in->v1.node[2], dst, 2, fmt); dst += 2;
88 uu_u64_2hex(in->v1.node[3], dst, 2, fmt); dst += 2;
89 uu_u64_2hex(in->v1.node[4], dst, 2, fmt); dst += 2;
90 uu_u64_2hex(in->v1.node[5], dst, 2, fmt); dst += 2;
91 *dst = 0;
92 }
93
94 void uu_parse_unparse_x3(const struct_uu_t *in, char *out, const char *fmt) {
95 char *dst = out;
96
97 uu_u64_2hex(in->v1.time_low, dst, 8, fmt); dst += 8; *dst++ = '-';
98 uu_u64_2hex(in->v1.time_mid, dst, 4, fmt); dst += 4; *dst++ = '-';
99 uu_u64_2hex(in->v1.time_high_and_version, dst, 4, fmt); dst += 4; *dst++ = '-';
100 uu_u64_2hex(in->v1.clock_seq_and_variant, dst, 4, fmt); dst += 4; *dst++ = '-';
101 uu_u64_2hex(in->v1.node[0], dst, 2, fmt); dst += 2;
102 uu_u64_2hex(in->v1.node[1], dst, 2, fmt); dst += 2;
103 uu_u64_2hex(in->v1.node[2], dst, 2, fmt); dst += 2;
104 uu_u64_2hex(in->v1.node[3], dst, 2, fmt); dst += 2;
105 uu_u64_2hex(in->v1.node[4], dst, 2, fmt); dst += 2;
106 uu_u64_2hex(in->v1.node[5], dst, 2, fmt); dst += 2;
107 *dst = 0;
108 }
109
110 void uu_parse_unparse_x4(const struct_uu_t *in, char *out, const char *fmt) {
111 char *dst = out;
112
113 uu_u64_2hex(in->v4.rand_a, dst, 8, fmt); dst += 8; *dst++ = '-';
114 uu_u64_2hex(in->v4.rand_b_and_version >> 16, dst, 4, fmt); dst += 4; *dst++ = '-';
115 uu_u64_2hex(in->v4.rand_b_and_version & 0xffff, dst, 4, fmt); dst += 4; *dst++ = '-';
116 uu_u64_2hex(in->v4.rand_c_and_variant >> 16, dst, 4, fmt); dst += 4; *dst++ = '-';
117 uu_u64_2hex(in->v4.rand_c_and_variant & 0xffff, dst, 4, fmt); dst += 4;
118 uu_u64_2hex(in->v4.rand_d, dst, 8, fmt); dst += 8;
119 *dst = 0;
120 }
121
122 void uu_parse_unparse_x5(const struct_uu_t *in, char *out, const char *fmt) {
123 char *dst = out;
124
125 uu_u64_2hex(in->v1.time_low, dst, 8, fmt); dst += 8; *dst++ = '-';
126 uu_u64_2hex(in->v1.time_mid, dst, 4, fmt); dst += 4; *dst++ = '-';
127 uu_u64_2hex(in->v1.time_high_and_version, dst, 4, fmt); dst += 4; *dst++ = '-';
128 uu_u64_2hex(in->v1.clock_seq_and_variant, dst, 4, fmt); dst += 4; *dst++ = '-';
129 uu_u64_2hex(in->v1.node[0], dst, 2, fmt); dst += 2;
130 uu_u64_2hex(in->v1.node[1], dst, 2, fmt); dst += 2;
131 uu_u64_2hex(in->v1.node[2], dst, 2, fmt); dst += 2;
132 uu_u64_2hex(in->v1.node[3], dst, 2, fmt); dst += 2;
133 uu_u64_2hex(in->v1.node[4], dst, 2, fmt); dst += 2;
134 uu_u64_2hex(in->v1.node[5], dst, 2, fmt); dst += 2;
135 *dst = 0;
136 }
137
138 void uu_parse_unparse_x6(const struct_uu_t *in, char *out, const char *fmt) {
139 char *dst = out;
140
141 uu_u64_2hex(in->v6.time_high, dst, 8, fmt); dst += 8; *dst++ = '-';
142 uu_u64_2hex(in->v6.time_mid, dst, 4, fmt); dst += 4; *dst++ = '-';
143 uu_u64_2hex(in->v6.time_low_and_version, dst, 4, fmt); dst += 4; *dst++ = '-';
144 uu_u64_2hex(in->v6.clock_seq_and_variant, dst, 4, fmt); dst += 4; *dst++ = '-';
145 uu_u64_2hex(in->v6.node[0], dst, 2, fmt); dst += 2;
146 uu_u64_2hex(in->v6.node[1], dst, 2, fmt); dst += 2;
147 uu_u64_2hex(in->v6.node[2], dst, 2, fmt); dst += 2;
148 uu_u64_2hex(in->v6.node[3], dst, 2, fmt); dst += 2;
149 uu_u64_2hex(in->v6.node[4], dst, 2, fmt); dst += 2;
150 uu_u64_2hex(in->v6.node[5], dst, 2, fmt); dst += 2;
151 *dst = 0;
152 }
153
154 void uu_parse_unparse_x7(const struct_uu_t *in, char *out, const char *fmt) {
155 char *dst = out;
156
157 uu_u64_2hex(in->v7.time_high, dst, 8, fmt); dst += 8; *dst++ = '-';
158 uu_u64_2hex(in->v7.time_low, dst, 4, fmt); dst += 4; *dst++ = '-';
159 uu_u64_2hex(in->v7.rand_a_and_version, dst, 4, fmt); dst += 4; *dst++ = '-';
160 uu_u64_2hex(in->v7.rand_b_and_variant >> 48, dst, 4, fmt); dst += 4; *dst++ = '-';
161 uu_u64_2hex(in->v7.rand_b_and_variant & 0xffffffffffffULL, dst, 12, fmt); dst += 12;
162 *dst = 0;
163 }
164
165 /* ex:set ts=2 sw=2 itab=spaces: */