iPXE
base16.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdio.h>
29#include <errno.h>
30#include <assert.h>
31#include <ipxe/string.h>
32#include <ipxe/vsprintf.h>
33#include <ipxe/base16.h>
34
35/** @file
36 *
37 * Base16 encoding
38 *
39 */
40
41/**
42 * Encode hexadecimal string (with optional byte separator character)
43 *
44 * @v separator Byte separator character, or 0 for no separator
45 * @v raw Raw data
46 * @v raw_len Length of raw data
47 * @v data Buffer
48 * @v len Length of buffer
49 * @ret len Encoded length
50 */
51size_t hex_encode ( char separator, const void *raw, size_t raw_len,
52 char *data, size_t len ) {
53 const uint8_t *bytes = raw;
54 const char delimiter[2] = { separator, '\0' };
55 size_t used = 0;
56 unsigned int i;
57
58 if ( len )
59 data[0] = 0; /* Ensure that a terminating NUL exists */
60 for ( i = 0 ; i < raw_len ; i++ ) {
61 used += ssnprintf ( ( data + used ), ( len - used ),
62 "%s%02x", ( used ? delimiter : "" ),
63 bytes[i] );
64 }
65 return used;
66}
67
68/**
69 * Decode hexadecimal string (with optional byte separator character)
70 *
71 * @v separator Byte separator character, or 0 for no separator
72 * @v encoded Encoded string
73 * @v data Buffer
74 * @v len Length of buffer
75 * @ret len Length of data, or negative error
76 */
77int hex_decode ( char separator, const char *encoded, void *data, size_t len ) {
78 uint8_t *out = data;
79 unsigned int count = 0;
80 unsigned int sixteens;
81 unsigned int units;
82 int optional;
83
84 /* Strip out optionality flag from separator character */
85 optional = ( separator & HEX_DECODE_OPTIONAL );
86 separator &= ~HEX_DECODE_OPTIONAL;
87
88 /* Decode string */
89 while ( *encoded ) {
90
91 /* Check separator, if applicable */
92 if ( count && separator ) {
93 if ( *encoded == separator ) {
94 encoded++;
95 } else if ( ! optional ) {
96 return -EINVAL;
97 }
98 }
99
100 /* Extract digits. Note that either digit may be NUL,
101 * which would be interpreted as an invalid value by
102 * digit_value(); there is therefore no need for an
103 * explicit end-of-string check.
104 */
105 sixteens = digit_value ( *(encoded++) );
106 if ( sixteens >= 16 )
107 return -EINVAL;
108 units = digit_value ( *(encoded++) );
109 if ( units >= 16 )
110 return -EINVAL;
111
112 /* Store result */
113 if ( count < len )
114 out[count] = ( ( sixteens << 4 ) | units );
115 count++;
116
117 }
118 return count;
119}
__be32 raw[7]
Definition CIB_PRM.h:0
__be32 out[4]
Definition CIB_PRM.h:8
unsigned char uint8_t
Definition stdint.h:10
Assertions.
int hex_decode(char separator, const char *encoded, void *data, size_t len)
Decode hexadecimal string (with optional byte separator character)
Definition base16.c:77
size_t hex_encode(char separator, const void *raw, size_t raw_len, char *data, size_t len)
Encode hexadecimal string (with optional byte separator character)
Definition base16.c:51
Base16 encoding.
static size_t raw_len
Definition base16.h:54
#define HEX_DECODE_OPTIONAL
Treat separator as optional while decoding.
Definition base16.h:17
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EINVAL
Invalid argument.
Definition errno.h:429
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
uint8_t bytes[64]
Definition ib_mad.h:5
String functions.
unsigned int digit_value(unsigned int character)
Calculate digit value.
Definition string.c:427
int ssnprintf(char *buf, ssize_t ssize, const char *fmt,...)
Version of vsnprintf() that accepts a signed buffer size.
Definition vsprintf.c:421
printf() and friends