From da8416ad632e6394148104ab65e274b76decbc69 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sat, 12 Jul 2003 00:25:40 +0000 Subject: [PATCH] Added a byte_cursor class for forward reading of a memory area. --- src/common.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 19 ++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/common.cpp b/src/common.cpp index 1d0edec5b..dbaf9e33c 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -155,6 +155,76 @@ void bitvalue_c::generate_random() { value[i] = (unsigned char)(255.0 * rand() / RAND_MAX); } +// --------------------------------------------------------------------- + +byte_cursor_c::byte_cursor_c(const unsigned char *ndata, int nsize) : + size(nsize), + data(ndata) { + pos = 0; + if (nsize < 0) + throw exception(); +} + +unsigned char byte_cursor_c::get_byte() { + if ((pos + 1) > size) + throw exception(); + + pos++; + + return data[pos - 1]; +} + +unsigned short byte_cursor_c::get_word() { + unsigned short v; + + if ((pos + 2) > size) + throw exception(); + + v = data[pos]; + v = (v << 8) | (data[pos + 1] & 0xff); + pos += 2; + + return v; +} + +unsigned int byte_cursor_c::get_dword() { + unsigned int v; + + if ((pos + 4) > size) + throw exception(); + + v = data[pos]; + v = (v << 8) | (data[pos + 1] & 0xff); + v = (v << 8) | (data[pos + 2] & 0xff); + v = (v << 8) | (data[pos + 3] & 0xff); + pos += 4; + + return v; +} + +void byte_cursor_c::skip(int n) { + if ((pos + n) > size) + throw exception(); + + pos += n; +} + +void byte_cursor_c::get_bytes(unsigned char *dst, int n) { + if ((pos + n) > size) + throw exception(); + + memcpy(dst, &data[pos], n); + pos += n; +} + +int byte_cursor_c::get_pos() { + return pos; +} + +int byte_cursor_c::get_len() { + return size - pos; +} + /* * Control functions */ diff --git a/src/common.h b/src/common.h index baa5e614a..36bd0c57c 100644 --- a/src/common.h +++ b/src/common.h @@ -209,6 +209,25 @@ public: } }; +class byte_cursor_c { +private: + int pos, size; + const unsigned char *data; + +public: + byte_cursor_c(const unsigned char *ndata, int nsize); + + virtual unsigned char get_byte(); + virtual unsigned short get_word(); + virtual unsigned int get_dword(); + virtual void get_bytes(unsigned char *dst, int n); + + virtual void skip(int n); + + virtual int get_pos(); + virtual int get_len(); +}; + class bitvalue_c { private: unsigned char *value;