You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compact_enc_det_hint_code.cc 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. ////////////////////////////////////////////////////////////////////////////////
  16. #include "compact_enc_det_hint_code.h"
  17. #include <ctype.h> // for isalpha
  18. #include <string.h> // for NULL, memchr, strlen, etc
  19. #include "util/basictypes.h" // for uint8, uint32
  20. #include "util/string_util.h"
  21. // Upper to lower, keep digits, everything else to minus '-' (2d)
  22. static const char kCharsetToLowerTbl[256] = {
  23. 0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d, 0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
  24. 0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d, 0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
  25. 0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d, 0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
  26. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, 0x38,0x39,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
  27. 0x2d,0x61,0x62,0x63,0x64,0x65,0x66,0x67, 0x68,0x69,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,0,0,0,0, 0,0,0,0,0,0,0,0,
  58. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  59. };
  60. static const char* kFakeEncodingName[] = {
  61. "FakeEnc100", "FakeEnc101", "FakeEnc102", "FakeEnc103", "FakeEnc104",
  62. "FakeEnc105", "FakeEnc106", "FakeEnc107", "FakeEnc108", "FakeEnc109",
  63. "FakeEnc110", "FakeEnc111", "FakeEnc112", "FakeEnc113", "FakeEnc114",
  64. "FakeEnc115", "FakeEnc116", "FakeEnc117", "FakeEnc118", "FakeEnc119",
  65. };
  66. static const char* kFakeEncodingName2[] = {
  67. "FakeEnc_0", "FakeEnc_1", "FakeEnc_2", "FakeEnc_3", "FakeEnc_4",
  68. };
  69. // Return name for extended encoding
  70. const char* MyEncodingName(Encoding enc) {
  71. if (enc < 0) {
  72. return "~";
  73. }
  74. if (enc == ISO_8859_1) {
  75. return "Latin1"; // I can't stand "ASCII" for this
  76. }
  77. if (enc < NUM_ENCODINGS) {
  78. return EncodingName(enc);
  79. }
  80. // allow fake names, for exploration
  81. if ((NUM_ENCODINGS <= enc) && (enc < (NUM_ENCODINGS + 4))) {
  82. return kFakeEncodingName2[enc - NUM_ENCODINGS];
  83. }
  84. if ((100 <= enc) && (enc < 120)) {
  85. return kFakeEncodingName[enc - 100];
  86. }
  87. return "~";
  88. }
  89. // Normalize ASCII string to first 4 alphabetic chars and last 4 digit chars
  90. // Letters are forced to lowercase ASCII
  91. // Used to normalize charset= values
  92. string MakeChar44(const string& str) {
  93. string res("________"); // eight underscores
  94. int l_ptr = 0;
  95. int d_ptr = 0;
  96. for (uint32 i = 0; i < str.size(); ++i) {
  97. uint8 uc = static_cast<uint8>(str[i]);
  98. if (kIsAlpha[uc]) {
  99. if (l_ptr < 4) { // Else ignore
  100. res[l_ptr] = kCharsetToLowerTbl[uc];
  101. l_ptr++;
  102. }
  103. } else if (kIsDigit[uc]) {
  104. if (d_ptr < 4) {
  105. res[4 + d_ptr] = kCharsetToLowerTbl[uc];
  106. } else {
  107. // Keep last 4 digits by shifting left
  108. res[4] = res[5];
  109. res[5] = res[6];
  110. res[6] = res[7];
  111. res[7] = kCharsetToLowerTbl[uc];
  112. }
  113. d_ptr++;
  114. } // If neither letter nor digit, drop entirely
  115. }
  116. return res;
  117. }
  118. // Normalize ASCII string to first 8 alphabetic/digit chars
  119. // Letters are forced to lowercase ASCII
  120. // Used to normalize TLD values
  121. string MakeChar4(const string& str) {
  122. string res("____"); // four underscores
  123. int l_ptr = 0;
  124. for (uint32 i = 0; i < str.size(); ++i) {
  125. uint8 uc = static_cast<uint8>(str[i]);
  126. if (kIsAlpha[uc] | kIsDigit[uc]) {
  127. if (l_ptr < 4) { // Else ignore
  128. res[l_ptr] = kCharsetToLowerTbl[uc];
  129. l_ptr++;
  130. }
  131. }
  132. }
  133. return res;
  134. }
  135. // Normalize ASCII string to first 8 alphabetic/digit chars
  136. // Letters are forced to lowercase ASCII
  137. // Used to normalize TLD values
  138. string MakeChar8(const string& str) {
  139. string res("________"); // eight dots
  140. int l_ptr = 0;
  141. for (uint32 i = 0; i < str.size(); ++i) {
  142. uint8 uc = static_cast<uint8>(str[i]);
  143. if (kIsAlpha[uc] | kIsDigit[uc]) {
  144. if (l_ptr < 8) { // Else ignore
  145. res[l_ptr] = kCharsetToLowerTbl[uc];
  146. l_ptr++;
  147. }
  148. }
  149. }
  150. return res;
  151. }