lib/helper.c
int
ipmi_parse_hex(const char *str, uint8_t *out, int size)
{
	const char *p;
	uint8_t *q;
	uint8_t d = 0;
	uint8_t b = 0;
	int shift = 4;
	int len;

	len = strlen(str);
	if (len == 0) {
		return 0;
	}

	if (len % 2 != 0) {
		return -1;
	}

	len /= 2; /* out bytes */
	if (!out) {
		return -2;
	}

	for (p = str, q = out; *p; p++) {
		if (!isxdigit(*p)) {
			return -3;
		}

		if (*p < 'A') {
			/* it must be 0-9 */
			d = *p - '0';
		} else {
			/* it's A-F or a-f */
			/* convert to lowercase and to 10-15 */
			d = (*p | 0x20) - 'a' + 10;
		}

		if (q < (out + size)) {
			/* there is space, store */
			b += d << shift;
			if (shift) {
				shift = 0;
			} else {
				shift = 4;
				*q = b;
				b = 0;
				q++;
			}
		}
	}

	return len;
}
/* ipmi_parse_hex - convert hexadecimal numbers to ascii string
 *                  Input string must be composed of two-characer
 *                  hexadecimal numbers.
 *                  There is no separator between the numbers. Each number
 *                  results in one byte of the converted string.
 *
 *                  Example: ipmi_parse_hex("50415353574F5244")
 *                  returns 'PASSWORD'
 *
 * @param str:  input string. It must contain only even number
 *              of '0'-'9','a'-'f' and 'A-F' characters.
 * @param out: pointer to output data
 * @param size: size of the output buffer
 * @returns 0 for empty input string
 *         -1 for string with odd length
 *         -2 if out is NULL
 *         -3 if there is non-hexadecimal char in string
 *         >0 length of resulting binary data even if it is > size
 */
convert hexadecimal numbers to ascii string
Input string must be composed of two-characer
hexadecimal numbers.
There is no separator between the numbers. Each number
results in one byte of the converted string.



@param str:  input string.
convert hexadecimal numbers to ascii string Input string must be composed of two - characer hexadecimal numbers . There is no separator between the numbers . Each number results in one byte of the converted string . @param str : input string .
int
ipmi_parse_hex(const char *str, uint8_t *out, int size)
{
	const char *p;
	uint8_t *q;
	uint8_t d = 0;
	uint8_t b = 0;
	int shift = 4;
	int len;
	len = strlen(str);
	if (len == 0) {
		return 0;
	}
	if (len % 2 != 0) {
		return -1;
	}
	len /= 2; 
	if (!out) {
		return -2;
	}
	for (p = str, q = out; *p; p++) {
		if (!isxdigit(*p)) {
			return -3;
		}
		if (*p < 'A') {
			d = *p - '0';
		} else {
			d = (*p | 0x20) - 'a' + 10;
		}
		if (q < (out + size)) {
			b += d << shift;
			if (shift) {
				shift = 0;
			} else {
				shift = 4;
				*q = b;
				b = 0;
				q++;
			}
		}
	}
	return len;
}
int ipmi_parse_hex ( const char * str , uint8_t * out , int size ) { const char * p ; uint8_t * q ; uint8_t d = 0 ; uint8_t b = 0 ; int shift = 4 ; int len ; len = strlen ( str ) ; if ( len == 0 ) { return 0 ; } if ( len % 2 != 0 ) { return -1 ; } len /= 2 ; if ( ! out ) { return -2 ; } for ( p = str , q = out ; * p ; p ++ ) { if ( ! isxdigit ( * p ) ) { return -3 ; } if ( * p < ' ' ) { d = * p - ' ' ; } else { d = ( * p | 0x20 ) - ' ' + 10 ; } if ( q < ( out + size ) ) { b += d << shift ; if ( shift ) { shift = 0 ; } else { shift = 4 ; * q = b ; b = 0 ; q ++ ; } } } return len ; }
ipmi_parse_hex - convert hexadecimal numbers to ascii string
                  Input string must be composed of two-characer
                  hexadecimal numbers.
ipmi_parse_hex - convert hexadecimal numbers to ascii string Input string must be composed of two - characer hexadecimal numbers .
/* out bytes */ /* it must be 0-9 */ /* it's A-F or a-f */ /* convert to lowercase and to 10-15 */ /* there is space, store */