1 /* $NetBSD: util.c,v 1.3 2023/02/02 08:21:32 mlelstv Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Netflix, Inc
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: util.c,v 1.3 2023/02/02 08:21:32 mlelstv Exp $");
32 #if 0
33 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/util.c 320423 2017-06-27 20:24:25Z imp $");
34 #endif
35 #endif
36
37 #include <sys/endian.h>
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "nvmectl.h"
43 #include "bn.h"
44
45 void
nvme_strvis(u_char * dst,int dlen,const u_char * src,int slen)46 nvme_strvis(u_char *dst, int dlen, const u_char *src, int slen)
47 {
48 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
49 /* Trim leading and trailing blanks and NULs. */
50 while (slen > 0 && STRVIS_ISWHITE(src[0]))
51 ++src, --slen;
52 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
53 --slen;
54
55 while (slen > 0) {
56 if (*src < 0x20 || *src >= 0x80) {
57 /* non-printable characters */
58 dlen -= 4;
59 if (dlen < 1)
60 break;
61 *dst++ = '\\';
62 *dst++ = ((*src & 0300) >> 6) + '0';
63 *dst++ = ((*src & 0070) >> 3) + '0';
64 *dst++ = ((*src & 0007) >> 0) + '0';
65 } else if (*src == '\\') {
66 /* quote characters */
67 dlen -= 2;
68 if (dlen < 1)
69 break;
70 *dst++ = '\\';
71 *dst++ = '\\';
72 } else {
73 /* normal characters */
74 if (--dlen < 1)
75 break;
76 *dst++ = *src;
77 }
78 ++src, --slen;
79 }
80
81 *dst++ = 0;
82 }
83
84 #define METRIX_PREFIX_BUFSIZ 17
85 #define NO_METRIX_PREFIX_BUFSIZ 42
86
87 static void
unit_string(BIGNUM * bn,const char * unit,long scale,char * out,size_t len)88 unit_string(BIGNUM *bn, const char *unit, long scale, char *out, size_t len)
89 {
90 size_t ulen = strlen(unit);
91 uint8_t tmp[4];
92 BN_CTX *ctx;
93 BIGNUM *sn;
94
95 if (6 + ulen + 3 >= len)
96 return;
97
98 if (scale > 1) {
99 ctx = BN_CTX_new();
100 if (ctx == NULL)
101 return;
102
103 tmp[0] = (scale >> 24) & 0xff;
104 tmp[1] = (scale >> 16) & 0xff;
105 tmp[2] = (scale >> 8) & 0xff;
106 tmp[3] = scale & 0xff;
107
108 sn = BN_bin2bn(tmp, sizeof(tmp), NULL);
109 if (sn != NULL) {
110 BN_mul(bn, bn, sn, ctx);
111 BN_free(sn);
112 }
113
114 BN_CTX_free(ctx);
115 }
116
117 strncpy(out, " (", len);
118 humanize_bignum(out+2, 6 + ulen, bn, unit, HN_AUTOSCALE, HN_DECIMAL);
119 strncat(out, ")", len);
120 }
121
122 void
print_bignum1(const char * title,uint64_t v[2],const char * suffix,const char * unit,long scale)123 print_bignum1(const char *title, uint64_t v[2], const char *suffix,
124 const char *unit, long scale)
125 {
126 char buf[64];
127 char buf2[64];
128 uint8_t tmp[16];
129 uint64_t h, l;
130
131 #if _BYTE_ORDER != _LITTLE_ENDIAN
132 /* Already Converted to host endian */
133 h = v[0];
134 l = v[1];
135 memcpy(tmp, v, sizeof(tmp));
136 #else
137 h = v[1];
138 l = v[0];
139
140 tmp[ 0] = (h >> 56) & 0xff;
141 tmp[ 1] = (h >> 48) & 0xff;
142 tmp[ 2] = (h >> 40) & 0xff;
143 tmp[ 3] = (h >> 32) & 0xff;
144 tmp[ 4] = (h >> 24) & 0xff;
145 tmp[ 5] = (h >> 16) & 0xff;
146 tmp[ 6] = (h >> 8) & 0xff;
147 tmp[ 7] = h & 0xff;
148 tmp[ 8] = (l >> 56) & 0xff;
149 tmp[ 9] = (l >> 48) & 0xff;
150 tmp[10] = (l >> 40) & 0xff;
151 tmp[11] = (l >> 32) & 0xff;
152 tmp[12] = (l >> 24) & 0xff;
153 tmp[13] = (l >> 16) & 0xff;
154 tmp[14] = (l >> 8) & 0xff;
155 tmp[15] = l & 0xff;
156 #endif
157
158 buf[0] = '\0';
159 buf2[0] = '\0';
160
161 BIGNUM *bn = BN_bin2bn(tmp, sizeof(tmp), NULL);
162 if (bn != NULL) {
163 humanize_bignum(buf, METRIX_PREFIX_BUFSIZ + strlen(suffix),
164 bn, suffix, HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE);
165 if (unit)
166 unit_string(bn, unit, scale, buf2, sizeof(buf2));
167 BN_free(bn);
168 }
169 if (buf[0] == '\0')
170 snprintf(buf, sizeof(buf), "0x%016" PRIx64 "%016" PRIx64, h, l);
171 printf("%-31s %s%s\n", title, buf, buf2);
172 }
173
174 void
print_bignum(const char * title,uint64_t v[2],const char * suffix)175 print_bignum(const char *title, uint64_t v[2], const char *suffix)
176 {
177 print_bignum1(title, v, suffix, NULL, 1);
178 }
179
180 /* "Missing" from endian.h */
181 uint64_t
le48dec(const void * pp)182 le48dec(const void *pp)
183 {
184 uint8_t const *p = (uint8_t const *)pp;
185
186 return (((uint64_t)le16dec(p + 4) << 32) | le32dec(p));
187 }
188