| 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 "base/macros.h" | |
| 6 #include "uri_encoder.h" | |
| 7 | |
| 8 using base::StringPiece; | |
| 9 | |
| 10 namespace { | |
| 11 | |
| 12 struct expansion { | |
| 13 uint8_t code; | |
| 14 const char* value; | |
| 15 }; | |
| 16 | |
| 17 // The two following data structures are the expansions code tables for URI | |
| 18 // encoding described in the following specification: | |
| 19 // https://github.com/google/uribeacon/blob/master/specification/AdvertisingMode
.md | |
| 20 | |
| 21 // For the prefix of the URI. | |
| 22 struct expansion prefix_expansions_list[] = { | |
| 23 {0, "http://www."}, | |
| 24 {1, "https://www."}, | |
| 25 {2, "http://"}, | |
| 26 {3, "https://"}, | |
| 27 {4, "urn:uuid:"}, | |
| 28 }; | |
| 29 | |
| 30 // For the remaining part of the URI. | |
| 31 struct expansion expansions_list[] = { | |
| 32 {0, ".com/"}, | |
| 33 {1, ".org/"}, | |
| 34 {2, ".edu/"}, | |
| 35 {3, ".net/"}, | |
| 36 {4, ".info/"}, | |
| 37 {5, ".biz/"}, | |
| 38 {6, ".gov/"}, | |
| 39 {7, ".com"}, | |
| 40 {8, ".org"}, | |
| 41 {9, ".edu"}, | |
| 42 {10, ".net"}, | |
| 43 {11, ".info"}, | |
| 44 {12, ".biz"}, | |
| 45 {13, ".gov"}, | |
| 46 }; | |
| 47 | |
| 48 struct expansion* CommonLookupExpansionByValue(struct expansion* table, | |
| 49 int table_length, | |
| 50 const std::string& input, | |
| 51 int input_index) { | |
| 52 int found = -1; | |
| 53 int found_length = -1; | |
| 54 | |
| 55 for (int k = 0; k < table_length; k++) { | |
| 56 const char* value = table[k].value; | |
| 57 int len = static_cast<int>(strlen(table[k].value)); | |
| 58 if (input_index + len <= static_cast<int>(input.size())) { | |
| 59 if (len > found_length && strncmp(&input[input_index], value, len) == 0) { | |
| 60 found = k; | |
| 61 found_length = len; | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 if (found == -1) | |
| 66 return NULL; | |
| 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); | |
| 106 else | |
| 107 exp = LookupExpansionByValue(input, i); | |
| 108 if (exp == NULL) { | |
| 109 output.push_back(static_cast<uint8_t>(input[i])); | |
| 110 i++; | |
| 111 } else { | |
| 112 output.push_back(exp->code); | |
| 113 i += static_cast<int>(strlen(exp->value)); | |
| 114 } | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void device::DecodeUriBeaconUri(const std::vector<uint8_t>& input, | |
| 119 std::string& output) { | |
| 120 int length = static_cast<int>(input.size()); | |
| 121 for (int i = 0; i < length; i++) { | |
| 122 struct expansion* exp; | |
| 123 if (i == 0) | |
| 124 exp = LookupPrefixExpansionByCode(input, i); | |
| 125 else | |
| 126 exp = LookupExpansionByCode(input, i); | |
| 127 if (exp == NULL) | |
| 128 output.push_back(static_cast<char>(input[i])); | |
| 129 else | |
| 130 output.append(exp->value); | |
| 131 } | |
| 132 } | |
| OLD | NEW |