iPXE
base16.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdio.h>
00028 #include <errno.h>
00029 #include <assert.h>
00030 #include <ipxe/string.h>
00031 #include <ipxe/vsprintf.h>
00032 #include <ipxe/base16.h>
00033 
00034 /** @file
00035  *
00036  * Base16 encoding
00037  *
00038  */
00039 
00040 /**
00041  * Encode hexadecimal string (with optional byte separator character)
00042  *
00043  * @v separator         Byte separator character, or 0 for no separator
00044  * @v raw               Raw data
00045  * @v raw_len           Length of raw data
00046  * @v data              Buffer
00047  * @v len               Length of buffer
00048  * @ret len             Encoded length
00049  */
00050 size_t hex_encode ( char separator, const void *raw, size_t raw_len,
00051                     char *data, size_t len ) {
00052         const uint8_t *bytes = raw;
00053         const char delimiter[2] = { separator, '\0' };
00054         size_t used = 0;
00055         unsigned int i;
00056 
00057         if ( len )
00058                 data[0] = 0; /* Ensure that a terminating NUL exists */
00059         for ( i = 0 ; i < raw_len ; i++ ) {
00060                 used += ssnprintf ( ( data + used ), ( len - used ),
00061                                     "%s%02x", ( used ? delimiter : "" ),
00062                                     bytes[i] );
00063         }
00064         return used;
00065 }
00066 
00067 /**
00068  * Decode hexadecimal string (with optional byte separator character)
00069  *
00070  * @v separator         Byte separator character, or 0 for no separator
00071  * @v encoded           Encoded string
00072  * @v data              Buffer
00073  * @v len               Length of buffer
00074  * @ret len             Length of data, or negative error
00075  */
00076 int hex_decode ( char separator, const char *encoded, void *data, size_t len ) {
00077         uint8_t *out = data;
00078         unsigned int count = 0;
00079         unsigned int sixteens;
00080         unsigned int units;
00081 
00082         while ( *encoded ) {
00083 
00084                 /* Check separator, if applicable */
00085                 if ( count && separator && ( ( *(encoded++) != separator ) ) )
00086                         return -EINVAL;
00087 
00088                 /* Extract digits.  Note that either digit may be NUL,
00089                  * which would be interpreted as an invalid value by
00090                  * digit_value(); there is therefore no need for an
00091                  * explicit end-of-string check.
00092                  */
00093                 sixteens = digit_value ( *(encoded++) );
00094                 if ( sixteens >= 16 )
00095                         return -EINVAL;
00096                 units = digit_value ( *(encoded++) );
00097                 if ( units >= 16 )
00098                         return -EINVAL;
00099 
00100                 /* Store result */
00101                 if ( count < len )
00102                         out[count] = ( ( sixteens << 4 ) | units );
00103                 count++;
00104 
00105         }
00106         return count;
00107 }