srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // support.cc |
| 2 | // Non-class support functions for gdisk program. |
| 3 | // Primarily by Rod Smith, February 2009, but with a few functions |
| 4 | // copied from other sources (see attributions below). |
| 5 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 6 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed |
| 7 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 8 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | #define __STDC_LIMIT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 12 | #include <stdio.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <errno.h> |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 15 | #include <fcntl.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 16 | #include <string.h> |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 17 | #include <sys/stat.h> |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame^] | 18 | #include <unicode/ustdio.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <iostream> |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 21 | #include <sstream> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 22 | #include "support.h" |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 26 | // As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if |
| 27 | // it's not already defined. This should become unnecessary in the future. |
| 28 | // Note that this is a Linux-only ioctl.... |
| 29 | #ifndef BLKPBSZGET |
| 30 | #define BLKPBSZGET _IO(0x12,123) |
| 31 | #endif |
| 32 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame^] | 35 | // Reads a string from stdin, returning it as a C++-style string. |
| 36 | // Note that the returned string will NOT include the carriage return |
| 37 | // entered by the user. |
| 38 | string ReadString(void) { |
| 39 | string inString; |
| 40 | |
| 41 | getline(cin, inString); |
| 42 | return inString; |
| 43 | } // ReadString() |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 44 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 45 | // Get a numeric value from the user, between low and high (inclusive). |
| 46 | // Keeps looping until the user enters a value within that range. |
| 47 | // If user provides no input, def (default value) is returned. |
| 48 | // (If def is outside of the low-high range, an explicit response |
| 49 | // is required.) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 50 | int GetNumber(int low, int high, int def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 51 | int response, num; |
| 52 | char line[255]; |
| 53 | |
| 54 | if (low != high) { // bother only if low and high differ... |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 55 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 56 | cout << prompt; |
| 57 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 58 | num = sscanf(line, "%d", &response); |
| 59 | if (num == 1) { // user provided a response |
| 60 | if ((response < low) || (response > high)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 61 | cout << "Value out of range\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 62 | } else { // user hit enter; return default |
| 63 | response = def; |
| 64 | } // if/else |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 65 | } while ((response < low) || (response > high)); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 66 | } else { // low == high, so return this value |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 67 | cout << "Using " << low << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 68 | response = low; |
| 69 | } // else |
| 70 | return (response); |
| 71 | } // GetNumber() |
| 72 | |
| 73 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 74 | char GetYN(void) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 75 | char response; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame^] | 76 | string line; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 77 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 78 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 79 | cout << "(Y/N): "; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame^] | 80 | line = ReadString(); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 81 | response = toupper(line[0]); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 82 | } while ((response != 'Y') && (response != 'N')); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 83 | return response; |
| 84 | } // GetYN(void) |
| 85 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 86 | // Obtains a sector number, between low and high, from the |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 87 | // user, accepting values prefixed by "+" to add sectors to low, |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 88 | // or the same with "K", "M", "G", "T", or "P" as suffixes to add |
| 89 | // kilobytes, megabytes, gigabytes, terabytes, or petabytes, |
| 90 | // respectively. If a "-" prefix is used, use the high value minus |
| 91 | // the user-specified number of sectors (or KiB, MiB, etc.). Use the |
| 92 | // def value as the default if the user just hits Enter. The sSize is |
| 93 | // the sector size of the device. |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 94 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize, |
| 95 | const string & prompt) { |
| 96 | uint64_t response; |
| 97 | char line[255]; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 98 | |
| 99 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 100 | cout << prompt; |
| 101 | cin.getline(line, 255); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 102 | response = IeeeToInt(line, sSize, low, high, def); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 103 | } while ((response < low) || (response > high)); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 104 | return response; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 105 | } // GetSectorNum() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 106 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 107 | // Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 108 | // number of sectors. If no units are appended, interprets as the number |
| 109 | // of sectors; otherwise, interprets as number of specified units and |
| 110 | // converts to sectors. For instance, with 512-byte sectors, "1K" converts |
| 111 | // to 2. If value includes a "+", adds low and subtracts 1; if SIValue |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 112 | // inclues a "-", subtracts from high. If IeeeValue is empty, returns def. |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 113 | // Returns integral sector value. |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 114 | uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) { |
| 115 | uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1; |
| 116 | size_t foundAt = 0; |
| 117 | char suffix, plusFlag = ' '; |
| 118 | string suffixes = "KMGTPE"; |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 119 | |
| 120 | if (sSize == 0) { |
| 121 | sSize = SECTOR_SIZE; |
| 122 | cerr << "Bug: Sector size invalid in SIToInt()!\n"; |
| 123 | } // if |
| 124 | |
| 125 | // Remove leading spaces, if present |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 126 | while (inValue[0] == ' ') |
| 127 | inValue.erase(0, 1); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 128 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 129 | // If present, flag and remove leading plus or minus sign |
| 130 | if ((inValue[0] == '+') || (inValue[0] == '-')) { |
| 131 | plusFlag = inValue[0]; |
| 132 | inValue.erase(0, 1); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 133 | } // if |
| 134 | |
| 135 | // Extract numeric response and, if present, suffix |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 136 | istringstream inString(inValue); |
| 137 | if (((inString.peek() >= '0') && (inString.peek() <= '9')) || (inString.peek() == -1)) { |
| 138 | inString >> response >> suffix; |
| 139 | suffix = toupper(suffix); |
| 140 | |
| 141 | // If no response, or if response == 0, use default (def) |
| 142 | if ((inValue.length() == 0) || (response == 0)) { |
| 143 | response = def; |
| 144 | suffix = ' '; |
| 145 | plusFlag = 0; |
| 146 | } // if |
| 147 | |
| 148 | // Find multiplication and division factors for the suffix |
| 149 | foundAt = suffixes.find(suffix); |
| 150 | if (foundAt != string::npos) { |
| 151 | bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1)); |
| 152 | mult = bytesPerUnit / sSize; |
| 153 | divide = sSize / bytesPerUnit; |
| 154 | } // if |
| 155 | |
| 156 | // Adjust response based on multiplier and plus flag, if present |
| 157 | if (mult > 1) |
| 158 | response *= mult; |
| 159 | else if (divide > 1) |
| 160 | response /= divide; |
| 161 | if (plusFlag == '+') { |
| 162 | // Recompute response based on low part of range (if default = high |
| 163 | // value, which should be the case when prompting for the end of a |
| 164 | // range) or the defaut value (if default != high, which should be |
| 165 | // the case for the first sector of a partition). |
| 166 | if (def == high) |
| 167 | response = response + low - UINT64_C(1); |
| 168 | else |
| 169 | response = response + def; |
| 170 | } else if (plusFlag == '-') { |
| 171 | response = high - response; |
| 172 | } // if |
| 173 | } else { // user input is invalid |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 174 | response = high + UINT64_C(1); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 175 | } // if/else |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 176 | |
| 177 | return response; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 178 | } // IeeeToInt() |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 179 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 180 | // Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB, |
| 181 | // GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in |
| 182 | // units of the sector size or, if that parameter is omitted, in bytes. |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 183 | // (sectorSize defaults to 1). |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 184 | string BytesToIeee(uint64_t size, uint32_t sectorSize) { |
| 185 | float sizeInIeee; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame^] | 186 | unsigned int index = 0; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 187 | string units, prefixes = " KMGTPE"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 188 | ostringstream theValue; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 189 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 190 | sizeInIeee = size * (float) sectorSize; |
| 191 | while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) { |
| 192 | index++; |
| 193 | sizeInIeee /= 1024.0; |
| 194 | } // while |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 195 | theValue.setf(ios::fixed); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 196 | if (prefixes[index] == ' ') { |
| 197 | units = " bytes"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 198 | theValue.precision(0); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 199 | } else { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 200 | units = " iB"; |
| 201 | units[1] = prefixes[index]; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 202 | theValue.precision(1); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 203 | } // if/else |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 204 | theValue << sizeInIeee << units; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 205 | return theValue.str(); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 206 | } // BlocksToIeee() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 207 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 208 | // Converts two consecutive characters in the input string into a |
| 209 | // number, interpreting the string as a hexadecimal number, starting |
| 210 | // at the specified position. |
| 211 | unsigned char StrToHex(const string & input, unsigned int position) { |
| 212 | unsigned char retval = 0x00; |
| 213 | unsigned int temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 214 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame^] | 215 | if (input.length() > position) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 216 | sscanf(input.substr(position, 2).c_str(), "%x", &temp); |
| 217 | retval = (unsigned char) temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 218 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 219 | return retval; |
| 220 | } // StrToHex() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 221 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 222 | // Returns 1 if input can be interpreted as a hexadecimal number -- |
| 223 | // all characters must be spaces, digits, or letters A-F (upper- or |
| 224 | // lower-case), with at least one valid hexadecimal digit; otherwise |
| 225 | // returns 0. |
| 226 | int IsHex(const string & input) { |
| 227 | int isHex = 1, foundHex = 0, i; |
| 228 | |
| 229 | for (i = 0; i < (int) input.length(); i++) { |
| 230 | if ((input[i] < '0') || (input[i] > '9')) { |
| 231 | if ((input[i] < 'A') || (input[i] > 'F')) { |
| 232 | if ((input[i] < 'a') || (input[i] > 'f')) { |
| 233 | if ((input[i] != ' ') && (input[i] != '\n')) { |
| 234 | isHex = 0; |
| 235 | } |
| 236 | } else foundHex = 1; |
| 237 | } else foundHex = 1; |
| 238 | } else foundHex = 1; |
| 239 | } // for |
| 240 | if (!foundHex) |
| 241 | isHex = 0; |
| 242 | return isHex; |
| 243 | } // IsHex() |
| 244 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 245 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 246 | int IsLittleEndian(void) { |
| 247 | int littleE = 1; // assume little-endian (Intel-style) |
| 248 | union { |
| 249 | uint32_t num; |
| 250 | unsigned char uc[sizeof(uint32_t)]; |
| 251 | } endian; |
| 252 | |
| 253 | endian.num = 1; |
| 254 | if (endian.uc[0] != (unsigned char) 1) { |
| 255 | littleE = 0; |
| 256 | } // if |
| 257 | return (littleE); |
| 258 | } // IsLittleEndian() |
| 259 | |
| 260 | // Reverse the byte order of theValue; numBytes is number of bytes |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 261 | void ReverseBytes(void* theValue, int numBytes) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 262 | char* tempValue = NULL; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 263 | int i; |
| 264 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 265 | tempValue = new char [numBytes]; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 266 | if (tempValue != NULL) { |
| 267 | memcpy(tempValue, theValue, numBytes); |
| 268 | for (i = 0; i < numBytes; i++) |
| 269 | ((char*) theValue)[i] = tempValue[numBytes - i - 1]; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 270 | delete[] tempValue; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 271 | } // if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 272 | } // ReverseBytes() |