Wed, 27 Jan 2021 18:55:14 +0200
fixed an off-by-one error
163
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
1 | /* |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
2 | Copyright 2016 Teemu Piippo |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
3 | All rights reserved. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
4 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
6 | modification, are permitted provided that the following conditions |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
7 | are met: |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
8 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
9 | 1. Redistributions of source code must retain the above copyright |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
10 | notice, this list of conditions and the following disclaimer. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
11 | 2. Redistributions in binary form must reproduce the above copyright |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
14 | 3. Neither the name of the copyright holder nor the names of its |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
15 | contributors may be used to endorse or promote products derived from |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
16 | this software without specific prior written permission. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
17 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
20 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
21 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
22 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
23 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
24 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
29 | */ |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
30 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
31 | #include "list.h" |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
32 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
33 | BEGIN_ZFC_NAMESPACE |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
34 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
35 | /*! |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
36 | * \brief Finds a suitable string representation for the provided byte. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
37 | * \param byte Byte to represent. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
38 | * \returns the string representation for the byte. |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
39 | */ |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
40 | const char* representByte(unsigned char byte) |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
41 | { |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
42 | static char buffer[8]; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
43 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
44 | if (byte < 8) |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
45 | { |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
46 | buffer[0] = '\\'; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
47 | buffer[1] = byte + '0'; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
48 | buffer[2] = '\0'; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
49 | return buffer; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
50 | } |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
51 | else switch(byte) |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
52 | { |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
53 | case '\a': return "\\a"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
54 | case '\b': return "\\b"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
55 | case '\t': return "\\t"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
56 | case '\n': return "\\n"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
57 | case '\v': return "\\v"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
58 | case '\f': return "\\f"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
59 | case '\r': return "\\r"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
60 | case '\e': return "\\e"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
61 | case '"': return "\\\""; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
62 | case '\\': return "\\\\"; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
63 | default: |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
64 | if (isprint(byte)) |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
65 | { |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
66 | buffer[0] = byte; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
67 | buffer[1] = '\0'; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
68 | return buffer; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
69 | } |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
70 | else |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
71 | { |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
72 | snprintf(buffer, sizeof buffer, "\\x%2x", byte); |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
73 | return buffer; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
74 | } |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
75 | } |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
76 | } |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
77 | |
182
20ca0a6be175
removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents:
180
diff
changeset
|
78 | std::string quote(const ByteArray &bytes) |
163
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
79 | { |
182
20ca0a6be175
removed the String class in favor of std::string
Teemu Piippo <teemu@hecknology.net>
parents:
180
diff
changeset
|
80 | std::string result; |
163
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
81 | |
180 | 82 | for (unsigned char byte : bytes) |
163
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
83 | result += representByte(byte); |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
84 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
85 | return "\"" + result + "\""; |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
86 | } |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
87 | |
5948441a1951
Added the ByteArray::quote() method to return a string representation of the byte array, and used it to print out the contents of unparseable packets.
Teemu Piippo <teemu@compsta2.com>
parents:
diff
changeset
|
88 | END_ZFC_NAMESPACE |