Skip to content

Commit 11504d6

Browse files
author
Pierre-Alexandre Meyer
committed
main: add more decoding tokens
Signed-off-by: Pierre-Alexandre Meyer <pierre@ning.com>
1 parent 943ce7f commit 11504d6

File tree

2 files changed

+89
-25
lines changed

2 files changed

+89
-25
lines changed

smile.c

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,96 @@ int main(int argc, char *argv[])
8383
ip = buf;
8484
eob = buf + nbytes;
8585
while (ip < eob) {
86-
if (*ip == (char)0x00) {
87-
// nothing
88-
}
89-
else if (*ip == SMILE_START_OBJECT) {
90-
putchar('{');
91-
putchar('\n');
92-
} else if (*ip == SMILE_END_OBJECT) {
93-
putchar('}');
94-
} else if (*ip == SMILE_START_ARRAY) {
95-
putchar('[');
96-
putchar('\n');
97-
} else if (*ip == SMILE_END_ARRAY) {
98-
putchar(']');
99-
} else {
100-
// Tiny Unicode, Small Unicode
101-
if (*ip >= (u8) 0x80 && *ip <= (u8) 0xBF) {
102-
length = (*ip & 0x1F);
103-
ip++;
86+
if (*ip >= (u8) 0x01 && *ip <= (u8) 0x1f) {
87+
// Reference value
88+
length = (*ip & 0xF8);
89+
} else if (*ip >= (u8) 0x20 && *ip <= (u8) 0x23) {
90+
if (*ip == 0x20) {
91+
// Empty String
10492
putchar('"');
105-
// humm, why +1?
106-
while (length + 1) {
107-
putchar(*ip);
108-
ip++;
109-
length--;
110-
}
11193
putchar('"');
94+
} else if (*ip == 0x21) {
95+
// null
96+
putchar('(');
97+
putchar('n');
98+
putchar('u');
99+
putchar('l');
100+
putchar('l');
101+
putchar(')');
102+
} else if (*ip == 0x22) {
103+
// false
104+
putchar('f');
105+
putchar('a');
106+
putchar('l');
107+
putchar('s');
108+
putchar('e');
109+
} else if (*ip == 0x21) {
110+
// true
111+
putchar('t');
112+
putchar('r');
113+
putchar('u');
114+
putchar('e');
115+
}
116+
// Integral numbers
117+
} else if (*ip >= (u8) 0x24 && *ip <= (u8) 0x27) {
118+
length = (*ip & 0x03);
119+
if (length == 0) {
120+
// 32-bit
121+
} else if (length == 1) {
122+
// 64-bit
123+
} else if (length == 2) {
124+
// BigInteger
125+
} else {
126+
// Reserved for future use
127+
}
128+
// Floating point numbers
129+
} else if (*ip >= (u8) 0x28 && *ip <= (u8) 0x2B) {
130+
// Reserved for future use
131+
} else if (*ip >= (u8) 0x2C && *ip <= (u8) 0x3F) {
132+
// Tiny ASCII
133+
} else if (*ip >= (u8) 0x40 && *ip <= (u8) 0x5F) {
134+
// 5 LSB used to indicate lengths from 1 to 32 (bytes == chars)
135+
length = (*ip & 0x1F);
136+
PRINT(ip, length)
137+
// Small ASCII
138+
} else if (*ip >= (u8) 0x60 && *ip <= (u8) 0x7F) {
139+
// 5 LSB used to indicate lengths from 33 to 64 (bytes == chars)
140+
length = (*ip & 0x1F) + 32;
141+
PRINT(ip, length)
142+
// Tiny Unicode
143+
} else if (*ip >= (u8) 0x80 && *ip <= (u8) 0x9F) {
144+
// 5 LSB used to indicate _byte_ lengths from 2 to 33
145+
length = (*ip & 0x1F) + 1;
146+
ip++;
147+
putchar('"');
148+
PRINT(ip, length)
149+
putchar('"');
150+
PRINT_EOK
151+
// Small Unicode
152+
} else if (*ip >= (u8) 0xA0 && *ip <= (u8) 0xBF) {
153+
// 5 LSB used to indicate _byte_ lengths from 34 to 65
154+
length = (*ip & 0x1F) + 33;
155+
ip++;
156+
putchar('"');
157+
PRINT(ip, length)
158+
putchar('"');
159+
PRINT_EOK
160+
// Small integers
161+
} else if (*ip >= (u8) 0xC0 && *ip <= (u8) 0xDF) {
162+
// Misc; binary / text / structure markers
163+
} else {
164+
if (*ip == SMILE_START_OBJECT) {
165+
putchar('{');
166+
putchar('\n');
167+
} else if (*ip == SMILE_END_OBJECT) {
168+
putchar('}');
169+
} else if (*ip == SMILE_START_ARRAY) {
170+
putchar('[');
171+
putchar('\n');
172+
} else if (*ip == SMILE_END_ARRAY) {
173+
putchar(']');
112174
} else {
113-
// TODO
175+
// TODO
114176
}
115177
}
116178
ip++;

smile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
typedef unsigned char u8;
55

66
#define DEBUG_BYTE(byte) printf("0x%02x ", (u8) byte);
7+
#define PRINT(ip, length) while (length--) {putchar(*ip++);}
8+
#define PRINT_EOK putchar(':'); putchar(' ');
79

810
#define SUCCESS 0
911

0 commit comments

Comments
 (0)