15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// String manipulation functions used in the RLZ library. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "rlz/lib/string_utils.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "rlz/lib/assert.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace rlz_lib { 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool IsAscii(unsigned char letter) { 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return letter < 0x80; 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GetHexValue(char letter, int* value) { 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!value) { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_STRING("GetHexValue: Invalid output paramter"); 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *value = 0; 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (letter >= '0' && letter <= '9') 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *value = letter - '0'; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (letter >= 'a' && letter <= 'f') 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *value = (letter - 'a') + 0xA; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (letter >= 'A' && letter <= 'F') 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *value = (letter - 'A') + 0xA; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int HexStringToInteger(const char* text) { 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!text) { 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_STRING("HexStringToInteger: text is NULL."); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int idx = 0; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Ignore leading whitespace. 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (text[idx] == '\t' || text[idx] == ' ') 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) idx++; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((text[idx] == '0') && 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (text[idx + 1] == 'X' || text[idx + 1] == 'x')) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) idx +=2; // String is of the form 0x... 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int number = 0; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int digit = 0; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (; text[idx] != '\0'; idx++) { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!GetHexValue(text[idx], &digit)) { 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Ignore trailing whitespaces, but assert on other trailing characters. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool only_whitespaces = true; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (only_whitespaces && text[idx]) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) only_whitespaces = (text[idx++] == ' '); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!only_whitespaces) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_STRING("HexStringToInteger: text contains non-hex characters."); 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return number; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) number = (number << 4) | digit; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return number; 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BytesToString(const unsigned char* data, 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int data_len, 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* string) { 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!string) 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string->clear(); 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (data_len < 1 || !data) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static const char kHex[] = "0123456789ABCDEF"; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Fix the buffer size to begin with to avoid repeated re-allocation. 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string->resize(data_len * 2); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int index = data_len; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (index--) { 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string->at(2 * index) = kHex[data[index] >> 4]; // high digit 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string->at(2 * index + 1) = kHex[data[index] & 0x0F]; // low digit 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace rlz_lib 93