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