Пример #1
0
static int
test_utf8( void )
{
    const char * in;
    char * out;

    in = "hello world";
    out = tr_utf8clean( in, -1 );
    check_streq (in, out);
    tr_free( out );

    in = "hello world";
    out = tr_utf8clean( in, 5 );
    check_streq ("hello", out);
    tr_free( out );

    /* this version is not utf-8 */
    in = "Трудно быть Богом";
    out = tr_utf8clean( in, 17 );
    check( out != NULL );
    check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) );
    check( tr_utf8_validate( out, -1, NULL ) );
    tr_free( out );

    /* same string, but utf-8 clean */
    in = "Òðóäíî áûòü Áîãîì";
    out = tr_utf8clean( in, -1 );
    check( out != NULL );
    check( tr_utf8_validate( out, -1, NULL ) );
    check_streq (in, out);
    tr_free( out );

    return 0;
}
Пример #2
0
static bool
getfile (char ** setme, const char * root, tr_variant * path, struct evbuffer * buf)
{
  bool success = false;
  size_t root_len = 0;

  *setme = NULL;

  /* root's already been checked by caller */
  assert (!path_component_is_suspicious (root));

  if (tr_variantIsList (path))
    {
      int i;
      const int n = tr_variantListSize (path);

      success = true;
      evbuffer_drain (buf, evbuffer_get_length (buf));
      root_len = strlen (root);
      evbuffer_add (buf, root, root_len);

      for (i=0; i<n; i++)
        {
          size_t len;
          const char * str;

          if (!tr_variantGetStr (tr_variantListChild (path, i), &str, &len) ||
              path_component_is_suspicious (str))
            {
              success = false;
              break;
            }

          if (!*str)
            continue;

          evbuffer_add (buf, TR_PATH_DELIMITER_STR, 1);
          evbuffer_add (buf, str, len);
        }
    }

  if (success && (evbuffer_get_length (buf) <= root_len))
    {
      success = false;
    }

  if (success)
    {
      *setme = tr_utf8clean ((char*)evbuffer_pullup (buf, -1), evbuffer_get_length (buf));
      /*fprintf (stderr, "[%s]\n", *setme);*/
    }

  return success;
}
Пример #3
0
static int
test_utf8 (void)
{
  const char * in;
  char * out;

  in = "hello world";
  out = tr_utf8clean (in, TR_BAD_SIZE);
  check_streq (in, out);
  tr_free (out);

  in = "hello world";
  out = tr_utf8clean (in, 5);
  check_streq ("hello", out);
  tr_free (out);

  /* this version is not utf-8 (but cp866) */
  in = "\x92\xE0\xE3\xA4\xAD\xAE \xA1\xEB\xE2\xEC \x81\xAE\xA3\xAE\xAC";
  out = tr_utf8clean (in, 17);
  check (out != NULL);
  check ((strlen (out) == 17) || (strlen (out) == 33));
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  tr_free (out);

  /* same string, but utf-8 clean */
  in = "Трудно быть Богом";
  out = tr_utf8clean (in, TR_BAD_SIZE);
  check (out != NULL);
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  check_streq (in, out);
  tr_free (out);

  in = "\xF4\x00\x81\x82";
  out = tr_utf8clean (in, 4);
  check (out != NULL);
  check ((strlen (out) == 1) || (strlen (out) == 2));
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  tr_free (out);

  in = "\xF4\x33\x81\x82";
  out = tr_utf8clean (in, 4);
  check (out != NULL);
  check ((strlen (out) == 4) || (strlen (out) == 7));
  check (tr_utf8_validate (out, TR_BAD_SIZE, NULL));
  tr_free (out);

  return 0;
}
Пример #4
0
static tr_bool
getfile( char ** setme, const char * root, tr_benc * path )
{
    tr_bool success = FALSE;

    if( tr_bencIsList( path ) )
    {
        int i;
        char * tmp;
        const int n = tr_bencListSize( path );
        struct evbuffer * buf = evbuffer_new( );

        evbuffer_add( buf, root, strlen( root ) );
        for( i = 0; i < n; ++i )
        {
            const char * str;
            if( tr_bencGetStr( tr_bencListChild( path, i ), &str ) )
            {
                evbuffer_add( buf, TR_PATH_DELIMITER_STR, 1 );
                evbuffer_add( buf, str, strlen( str ) );
            }
        }

        tmp = evbuffer_free_to_str( buf );
        *setme = tr_utf8clean( tmp, -1 );
        tr_free( tmp );
        /* fprintf( stderr, "[%s]\n", *setme ); */
        success = TRUE;
    }

    if( ( *setme != NULL ) && path_is_suspicious( *setme ) )
    {
        tr_free( *setme );
        *setme = NULL;
        success = FALSE;
    }

    return success;
}
Пример #5
0
static bool
getfile (char ** setme, const char * root, tr_variant * path, struct evbuffer * buf)
{
  bool success = false;

  if (tr_variantIsList (path))
    {
      int i;
      const int n = tr_variantListSize (path);

      evbuffer_drain (buf, evbuffer_get_length (buf));
      evbuffer_add (buf, root, strlen (root));
      for (i=0; i<n; i++)
        {
          size_t len;
          const char * str;

          if (tr_variantGetStr (tr_variantListChild (path, i), &str, &len))
            {
              evbuffer_add (buf, TR_PATH_DELIMITER_STR, 1);
              evbuffer_add (buf, str, len);
            }
        }

      *setme = tr_utf8clean ((char*)evbuffer_pullup (buf, -1), evbuffer_get_length (buf));
      /* fprintf (stderr, "[%s]\n", *setme); */
      success = true;
    }

  if ((*setme != NULL) && path_is_suspicious (*setme))
    {
      tr_free (*setme);
      *setme = NULL;
      success = false;
    }

  return success;
}
Пример #6
0
static int
getfile( char        ** setme,
         const char   * root,
         tr_benc      * path )
{
    int err;

    if( !tr_bencIsList( path ) )
    {
        err = TR_EINVALID;
    }
    else
    {
        struct evbuffer * buf = tr_getBuffer( );
        int               n = tr_bencListSize( path );
        int               i;

        evbuffer_add( buf, root, strlen( root ) );
        for( i = 0; i < n; ++i )
        {
            const char * str;
            if( tr_bencGetStr( tr_bencListChild( path, i ), &str )
              && strcmp( str, ".." ) )
            {
                evbuffer_add( buf, TR_PATH_DELIMITER_STR, 1 );
                evbuffer_add( buf, str, strlen( str ) );
            }
        }

        *setme = tr_utf8clean( (char*)EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ), NULL );
        /* fprintf( stderr, "[%s]\n", *setme ); */
        tr_releaseBuffer( buf );
        err = 0;
    }

    return err;
}