1 /* $NetBSD: util.c,v 1.2 2018/04/18 10:11:44 nonaka 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.2 2018/04/18 10:11:44 nonaka 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 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 void 88 print_bignum(const char *title, uint64_t v[2], const char *suffix) 89 { 90 char buf[64]; 91 uint8_t tmp[16]; 92 uint64_t h, l; 93 94 #if _BYTE_ORDER != _LITTLE_ENDIAN 95 /* Already Converted to host endian */ 96 h = v[0]; 97 l = v[1]; 98 memcpy(tmp, v, sizeof(tmp)); 99 #else 100 h = v[1]; 101 l = v[0]; 102 103 tmp[ 0] = (h >> 56) & 0xff; 104 tmp[ 1] = (h >> 48) & 0xff; 105 tmp[ 2] = (h >> 40) & 0xff; 106 tmp[ 3] = (h >> 32) & 0xff; 107 tmp[ 4] = (h >> 24) & 0xff; 108 tmp[ 5] = (h >> 16) & 0xff; 109 tmp[ 6] = (h >> 8) & 0xff; 110 tmp[ 7] = h & 0xff; 111 tmp[ 8] = (l >> 56) & 0xff; 112 tmp[ 9] = (l >> 48) & 0xff; 113 tmp[10] = (l >> 40) & 0xff; 114 tmp[11] = (l >> 32) & 0xff; 115 tmp[12] = (l >> 24) & 0xff; 116 tmp[13] = (l >> 16) & 0xff; 117 tmp[14] = (l >> 8) & 0xff; 118 tmp[15] = l & 0xff; 119 #endif 120 121 buf[0] = '\0'; 122 BIGNUM *bn = BN_bin2bn(tmp, sizeof(tmp), NULL); 123 if (bn != NULL) { 124 humanize_bignum(buf, METRIX_PREFIX_BUFSIZ + strlen(suffix), 125 bn, suffix, HN_AUTOSCALE, HN_DECIMAL); 126 BN_free(bn); 127 } 128 if (buf[0] == '\0') 129 snprintf(buf, sizeof(buf), "0x%016" PRIx64 "%016" PRIx64, h, l); 130 printf("%-31s %s\n", title, buf); 131 } 132 133 /* "Missing" from endian.h */ 134 uint64_t 135 le48dec(const void *pp) 136 { 137 uint8_t const *p = (uint8_t const *)pp; 138 139 return (((uint64_t)le16dec(p + 4) << 32) | le32dec(p)); 140 } 141