OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "uri_encoder.h" | |
6 | |
7 using base::StringPiece; | |
8 | |
9 namespace { | |
10 | |
11 struct expansion { | |
12 uint8_t code; | |
13 const char* value; | |
14 }; | |
15 | |
16 // The two following data structures are the expansions code tables for URI | |
17 // encoding described in the following specification: | |
18 // https://github.com/google/uribeacon/blob/master/specification/AdvertisingMode .md | |
19 | |
20 // For the prefix of the URI. | |
21 struct expansion prefix_expansions_list[] = { | |
22 {0, "http://www."}, | |
23 {1, "https://www."}, | |
24 {2, "http://"}, | |
25 {3, "https://"}, | |
26 {4, "urn:uuid:"}, | |
27 }; | |
28 | |
29 // For the remaining part of the URI. | |
30 struct expansion expansions_list[] = { | |
31 {0, ".com/"}, | |
32 {1, ".org/"}, | |
33 {2, ".edu/"}, | |
34 {3, ".net/"}, | |
35 {4, ".info/"}, | |
36 {5, ".biz/"}, | |
37 {6, ".gov/"}, | |
38 {7, ".com"}, | |
39 {8, ".org"}, | |
40 {9, ".edu"}, | |
41 {10, ".net"}, | |
42 {11, ".info"}, | |
43 {12, ".biz"}, | |
44 {13, ".gov"}, | |
45 }; | |
46 | |
47 struct expansion* CommonLookupExpansionByValue(struct expansion* table, | |
48 int table_length, | |
49 const std::string& input, | |
50 int input_index) { | |
51 int found = -1; | |
52 int found_length = -1; | |
53 | |
54 for (int k = 0; k < table_length; k++) { | |
55 const char* value = table[k].value; | |
56 int len = strlen(table[k].value); | |
57 if (input_index + len <= static_cast<int>(input.size())) { | |
58 if (len > found_length && strncmp(&input[input_index], value, len) == 0) { | |
59 found = k; | |
60 found_length = len; | |
61 } | |
62 } | |
63 } | |
64 if (found == -1) { | |
65 return NULL; | |
66 } | |
armansito
2015/03/06 22:00:30
nit: no need for curly braces in single-line if bo
dvh
2015/03/09 20:52:00
Done.
| |
67 return &table[found]; | |
68 } | |
69 | |
70 struct expansion* LookupExpansionByValue(const std::string& input, | |
71 int input_index) { | |
72 return CommonLookupExpansionByValue( | |
73 expansions_list, arraysize(expansions_list), input, input_index); | |
74 } | |
75 | |
76 struct expansion* LookupPrefixExpansionByValue(const std::string& input, | |
77 int input_index) { | |
78 return CommonLookupExpansionByValue(prefix_expansions_list, | |
79 arraysize(prefix_expansions_list), input, | |
80 input_index); | |
81 } | |
82 | |
83 struct expansion* LookupExpansionByCode(const std::vector<uint8_t>& input, | |
84 int input_index) { | |
85 if (input[input_index] >= arraysize(expansions_list)) | |
86 return NULL; | |
87 return &expansions_list[input[input_index]]; | |
88 } | |
89 | |
90 struct expansion* LookupPrefixExpansionByCode(const std::vector<uint8_t>& input, | |
91 int input_index) { | |
92 if (input[input_index] >= arraysize(prefix_expansions_list)) | |
93 return NULL; | |
94 return &prefix_expansions_list[input[input_index]]; | |
95 } | |
96 | |
97 } // namespace | |
98 | |
99 void device::EncodeUriBeaconUri(const std::string& input, | |
100 std::vector<uint8_t>& output) { | |
101 int i = 0; | |
102 while (i < static_cast<int>(input.size())) { | |
103 struct expansion* exp; | |
104 if (i == 0) { | |
105 exp = LookupPrefixExpansionByValue(input, i); | |
armansito
2015/03/06 22:00:30
nit: ditto
dvh
2015/03/09 20:52:00
Done.
| |
106 } else { | |
armansito
2015/03/06 22:00:30
nit: ditto
dvh
2015/03/09 20:52:00
Done.
| |
107 exp = LookupExpansionByValue(input, i); | |
108 } | |
109 if (exp == NULL) { | |
110 output.push_back(static_cast<uint8_t>(input[i])); | |
111 i++; | |
112 } else { | |
113 output.push_back(exp->code); | |
114 i += strlen(exp->value); | |
115 } | |
116 } | |
117 } | |
118 | |
119 void device::DecodeUriBeaconUri(const std::vector<uint8_t>& input, | |
120 std::string& output) { | |
121 int length = input.size(); | |
122 for (int i = 0; i < length; i++) { | |
123 struct expansion* exp; | |
124 if (i == 0) { | |
armansito
2015/03/06 22:00:30
nit: ditto
dvh
2015/03/09 20:52:00
Done.
| |
125 exp = LookupPrefixExpansionByCode(input, i); | |
126 } else { | |
127 exp = LookupExpansionByCode(input, i); | |
128 } | |
129 if (exp == NULL) { | |
armansito
2015/03/06 22:00:30
nit: ditto
dvh
2015/03/09 20:52:00
Done.
| |
130 output.push_back(static_cast<char>(input[i])); | |
131 } else { | |
132 output.append(exp->value); | |
133 } | |
134 } | |
135 } | |
OLD | NEW |