Source

blake / blake_contact.c

Full commit
/*
Copyright (C) 2001-2006 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/*
  *---------------------------------------------------------------------*
  * OverChat Blake - A communications protocol and helper library       *
  * by David Duncan Ross Palmer.                                        *
  *---------------------------------------------------------------------*
  * this module simply contains the authorititive version of the        *
  * contact information for anybody who has ever done any work for the  *
  * OverChat project.  overchatd displays it with the --contact flag    *
  * and tools/contact_info displays it for output to a CONTACT file.    *
  *---------------------------------------------------------------------*
*/

#define BLAKE_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/

#include <time.h>
#include <stdio.h>

#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif /*HAVE_LIMITS_H*/

#ifdef HAVE_STRING_H
# include <string.h>
#endif /*HAVE_STRING_H*/

#ifdef BLAKE_HDRSTOP
# pragma hdrstop
#endif /*BLAKE_HDRSTOP*/

#ifdef HAVE_DLSTDINC_H
# include "dlstdinc.h"
#endif

#ifdef HAVE_DPCRTLMM_H
# define USING_DPCRTLMM
# include "dpcrtlmm.h"
#endif /*HAVE_DPCRTLMM_H*/

#include "blake_threads.h"
#include "blake.h" /* Public lib header */
#include "blake_cb.h"
/*-------------------------------------------------------------------------*/
static const char *rcsid =
  "$Id: blake_contact.c,v 0.6 2007/01/14 16:00:00 ddrp Exp $";

static struct {
  const char* Email; /* Email address */
  const char* Name; /* Name of maintainer / author */
  const char* Tasks; /* Responsibillities */
} ContactInfo[] = {
  { "http://www.dagsylad.com",
    "Neil Dagger",
    "Basic port\0Sarah client\0"
  },
  { "http://www.netchaos.net/",
    "Craig Gosman",
    "Structure Query Language/Programming Consultant\0"
  },
  { "http://www.daybologic.co.uk/mailddrp/",
    "David Duncan Ross Palmer",
    "General server work\0Protocol\0Security\0OberChatten\0"
  },
  { "ccollect@juno.com",
    "Jeff M. Robeano",
    "Beta testing\0Security\0Bugs\0"
  },
  { "meanmogitta@yahoo.com",
    "Ken Upright",
    "OberChatten sound effects and glyphs\0"
  },
  { "smatkitten@yahoo.com",
    "Tammy Davis",
    "Logos\0"
  },
  { "http://www.digiserv.net/",
    "digiServ Web Solutions",
    "Client emoticons\0Web site\0"
  }
};

static void CopyStringToCaller(
  const char *Source,
  char *PBuffOut,
  const size_t MaxBuffSz
);

static void CopySpecialStringToCaller(
  const char *Source,
  char *PBuffOut,
  const size_t MaxBuffSz
);

static unsigned int SpecialStrLen(
  const char *SpecialString
);

static enum blakeError CheckParamErrors(
  const enum blakeContactInfoType,
  const unsigned int Index
);

