Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/lib.c

1/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include "libsaio.h"
31
32/*
33 * Convert a number in BCD format to decimal.
34 *
35 * @param b The BCD number.
36 * @return The given BCD number in decimal format.
37 */
38int bcd2dec(int b)
39{
40return ((b >> 4) & 0x0f) * 10 + (b & 0x0f);
41}
42
43/*
44 * Convert a number in decimal format into the BCD format.
45 *
46 * @param d The decimal number.
47 * @return The given decimal number in BCD format.
48 */
49int dec2bcd(int d)
50{
51return ((d / 10) << 4) | (d % 10);
52}
53#if UNUSED
54/**
55 * Return the absolute value of the specified integer.
56 *
57 * @param j The integer of which we want to know the absolute value.
58 * @return The absolute value of the specified integer.
59 */
60int abs(int j)
61{
62return (j >= 0 ? j : -j);
63}
64
65long int labs(long int j)
66{
67return (j >= 0 ? j : -j);
68}
69
70long long int llabs(long long int j)
71{
72return (j >= 0 ? j : -j);
73}
74
75/**
76 * Given a 4-bit value, return the ASCII hex representation of it.
77 *
78 * @param b A 4-bit value which shall be converted to ASCII hex.
79 * @return The ASCII hex representation of the specified 4-bit value.
80 * Returned hex-characters will always be lower-case (a-f, not A-F).
81 */
82u8 bin2hex(u8 b)
83{
84return (b < 10) ? '0' + b : 'a' + (b - 10);
85}
86
87/**
88 * Given an ASCII hex input character, return its integer value.
89 *
90 * For example, the input value '6' will be converted to 6, 'a'/'A' will
91 * be converted to 10, 'f'/'F' will be converted to 15, and so on.
92 *
93 * The return value for invalid input characters is 0.
94 *
95 * @param h The input byte in ASCII hex format.
96 * @return The integer value of the specified ASCII hex byte.
97 */
98u8 hex2bin(u8 h)
99{
100return (('0' <= h && h <= '9') ? (h - '0') : \
101 ('A' <= h && h <= 'F') ? (h - 'A' + 10) : \
102 ('a' <= h && h <= 'f') ? (h - 'a' + 10) : 0);
103}
104#endif
105

Archive Download this file

Revision: 1984