What does this code do?

I found this code at a snippets archive (not DIC)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1820 Views - Last Post: 12 January 2010 - 06:33 PM Rate Topic: -----

#1 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

What does this code do?

Posted 07 December 2009 - 11:01 AM

// Implements Avatar display class
// public domain
// by David Nugent <davidn@unique.blaze.net.au>
// 3:632/348@fidonet

#include <iostream.h>
#include <ctype.h>
#include <string.h>

#include "avatar.hpp"

#define AVT_DEFAULT   3
#define AVT_DLE       0x10


avatar::avatar(video & v)
  : scrinterp(v),
    dispfunc(0),
    counter(-1),
    arg1(-1),
    rptlen(-1)
{}

void
avatar::reset()
{
  dispfunc = 0;
  counter = -1;
  arg1 = -1;
  insertmode = 0;
  rptlen = -1;
  scrinterp::reset();
}

  // This is the top level dispatcher. It really only
  // Looks for special control characters

#define ctrl(x)   (x-'@')

void
avatar::putch(int ch)
{
  if (dispfunc)
    (this->*dispfunc)(ch);
  else
  {
    switch (ch)
    {
      case ctrl('L'):             // cls
        flushbuf();
        vs.gotoxy(0, 0);
        vs.setattr(AVT_DEFAULT);
        vs.cls();
        insertmode = 0;
        break;

      case ctrl('Y'):             // repeat chr
        flushbuf();
        dispfunc = &avatar::rptchar;
        counter = -1;
        break;

      case ctrl('V'):             // Video
        flushbuf();
        dispfunc = &avatar::setvideo;
        break;

      default:
        scrinterp::putch(ch);
        break;
    }
  }
}

void
avatar::rptchar(int ch)
{
  if (counter == -1)
    counter = ch;
  else
  {
    dispfunc = 0;
    int count = ch;
    ch = counter;
    counter = -1;
    while (count-- > 0)
      putch(ch);
  }
}

void
avatar::setvideo(int ch)
{
  cell_t  x, y,
          mx, my;
  int     nx =0,
          ny =0;

  switch (ch)
  {
    case ctrl('A'):
      dispfunc = &avatar::setattr;
      arg1 = -1;
      break;
    case ctrl('B'):       // Blink
      vs.setbg(cell_t(vs.getbg() | 0x8));
      dispfunc = 0;
      break;
    case ctrl('C'):       // Row up
      ny = -1;
      goto mvxy;
    case ctrl('D'):       // Row down
      ny = 1;
      goto mvxy;
    case ctrl('E'):       // Column to left (erase)
      nx = -1;
      goto mvxy;
    case ctrl('F'):       // Column to right (forward)
      nx = 1;
    mvxy:
      vs.wherexy(x, y);
      x = cell_t(x + nx);
      y = cell_t(y + ny);
      vs.maxxy(mx, my);
      if (x < mx && y < my)
        vs.gotoxy(x, y);
      dispfunc = 0;
      break;
    case ctrl('H'):       // Set cursor position
      arg1 = -1;
      dispfunc = &avatar::setpos;
      break;
    case ctrl('I'):       // Toggle insertmode on
      insertmode = 1;
      dispfunc = 0;
      return;
    case ctrl('J'):       // scroll area up
    case ctrl('K'):       // scroll area down
      dispfunc = &avatar::scroll;
      arg1 = ch;          // Save type of scroll
      counter = -1;       // Used as index to area coord
      break;
    case ctrl('L'):       // Clear area to attribute
      dispfunc = &avatar::clrarea;
      counter = -1;
      break;
    case ctrl('M'):
      dispfunc = &avatar::setarea;
      counter = -1;
      break;
    case ctrl('N'):
      // unsupported
      dispfunc = 0;
      break;
    case ctrl('G'):       // Clear to eol
      vs.wherexy(x, y);
      vs.maxxy(mx, my);
      vs.repchr(' ', mx - x);
      vs.gotoxy(x, y);
      dispfunc = 0;
      break;
    case ctrl('Y'):       // Repeat sequence
      dispfunc = &avatar::rptseq;
      counter = rptlen = -1;
      return;   // Don't reset insertmode
  }
  insertmode = 0;
}

