Commits

Anonymous committed fbe7a79

Steal some code from the lsw project (see lsw repository) @ r1

  • Participants

Comments (0)

Files changed (2)

+/*
+  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  % Shared support for CGI programs for OverChat.  Please put %
+  % anything which can be used by other CGI programs here to  %
+  % save future CGI programming efforts.                      %
+  %                                                           %
+  % (C) Copyright 2001 Daybo Logic, all rights reserved.      %
+  % Maintained by David Duncan Ross Palmer                    %
+  %                             <Overlord@DayboLogic.co.uk>   %
+  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  2nd October 2001    13:22:48 - Created
+  List ammendments and additions and a very quick note
+  14th November 2001  16:18:00 - overlord (RecoverSpaces)
+*/
+
+/*#include "overchat_config.h"*/
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <limits.h>
+#ifdef HDRSTOP
+# pragma hdrstop
+#endif
+
+typedef enum {
+  false,
+  true
+} bool;
+
+/*#include "ddrptype.h"*/
+#include "cgihelp.h"
+/*-------------------------------------------------------------------------*/
+static char* FindChrByURLRFC(char* URLStr, unsigned char CharNum);
+/*-------------------------------------------------------------------------*/
+char* cgihelp_ExtractCGIQuery(const char* QueryStringIn, const char* VarNameIn, size_t* PLengthOut)
+{
+  char* ret = NULL;
+
+  if ( PLengthOut )
+    *PLengthOut = 0U;
+
+  if ( QueryStringIn && VarNameIn ) {
+    ret = strstr(QueryStringIn, VarNameIn);
+    if ( ret ) {
+      char test;
+      ret += (strlen(VarNameIn)+1); /* Move past '=' */
+      test = *ret;
+      if ( test == '\0' || test == '&' ) {
+        ret = NULL;
+        return ret;
+      }
+      /* Get the length of the variable contents by finding the next NULL or amphersand */
+      if ( PLengthOut ) { /* Provided user wants it */
+        char* next = strchr(ret, '&');
+        if ( !next ) /* No amp? */
+          next = strchr(ret, '\0'); /* Try for this instead */
+
+        if ( next ) { /* Technically there must be, so this test is probally paranoia */
+          unsigned int distance = (unsigned int)next - (unsigned int)ret;
+          *PLengthOut = distance;
+        }
+      }
+    }
+  }
+  return ret;
+}
+/*-------------------------------------------------------------------------*/
+bool cgihelp_WebPage(const char* Filename)
+{
+  FILE* h;
+
+  if ( !Filename ) return false;
+  h = fopen(Filename, "rt");
+  if ( h ) {
+    int character;
+    do {
+      character = fgetc(h);
+      if ( character == EOF ) break;
+      fputc(character, stdout);
+    } while ( character != EOF );
+    fclose(h);
+    return true;
+  }
+  return false;
+}
+/*-------------------------------------------------------------------------*/
+bool cgihelp_RFCURL_RecoverChar(char* URL, unsigned char CharNum)
+{
+  char* pos = FindChrByURLRFC(URL, CharNum);
+  if ( pos ) { /* Anything to go on? */
+    *pos = (char)CharNum; /* Overwrite '%' */
+
+    strcpy(pos+1, pos+3); /* Shift up <- Overlapping regions.. will some RTL's play nasty? */
+    return true; /* Success */
+  }
+  return false; /* None to convert */
+}
+/*-------------------------------------------------------------------------*/
+char* cgihelp_RFCURL_RecoverAll(char* URLStr)
+{
+  unsigned char currentCharCode;
+
+  for ( currentCharCode = 0; currentCharCode < UCHAR_MAX; currentCharCode++ ) {
+    bool cont;
+
+    do {
+      cont = cgihelp_RFCURL_RecoverChar(URLStr, currentCharCode);
+    } while ( cont );
+  }
+  return URLStr;
+}
+/*-------------------------------------------------------------------------*/
+char* cgihelp_RFCURL_RecoverSpaces(char* URLStr)
+{
+  if ( URLStr ) {
+    char blah;
+    unsigned int i = 0U;
+
+    do {
+      blah = *(URLStr + i);
+      if ( blah == '+' ) { /* URLs use this for spaces */
+        blah = ' '; /* Change to space */
+        *(URLStr+i) = blah; /* Write back */
+      }
+      i++;
+    } while ( blah != '\0' );
+  }
+  return URLStr;
+}
+/*-------------------------------------------------------------------------*/
+static char* FindChrByURLRFC(char* URLStr, unsigned char CharNum)
+{
+  char searchStr[4];
+  char* ret;
+
+  assert(sizeof(CharNum)==1); /* Paranoia */
+  searchStr[0] = '%';
+  sprintf(searchStr+1, "%2x", (unsigned int)CharNum);
+  ret = strstr(URLStr, searchStr);
+  if ( ret ) return ret;
+  /* Now try uppercase hexes */
+  sprintf(searchStr+1, "%2X", (unsigned int)CharNum);
+  return strstr(URLStr, searchStr);
+}
+/*-------------------------------------------------------------------------*/
+/*
+  Include this file in CGI apps which can share functions with other CGI
+  apps.  See cgihelp.c for full copyright information.
+*/
+
+#ifndef __INC_CGIHELP_H
+#define __INC_CGIHELP_H
+/*-------------------------------------------------------------------------*/
+char* cgihelp_ExtractCGIQuery(const char* QueryStringIn, const char* VarNameIn, size_t* PLengthOut);
+bool cgihelp_WebPage(const char* Filename);
+bool cgihelp_RFCURL_RecoverChar(char* URL, unsigned char CharNum);
+char* cgihelp_RFCURL_RecoverAll(char* URLStr);
+char* cgihelp_RFCURL_RecoverSpaces(char* URLStr);
+/*-------------------------------------------------------------------------*/
+#endif /*!__INC_CGIHELP_H*/