Chromium Code Reviews| Index: device/bluetooth/uribeacon/uri_encoder.cc |
| diff --git a/device/bluetooth/uribeacon/uri_encoder.cc b/device/bluetooth/uribeacon/uri_encoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2604b4fc0612cd3a37f29e21dce3ce52ff373576 |
| --- /dev/null |
| +++ b/device/bluetooth/uribeacon/uri_encoder.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "uri_encoder.h" |
| + |
| +using base::StringPiece; |
| + |
| +namespace { |
| + |
| +struct expansion { |
| + char code; |
| + const char* value; |
| +}; |
| + |
| +// The two following data structures are the expansions code tables for URI |
| +// encoding described in the following specification: |
| +// https://github.com/google/uribeacon/blob/master/specification/AdvertisingMode.md |
| + |
| +// For the prefix of the URI. |
| +struct expansion prefix_expansions_list[] = { |
| + {0, "http://www."}, |
| + {1, "https://www."}, |
| + {2, "http://"}, |
| + {3, "https://"}, |
| + {4, "urn:uuid:"}, |
| +}; |
| + |
| +// For the remaining part of the URI. |
| +struct expansion expansions_list[] = { |
| + {0, ".com/"}, |
| + {1, ".org/"}, |
| + {2, ".edu/"}, |
| + {3, ".net/"}, |
| + {4, ".info/"}, |
| + {5, ".biz/"}, |
| + {6, ".gov/"}, |
| + {7, ".com"}, |
| + {8, ".org"}, |
| + {9, ".edu"}, |
| + {10, ".net"}, |
| + {11, ".info"}, |
| + {12, ".biz"}, |
| + {13, ".gov"}, |
| +}; |
| + |
| +} // namespace |
| + |
| +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.
|
| + unsigned int i = 0; |
| + const char* bytes = input.data(); |
| + while (i < input.size()) { |
| + int found = -1; |
| + 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.
|
| + struct expansion* table = NULL; |
| + unsigned int tableLength = 0; |
| + if (i == 0) { |
| + // Prefix of the URI. |
| + table = prefix_expansions_list; |
| + tableLength = |
| + 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.
|
| + } else { |
| + table = expansions_list; |
| + tableLength = sizeof(expansions_list) / sizeof(expansions_list[0]); |
| + } |
| + // Search for the longest match. |
| + for (unsigned int k = 0; k < tableLength; k++) { |
| + const char* value = table[k].value; |
| + int len = strlen(table[k].value); |
| + if (strncmp(bytes + i, value, len) == 0 && len > foundLength) { |
| + found = k; |
| + foundLength = len; |
| + } |
| + } |
| + if (found != -1) { |
| + char b = (char)found; |
| + output->append(&b, 1); |
| + i += strlen(table[found].value); |
| + } else { |
| + output->append(bytes + i, 1); |
| + i++; |
| + } |
| + } |
| +} |
|
armansito
2015/02/26 00:39:15
You should generally make sure that the native C++
dvh
2015/03/03 20:26:03
Done.
|
| + |
| +void device::DecodeUriBeaconUri(const StringPiece& input, std::string* output) { |
| + const char* bytes = input.data(); |
| + unsigned int length = input.size(); |
| + for (unsigned int i = 0; i < length; i++) { |
| + const char* expansion_value = NULL; |
| + if (i == 0 && |
| + (unsigned char)bytes[i] < sizeof(prefix_expansions_list) / |
| + sizeof(prefix_expansions_list[0])) { |
| + // Prefix of the URI. |
| + expansion_value = prefix_expansions_list[(unsigned char)bytes[i]].value; |
| + } else if ((unsigned char)bytes[i] < |
| + sizeof(expansions_list) / sizeof(expansions_list[0])) { |
| + expansion_value = expansions_list[(unsigned char)bytes[i]].value; |
| + } |
| + if (expansion_value == NULL) { |
| + output->append(&bytes[i], 1); |
| + } else { |
| + output->append(expansion_value, strlen(expansion_value)); |
| + } |
| + } |
| +} |