/******************************************************************************/
/*                       Copyright 2001 Broadcom Corporation                  */
/*                                                                            */
/* This material is the confidential trade secret and proprietary information */
/* of Broadcom Corp.  It may not be reproduced, used, sold or transferred to  */
/* any third party without the prior written consent of Broadcom Corp.        */
/* All rights reserved.                                                       */
/******************************************************************************/
/*   FileName        : bcm_asciCodec.c                                        */
/*   Purpose         : simple encoding & decoding for telnet use (7 bits)     */
/*   Limitations     : None                                                   */
/*   Author          : Emmanuel Hanssens                                      */
/*   Creation Date   : 19-Dec-2001                                            */
/*   History         : Compiled by CVS                                        */
/******************************************************************************/
/*   $Id: bcm_asciCodec.c,v 1.4 2003/11/05 17:07:53 hanssens Exp $  */
/******************************************************************************/

#include <stdio.h>
#include <string.h>
#include "bcm_asciCodec.h"

/* {{{ asciDecode */

uint32 asciDecode(uint8 *data, uint32 size, int prefix)
{
  uint32 newSize = 0, i = 0;
  uint8 *dest    = data;                /* decoding in place */
  
  /* skip line feed and return */
  while (*data == '\r' || *data == '\n')
  {
    data++;
    i++;
  }
  
  /* skip non valid line */
  if (strncmp((char *) data, "bcm_", 4) && prefix)
    return 0;

  while (i<size)
  {
    if (*data >= '0' && *data <= '9' + 6)
    {
      /* valid data byte => decode (next byte must also be valid) */
      *dest    =  (*data++ - '0');      /* least sign. nibble */
      *dest++ += ((*data   - '0')<<4);  /* most sign. nibble */
      newSize++;                        /* 1 dest byte computed */
      i++;
    }
    data++;
    i++;
  }
  /* return decoded size */
  return newSize;
}

/* }}} */
/* {{{ asciEncode */

uint32 asciEncode(uint8 *dest, uint8 *src, uint32 size, int prefix)
{
  uint32 newSize = 0, i;

  if (prefix)
  {
    *dest++ = 'b';
    *dest++ = 'c';
    *dest++ = 'm';
    *dest++ = '_';
    newSize+= 4;
  }
  
  for (i=0; i<size; i++)
  {
    *dest++ =  (*src   & 0x0F)     + '0';
    *dest++ = ((*src++ & 0xF0)>>4) + '0';
    newSize+=2;
  }

  /* add a return linefeed at the end */
  *dest++ = '\n';
  newSize++;
  *dest++ = 0x0A;                       /* linefeed */
  newSize++;

  return newSize;
}
/* }}} */
/* {{{ getline: get bytes from stdin until \n, and return length */

int getLine(char *s, int lim)
{
  int c=0,i;
  
  for (i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i)
    s[i]=c;
  if (c=='\n') s[i++]=c;
  s[i]='\0';
  return(i);
}

/* }}} */
