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

Unified Diff: components/physical_web/eddystone/eddystone_encoder.cc

Issue 2731273004: Add Platform Independent Eddystone Encoder (Closed)
Patch Set: Fixing more Casting Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
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..73b59f0d58413c43b813a0c5c666d2b0a91ae5e7
--- /dev/null
+++ b/components/physical_web/eddystone/eddystone_encoder.cc
@@ -0,0 +1,90 @@
+// 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 {
+size_t EncodeUrl(const std::string& url, std::vector<uint8_t>* encoded_url) {
+ /*
+ * Sanitize input.
+ */
+
+ if (encoded_url == NULL)
+ return kNullEncodedUrl;
+ if (url.empty())
+ return kEmptyUrl;
+
+ GURL gurl(url);
+ if (!gurl.is_valid())
+ return kInvalidUrl;
+ if (gurl.HostIsIPAddress() || !gurl.SchemeIsHTTPOrHTTPS())
+ return kInvalidFormat;
+
+ /*
+ * Necessary Declarations.
+ */
+ const char* raw_url_position = url.c_str();
+
+ size_t prefix_lengths[kNumPrefixes];
+ size_t suffix_lengths[kNumSuffixes];
+ for (size_t i = 0; i < kNumPrefixes; i++) {
+ prefix_lengths[i] = strlen(kPrefixes[i]);
+ }
+ for (size_t i = 0; i < kNumSuffixes; i++) {
+ suffix_lengths[i] = strlen(kSuffixes[i]);
+ }
+
+ /*
+ * 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(static_cast<uint8_t>(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(static_cast<uint8_t>(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);
+ raw_url_position++;
+ }
+ }
+
+ return encoded_url->size();
+}
+}
« no previous file with comments | « components/physical_web/eddystone/eddystone_encoder.h ('k') | components/physical_web/eddystone/eddystone_encoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698