void
avatar::resetattr(int ch)
{
  vs.setattr(cell_t(ch & 0x7f));
  dispfunc = 0;
  arg1 = -1;
}

void
avatar::setattr(int ch)
{
  if (arg1 == -1)         // No DLE
  {
    if (ch == AVT_DLE)    // Got a DLE
    {
      arg1 = 0;           // Call with next byte, and do ESC
      return;
    }
    resetattr(ch);
  }
  else    // Previous char was DLE escaped
  {
    if (ch & 0x80)        // This has a hi-bit
      resetattr(ch);      // So displaying is ok
    else
    {
      resetattr(AVT_DLE); // Else use DLE as attribute
      putch(ch);          // And redisplay this character
    }
  }
}

void
avatar::setpos(int ch)
{
  if (arg1 == -1)
    arg1 = ch;
  else
  {
    cell_t  x = cell_t(ch),
            y = cell_t(arg1);
    cell_t  mx, my;
    vs.maxxy(mx, my);
    --x; --y; // Avatar uses 1-based coords
    if (x < mx && y < my)
      vs.gotoxy(x, y);
    counter = arg1 = -1;
    dispfunc = 0;
  }
}

void
avatar::rptseq(int ch)
{
  if (rptlen == -1)             // Counter
  {
    counter = (ch & 0xff);
    ++rptlen;
  }
  else if (rptlen == counter)   // We've reached the end
  {
    int count = ch;             // Save these locally so we don't run into
    int len = rptlen;           // reentrancy problems (but recusion problems, yes!)
    rptlen = 0;                 // Reset everything to norm
    dispfunc = 0;
    arg1 = counter = -1;
    if (len)                    // For zero count, we can ignore the lot
    {
      char tmp[256];            // Make local copy of repeated sequence too
      memcpy(tmp, rptbuf, len); // Now, output the whole thing 'count' times
      for (int times = 0; times < count; times++)
        for (int idx = 0; idx < len; idx++)
          putch(tmp[idx]);
    }
  }                             // Just storing repeated sequence
  else rptbuf[rptlen++] = char(ch);
}

void
avatar::scroll(int ch)
{
  area[counter++] = cell_t(ch);   // Store data
  if (counter == 5)
  {
    #define s_lines area[0]
    #define s_upper area[1]
    #define s_left  area[2]
    #define s_lower area[3]
    #define s_right area[4]
    vs.scroll(--s_left, --s_upper, --s_right, --s_lower, s_lines, arg1 == ctrl('J'));
    rptlen = arg1 = counter = -1;
    dispfunc = 0;
  }
}

void
avatar::clrarea(int ch)
{
  area[counter++] = cell_t(ch);     // Store data
  if (counter == 3)
  {
    #define cl_attr  area[0]
    #define cl_rows  area[1]
    #define cl_cols  area[2]
    vs.setattr(cl_attr);
    cell_t x, y;
    vs.wherexy(x, y);
    vs.scroll(x, y, cell_t(x + cl_cols - 1), cell_t(y + cl_rows - 1), cl_rows, 1);
    rptlen = arg1 = counter = -1;
    dispfunc = 0;
  }
}

void
avatar::setarea(int ch)
{
  area[counter++] = cell_t(ch);   // Store data
  if (counter == 4)
  {
    #define sa_attr  area[0]
    #define sa_char  area[1]
    #define sa_rows  area[2]
    #define sa_cols  area[3]
    vs.setattr(sa_attr);
    vs.setfill(sa_char);
    cell_t x, y;
    vs.wherexy(x, y);
    vs.scroll(x, y, cell_t(x + sa_cols - 1), cell_t(y + sa_rows - 1), sa_rows, 1);
    vs.setfill(' ');
    rptlen = arg1 = counter = -1;
    dispfunc = 0;
  }
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 07 December 2009 - 11:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: What does this code do?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: What does this code do?

Posted 07 December 2009 - 11:04 AM

theres no way of know with out the header for all of those functions
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,165
  • Joined: 14-September 07

Re: What does this code do?

Posted 07 December 2009 - 11:06 AM

You couldn't even use code tags?
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: What does this code do?

Posted 07 December 2009 - 11:11 AM

The best way to find out what some code does is to compile and run it for yourself
Was This Post Helpful? 0
  • +
  • -

#5 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

Re: What does this code do?

Posted 07 December 2009 - 01:18 PM

View PostKYA, on 7 Dec, 2009 - 10:06 AM, said:

You couldn't even use code tags?


Sorry, just forgot, new to DIC
Was This Post Helpful? 0
  • +
  • -

#6 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

Re: What does this code do?

Posted 07 December 2009 - 01:25 PM

View PostBench, on 7 Dec, 2009 - 10:11 AM, said:

The best way to find out what some code does is to compile and run it for yourself


I'd like to, but the compile fails, and I can't figure out why. Errors are included in the attachment in .jpg format. Also including .cpp and .hpp files

// Implements Avatar display class
// public domain
// by David Nugent <davidn@unique.blaze.net.au>
// 3:632/348@fidonet

#include <iostream.h>
#include <ctype.h>
#include <string.h>

#include "avatar.hpp"

#define AVT_DEFAULT   3
#define AVT_DLE	   0x10


avatar::avatar(video & v)
  : scrinterp(v),
	dispfunc(0),
	counter(-1),
	arg1(-1),
	rptlen(-1)
{}

void
avatar::reset()
{
  dispfunc = 0;
  counter = -1;
  arg1 = -1;
  insertmode = 0;
  rptlen = -1;
  scrinterp::reset();
}

  // This is the top level dispatcher. It really only
  // Looks for special control characters

#define ctrl(x)   (x-'@')

void
avatar::putch(int ch)
{
  if (dispfunc)
	(this->*dispfunc)(ch);
  else
  {
	switch (ch)
	{
	  case ctrl('L'):			 // cls
		flushbuf();
		vs.gotoxy(0, 0);
		vs.setattr(AVT_DEFAULT);
		vs.cls();
		insertmode = 0;
		break;

	  case ctrl('Y'):			 // repeat chr
		flushbuf();
		dispfunc = &avatar::rptchar;
		counter = -1;
		break;

	  case ctrl('V'):			 // Video
		flushbuf();
		dispfunc = &avatar::setvideo;
		break;

	  default:
		scrinterp::putch(ch);
		break;
	}
  }
}

void
avatar::rptchar(int ch)
{
  if (counter == -1)
	counter = ch;
  else
  {
	dispfunc = 0;
	int count = ch;
	ch = counter;
	counter = -1;
	while (count-- > 0)
	  putch(ch);
  }
}

void
avatar::setvideo(int ch)
{
  cell_t  x, y,
		  mx, my;
  int	 nx =0,
		  ny =0;

  switch (ch)
  {
	case ctrl('A'):
	  dispfunc = &avatar::setattr;
	  arg1 = -1;
	  break;
	case ctrl('B'):	   // Blink
	  vs.setbg(cell_t(vs.getbg() | 0x8));
	  dispfunc = 0;
	  break;
	case ctrl('C'):	   // Row up
	  ny = -1;
	  goto mvxy;
	case ctrl('D'):	   // Row down
	  ny = 1;
	  goto mvxy;
	case ctrl('E'):	   // Column to left (erase)
	  nx = -1;
	  goto mvxy;
	case ctrl('F'):	   // Column to right (forward)
	  nx = 1;
	mvxy:
	  vs.wherexy(x, y);
	  x = cell_t(x + nx);
	  y = cell_t(y + ny);
	  vs.maxxy(mx, my);
	  if (x < mx && y < my)
		vs.gotoxy(x, y);
	  dispfunc = 0;
	  break;
	case ctrl('H'):	   // Set cursor position
	  arg1 = -1;
	  dispfunc = &avatar::setpos;
	  break;
	case ctrl('I'):	   // Toggle insertmode on
	  insertmode = 1;
	  dispfunc = 0;
	  return;
	case ctrl('J'):	   // scroll area up
	case ctrl('K'):	   // scroll area down
	  dispfunc = &avatar::scroll;
	  arg1 = ch;		  // Save type of scroll
	  counter = -1;	   // Used as index to area coord
	  break;
	case ctrl('L'):	   // Clear area to attribute
	  dispfunc = &avatar::clrarea;
	  counter = -1;
	  break;
	case ctrl('M'):
	  dispfunc = &avatar::setarea;
	  counter = -1;
	  break;
	case ctrl('N'):
	  // unsupported
	  dispfunc = 0;
	  break;
	case ctrl('G'):	   // Clear to eol
	  vs.wherexy(x, y);
	  vs.maxxy(mx, my);
	  vs.repchr(' ', mx - x);
	  vs.gotoxy(x, y);
	  dispfunc = 0;
	  break;
	case ctrl('Y'):	   // Repeat sequence
	  dispfunc = &avatar::rptseq;
	  counter = rptlen = -1;
	  return;   // Don't reset insertmode
  }
  insertmode = 0;
}

void
avatar::resetattr(int ch)
{
  vs.setattr(cell_t(ch & 0x7f));
  dispfunc = 0;
  arg1 = -1;
}

void
avatar::setattr(int ch)
{
  if (arg1 == -1)		 // No DLE
  {
	if (ch == AVT_DLE)	// Got a DLE
	{
	  arg1 = 0;		   // Call with next byte, and do ESC
	  return;
	}
	resetattr(ch);
  }
  else	// Previous char was DLE escaped
  {
	if (ch & 0x80)		// This has a hi-bit
	  resetattr(ch);	  // So displaying is ok
	else
	{
	  resetattr(AVT_DLE); // Else use DLE as attribute
	  putch(ch);		  // And redisplay this character
	}
  }
}

void
avatar::setpos(int ch)
{
  if (arg1 == -1)
	arg1 = ch;
  else
  {
	cell_t  x = cell_t(ch),
			y = cell_t(arg1);
	cell_t  mx, my;
	vs.maxxy(mx, my);
	--x; --y; // Avatar uses 1-based coords
	if (x < mx && y < my)
	  vs.gotoxy(x, y);
	counter = arg1 = -1;
	dispfunc = 0;
  }
}

void
avatar::rptseq(int ch)
{
  if (rptlen == -1)			 // Counter
  {
	counter = (ch & 0xff);
	++rptlen;
  }
  else if (rptlen == counter)   // We've reached the end
  {
	int count = ch;			 // Save these locally so we don't run into
	int len = rptlen;		   // reentrancy problems (but recusion problems, yes!)
	rptlen = 0;				 // Reset everything to norm
	dispfunc = 0;
	arg1 = counter = -1;
	if (len)					// For zero count, we can ignore the lot
	{
	  char tmp[256];			// Make local copy of repeated sequence too
	  memcpy(tmp, rptbuf, len); // Now, output the whole thing 'count' times
	  for (int times = 0; times < count; times++)
		for (int idx = 0; idx < len; idx++)
		  putch(tmp[idx]);
	}
  }							 // Just storing repeated sequence
  else rptbuf[rptlen++] = char(ch);
}

void
avatar::scroll(int ch)
{
  area[counter++] = cell_t(ch);   // Store data
  if (counter == 5)
  {
	#define s_lines area[0]
	#define s_upper area[1]
	#define s_left  area[2]
	#define s_lower area[3]
	#define s_right area[4]
	vs.scroll(--s_left, --s_upper, --s_right, --s_lower, s_lines, arg1 == ctrl('J'));
	rptlen = arg1 = counter = -1;
	dispfunc = 0;
  }
}

void
avatar::clrarea(int ch)
{
  area[counter++] = cell_t(ch);	 // Store data
  if (counter == 3)
  {
	#define cl_attr  area[0]
	#define cl_rows  area[1]
	#define cl_cols  area[2]
	vs.setattr(cl_attr);
	cell_t x, y;
	vs.wherexy(x, y);
	vs.scroll(x, y, cell_t(x + cl_cols - 1), cell_t(y + cl_rows - 1), cl_rows, 1);
	rptlen = arg1 = counter = -1;
	dispfunc = 0;
  }
}

void
avatar::setarea(int ch)
{
  area[counter++] = cell_t(ch);   // Store data
  if (counter == 4)
  {
	#define sa_attr  area[0]
	#define sa_char  area[1]
	#define sa_rows  area[2]
	#define sa_cols  area[3]
	vs.setattr(sa_attr);
	vs.setfill(sa_char);
	cell_t x, y;
	vs.wherexy(x, y);
	vs.scroll(x, y, cell_t(x + sa_cols - 1), cell_t(y + sa_rows - 1), sa_rows, 1);
	vs.setfill(' ');
	rptlen = arg1 = counter = -1;
	dispfunc = 0;
  }
}




Here's the header file

// Class to display AVATAR/0+
// public domain
// by David Nugent <davidn@unique.blaze.net.au>
// 3:632/348@fidonet

#if !defined(_avatar_h)
#define _avatar_h
#include "scrintrp.hpp"

class avatar : public scrinterp
{
  typedef void (avatar::*pdisp)(int ch);
  public:
	avatar(video & v);

	virtual void putch(int ch);
	virtual void reset();

  private:
	pdisp dispfunc;		 // Display function (state)

	int counter;
	int arg1;
	int rptlen;
	char rptbuf[256];
	cell_t area[5];

	void rptchar(int ch);
	void setvideo(int ch);
	void setattr(int ch);
	void setpos(int ch);
	void rptseq(int ch);
	void scroll(int ch);
	void clrarea(int ch);
	void setarea(int ch);

	void resetattr(int ch);
};

#endif



Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: What does this code do?

Posted 07 December 2009 - 01:56 PM

It looks like that code was meant to be compiled on Borland with the graphics.h header file but I'm not sure.
Was This Post Helpful? 1
  • +
  • -

#8 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

Re: What does this code do?

Posted 10 January 2010 - 12:52 AM

Eh, I'll just let it be and make my own programs.
Was This Post Helpful? 0
  • +
  • -

#9 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 529
  • Joined: 10-October 09

Re: What does this code do?

Posted 10 January 2010 - 03:22 PM

maybe you could e-mail david nugent and ask him? ;)
Was This Post Helpful? 1
  • +
  • -

