Chromium Code Reviews| Index: components/physical_web/eddystone/eddystone_encoder.cc |
| diff --git a/components/physical_web/eddystone/eddystone_encoder.cc b/components/physical_web/eddystone/eddystone_encoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..40a6a85c4832fb001209ff8523278c6cfd68c03f |
| --- /dev/null |
| +++ b/components/physical_web/eddystone/eddystone_encoder.cc |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2017 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 "components/physical_web/eddystone/eddystone_encoder.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| +const char* kPrefixes[] = {"http://www.", "https://www.", "http://", |
| + "https://"}; |
| +const size_t kNumPrefixes = sizeof(kPrefixes) / sizeof(char*); |
| + |
| +const char* kSuffixes[] = {".com/", ".org/", ".edu/", ".net/", ".info/", |
| + ".biz/", ".gov/", ".com", ".org", ".edu", |
| + ".net", ".info", ".biz", ".gov"}; |
| +const size_t kNumSuffixes = sizeof(kSuffixes) / sizeof(char*); |
| +} |
| + |
| +namespace physical_web { |
| +int EncodeUrl(const std::string& url, std::vector<uint8_t>* encoded_url) { |
|
mattreynolds
2017/03/09 22:11:30
Add a check for encoded_url == NULL.
iankc
2017/03/09 23:17:50
Done.
|
| + /* |
| + * Sanatize url. |
|
mattreynolds
2017/03/09 22:11:31
Sanitize URL.
iankc
2017/03/09 23:17:50
Done.
|
| + */ |
| + if (url.empty()) { |
| + return -1; |
|
mattreynolds
2017/03/09 22:11:31
Define these error codes as constants in eddystone
iankc
2017/03/09 23:17:50
Done.
|
| + } |
| + |
| + GURL gurl(url); |
| + if (!gurl.is_valid()) { |
| + return -2; |
| + } |
| + |
| + if (gurl.HostIsIPAddress() || !gurl.SchemeIsHTTPOrHTTPS()) { |
| + return -3; |
| + } |
| + |
| + /* |
| + * Necessary Declarations. |
| + */ |
| + const char* raw_url_position = url.c_str(); |
| + |
| + static bool prefix_lengths_loaded = false; |
|
mattreynolds
2017/03/09 22:11:30
How about "lengths_initialized"? (we aren't really
iankc
2017/03/09 23:17:50
Done.
|
| + static size_t prefix_lengths[kNumPrefixes]; |
| + if (!prefix_lengths_loaded) { |
| + for (size_t i = 0; i < kNumPrefixes; i++) { |
| + prefix_lengths[i] = strlen(kPrefixes[i]); |
| + } |
| + prefix_lengths_loaded = true; |
| + } |
| + |
| + static bool suffix_lengths_loaded = false; |
|
mattreynolds
2017/03/09 22:11:30
You can use the same bool for both array initializ
iankc
2017/03/09 23:17:50
Done.
|
| + static size_t suffix_lengths[kNumSuffixes]; |
| + if (!suffix_lengths_loaded) { |
| + for (size_t i = 0; i < kNumSuffixes; i++) { |
| + suffix_lengths[i] = strlen(kSuffixes[i]); |
| + } |
| + suffix_lengths_loaded = true; |
| + } |
| + /* |
| + * Handle prefix. |
| + */ |
| + for (size_t i = 0; i < kNumPrefixes; i++) { |
| + size_t prefix_len = prefix_lengths[i]; |
| + if (strncmp(raw_url_position, kPrefixes[i], prefix_len) == 0) { |
| + encoded_url->push_back(i); |
| + raw_url_position += prefix_len; |
| + break; |
| + } |
| + } |
| + |
| + /* |
| + * Handle suffixes. |
| + */ |
| + while (*raw_url_position) { |
| + /* check for suffix match */ |
| + size_t i; |
| + for (i = 0; i < kNumSuffixes; i++) { |
| + size_t suffix_len = suffix_lengths[i]; |
| + if (strncmp(raw_url_position, kSuffixes[i], suffix_len) == 0) { |
| + encoded_url->push_back(i); |
| + raw_url_position += suffix_len; |
| + break; /* from the for loop for checking against suffixes */ |
| + } |
| + } |
| + /* This is the default case where we've got an ordinary character which |
| + * doesn't match a suffix. */ |
| + if (i == kNumSuffixes) { |
| + encoded_url->push_back(*raw_url_position++); |
| + } |
| + } |
| + |
| + return encoded_url->size(); |
| +} |
| +} |