media/mtransport/third_party/nrappkit/src/util/hex.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/third_party/nrappkit/src/util/hex.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/**
     1.5 +   hex.c
     1.6 +
     1.7 +
     1.8 +   Copyright (C) 2001-2003, Network Resonance, Inc.
     1.9 +   Copyright (C) 2006, Network Resonance, Inc.
    1.10 +   All Rights Reserved
    1.11 +
    1.12 +   Redistribution and use in source and binary forms, with or without
    1.13 +   modification, are permitted provided that the following conditions
    1.14 +   are met:
    1.15 +
    1.16 +   1. Redistributions of source code must retain the above copyright
    1.17 +      notice, this list of conditions and the following disclaimer.
    1.18 +   2. Redistributions in binary form must reproduce the above copyright
    1.19 +      notice, this list of conditions and the following disclaimer in the
    1.20 +      documentation and/or other materials provided with the distribution.
    1.21 +   3. Neither the name of Network Resonance, Inc. nor the name of any
    1.22 +      contributors to this software may be used to endorse or promote
    1.23 +      products derived from this software without specific prior written
    1.24 +      permission.
    1.25 +
    1.26 +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
    1.27 +   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.28 +   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.29 +   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.30 +   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.31 +   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.32 +   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.33 +   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.34 +   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.35 +   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.36 +   POSSIBILITY OF SUCH DAMAGE.
    1.37 +
    1.38 +
    1.39 +   briank@network-resonance.com
    1.40 + */
    1.41 +
    1.42 +
    1.43 +static char *RCSSTRING __UNUSED__ ="$Id: hex.c,v 1.3 2007/06/26 22:37:56 adamcain Exp $";
    1.44 +
    1.45 +#include <sys/types.h>
    1.46 +#include <string.h>
    1.47 +#include <ctype.h>
    1.48 +#include "r_common.h"
    1.49 +#include "hex.h"
    1.50 +#include "r_log.h"
    1.51 +
    1.52 +static char bin2hex_map[][3] = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff" };
    1.53 +
    1.54 +static int hex2bin_map[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0 /* '0' */, 1 /* '1' */, 2 /* '2' */, 3 /* '3' */, 4 /* '4' */, 5 /* '5' */, 6 /* '6' */, 7 /* '7' */, 8 /* '8' */, 9 /* '9' */, -1, -1, -1, -1, -1, -1, -1, 10 /* 'A' */, 11 /* 'B' */, 12 /* 'C' */, 13 /* 'D' */, 14 /* 'E' */, 15 /* 'F' */, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10 /* 'a' */, 11 /* 'b' */, 12 /* 'c' */, 13 /* 'd' */, 14 /* 'e' */, 15 /* 'f' */, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
    1.55 +
    1.56 +int
    1.57 +nr_nbin2hex(UCHAR *bin, int binlen, char hex[], size_t size, int *len)
    1.58 +{
    1.59 +    int _status;
    1.60 +    int i;
    1.61 +
    1.62 +    if (size < (2*binlen))
    1.63 +        ABORT(R_BAD_ARGS);
    1.64 +
    1.65 +    for (i = 0; i < binlen; ++i) {
    1.66 +        *hex++ = bin2hex_map[bin[i]][0];
    1.67 +        *hex++ = bin2hex_map[bin[i]][1];
    1.68 +    }
    1.69 +
    1.70 +    if (size >= (2*binlen)+1)
    1.71 +        *hex = '\0';
    1.72 +
    1.73 +    *len = (2*binlen);
    1.74 +
    1.75 +    _status=0;
    1.76 +  abort:
    1.77 +    return(_status);
    1.78 +}
    1.79 +
    1.80 +
    1.81 +int
    1.82 +nr_nhex2bin(char *hex, int hexlen, UCHAR bin[], size_t size, int *len)
    1.83 +{
    1.84 +    int _status;
    1.85 +    int binlen;
    1.86 +    int h1;
    1.87 +    int h2;
    1.88 +    int i;
    1.89 +
    1.90 +    if (hexlen % 2)
    1.91 +        ABORT(R_BAD_ARGS);
    1.92 +
    1.93 +    binlen = hexlen/2;
    1.94 +
    1.95 +    if (size < binlen)
    1.96 +        ABORT(R_BAD_ARGS);
    1.97 +
    1.98 +    for (i = 0; i < binlen; ++i) {
    1.99 +        h1 = hex2bin_map[(int)*hex++];
   1.100 +        h2 = hex2bin_map[(int)*hex++];
   1.101 +
   1.102 +        if (h1 == -1 || h2 == -1)
   1.103 +            ABORT(R_BAD_ARGS);
   1.104 +
   1.105 +        bin[i] = (h1 << 4) | h2;
   1.106 +    }
   1.107 +
   1.108 +    *len = binlen;
   1.109 +
   1.110 +    _status=0;
   1.111 +  abort:
   1.112 +    return(_status);
   1.113 +}

mercurial