wheee!
[asdcplib.git] / hex_utils.h
1 /*
2 Copyright (c) 2004-2005, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    hex_utils.h
28     \version $Id$       
29     \brief   AS-DCP library, printable hex utilities
30 */
31
32 #ifndef _AS_DCP_HEX_UTILS_H__
33 #define _AS_DCP_HEX_UTILS_H__
34
35 namespace ASDCP
36 {
37   // Convert NULL-terminated UTF-8 hexadecimal string to binary, returns 0 if
38   // the binary buffer was large enough to hold the result. The output parameter
39   // 'char_count' will contain the length of the converted string. If the output
40   // buffer is too small or any of the pointer arguments are NULL, the subroutine
41   // will return -1 and set 'char_count' to the required buffer size. No data will
42   // be written to 'buf' if the subroutine fails.
43   i32_t       hex2bin(const char* str, byte_t* buf, ui32_t buf_len, ui32_t* char_count);
44
45   // Convert a binary string to NULL-terminated UTF-8 hexadecimal, returns the buffer
46   // if the binary buffer was large enough to hold the result. If the output buffer
47   // is too small or any of the pointer arguments are NULL, the subroutine will
48   // return 0.
49   //
50   const char* bin2hex(const byte_t* bin_buf, ui32_t bin_len, char* str_buf, ui32_t str_len);
51
52
53   // print to a stream the contents of the buffer as hexadecimal numbers in numbered
54   // rows of 16-bytes each.
55   //
56   void hexdump(const byte_t* buf, ui32_t dump_len, FILE* stream = 0);
57
58   // Return the length in bytes of a BER encoded value
59   inline ui32_t BER_length(const byte_t* buf)
60     {
61       if ( buf == 0 || (*buf & 0xf0) != 0x80 )
62         return 0;
63
64       return (*buf & 0x0f) + 1;
65     }
66
67   // read a BER value
68   bool read_BER(const byte_t* buf, ui64_t* val);
69
70   // decode a ber value and compare it to a test value
71   bool read_test_BER(byte_t **buf, ui64_t test_value);
72
73   // create BER encoding of integer value
74   bool write_BER(byte_t* buf, ui64_t val, ui32_t ber_len = 0);
75
76 } // namespace ASDCP
77
78 #endif // _AS_DCP_HEX_UTILS_H__
79
80 //
81 // end hex_utils.cpp
82 //
83