Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixes *.mrb dump/load format with escaped character. #515

Merged
merged 1 commit into from

2 participants

@akiray03
  • hex-style / octal-style string support
  • mrb format changes like:
    • "\n" (before: \n -> after: \n)
    • '\n' (before: \n -> after: \\n)
@akiray03 akiray03 fixes *.mrb dump/load format with escaped character.
 - hex-style string support
 - mrb format changes like:
  - "\n" (before: \n -> after: \n)
  - '\n' (before: \n -> after: \\n)
94d1680
@matz matz merged commit b720156 into from
@akiray03 akiray03 deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 29, 2012
  1. @akiray03

    fixes *.mrb dump/load format with escaped character.

    akiray03 authored
     - hex-style string support
     - mrb format changes like:
      - "\n" (before: \n -> after: \n)
      - '\n' (before: \n -> after: \\n)
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 8 deletions.
  1. +23 −5 src/dump.c
  2. +16 −3 src/load.c
View
28 src/dump.c
@@ -6,6 +6,7 @@
#include <string.h>
#include "mruby/dump.h"
+#include <ctype.h>
#include "mruby/string.h"
#ifdef ENABLE_REGEXP
@@ -119,13 +120,16 @@ uint32_dump(uint32_t bin, char *hex, int type)
}
}
+#define CHAR_ESC_LEN 13 /* sizeof(\x{ hex of 32bit unsigned int } \0) */
+
static char*
str_dump(char *str, char *hex, uint16_t len, int type)
{
if (type == DUMP_TYPE_BIN)
memcpy(hex, str, len);
else {
- char *src, *dst;
+ char *src, *dst, buf[CHAR_ESC_LEN + 1];
+ int n;
for (src = str, dst = hex; len > 0; src++, dst++, len--) {
switch (*src) {
@@ -136,11 +140,19 @@ str_dump(char *str, char *hex, uint16_t len, int type)
case 0x0B:/* VT */ *dst++ = '\\'; *dst = 'v'; break;
case 0x0C:/* FF */ *dst++ = '\\'; *dst = 'f'; break;
case 0x0D:/* CR */ *dst++ = '\\'; *dst = 'r'; break;
+ case 0x5C:/* \ */ *dst++ = '\\'; *dst = '\\'; break;
case 0x22:/* " */ /* fall through */
case 0x27:/* ' */ /* fall through */
// case 0x3F:/* ? */ /* fall through */
- case 0x5C:/* \ */ /* fall through */
- default: *dst = *src; break;
+ default:
+ if (*src >= ' ' && *src <= '~') {
+ *dst = *src;
+ } else {
+ n = sprintf(buf, "\\%03o", *src & 0377);
+ memcpy(dst, buf, n);
+ dst += (n-1);
+ }
+ break;
}
}
}
@@ -167,15 +179,21 @@ str_dump_len(char *str, uint16_t len, int type)
case 0x0B:/* VT */ /* fall through */
case 0x0C:/* FF */ /* fall through */
case 0x0D:/* CR */ /* fall through */
+ case 0x5C:/* \ */ /* fall through */
dump_len += 2;
break;
case 0x22:/* " */ /* fall through */
case 0x27:/* ' */ /* fall through */
// case 0x3F:/* ? */ /* fall through */
- case 0x5C:/* \ */ /* fall through */
default:
- dump_len++; break;
+ if (*src >= ' ' && *src <= '~') {
+ dump_len++;
+ } else {
+ // dump_len += sprintf(buf, "\\%03o", *src & 0377);
+ dump_len += 4;
+ }
+ break;
}
}
}
View
19 src/load.c
@@ -611,8 +611,8 @@ hex_to_uint32(unsigned char *hex)
static char*
hex_to_str(char *hex, char *str, uint16_t *str_len)
{
- char *src, *dst;
- int escape = 0;
+ char *src, *dst, buf[4];
+ int escape = 0, base = 0;
*str_len = 0;
for (src = hex, dst = str; *src != '\0'; src++) {
@@ -629,7 +629,20 @@ hex_to_str(char *hex, char *str, uint16_t *str_len)
case '\'': /* fall through */
case '\?': /* fall through */
case '\\': *dst++ = *src; break;
- default:break;
+ default:
+ if (*src >= '0' && *src <= '7') {
+ base = 8;
+ strncpy(buf, src, 3);
+ } else if (*src == 'x' || *src == 'X') {
+ base = 16;
+ src++;
+ strncpy(buf, src, 2);
+ }
+
+ char *err_ptr;
+ *dst++ = (unsigned char) strtol(buf, &err_ptr, base) & 0xff;
+ src += (err_ptr - buf - 1);
+ break;
}
escape = 0;
} else {
Something went wrong with that request. Please try again.