Skip to content

Commit 03760e6

Browse files
author
Pierre-Alexandre Meyer
committed
decode: decode header options
Signed-off-by: Pierre-Alexandre Meyer <pierre@ning.com>
1 parent e95569a commit 03760e6

File tree

10 files changed

+125
-26
lines changed

10 files changed

+125
-26
lines changed

c/src/api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _API_H_
2-
#define _API_H_
1+
#ifndef _SMILE_API_H_
2+
#define _SMILE_API_H_
33

44
typedef unsigned char u8;
55

c/src/printer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ int main(int argc, char *argv[])
138138
perror ("");
139139
goto exit;
140140
} else {
141-
if (!smile_decode_header(header)) {
141+
if (!smile_decode_header(header).valid) {
142142
fprintf(stderr, "%s: bad header: %s\n", fname, header);
143143
goto exit;
144144
} else {

c/src/smile_decode.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include "smile_decode.h"
2727
#include "smile_utils.h"
2828

29+
/* By default, shared keys are enabled, but not values */
30+
const struct smile_header DEFAULT_SMILE_HEADER = {0, 0, 1, 1, 0};
31+
2932
void smile_decode_key(u8** orig_data, struct content_handler* handler)
3033
{
3134
int length = 0;
@@ -229,9 +232,27 @@ void smile_decode_value(u8** orig_data, struct content_handler* handler)
229232
}
230233
}
231234

232-
int smile_decode_header(u8* header)
235+
struct smile_header smile_decode_header(u8* raw_header)
233236
{
234-
return (header[0] == ':' && header[1] == ')' && header[2] == '\n');
237+
struct smile_header header = DEFAULT_SMILE_HEADER;
238+
239+
header.valid = (raw_header[0] == ':' && raw_header[1] == ')' && raw_header[2] == '\n');
240+
241+
// Optional properties
242+
u8 options = raw_header[3];
243+
244+
// 0x00 for current version
245+
header.version = (options & 0xF0);
246+
247+
// Whether raw binary (unescaped 8-bit) values may be present in content
248+
header.raw_binary = (options & 0x04) >> 2;
249+
250+
// Shared String key
251+
header.shared_key_names = (options & 0x01);
252+
// Shared String value
253+
header.shared_value_names = (options & 0x02) >> 1;
254+
255+
return header;
235256
}
236257

237258
void smile_decode(u8* orig_data, int nbytes, struct content_handler* handler)

c/src/smile_decode.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _SMILE_H_
2-
#define _SMILE_H_
1+
#ifndef _SMILE_DECODE_H_
2+
#define _SMILE_DECODE_H_
33

44
#include "api.h"
55

@@ -21,9 +21,19 @@
2121
#define SMILE_START_OBJECT (u8) 0xFA
2222
#define SMILE_END_OBJECT (u8) 0xFB
2323

24-
int smile_decode_header(u8*);
24+
struct smile_header {
25+
// Whether the header is valid
26+
int valid;
27+
28+
int version;
29+
int raw_binary;
30+
int shared_key_names;
31+
int shared_value_names;
32+
};
33+
34+
struct smile_header smile_decode_header(u8*);
2535
void smile_decode_key(u8**, struct content_handler*);
2636
void smile_decode_value(u8**, struct content_handler*);
2737
void smile_decode(u8*, int, struct content_handler*);
2838

29-
#endif /* !_SMILE_H_ */
39+
#endif /* !_SMILE_DECODE_H_ */

c/test/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AM_CPPFLAGS = -I$(top_srcdir)
22
noinst_PROGRAMS = smile_tests
33

4-
smile_tests_SOURCES = smile_tests.c test_varint.c test_decode.c
4+
smile_tests_SOURCES = smile_tests.c test_varint.c test_decode.c test_header.c
55
smile_tests_LDADD = ../src/libsmile_decode-1.0.la -lsmile_decode-1.0

c/test/smile_tests.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int main()
3232
test_varint();
3333
test_zigzag();
3434
test_decode();
35+
test_header();
3536

3637
end = clock();
3738
printf("Tests run: %d, Time elapsed: %f sec\n", 3, ((double) (end - start)) / CLOCKS_PER_SEC);

c/test/smile_tests.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
#ifndef _SMILE_TESTS_H_
22
#define _SMILE_TESTS_H_
33

4+
#define ASSERT_EQUAL(expected, actual) \
5+
if (expected != actual) { \
6+
fprintf (stderr, "Test failure: expected %ld (%c), got %ld (%c)\n", (long) expected, (char) expected, (long) actual, (char) actual); \
7+
exit(1); \
8+
} else { \
9+
pass++; \
10+
}
11+
412
#endif

c/test/test_decode.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@
2525

2626
static int pass = 0;
2727

28-
#define ASSERT_EQUAL(expected, actual) \
29-
if (expected != actual) { \
30-
fprintf (stderr, "Test failure: expected %d (%c), got %d (%c)\n", expected, expected, actual, actual); \
31-
exit(1); \
32-
} else { \
33-
pass++; \
34-
}
35-
3628
/*
3729
* {
3830
* "eventDate": 1311225160270,

c/test/test_header.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2011 Pierre-Alexandre Meyer
3+
*
4+
* Pierre-Alexandre Meyer licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
#include <errno.h>
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
21+
#include "smile_tests.h"
22+
#include "../src/api.h"
23+
#include "../src/smile_decode.h"
24+
#include "../src/smile_utils.h"
25+
26+
static int pass = 0;
27+
28+
void test_encoded_shared_values_shared_names()
29+
{
30+
u8 header[] = {0x3a, 0x29, 0x0a, 0x03};
31+
32+
struct smile_header sheader = smile_decode_header(header);
33+
ASSERT_EQUAL(0, sheader.raw_binary)
34+
ASSERT_EQUAL(1, sheader.shared_key_names)
35+
ASSERT_EQUAL(1, sheader.shared_value_names)
36+
}
37+
38+
void test_raw_no_share()
39+
{
40+
u8 header[] = {0x3a, 0x29, 0x0a, 0x04};
41+
42+
struct smile_header sheader = smile_decode_header(header);
43+
ASSERT_EQUAL(1, sheader.raw_binary)
44+
ASSERT_EQUAL(0, sheader.shared_key_names)
45+
ASSERT_EQUAL(0, sheader.shared_value_names)
46+
}
47+
48+
void test_raw_shared_names()
49+
{
50+
u8 header[] = {0x3a, 0x29, 0x0a, 0x05};
51+
52+
struct smile_header sheader = smile_decode_header(header);
53+
ASSERT_EQUAL(1, sheader.raw_binary)
54+
ASSERT_EQUAL(1, sheader.shared_key_names)
55+
ASSERT_EQUAL(0, sheader.shared_value_names)
56+
}
57+
58+
void test_raw_shared_values_shared_names()
59+
{
60+
u8 header[] = {0x3a, 0x29, 0x0a, 0x07};
61+
62+
struct smile_header sheader = smile_decode_header(header);
63+
ASSERT_EQUAL(1, sheader.raw_binary)
64+
ASSERT_EQUAL(1, sheader.shared_key_names)
65+
ASSERT_EQUAL(1, sheader.shared_value_names)
66+
}
67+
68+
void test_header()
69+
{
70+
test_encoded_shared_values_shared_names();
71+
test_raw_no_share();
72+
test_raw_shared_names();
73+
test_raw_shared_values_shared_names();
74+
printf("header tests run: %d assertions passed\n", pass);
75+
}

c/test/test_varint.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424

2525
static int pass = 0;
2626

27-
#define ASSERT_EQUAL(expected, actual) \
28-
if (expected != actual) { \
29-
fprintf (stderr, "Test failure: expected %d, got %d\n", expected, actual); \
30-
exit(1); \
31-
} else { \
32-
pass++; \
33-
}
34-
3527
void test_number_encoding()
3628
{
3729
u8 buf[3];

0 commit comments

Comments
 (0)