<commit_before>/* Swfdec
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

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

#include <math.h>
#include <string.h>

#include "swfdec_as_string.h"
#include "swfdec_as_array.h"
#include "swfdec_as_context.h"
#include "swfdec_as_internal.h"
#include "swfdec_as_native_function.h"
#include "swfdec_as_strings.h"
#include "swfdec_debug.h"

G_DEFINE_TYPE (SwfdecAsString, swfdec_as_string, SWFDEC_TYPE_AS_OBJECT)

static void
swfdec_as_string_do_mark (SwfdecAsObject *object)
{
  SwfdecAsString *string = SWFDEC_AS_STRING (object);

  swfdec_as_string_mark (string->string);

  SWFDEC_AS_OBJECT_CLASS (swfdec_as_string_parent_class)->mark (object);
}

static char *
swfdec_as_string_debug (SwfdecAsObject *object)
{
  SwfdecAsString *string = SWFDEC_AS_STRING (object);

  return g_strdup (string->string);
}

static void
swfdec_as_string_class_init (SwfdecAsStringClass *klass)
{
  SwfdecAsObjectClass *asobject_class = SWFDEC_AS_OBJECT_CLASS (klass);

  asobject_class->mark = swfdec_as_string_do_mark;
  asobject_class->debug = swfdec_as_string_debug;
}

static void
swfdec_as_string_init (SwfdecAsString *string)
{
  string->string = SWFDEC_AS_STR_EMPTY;
}

/*** AS CODE ***/

static const char *
swfdec_as_string_object_to_string (SwfdecAsContext *context,
    SwfdecAsObject *object)
{
  SwfdecAsValue val;

  g_return_val_if_fail (object == NULL || SWFDEC_IS_AS_OBJECT (object),
      SWFDEC_AS_STR_EMPTY);

  if (object == NULL) {
    SWFDEC_FIXME ("What to do when this is null in string functions");
    return SWFDEC_AS_STR_EMPTY;
  }

  SWFDEC_AS_VALUE_SET_OBJECT (&val, object);

  return swfdec_as_value_to_string (context, &val);
}

static inline const char *
swfdec_as_str_nth_char (const char *s, guint n)
{
  while (*s && n--)
    s = g_utf8_next_char (s);
  return s;
}

SWFDEC_AS_NATIVE (251, 9, swfdec_as_string_lastIndexOf)
void
swfdec_as_string_lastIndexOf (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  const char *string = swfdec_as_string_object_to_string (cx, object);
  gsize len;
  const char *s;

  if (argc < 1)
    return;

  s = swfdec_as_value_to_string (object->context, &argv[0]);
  if (argc == 2) {
    int offset = swfdec_as_value_to_integer (object->context, &argv[1]);
    if (offset < 0) {
      SWFDEC_AS_VALUE_SET_INT (ret, -1);
      return;
    }
    len = g_utf8_offset_to_pointer (string, offset + 1) - string;
  } else {
    len = G_MAXSIZE;
  }
  s = g_strrstr_len (string, len, s);
  if (s) {
    SWFDEC_AS_VALUE_SET_INT (ret, g_utf8_pointer_to_offset (string, s));
  } else {
    SWFDEC_AS_VALUE_SET_INT (ret, -1);
  }
}

SWFDEC_AS_NATIVE (251, 8, swfdec_as_string_indexOf)
void
swfdec_as_string_indexOf (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  const char *string = swfdec_as_string_object_to_string (cx, object);
  int offset=0, len, i=-1;
  const char *s, *t = NULL;

  if (argc < 1)
    return;

  s = swfdec_as_value_to_string (object->context, &argv[0]);
  if (argc == 2)
    offset = swfdec_as_value_to_integer (object->context, &argv[1]);
  if (offset < 0)
    offset = 0;
  len = g_utf8_strlen (string, -1);
  if (offset < len) {
    t = strstr (g_utf8_offset_to_pointer (string, offset), s);
  }
  if (t != NULL) {
    i = g_utf8_pointer_to_offset (string, t);
  }

  SWFDEC_AS_VALUE_SET_INT (ret, i);
}

