⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swfdec_as_string.c

📁 Swfdec is a decoder/renderer for Macromedia Flash animations. The decoding and rendering engine is
💻 C
📖 第 1 页 / 共 2 页
字号:
      if (*str)	end = str + 1;      else	break;    } else {      end = strstr (str, delim);      if (end == NULL) {	SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_get_string (cx, str));	swfdec_as_array_push (arr, &val);	break;      }    }    SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_give_string (cx, g_strndup (str, end - str)));    swfdec_as_array_push (arr, &val);    if (count)      count--;    str = end + len;  }}SWFDEC_AS_NATIVE (251, 12, swfdec_as_string_split)voidswfdec_as_string_split (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  if (cx->version == 5) {    swfdec_as_string_split_5 (cx, object, argc, argv, ret);  } else {    swfdec_as_string_split_6 (cx, object, argc, argv, ret);  }}static const char *swfdec_as_str_sub (SwfdecAsContext *cx, const char *str, guint offset, guint len){  const char *end;  str = g_utf8_offset_to_pointer (str, offset);  end = g_utf8_offset_to_pointer (str, len);  str = swfdec_as_context_give_string (cx, g_strndup (str, end - str));  return str;}SWFDEC_AS_NATIVE (251, 13, swfdec_as_string_substr)voidswfdec_as_string_substr (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  const char *string = swfdec_as_string_object_to_string (cx, object);  int from, to, len;  from = swfdec_as_value_to_integer (cx, &argv[0]);  len = g_utf8_strlen (string, -1);    if (argc > 1) {    to = swfdec_as_value_to_integer (cx, &argv[1]);    /* FIXME: wtf? */    if (to < 0) {      if (-to <= from)	to = 0;      else	to += len;      if (to < 0)	to = 0;      if (from < 0 && to >= -from)	to = 0;    }  } else {    to = G_MAXINT;  }  if (from < 0)    from += len;  from = CLAMP (from, 0, len);  to = CLAMP (to, 0, len - from);  SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_str_sub (cx, string, from, to));}SWFDEC_AS_NATIVE (251, 11, swfdec_as_string_substring)voidswfdec_as_string_substring (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  const char *string = swfdec_as_string_object_to_string (cx, object);  int from, to, len;  len = g_utf8_strlen (string, -1);  from = swfdec_as_value_to_integer (cx, &argv[0]);  if (argc > 1) {    to = swfdec_as_value_to_integer (cx, &argv[1]);  } else {    to = len;  }  from = MAX (from, 0);  if (from >= len) {    SWFDEC_AS_VALUE_SET_STRING (ret, SWFDEC_AS_STR_EMPTY);    return;  }  to = CLAMP (to, 0, len);  if (to < from) {    int tmp = to;    to = from;    from = tmp;  }  SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_str_sub (cx, string, from, to - from));}SWFDEC_AS_NATIVE (251, 4, swfdec_as_string_toLowerCase)voidswfdec_as_string_toLowerCase (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  const char *string = swfdec_as_string_object_to_string (cx, object);  char *s;  s = g_utf8_strdown (string, -1);  SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_get_string (cx, s));  g_free (s);}SWFDEC_AS_NATIVE (251, 3, swfdec_as_string_toUpperCase)voidswfdec_as_string_toUpperCase (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  const char *string = swfdec_as_string_object_to_string (cx, object);  char *s;  s = g_utf8_strup (string, -1);  SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_get_string (cx, s));  g_free (s);}/* escape and unescape are implemented here so the mad string functions share the same place */static char *swfdec_as_string_unescape_5 (SwfdecAsContext *cx, const char *msg){  GByteArray *array;  char cur = 0; /* currently decoded character */  char *out, *in, *s;  guint decoding = 0; /* set if we're decoding a %XY string *//* attention: c is a char* */#define APPEND(chr) G_STMT_START{ \  g_byte_array_append (array, (guchar *) chr, 1); \}G_STMT_END  array = g_byte_array_new ();  in = s = g_convert (msg, -1, "LATIN1", "UTF-8", NULL, NULL, NULL);  if (s == NULL) {    SWFDEC_FIXME ("%s can not be converted to utf8 - is this Flash 5 or what?", msg);    return NULL;  }  while (*s != 0) {    if (decoding) {      decoding++;      if (*s >= '0' && *s <= '9') {	cur = cur * 16 + *s - '0';      } else if (*s >= 'A' && *s <= 'F') {	cur = cur * 16 + *s - 'A' + 10;      } else if (*s >= 'a' && *s <= 'f') {	cur = cur * 16 + *s - 'a' + 10;      } else {	cur = 0;	decoding = 0;      }      if (decoding == 3) {	APPEND (&cur);	cur = 0;	decoding = 0;      }    } else if (*s == '%') {      decoding = 1;    } else if (*s == '+') {      char tmp = ' ';      APPEND (&tmp);    } else {      APPEND (s);    }    s++;  }  g_free (in);  if (array->len == 0)    return NULL;  cur = 0;  g_byte_array_append (array, (guchar *) &cur, 1);  out = g_convert ((char *) array->data, -1, "UTF-8", "LATIN1", NULL, NULL, NULL);  g_byte_array_free (array, TRUE);  if (out) {    return out;  } else {    g_warning ("can't convert %s to UTF-8", msg);    g_free (out);    return g_strdup ("");  }#undef APPEND}static voidswfdec_as_string_escape (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  GByteArray *array;  const char *s;  char *in = NULL;  array = g_byte_array_new ();  s = swfdec_as_value_to_string (cx, &argv[0]);  if (cx->version <= 5) {    in = g_convert (s, -1, "LATIN1", "UTF-8", NULL, NULL, NULL);    if (s == NULL) {      SWFDEC_FIXME ("%s can not be converted to utf8 - is this Flash 5 or what?", s);      return;    } else {      s = in;    }  }  while (*s) {    if ((*s >= '0' && *s <= '9') ||        (*s >= 'A' && *s <= 'Z') ||        (*s >= 'a' && *s <= 'z')) {      g_byte_array_append (array, (guchar *) s, 1);    } else {      guchar add[3] = { '%', 0, 0 };      add[1] = (guchar) *s / 16;      add[2] = (guchar) *s % 16;      add[1] += add[1] < 10 ? '0' : ('A' - 10);      add[2] += add[2] < 10 ? '0' : ('A' - 10);      g_byte_array_append (array, add, 3);    }    s++;  }  g_byte_array_append (array, (guchar *) s, 1);  SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_get_string (cx, (char *) array->data));  g_byte_array_free (array, TRUE);  g_free (in);}static char *swfdec_as_string_unescape_6 (SwfdecAsContext *cx, const char *s){  GByteArray *array;  const char *msg;  char cur = 0; /* currently decoded character */  guint decoding = 0; /* set if we're decoding a %XY string */  guint utf8left = 0; /* how many valid utf8 chars are still required */  const guchar invalid[3] = { 0xEF, 0xBF, 0xBD };/* attention: c is a char* */#define APPEND(chr) G_STMT_START{ \  guchar c = *chr; \  if (utf8left) { \    if ((c & 0xC0) == 0x80) { \      g_byte_array_append (array, &c, 1); \      utf8left--; \    } else { \      guint __len = array->len - 1; \      while ((array->data[__len] & 0xC0) != 0xC0) \	__len--; \      g_byte_array_set_size (array, __len); \      g_byte_array_append (array, invalid, 3); \      utf8left = 0; \    } \  } else { \    if (c < 0x80) { \      g_byte_array_append (array, &c, 1); \    } else if (c < 0xC0) { \      guchar __foo = 0xC2; \      g_byte_array_append (array, &__foo, 1); \      g_byte_array_append (array, &c, 1); \    } else if (c > 0xF7) { \      break; \    } else { \      g_byte_array_append (array, &c, 1); \      utf8left = (c < 0xE0) ? 1 : ((c < 0xF0) ? 2 : 3); \    } \  } \}G_STMT_END  array = g_byte_array_new ();  msg = s;  while (*s != 0) {    if (decoding) {      decoding++;      if (*s >= '0' && *s <= '9') {	cur = cur * 16 + *s - '0';      } else if (*s >= 'A' && *s <= 'F') {	cur = cur * 16 + *s - 'A' + 10;      } else if (*s >= 'a' && *s <= 'f') {	cur = cur * 16 + *s - 'a' + 10;      } else {	cur = 0;	decoding = 0;	if ((guchar) *s > 0x7F) {	  APPEND (s);	}      }      if (decoding == 3) {	APPEND (&cur);	cur = 0;	decoding = 0;      }    } else if (*s == '%') {      decoding = 1;    } else if (*s == '+') {      char tmp = ' ';      APPEND (&tmp);    } else {      APPEND (s);    }    s++;  }  cur = 0;  /* loop for break statement in APPEND macro */  if (utf8left) {    guint __len = array->len - 1;    while ((array->data[__len] & 0xC0) != 0xC0)      __len--;    g_byte_array_set_size (array, __len);  }  g_byte_array_append (array, (guchar *) &cur, 1);  if (g_utf8_validate ((char *) array->data, -1, NULL)) {    return (char *) g_byte_array_free (array, FALSE);  } else {    g_warning ("%s unescaped is invalid UTF-8", msg);    g_byte_array_free (array, TRUE);    return g_strdup ("");  }#undef APPEND}char *swfdec_as_string_unescape (SwfdecAsContext *context, const char *string){  if (context->version < 6) {    return swfdec_as_string_unescape_5 (context, string);  } else {    return swfdec_as_string_unescape_6 (context, string);  }}static voidswfdec_as_string_unescape_internal (SwfdecAsContext *cx, SwfdecAsObject *object,    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret){  char *result;  result =    swfdec_as_string_unescape (cx, swfdec_as_value_to_string (cx, &argv[0]));  if (result != NULL) {    SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_get_string (cx, result));    g_free (result);  } else {    SWFDEC_AS_VALUE_SET_UNDEFINED (ret);  }}voidswfdec_as_string_init_context (SwfdecAsContext *context, guint version){  SwfdecAsObject *string, *proto;  SwfdecAsValue val;  g_return_if_fail (SWFDEC_IS_AS_CONTEXT (context));  proto = swfdec_as_object_new_empty (context);  if (proto == NULL)    return;  string = SWFDEC_AS_OBJECT (swfdec_as_object_add_constructor (context->global,      SWFDEC_AS_STR_String, 0, SWFDEC_TYPE_AS_STRING,       swfdec_as_string_construct, 0, proto));  if (!string)    return;  /* set the right properties on the String object */  if (version <= 5) {    swfdec_as_object_add_function (string, SWFDEC_AS_STR_fromCharCode, 0, swfdec_as_string_fromCharCode_5, 0);  } else {    swfdec_as_object_add_function (string, SWFDEC_AS_STR_fromCharCode, 0, swfdec_as_string_fromCharCode, 0);  }  /* set the right properties on the String.prototype object */  SWFDEC_AS_VALUE_SET_OBJECT (&val, string);  swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR_constructor,      &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_charAt, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_charAt, 1);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_indexOf, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_indexOf, 1);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_lastIndexOf, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_lastIndexOf, 1);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_charCodeAt, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_charCodeAt, 1);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_substr, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_substr, 1);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_substring, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_substring, 1);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_toLowerCase, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_toLowerCase, 0);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_toString, SWFDEC_TYPE_AS_STRING, swfdec_as_string_toString, 0);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_toUpperCase, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_toUpperCase, 0);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_valueOf, SWFDEC_TYPE_AS_STRING, swfdec_as_string_valueOf, 0);  swfdec_as_object_add_function (proto, SWFDEC_AS_STR_split, SWFDEC_TYPE_AS_OBJECT, swfdec_as_string_split, 1);  SWFDEC_AS_VALUE_SET_OBJECT (&val, context->Object_prototype);  swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR___proto__, &val,      SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);  /* add properties to global object */  swfdec_as_object_add_function (context->global, SWFDEC_AS_STR_escape, 0, swfdec_as_string_escape, 1);  swfdec_as_object_add_function (context->global, SWFDEC_AS_STR_unescape, 0, swfdec_as_string_unescape_internal, 1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -