| OLD | NEW |
| (Empty) |
| 1 // Copyright 2004-2009 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 "omaha/base/cgi.h" | |
| 17 | |
| 18 #include <tchar.h> | |
| 19 #include "base/basictypes.h" | |
| 20 #include "omaha/base/debug.h" | |
| 21 | |
| 22 namespace omaha { | |
| 23 | |
| 24 static uint32 _needs_escape[8] = { | |
| 25 0xffffffffL, | |
| 26 0xf80008fdL, | |
| 27 0x78000001L, | |
| 28 0xb8000001L, | |
| 29 0xffffffffL, | |
| 30 0xffffffffL, | |
| 31 0xffffffffL, | |
| 32 0xffffffffL | |
| 33 }; | |
| 34 #define needs_escape(c) (_needs_escape[(c)>>5]&(1<<((c)&31))) | |
| 35 | |
| 36 // Here are a couple utility methods to change ints to hex chars & back. | |
| 37 inline int int_to_hex_digit(int i) { | |
| 38 ASSERT((i >= 0) && (i <= 16), (_T(""))); | |
| 39 return ((i < 10) ? (i + '0') : ((i - 10) + 'A')); | |
| 40 } | |
| 41 | |
| 42 inline int hex_digit_to_int(TCHAR c) { | |
| 43 ASSERT(isxdigit(c), (_T(""))); | |
| 44 return ((c >= 'a') ? ((c - 'a') + 10) : | |
| 45 (c >= 'A') ? ((c - 'A') + 10) : | |
| 46 (c - '0')); | |
| 47 } | |
| 48 | |
| 49 bool CGI::EscapeString(const TCHAR* src, int srcn, TCHAR* dst, int dstn) { | |
| 50 ASSERT1(src != dst); // In-place escaping will fail. | |
| 51 ASSERT1(srcn >= 0); | |
| 52 ASSERT1(dstn >= 1); | |
| 53 dstn--; // Number of characters we can write, not including null terminator. | |
| 54 | |
| 55 int i, j; | |
| 56 for (i = 0, j = 0; i < srcn && j < dstn; i++) { | |
| 57 TCHAR c = src[i]; | |
| 58 if (c == ' ') { | |
| 59 dst[j++] = '+'; | |
| 60 } else if (!needs_escape(c)) { | |
| 61 dst[j++] = c; | |
| 62 } else if (j + 3 > dstn) { | |
| 63 break; // Escape sequence will not fit. | |
| 64 } else { | |
| 65 dst[j++] = '%'; | |
| 66 dst[j++] = static_cast<TCHAR>(int_to_hex_digit((c >> 4) & 0xf)); | |
| 67 dst[j++] = static_cast<TCHAR>(int_to_hex_digit(c & 0xf)); | |
| 68 } | |
| 69 } | |
| 70 dst[j] = '\0'; | |
| 71 return i == srcn; | |
| 72 } | |
| 73 | |
| 74 bool CGI::UnescapeString(const TCHAR* src, int srcn, TCHAR* dst, int dstn) { | |
| 75 ASSERT1(srcn >= 0); | |
| 76 ASSERT1(dstn >= 1); | |
| 77 dstn--; // Number of characters we can write, not including null terminator. | |
| 78 | |
| 79 int i, j; | |
| 80 for (i = 0, j = 0; i < srcn && j < dstn; ++j) { | |
| 81 TCHAR c = src[i++]; | |
| 82 if (c == '+') { | |
| 83 dst[j] = ' '; | |
| 84 } else if (c != '%') { | |
| 85 dst[j] = c; | |
| 86 } else if (i + 2 > srcn) { | |
| 87 break; // Escape sequence is incomplete. | |
| 88 } else if (!isxdigit(src[i]) || !isxdigit(src[i + 1])) { | |
| 89 break; // Escape sequence isn't hex. | |
| 90 } else { | |
| 91 int num = hex_digit_to_int(src[i++]) << 4; | |
| 92 num += hex_digit_to_int(src[i++]); | |
| 93 dst[j] = static_cast<TCHAR>(num); | |
| 94 } | |
| 95 } | |
| 96 dst[j] = '\0'; | |
| 97 return i == srcn; | |
| 98 } | |
| 99 | |
| 100 } // namespace omaha | |
| 101 | |
| OLD | NEW |