Exemplo n.º 1
0
int find_keyword(const char* inbytes, size_t* length, const language_zh_map *m, int begin, int end, const int whence)
{
	int location, offset;
	size_t wwidth, nwidth;

	if((offset = binary_find(inbytes, length, m, begin, end)) == -1)
		return -1;

	/* match the most accurate value */
	wwidth = *length;
	do{
		location = offset;
		*length  = wwidth;
		nwidth   = utf8_char_width(const_bin_c_str(inbytes+wwidth));
		wwidth  += nwidth;
	}
	while(nwidth != 0 && (offset = binary_find(inbytes, &wwidth, m, offset, end)) != -1);

	/* extention word fix */
	if(!match_cond(cond_ptr(m, location), inbytes, strlen(map_key(m, location)), whence))
	{
		*length = utf8_char_width(const_bin_c_str(inbytes));
		return -1;
	}

	return location;
}
Exemplo n.º 2
0
/* {{{ int binary_find(cconv_t cd, const char* inbytes, int length, int begin, int end) */
int binary_find(const char* inbytes, size_t* length, const language_zh_map *m, int begin, int end)
{
	int middle, last, next_fix = 0;
	int ret, offset = -1;
	size_t width, wwidth, nwidth;

	middle = (begin + end) >> 1;
	width  = *length;
	last   = end;	
	while(1)
	{
		ret = memcmp(m[middle].key, inbytes, width);
		if(ret == 0)
		{
			if(width == strlen(m[middle].key))
				return middle;

			/* word key */
			if(next_fix == 0)
			{
				nwidth = utf8_char_width(const_bin_c_str(inbytes+width));
				wwidth = width + nwidth;
				if(nwidth != 0 && memcmp(m[middle].key, inbytes, wwidth) <= 0)
				{
					while (nwidth != 0
						&& (offset = binary_find(inbytes, &wwidth, m, offset, end)) != -1)
					{
						if(wwidth == strlen(m[offset].key))
							return offset;

						nwidth = utf8_char_width(const_bin_c_str(inbytes+width));
						wwidth += nwidth;
					}

					next_fix = 1;
				}
			}
			ret = 1;
		}

		if(ret > 0)
		{
			end = middle - 1;
			middle = (begin + end) >> 1;
		}
		else if(0 > ret)
Exemplo n.º 3
0
size_t cconv_utf8(const char** inbuf, size_t* inleft, char**  outbuf, size_t* outleft, const language_zh_map *m, int map_size)
{
	const char *ps_inbuf;
	char *ps_outbuf;
	int index;
	size_t i_proc, o_proc, i_conv = 0, o_conv;

	ps_inbuf  = *inbuf;
	ps_outbuf = *outbuf;
	for (; *inleft > 0 && *outleft > 0; )
	{
		if((i_proc = utf8_char_width(const_bin_c_str(ps_inbuf))) > *inleft)
			break;

		if(i_proc > 1 &&
		  (index = find_keyword(ps_inbuf, &i_proc, m, 0, map_size - 1, i_conv)) != -1)
		{
			o_proc = strlen(map_val(m, index));
			memcpy(ps_outbuf, map_val(m, index), o_proc);
			ps_inbuf  += i_proc;
			ps_outbuf += o_proc;
			*inleft   -= i_proc;
			*outleft  -= o_proc;
			i_conv    += i_proc;
			continue;
		}
		
		if(i_proc == -1)
		{
			errno  = EINVAL;
			return (size_t)(-2);
		}

		memcpy(ps_outbuf, ps_inbuf, i_proc);
		ps_inbuf  += i_proc;
		ps_outbuf += i_proc;
		*inleft   -= i_proc;
		*outleft  -= i_proc;
		i_conv    += i_proc;
	}

	o_conv = ps_outbuf - *outbuf;
	*ps_outbuf = '\0';
	*inbuf  = ps_inbuf;
	*outbuf = ps_outbuf;
	return o_conv;
}
Exemplo n.º 4
0
static enum mu_filter_result
_xml_encoder (void *xd,
	      enum mu_filter_command cmd,
	      struct mu_filter_io *iobuf)
{
  struct xml_encode_state *cp = xd;
  const unsigned char *iptr;
  size_t isize;
  char *optr;
  size_t osize;
  
  switch (cmd)
    {
    case mu_filter_init:
      cp->idx = -1;
      return mu_filter_ok;
      
    case mu_filter_done:
      return mu_filter_ok;
      
    default:
      break;
    }

  iptr = (unsigned char*) iobuf->input;
  isize = iobuf->isize;
  optr = iobuf->output;
  osize = iobuf->osize;

  while (osize)
    {
      if (cp->idx > 0)
	{
	  *optr++ = cp->buf[--cp->idx];
	  --osize;
	}
      else if (isize == 0)
	break;
      else
	{
	  struct transcode_map *p;
	  unsigned int c = *iptr;

	  p = ch2ent (c);
	  if (p)
	    {
	      store_buf (cp, p->ent, p->len);
	      ++iptr;
	      --isize;
	    }
	  else
	    {
	      int count = utf8_char_width (c);

	      if (count == 0)
		{
		  store_char_ref (cp, *iptr);
		  ++iptr;
		  --isize;
		}
	      else if (count > isize)
		{
		  if (cmd == mu_filter_lastbuf)
		    {
		      store_buf (cp, (char*) iptr, isize);
		      iptr += isize;
		      isize = 0;
		    }
		  else
		    break;
		}
	      else
		{
		  unsigned wc;
		  int rc;

		  rc = utf8_mbtowc (iptr, isize, &wc);
		  
		  if (rc == -1)
		    {	
		      store_char_ref (cp, *iptr);
		      ++iptr;
		      --isize;
		    }
		  else
		    {
/* http://www.w3.org/TR/xml/#dt-charref:

   Character Range

   Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] |
            [#xE000-#xFFFD] | [#x10000-#x10FFFF]

   (any Unicode character, excluding the surrogate blocks, FFFE, and FFFF)
*/
		      if (wc == 0x9
			  || wc == 0xa
			  || wc == 0xd
			  || (wc >= 0x20 && wc < 0xd7ff)
			  || (wc >= 0xe000 && wc < 0xfffd)
			  || (wc >= 0x10000 && wc < 0x10FFFF))
			{
			  if (osize >= count)
			    {
			      memcpy (optr, iptr, count);
			      
			      optr += count;
			      osize -= count;

			      iptr += count;
			      isize -= count;
			    }
			  else
			    {
			      store_buf (cp, (char*) iptr, count);
			      iptr += count;
			      isize -= count;
			    }
			}
		      else
			{
                          store_char_ref (cp, wc);
			  iptr += count;
			  isize -= count;
			}
		    }
		}
	    }
	}
    }
  iobuf->isize -= isize;
  iobuf->osize -= osize;

  return mu_filter_ok;
}
Exemplo n.º 5
0
void
_strip_html( Stripper * stripper, char * raw, char * output, int is_utf8_p ) {
  char * p_raw = raw;
  char * raw_end = raw + strlen(raw);
  char * p_output = output;
  int width;

  if( stripper->o_debug ) {
      printf( "[DEBUG] input string: %s\n", p_raw );
  }

  while( p_raw < raw_end ) {
    width = is_utf8_p ? utf8_char_width(p_raw) : 1;
    // either a single char or a set of unicode code points

    if( stripper->o_debug ) {
      printf( "[DEBUG] char:%C w%i state:%c%c%c tag:%5s last:%c%c%c%c in:%c%c%c quote:%c ",
        *p_raw,
        width,
        (stripper->f_closing ? 'C' : ' '),
        (stripper->f_in_tag ? 'T' : ' '),
        (stripper->f_full_tagname ? 'F' : ' '),
        stripper->tagname,
        (stripper->f_just_seen_tag ? 'T' : ' '),
        (stripper->f_outputted_space ? 'S' : ' '),
        (stripper->f_lastchar_slash ? '/' : ' '),
        (stripper->f_lastchar_minus ? '-' : ' '),
        (stripper->f_in_decl ? 'D' : ' '),
        (stripper->f_in_comment ? 'C' : ' '),
        (stripper->f_in_striptag ? 'X' : ' '),
        (stripper->f_in_quote ? stripper->quote : ' ')
      );
    }

    if( stripper->f_in_tag ) {
      /* inside a tag */
      /* check we don't know either the tagname, or that we're in a declaration */
      if( !stripper->f_full_tagname && !stripper->f_in_decl ) {
        /* if this is the first character, check if it's a '!'; if so, we're in a declaration */
        if( stripper->p_tagname == stripper->tagname && *p_raw == '!' ) {
          stripper->f_in_decl = 1;
        }
        /* then check if the first character is a '/', in which case, this is a closing tag */
        else if( stripper->p_tagname == stripper->tagname && *p_raw == '/' ) {
          stripper->f_closing = 1;
        }
        /* if the first character wasn't a '/', and we're in a stripped block,
         * assume any previous '<' was a mathematical operator and reset */
        else if( !stripper->f_closing && stripper->f_in_striptag && stripper->p_tagname == stripper->tagname && *p_raw != '/' ) {
          stripper->f_in_tag = 0;
          stripper->f_closing = 0;
        /* within a stripped tags block (e.g. scripts), we only care about closing tags
         * within normal tags, we care about both opening and closing tags */
        } else if( !stripper->f_in_striptag || stripper->f_closing ) {
          /* if we don't have the full tag name yet, add p_raw character unless it's whitespace, a '/', or a '>';
             otherwise null pad the string and set the full tagname flag, and check the tagname against stripped ones.
             also sanity check we haven't reached the array bounds, and truncate the tagname here if we have */
          if( (!isspace( *p_raw ) && *p_raw != '/' && *p_raw != '>') &&
              !( (stripper->p_tagname - stripper->tagname) == MAX_TAGNAMELENGTH ) ) {
            *stripper->p_tagname++ = *p_raw;
          } else {
            *stripper->p_tagname = 0;
            stripper->f_full_tagname = 1;
            /* if we're in a stripped tag block, and this is a closing tag, check to see if it ends the stripped block */
            if( stripper->f_in_striptag && stripper->f_closing ) {
              if( strcasecmp( stripper->tagname, stripper->striptag ) == 0 ) {
                stripper->f_in_striptag = 0;
              }
              /* if we're outside a stripped tag block, check if tagname represents a newline,
               * then check tagname against stripped tag list */
            } else if( !stripper->f_in_striptag && !stripper->f_closing ) {
              if( strcasecmp( stripper->tagname, "p" ) ||
                  strcasecmp( stripper->tagname, "br" ) ) {
                if( stripper->o_emit_newlines ) {
                  if( stripper->o_debug ) {
                    printf("NEWLINE ");
                  }
                  *p_output++ = '\n';
                  stripper->f_outputted_space = 1;
                }
              }
              int i;
              for( i = 0; i < stripper->numstriptags; i++ ) {
                if( strcasecmp( stripper->tagname, stripper->o_striptags[i] ) == 0 ) {
                  stripper->f_in_striptag = 1;
                  strcpy( stripper->striptag, stripper->tagname );
                  break;
                }
              }
            }
            check_end( stripper, *p_raw );
          }
        }
      }
      /* we know the tagname, or that we're in a decl */
      else {
        if( stripper->f_in_quote ) {
          /* inside a quote */
          /* end of quote if p_raw character matches the opening quote character */
          if( *p_raw == stripper->quote ) {
            stripper->quote = 0;
            stripper->f_in_quote = 0;
          }
        } else {
          /* not in a quote */
          /* check for quote characters, but not in a comment */
          if( !stripper->f_in_comment &&
              ( *p_raw == '\'' || *p_raw == '\"' ) ) {
            stripper->f_in_quote = 1;
            stripper->quote = *p_raw;
            /* reset lastchar_* flags in case we have something perverse like '-"' or '/"' */
            stripper->f_lastchar_minus = 0;
            stripper->f_lastchar_slash = 0;
          } else {
            if( stripper->f_in_decl ) {
              /* inside a declaration */
              if( stripper->f_lastchar_minus ) {
                /* last character was a minus, so if p_raw one is, then we're either entering or leaving a comment */
                if( *p_raw == '-' ) {
                  stripper->f_in_comment = !stripper->f_in_comment;
                }
                stripper->f_lastchar_minus = 0;
              } else {
                /* if p_raw character is a minus, we might be starting a comment marker */
                if( *p_raw == '-' ) {
                  stripper->f_lastchar_minus = 1;
                }
              }
              if( !stripper->f_in_comment ) {
                check_end( stripper, *p_raw );
              }
            } else {
              check_end( stripper, *p_raw );
            }
          } /* quote character check */
        } /* in quote check */
      } /* full tagname check */
    }
    else {
      /* not in a tag */
      /* check for tag opening, and reset parameters if one has */
      if( *p_raw == '<' ) {
        stripper->f_in_tag = 1;
        stripper->tagname[0] = 0;
        stripper->p_tagname = stripper->tagname;
        stripper->f_full_tagname = 0;
        stripper->f_closing = 0;
        stripper->f_just_seen_tag = 1;
      }
      else {
        /* copy to stripped provided we're not in a stripped block */
        if( !stripper->f_in_striptag ) {
          /* only emit spaces if we're configured to do so (on by default) */
          if( stripper->o_emit_spaces ){
            /* output a space in place of tags we have previously parsed,
               and set a flag so we only do this once for every group of tags.
               done here to prevent unnecessary trailing spaces */
            if( !isspace(*p_raw) &&
              /* don't output a space if this character is one anyway */
                !stripper->f_outputted_space &&
                stripper->f_just_seen_tag ) {
              if( stripper->o_debug ) {
                printf("SPACE ");
              }
              *p_output++ = ' ';
              stripper->f_outputted_space = 1;
            }
          }
          strncpy(p_output, p_raw, width);
          if( stripper->o_debug ) {
              printf("CHAR %c", *p_raw);
          }
          p_output += width;

          /* reset 'just seen tag' flag */
          stripper->f_just_seen_tag = 0;
          /* reset 'outputted space' flag if character is not one */
          if (!isspace(*p_raw)) {
            stripper->f_outputted_space = 0;
          } else {
            stripper->f_outputted_space = 1;
          }
        }
      }
    } /* in tag check */
    p_raw += width;
    if( stripper->o_debug ) {
      printf("\n");
    }
  } /* while loop */

  *p_output = 0;

  if (stripper->o_auto_reset) {
    _reset( stripper );
  }
}