SWFDEC_AS_NATIVE (251, 5, swfdec_as_string_charAt)
void
swfdec_as_string_charAt (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  const char *string = swfdec_as_string_object_to_string (cx, object);
  int i;
  const char *s, *t;

  if (argc < 1)
    return;

  i = swfdec_as_value_to_integer (object->context, &argv[0]);<commit_msg>Fix some crashes in string functions when this is null<commit_after>/* Swfdec
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

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

#include <math.h>
#include <string.h>

#include "swfdec_as_string.h"
#include "swfdec_as_array.h"
#include "swfdec_as_context.h"
#include "swfdec_as_internal.h"
#include "swfdec_as_native_function.h"
#include "swfdec_as_strings.h"
#include "swfdec_debug.h"

G_DEFINE_TYPE (SwfdecAsString, swfdec_as_string, SWFDEC_TYPE_AS_OBJECT)

static void
swfdec_as_string_do_mark (SwfdecAsObject *object)
{
  SwfdecAsString *string = SWFDEC_AS_STRING (object);

  swfdec_as_string_mark (string->string);

  SWFDEC_AS_OBJECT_CLASS (swfdec_as_string_parent_class)->mark (object);
}

static char *
swfdec_as_string_debug (SwfdecAsObject *object)
{
  SwfdecAsString *string = SWFDEC_AS_STRING (object);

  return g_strdup (string->string);
}

static void
swfdec_as_string_class_init (SwfdecAsStringClass *klass)
{
  SwfdecAsObjectClass *asobject_class = SWFDEC_AS_OBJECT_CLASS (klass);

  asobject_class->mark = swfdec_as_string_do_mark;
  asobject_class->debug = swfdec_as_string_debug;
}

static void
swfdec_as_string_init (SwfdecAsString *string)
{
  string->string = SWFDEC_AS_STR_EMPTY;
}

/*** AS CODE ***/

static const char *
swfdec_as_string_object_to_string (SwfdecAsContext *context,
    SwfdecAsObject *object)
{
  SwfdecAsValue val;

  g_return_val_if_fail (object == NULL || SWFDEC_IS_AS_OBJECT (object),
      SWFDEC_AS_STR_EMPTY);

  if (object == NULL) {
    SWFDEC_FIXME ("What to do when this is null in string functions");
    return SWFDEC_AS_STR_EMPTY;
  }

  SWFDEC_AS_VALUE_SET_OBJECT (&val, object);

  return swfdec_as_value_to_string (context, &val);
}

static inline const char *
swfdec_as_str_nth_char (const char *s, guint n)
{
  while (*s && n--)
    s = g_utf8_next_char (s);
  return s;
}

SWFDEC_AS_NATIVE (251, 9, swfdec_as_string_lastIndexOf)
void
swfdec_as_string_lastIndexOf (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  const char *string = swfdec_as_string_object_to_string (cx, object);
  gsize len;
  const char *s;

  if (argc < 1)
    return;

  s = swfdec_as_value_to_string (cx, &argv[0]);
  if (argc == 2) {
    int offset = swfdec_as_value_to_integer (cx, &argv[1]);
    if (offset < 0) {
      SWFDEC_AS_VALUE_SET_INT (ret, -1);
      return;
    }
    len = g_utf8_offset_to_pointer (string, offset + 1) - string;
  } else {
    len = G_MAXSIZE;
  }
  s = g_strrstr_len (string, len, s);
  if (s) {
    SWFDEC_AS_VALUE_SET_INT (ret, g_utf8_pointer_to_offset (string, s));
  } else {
    SWFDEC_AS_VALUE_SET_INT (ret, -1);
  }
}

SWFDEC_AS_NATIVE (251, 8, swfdec_as_string_indexOf)
void
swfdec_as_string_indexOf (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  const char *string = swfdec_as_string_object_to_string (cx, object);
  int offset=0, len, i=-1;
  const char *s, *t = NULL;

  if (argc < 1)
    return;

  s = swfdec_as_value_to_string (cx, &argv[0]);
  if (argc == 2)
    offset = swfdec_as_value_to_integer (cx, &argv[1]);
  if (offset < 0)
    offset = 0;
  len = g_utf8_strlen (string, -1);
  if (offset < len) {
    t = strstr (g_utf8_offset_to_pointer (string, offset), s);
  }
  if (t != NULL) {
    i = g_utf8_pointer_to_offset (string, t);
  }

  SWFDEC_AS_VALUE_SET_INT (ret, i);
}

SWFDEC_AS_NATIVE (251, 5, swfdec_as_string_charAt)
void
swfdec_as_string_charAt (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  const char *string = swfdec_as_string_object_to_string (cx, object);
  int i;
  const char *s, *t;

  if (argc < 1)
    return;

  i = swfdec_as_value_to_integer (cx, &argv[0]);
{'fraction_non_alphanumeric': 0.10256973424115967, 'fraction_numerical': 0.011860311882275423, 'mean_word_length': 4.542909312233719, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1755026', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3485, 'n_words': 888}