1 /* $NetBSD: utils.h,v 1.1 2025/02/24 13:47:57 christos Exp $ */ 2 3 /* 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #ifndef _UTILS_H_ 27 #define _UTILS_H_ 28 29 #ifndef lint 30 __RCSID("$NetBSD: utils.h,v 1.1 2025/02/24 13:47:57 christos Exp $"); 31 #endif /* not lint */ 32 33 #if 0 34 /* See sys/kern/kern_uuid.c */ 35 int uuid_printf(const struct uuid *); 36 void uuid_dec_be(const void *, struct uuid *); 37 void uuid_dec_le(const void *, struct uuid *); 38 void uuid_enc_be(void *, const struct uuid *); 39 void uuid_enc_le(void *, const struct uuid *); 40 int uuidgen(struct uuid *, int); 41 #endif 42 43 #include <string.h> 44 #include <util.h> 45 46 struct uuid; 47 48 static inline void * 49 memdup(const void *src, size_t sz) 50 { 51 void *dst; 52 53 dst = emalloc(sz); 54 memcpy(dst, src, sz); 55 return dst; 56 } 57 58 static inline char * 59 encode_data(const uint8_t *data, size_t len) 60 { 61 char *bp; 62 63 bp = emalloc(len * 2 + 1); 64 for (size_t i = 0; i < len; i++) 65 snprintf(&bp[i * 2], 3, "%02x", data[i]); 66 bp[len * 2] = '\0'; 67 68 return bp; 69 } 70 71 static inline size_t 72 ucs2nlen(const uint16_t *src, size_t maxlen) 73 { 74 size_t i; 75 76 for (i = 0; i < maxlen; i++) { 77 if (src[i] == '\0') 78 break; 79 } 80 return i; 81 } 82 83 char *ucs2_to_utf8(const uint16_t *, size_t, char *, size_t *); 84 uint16_t *utf8_to_ucs2(const char *, size_t, uint16_t *, size_t *); 85 size_t utf8_to_ucs2_size(const char *); 86 87 int uuid_scanf(struct uuid *, const char *); 88 int uuid_snprintf(char *, size_t, const struct uuid *); 89 int uuid_printf(const struct uuid *); 90 91 void show_data(const uint8_t *, size_t, const char *); 92 uint16_t strtous(const char *, char **, int); 93 94 char *read_file(const char *, size_t *); 95 96 #endif /* _UTILS_H_ */ 97