| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 1.91

 

http_encode_meta

#include "sflhttp.h"
size_t
http_encode_meta (
    char       *output,
    const char *input,
    size_t      outmax)

Synopsis

Translates special characters into HTML/SGML metacharacters. The input buffer is not modified; you supply an output buffer and specify the maximum size of this buffer. The input buffer must end in a null. Returns the final size of the translated data excluding the final null byte. If the resulting data is too long, it is brutally truncated.

Source Code - (sflhttp.c)

{
    /*  This lookup table provides a translation string for each byte
     *  in the character set.  We assume 8-bit characters.  When the
     *  string is NULL, the character is copied without translation.
     */
    static char
        *meta [256];                    /*  Metacharacter translation table  */
    static Bool
        first_time = TRUE;              /*  First time flag                  */
    int
        char_index;                     /*  Index into translation table     */
    size_t
        space_left;                     /*  Space left in destination        */
    const char
        *source;                        /*  Pointer to source string         */
    char
        *dest,                          /*  Pointer to result string         */
        *meta_char;                     /*  Pointer through metachar string  */

    ASSERT (input);
    ASSERT (output);

#   define OUTPUT(c)  if (!space_left) ; else { *dest++ = (c); space_left--; }

    /*  Initialise translation table first time through                      */
    if (first_time)
      {
        first_time = FALSE;
        for (char_index = 0; char_index < 256; char_index++)
            meta [char_index] = NULL;

#if (defined (__UNIX__) || defined (__WINDOWS__))
        /*  UNIX and Windows generally use ISO-8859-1 (Latin-1)              */
        meta [0xC4] = "Auml";
        meta [0xC5] = "Aring";
        meta [0xC6] = "AElig";
        meta [0xD6] = "Ouml";
        meta [0xDC] = "Uuml";
        meta [0xE0] = "agrave";
        meta [0xE1] = "aacute";
        meta [0xE2] = "acirc";
        meta [0xE4] = "auml";
        meta [0xE5] = "aring";
        meta [0xE6] = "aelig";
        meta [0xE7] = "ccedil";
        meta [0xE8] = "egrave";
        meta [0xE9] = "eacute";
        meta [0xEA] = "ecirc";
        meta [0xEB] = "euml";
        meta [0xEC] = "igrave";
        meta [0xED] = "iacute";
        meta [0xEE] = "icirc";
        meta [0xEF] = "iuml";
        meta [0xF2] = "ograve";
        meta [0xF3] = "oacute";
        meta [0xF4] = "ocirc";
        meta [0xF6] = "ouml";
        meta [0xF9] = "ugrave";
        meta [0xFA] = "uacute";
        meta [0xFB] = "ucirc";
        meta [0xFC] = "uuml";
        meta [0xFD] = "yuml";

#elif (defined (__MSDOS__))
        /*  DOS generally uses the PC-1 alphabet                             */
        meta [0x80] = "uuml";
        meta [0x82] = "eacute";
        meta [0x83] = "acirc";
        meta [0x84] = "auml";
        meta [0x85] = "agrave";
        meta [0x86] = "aring";
        meta [0x87] = "ccedil";
        meta [0x88] = "ecirc";
        meta [0x89] = "euml";
        meta [0x8A] = "egrave";
        meta [0x8B] = "iuml";
        meta [0x8C] = "icirc";
        meta [0x8D] = "igrave";
        meta [0x8E] = "Auml";
        meta [0x2F] = "Aring";
        meta [0x91] = "aelig";
        meta [0x92] = "AElig";
        meta [0x93] = "ocirc";
        meta [0x94] = "ouml";
        meta [0x95] = "ograve";
        meta [0x96] = "ucirc";
        meta [0x97] = "ugrave";
        meta [0x98] = "yuml";
        meta [0x99] = "Ouml";
        meta [0x9A] = "Uuml";
        meta [0xA0] = "aacute";
        meta [0xA1] = "iacute";
        meta [0xA2] = "oacute";
        meta [0xA3] = "uacute";
#endif
      }
    if (outmax == 0)                    /*  Special case for zero space      */
        return (0);

    space_left = outmax - 1;            /*  Allow for final null byte        */
    dest = output;
    for (source = input; *source; source++)
      {
        meta_char = meta [(int) *source & 255];
        if (meta_char)
          {
            OUTPUT ('&');
            while (*meta_char)
              {
                OUTPUT (*meta_char);
                meta_char++;
              }
            OUTPUT (';');
          }
        else
            OUTPUT (*source);
      }
    *dest = '\0';
    return ((size_t) (dest - output));
}

| << | < | > | >> iMatix Copyright © 1996-98 iMatix