1 /*****************************************************************************\
3 * Name : byte_formatter *
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 1992-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "byte_formatter.h"
16 #include "parser_bits.h"
17 #include "string_manipulation.h"
19 #include <basis/functions.h>
20 #include <structures/bit_vector.h>
21 #include <structures/string_array.h>
23 //#define DEBUG_BYTE_FORMAT
24 // uncomment for noisier version.
27 #ifdef DEBUG_BYTE_FORMAT
28 #define LOG(s) printf("%s\n", astring(s).s())
35 using namespace basis;
36 using namespace structures;
40 void byte_formatter::print_char(abyte to_print, astring &out, char replace)
42 int temp = to_print % 128;
43 if (!parser_bits::is_printable_ascii(to_print)) out += replace;
44 else out += char(temp);
47 void byte_formatter::print_chars(const abyte *to_print, int len, astring &out, char replace)
49 for (int i = 0; i < len; i++)
50 print_char(to_print[i], out, replace);
53 void byte_formatter::make_eight(basis::un_int num, astring &out)
55 basis::un_int thresh = 0x10000000;
56 while (thresh >= 0x10) {
59 thresh >>= 4; // zap a nibble.
63 astring byte_formatter::text_dump(const abyte *location, basis::un_int length, basis::un_int label,
67 text_dump(to_return, location, length, label, eol);
71 void byte_formatter::text_dump(astring &output, const byte_array &to_dump, basis::un_int label,
74 text_dump(output, to_dump.observe(), to_dump.length(), label, eol);
77 astring byte_formatter::text_dump(const byte_array &to_dump, basis::un_int label, const char *eol)
80 text_dump(output, to_dump.observe(), to_dump.length(), label, eol);
84 // this is the real version of text_dump. all the others use it.
85 void byte_formatter::text_dump(astring &to_return, const abyte *location, basis::un_int length,
86 basis::un_int label, const char *eol)
92 basis::un_int entries_per_line = (LINE_SIZE - preamble) / entry_size;
94 for (basis::un_int i = 0; i < length; i += entries_per_line) {
95 make_eight(i + label, to_return);
96 to_return += astring(astring::SPRINTF, "%x", i + label) + astring(" | ");
97 for (basis::un_int j = 0; j < entries_per_line; j++) {
98 if (i + j >= length) {
99 // if at the end of the loop, just print spaces.
102 int ord_of_current_char = *(location + i + j) & 0xFF;
103 if (ord_of_current_char < 0x10) to_return += '0';
104 to_return += astring(astring::SPRINTF, "%x", int(ord_of_current_char));
110 for (basis::un_int k = i; k < i + entries_per_line; k++) {
111 if (k >= length) to_return += ' ';
112 // if past the end of the block, just add spaces.
113 else print_char(*(location + k), to_return);
115 to_return += astring(" |") + eol;
119 void byte_formatter::parse_dump(const astring &dumped_form, byte_array &bytes_found)
122 string_array lines_found;
123 // iterate over the string and break it up into lines.
124 for (int i = 0; i < dumped_form.length(); i++) {
125 int indy = dumped_form.find('\n', i);
126 //hmmm: not platform invariant. what about '\r' if we see it?
128 if (negative(indy)) {
129 // no more lines found.
130 if (i < dumped_form.length() - 1) {
131 // grab the last bit as a line.
132 lines_found += dumped_form.substring(i, dumped_form.length() - 1);
136 // found a normal line ending, so drop everything from the current
137 // position up to the ending into the list of strings.
138 lines_found += dumped_form.substring(i, indy - 1);
139 i = indy + 1; // jump to next potential line.
141 // now process the lines that we've found.
142 for (int j = 0; j < lines_found.length(); j++) {
143 // first step is to find the pipe character that brackets the actual
144 // data. we ignore the "address" located before the pipe.
145 astring &s = lines_found[j];
146 int bar_one = s.find('|', 0);
147 if (negative(bar_one)) continue; // skip this one; it's malformed.
148 // now we look for the second pipe that comes before the text form of
149 // the data. we don't care about the text or anything after.
150 int bar_two = s.find('|', bar_one + 1);
151 if (negative(bar_two)) continue; // skip for same reason.
152 astring s2 = s.substring(bar_one + 1, bar_two - 1);
153 byte_array this_part;
154 string_to_bytes(s2, this_part);
155 bytes_found += this_part;
161 void byte_formatter::bytes_to_string(const abyte *to_convert, int length, astring &as_string,
162 bool space_delimited)
164 if (!to_convert || !length) return; // nothing to do.
165 if (negative(length)) return; // bunk.
166 as_string = ""; // reset the output parameter.
168 // the pattern is used for printing the bytes and considering the delimiter.
169 astring pattern("%02x");
170 if (space_delimited) pattern += " ";
172 // now zip through the array and dump it into the string.
173 for (int i = 0; i < length; i++)
174 as_string += astring(astring::SPRINTF, pattern.s(), to_convert[i]);
177 // returns true if the character is within the valid ranges of hexadecimal
178 // nibbles (as text).
179 bool byte_formatter::in_hex_range(char to_check)
180 //hmmm: move this to parser bits.
182 return ( (to_check <= '9') && (to_check >= '0') )
183 || ( (to_check <= 'f') && (to_check >= 'a') )
184 || ( (to_check <= 'F') && (to_check >= 'A') );
187 void byte_formatter::string_to_bytes(const char *to_convert, byte_array &as_array)
189 as_array.reset(); // clear the array.
190 const int len = int(strlen(to_convert));
192 // the parser uses a simple state machine for processing the string.
193 enum states { FINDING_HEX, IGNORING_JUNK };
194 states state = IGNORING_JUNK;
196 int digits = 0; // the number of digits we've currently found.
197 int accumulator = 0; // the current hex duo.
199 // loop through the string.
200 for (int i = 0; i < len; i++) {
202 case IGNORING_JUNK: {
203 if (in_hex_range(to_convert[i])) {
204 i--; // skip back to where we were before now.
206 continue; // jump to the other state.
208 // otherwise, we could care less what the character is.
213 // we have finished a hex byte.
214 as_array += abyte(accumulator);
217 i--; // skip back for the byte we haven't eaten yet.
218 state = IGNORING_JUNK; // jump to other state for a new item.
221 // we really think this is a digit here and we're not through with
222 // accumulating them.
225 accumulator += string_manipulation::char_to_hex(to_convert[i]);
227 // now we sneakily check the next character.
228 if (!in_hex_range(to_convert[i+1])) {
229 // we now know we should not be in this state for long.
231 // there's still some undigested stuff.
232 digits = 2; // fake a finished byte.
233 continue; // keep going, but eat the character we were at.
235 // well, there's nothing lost if we just jump to that state.
236 state = IGNORING_JUNK;
244 // snag the last unfinished bit.
245 as_array += abyte(accumulator);
249 void byte_formatter::bytes_to_string(const byte_array &to_convert, astring &as_string,
250 bool space_delimited)
252 bytes_to_string(to_convert.observe(), to_convert.length(), as_string,
256 void byte_formatter::string_to_bytes(const astring &to_convert, byte_array &as_array)
257 { string_to_bytes(to_convert.s(), as_array); }
259 void byte_formatter::bytes_to_shifted_string(const byte_array &to_convert, astring &as_string)
261 #ifdef DEBUG_BYTE_FORMAT
262 FUNCDEF("bytes_to_shifted_string");
264 bit_vector splitter(8 * to_convert.length(), to_convert.observe());
265 int i; // track our current position.
266 for (i = 0; i < splitter.bits(); i += 7) {
267 abyte curr = 1; // start with a bit set already.
268 for (int j = i; j < i + 7; j++) {
269 curr <<= 1; // move to the left.
270 if (j < splitter.bits())
271 curr |= abyte(splitter.on(j)); // or in the current position.
273 as_string += char(curr);
275 #ifdef DEBUG_BYTE_FORMAT
276 LOG(a_sprintf("%d bytes comes out as %d char string.",
277 to_convert.length(), as_string.length()).s());
281 void byte_formatter::shifted_string_to_bytes(const astring &to_convert, byte_array &as_array)
283 #ifdef DEBUG_BYTE_FORMAT
284 FUNCDEF("shifted_string_to_bytes");
286 bit_vector accumulator;
288 for (int i = 0; i < to_convert.length(); i++) {
289 abyte current = abyte(to_convert[i]) & 0x7F;
290 // get the current bits but remove the faux sign bit.
291 accumulator.resize(accumulator.bits() + 7);
292 // now shift off the individual pieces.
293 for (int j = 0; j < 7; j++) {
294 // get current bit's value.
295 current <<= 1; // shift it up.
296 abyte set_here = current & 0x80; // test the highest order bit.
297 // now flip that bit on or off based on what we saw.
298 accumulator.set_bit(i * 7 + j, bool(set_here));
302 int remainder = accumulator.bits() % 8;
303 accumulator.resize(accumulator.bits() - remainder);
304 // chop off any extraneous bits that are due to our shifting.
306 #ifdef DEBUG_BYTE_FORMAT
307 // there should be no remainder. and the number of bits should be a multiple
309 if (accumulator.bits() % 8)
310 deadly_error("byte_formatter", func, "number of bits is erroneous.");
313 const byte_array &accumref = accumulator;
314 for (int q = 0; q < accumulator.bits() / 8; q++)
315 as_array += accumref[q];
317 #ifdef DEBUG_BYTE_FORMAT
318 LOG(a_sprintf("%d chars comes out as %d bytes.",
319 to_convert.length(), as_array.length()).s());