/*-------------------------------------------------------------------------*/
static void CopyStringToCaller(
  const char *Source,
  char *PBuffOut,
  const size_t MaxBuffSz
)
{
  if ( PBuffOut && MaxBuffSz ) {
    PBuffOut[0] = '\0';
    strncat(PBuffOut, Source, MaxBuffSz-1);
  }
}
/*-------------------------------------------------------------------------*/
static void CopySpecialStringToCaller(
  const char *Source,
  char *PBuffOut,
  const size_t MaxBuffSz
)
{
  if ( PBuffOut && MaxBuffSz ) {
    unsigned int i;
    bool flag = false;
    PBuffOut[0] = '\0';

    for ( i = 0U; i < MaxBuffSz; i++ ) {
      PBuffOut[i] = Source[i];
      if ( flag ) {
        if ( (*(PBuffOut +i)) == '\0' )
          break;
        else
          flag = false;
      }
      else {
        if ( (*(PBuffOut +i)) == '\0' )
          flag = true;
        else
          flag = false;
      }
    }
  }
}
/*-------------------------------------------------------------------------*/
static unsigned int SpecialStrLen(
  const char *SpecialString
)
{
  unsigned int i = 0;
  bool flag = false;

  do {
    if ( flag ) { /* Expecting another NULL */
      if ( SpecialString[i] == '\0' ) {
        /* Two nulls next to each other, this means the end */
        break;
      }
      else
        flag = false;
    }
    else { /* Not expecting a NULL */
      if ( SpecialString[i] == '\0' )
        flag = true;
      else
        flag = false;
    }
    i++;
  } while ( 1 );

  return i;
}
/*-------------------------------------------------------------------------*/
static enum blakeError CheckParamErrors(
  const enum blakeContactInfoType ContactInfoType,
  const unsigned int Index
)
{
    blake_Unused(rcsid);

  if ( (ContactInfoType != blakeCitEmail) &&
       (ContactInfoType != blakeCitName) &&
       (ContactInfoType != blakeCitTasks)
  )
    return blakeInvalidParam;

  if ( Index >= sizeof(ContactInfo)/sizeof(ContactInfo[0]) )
    return blakeRange;

  return blakeSuccess;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_GetContactInfo(
  const enum blakeContactInfoType ContactInfoType,
  const unsigned int Index,
  char* OutBuff,
  const size_t MaxBuffSz
)
{
  enum blakeError err = CheckParamErrors(ContactInfoType, Index);
  const char* ptr = NULL;

  if ( !OutBuff ) return blakeInvalidParam;
  if ( err != blakeSuccess ) return err;

  if ( ContactInfoType == blakeCitEmail )
    ptr = ContactInfo[Index].Email;
  else if ( ContactInfoType == blakeCitName )
    ptr = ContactInfo[Index].Name;
  else if ( ContactInfoType == blakeCitTasks )
    ptr = ContactInfo[Index].Tasks;

  if ( ptr ) {
    if ( ContactInfoType == blakeCitTasks ) {
      if ( ((SpecialStrLen(ptr)+1) * sizeof(char)) > MaxBuffSz )
        return blakeBufferTooSmall;
      else
        CopySpecialStringToCaller(ptr, OutBuff, MaxBuffSz);
    }
    else { /* Single, simple string */
      if ( ((strlen(ptr)+1) * sizeof(char)) > MaxBuffSz )
        return blakeBufferTooSmall;
      else
        CopyStringToCaller(ptr, OutBuff, MaxBuffSz);
    }
  }
  else {
    blake_word32_t params[3];
    params[0] = (blake_word32_t)blake_Here(0);
    params[1] = (blake_word32_t)ptr;
    params[2] = (blake_word32_t)(*ptr);
    PANIC(
      BLAKESTOP_DDRP_LOGICASSERT,
      sizeof(params)/sizeof(params[0]),
      params
    );
  }

  return err;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_GetContactInfoLen(
  const enum blakeContactInfoType ContactInfoType,
  const unsigned int Index,
  unsigned int* PLen
)
{
  enum blakeError err = CheckParamErrors(ContactInfoType, Index);
  if ( !PLen ) err = blakeInvalidParam;
  if ( err != blakeSuccess ) return err;

  if ( ContactInfoType == blakeCitEmail )
    *PLen = strlen(ContactInfo[Index].Email);
  else if ( ContactInfoType == blakeCitName )
    *PLen = strlen(ContactInfo[Index].Name);
  else if ( ContactInfoType == blakeCitTasks )
    *PLen = strlen(ContactInfo[Index].Tasks);

  return err;
}
/*-------------------------------------------------------------------------*/
BLAKEAPI(enum blakeError) blake_GetContactInfoMaxLen(
  unsigned int* PLen
)
{
  static unsigned int maxlen;

  if ( !maxlen ) {
    unsigned int i;
    for ( i = 0U; i < sizeof(ContactInfo)/sizeof(ContactInfo[0]); i++ ) {
      unsigned int len = strlen(ContactInfo[i].Email);
      if ( len > maxlen ) maxlen = len;
      len = strlen(ContactInfo[i].Name);
      if ( len > maxlen ) maxlen = len;
      len = SpecialStrLen(ContactInfo[i].Tasks);
      if ( len > maxlen ) maxlen = len;
    }
  }

  if ( PLen ) {
    *PLen = maxlen;
    return blakeSuccess;
  }
  return blakeInvalidParam;
}
/*-------------------------------------------------------------------------*/