Commit 94d16805 authored by Akira Yumiyama's avatar Akira Yumiyama

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)
parent 509dbf9b
......@@ -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;
}
}
}
......
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment