Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: device/bluetooth/uribeacon/uri_encoder.cc

Issue 950083003: device/bluetooth: Implemented UriBeacon decoder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 char 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 } // namespace
48
49 void device::EncodeUriBeaconUri(const StringPiece& input, std::string* output) {
armansito 2015/02/26 00:39:15 It's generally difficult to understand what the co
dvh 2015/03/03 20:26:03 Done.
50 unsigned int i = 0;
51 const char* bytes = input.data();
52 while (i < input.size()) {
53 int found = -1;
54 int foundLength = -1;
armansito 2015/02/26 00:39:15 Variable names need to be all lowercase and using
dvh 2015/03/03 20:26:03 Done.
55 struct expansion* table = NULL;
56 unsigned int tableLength = 0;
57 if (i == 0) {
58 // Prefix of the URI.
59 table = prefix_expansions_list;
60 tableLength =
61 sizeof(prefix_expansions_list) / sizeof(prefix_expansions_list[0]);
armansito 2015/02/26 00:39:15 Use arraysize(x) from base/ (i.e. arraysize(prefix
dvh 2015/03/03 20:26:03 Done.
62 } else {
63 table = expansions_list;
64 tableLength = sizeof(expansions_list) / sizeof(expansions_list[0]);
65 }
66 // Search for the longest match.
67 for (unsigned int k = 0; k < tableLength; k++) {
68 const char* value = table[k].value;
69 int len = strlen(table[k].value);
70 if (strncmp(bytes + i, value, len) == 0 && len > foundLength) {
71 found = k;
72 foundLength = len;
73 }
74 }
75 if (found != -1) {
76 char b = (char)found;
77 output->append(&b, 1);
78 i += strlen(table[found].value);
79 } else {
80 output->append(bytes + i, 1);
81 i++;
82 }
83 }
84 }
armansito 2015/02/26 00:39:15 You should generally make sure that the native C++
dvh 2015/03/03 20:26:03 Done.
85
86 void device::DecodeUriBeaconUri(const StringPiece& input, std::string* output) {
87 const char* bytes = input.data();
88 unsigned int length = input.size();
89 for (unsigned int i = 0; i < length; i++) {
90 const char* expansion_value = NULL;
91 if (i == 0 &&
92 (unsigned char)bytes[i] < sizeof(prefix_expansions_list) /
93 sizeof(prefix_expansions_list[0])) {
94 // Prefix of the URI.
95 expansion_value = prefix_expansions_list[(unsigned char)bytes[i]].value;
96 } else if ((unsigned char)bytes[i] <
97 sizeof(expansions_list) / sizeof(expansions_list[0])) {
98 expansion_value = expansions_list[(unsigned char)bytes[i]].value;
99 }
100 if (expansion_value == NULL) {
101 output->append(&bytes[i], 1);
102 } else {
103 output->append(expansion_value, strlen(expansion_value));
104 }
105 }
106 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698