#10 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: What does this code do?

Posted 10 January 2010 - 07:13 PM

Maybe I'm just paranoid, but I wouldn't compile and execute any code that I didn't either write or trust. I mean, my code is unpredictable enough....

This post has been edited by Delta_Echo: 10 January 2010 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 529
  • Joined: 10-October 09

Re: What does this code do?

Posted 10 January 2010 - 07:22 PM

lol @ Delta Echo im right there with you bud. :)

A lot of code out there is meant for malicious purposes, so you should watch out :)
Was This Post Helpful? 0
  • +
  • -

#12 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

Re: What does this code do?

Posted 11 January 2010 - 12:33 AM

How true, though as I looked through it, I didn't see anything that looked like it would be malicious. But thanks, I think I'll just let this topic fade into obscurity.
Was This Post Helpful? 0
  • +
  • -

#13 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Re: What does this code do?

Posted 11 January 2010 - 03:18 AM

View Postoptix212, on 10 Jan, 2010 - 06:22 PM, said:

A lot of code out there is meant for malicious purposes, so you should watch out :)


Tell me who the hell would offer his sourcecode of a malicious software? If I offer my sourcecodes, I am aware that only expert will use it. But well, we don't know the thread authors source and thereforce I would handle the code carefully.

@borninlioko: where have you got the code snippet from? Without the header (already said) we cannot help you - and the code is not very clear, too.
Was This Post Helpful? 0
  • +
  • -

#14 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

Re: What does this code do?

Posted 11 January 2010 - 03:36 AM

The header is in here, a few posts down, and I'm just giving up on the code, I don't even care what it does any more.
Was This Post Helpful? 0
  • +
  • -

#15 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 529
  • Joined: 10-October 09

Re: What does this code do?

Posted 11 January 2010 - 10:17 AM

@ Metropoler

check out milw0rm.com, and you will see that there are alot of authors out there that write for malicious purposes.

In this case, you can easily see that it is not malicious code because the author gives his name and e-mail, but in other cases, people are not so lucky.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2