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 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <iostream> |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 20 | #include <sstream> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 21 | #include "support.h" |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 25 | // As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if |
| 26 | // it's not already defined. This should become unnecessary in the future. |
| 27 | // Note that this is a Linux-only ioctl.... |
| 28 | #ifndef BLKPBSZGET |
| 29 | #define BLKPBSZGET _IO(0x12,123) |
| 30 | #endif |
| 31 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 32 | using namespace std; |
| 33 | |
| 34 | // Get a numeric value from the user, between low and high (inclusive). |
| 35 | // Keeps looping until the user enters a value within that range. |
| 36 | // If user provides no input, def (default value) is returned. |
| 37 | // (If def is outside of the low-high range, an explicit response |
| 38 | // is required.) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 39 | int GetNumber(int low, int high, int def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 40 | int response, num; |
| 41 | char line[255]; |
| 42 | |
| 43 | if (low != high) { // bother only if low and high differ... |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 44 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 45 | cout << prompt; |
| 46 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 47 | num = sscanf(line, "%d", &response); |
| 48 | if (num == 1) { // user provided a response |
| 49 | if ((response < low) || (response > high)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 50 | cout << "Value out of range\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 51 | } else { // user hit enter; return default |
| 52 | response = def; |
| 53 | } // if/else |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 54 | } while ((response < low) || (response > high)); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 55 | } else { // low == high, so return this value |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 56 | cout << "Using " << low << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 57 | response = low; |
| 58 | } // else |
| 59 | return (response); |
| 60 | } // GetNumber() |
| 61 | |
| 62 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 63 | char GetYN(void) { |
| 64 | char line[255]; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 65 | char response; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 66 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 67 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 68 | cout << "(Y/N): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 69 | if (!fgets(line, 255, stdin)) { |
| 70 | cerr << "Critical error! Failed fgets() in GetYN()\n"; |
| 71 | exit(1); |
| 72 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 73 | sscanf(line, "%c", &response); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 74 | if (response == 'y') |
| 75 | response = 'Y'; |
| 76 | if (response == 'n') |
| 77 | response = 'N'; |
| 78 | } while ((response != 'Y') && (response != 'N')); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | return response; |
| 80 | } // GetYN(void) |
| 81 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 82 | // Obtains a sector number, between low and high, from the |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 83 | // user, accepting values prefixed by "+" to add sectors to low, |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 84 | // or the same with "K", "M", "G", "T", or "P" as suffixes to add |
| 85 | // kilobytes, megabytes, gigabytes, terabytes, or petabytes, |
| 86 | // respectively. If a "-" prefix is used, use the high value minus |
| 87 | // the user-specified number of sectors (or KiB, MiB, etc.). Use the |
| 88 | // def value as the default if the user just hits Enter. The sSize is |
| 89 | // the sector size of the device. |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 90 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize, |
| 91 | const string & prompt) { |
| 92 | uint64_t response; |
| 93 | char line[255]; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 94 | |
| 95 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 96 | cout << prompt; |
| 97 | cin.getline(line, 255); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 98 | response = SIToInt(line, sSize, low, high, def); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 99 | } while ((response < low) || (response > high)); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 100 | return response; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 101 | } // GetSectorNum() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 102 | |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 103 | // Convert an SI value (K, M, G, T, or P) to its equivalent in |
| 104 | // number of sectors. If no units are appended, interprets as the number |
| 105 | // of sectors; otherwise, interprets as number of specified units and |
| 106 | // converts to sectors. For instance, with 512-byte sectors, "1K" converts |
| 107 | // to 2. If value includes a "+", adds low and subtracts 1; if SIValue |
| 108 | // inclues a "-", subtracts from high. If SIValue is empty, returns def. |
| 109 | // Returns integral sector value. |
| 110 | uint64_t SIToInt(string SIValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) { |
| 111 | int plusFlag = 0, badInput = 0; |
| 112 | uint64_t response = def, mult = 1, divide = 1; |
| 113 | char suffix; |
| 114 | |
| 115 | if (sSize == 0) { |
| 116 | sSize = SECTOR_SIZE; |
| 117 | cerr << "Bug: Sector size invalid in SIToInt()!\n"; |
| 118 | } // if |
| 119 | |
| 120 | // Remove leading spaces, if present |
| 121 | while (SIValue[0] == ' ') |
| 122 | SIValue.erase(0, 1); |
| 123 | |
| 124 | // If present, flag and remove leading plus sign |
| 125 | if (SIValue[0] == '+') { |
| 126 | plusFlag = 1; |
| 127 | SIValue.erase(0, 1); |
| 128 | } // if |
| 129 | |
| 130 | // If present, flag and remove leading minus sign |
| 131 | if (SIValue[0] == '-') { |
| 132 | plusFlag = -1; |
| 133 | SIValue.erase(0, 1); |
| 134 | } // if |
| 135 | |
| 136 | // Extract numeric response and, if present, suffix |
| 137 | istringstream inString(SIValue); |
| 138 | if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1)) |
| 139 | badInput = 1; |
| 140 | inString >> response >> suffix; |
| 141 | |
| 142 | // If no response, or if response == 0, use default (def) |
| 143 | if ((SIValue.length() == 0) || (response == 0)) { |
| 144 | response = def; |
| 145 | suffix = ' '; |
| 146 | plusFlag = 0; |
| 147 | } // if |
| 148 | |
| 149 | // Set multiplier based on suffix |
| 150 | switch (suffix) { |
| 151 | case 'K': |
| 152 | case 'k': |
| 153 | mult = UINT64_C(1024) / sSize; |
| 154 | divide = sSize / UINT64_C(1024); |
| 155 | break; |
| 156 | case 'M': |
| 157 | case 'm': |
| 158 | mult = UINT64_C(1048576) / sSize; |
| 159 | divide = sSize / UINT64_C(1048576); |
| 160 | break; |
| 161 | case 'G': |
| 162 | case 'g': |
| 163 | mult = UINT64_C(1073741824) / sSize; |
| 164 | break; |
| 165 | case 'T': |
| 166 | case 't': |
| 167 | mult = UINT64_C(1099511627776) / sSize; |
| 168 | break; |
| 169 | case 'P': |
| 170 | case 'p': |
| 171 | mult = UINT64_C(1125899906842624) / sSize; |
| 172 | break; |
| 173 | default: |
| 174 | mult = 1; |
| 175 | } // switch |
| 176 | |
| 177 | // Adjust response based on multiplier and plus flag, if present |
| 178 | if (mult > 1) |
| 179 | response *= mult; |
| 180 | else if (divide > 1) |
| 181 | response /= divide; |
| 182 | if (plusFlag == 1) { |
| 183 | // Recompute response based on low part of range (if default = high |
| 184 | // value, which should be the case when prompting for the end of a |
| 185 | // range) or the defaut value (if default != high, which should be |
| 186 | // the case for the first sector of a partition). |
| 187 | if (def == high) |
| 188 | response = response + low - UINT64_C(1); |
| 189 | else |
| 190 | response = response + def; |
| 191 | } // if |
| 192 | if (plusFlag == -1) { |
| 193 | response = high - response; |
| 194 | } // if |
| 195 | |
| 196 | if (badInput) |
| 197 | response = high + UINT64_C(1); |
| 198 | |
| 199 | return response; |
| 200 | } // SIToInt() |
| 201 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 202 | // Takes a size and converts this to a size in SI units (KiB, MiB, GiB, |
| 203 | // TiB, or PiB), returned in C++ string form. The size is either in units |
| 204 | // of the sector size or, if that parameter is omitted, in bytes. |
| 205 | // (sectorSize defaults to 1). |
| 206 | string BytesToSI(uint64_t size, uint32_t sectorSize) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 207 | string units; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 208 | ostringstream theValue; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 209 | float sizeInSI; |
| 210 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 211 | sizeInSI = (float) size * (float) sectorSize; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 212 | units = " bytes"; |
| 213 | if (sizeInSI > 1024.0) { |
| 214 | sizeInSI /= 1024.0; |
| 215 | units = " KiB"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 216 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 217 | if (sizeInSI > 1024.0) { |
| 218 | sizeInSI /= 1024.0; |
| 219 | units = " MiB"; |
| 220 | } // if |
| 221 | if (sizeInSI > 1024.0) { |
| 222 | sizeInSI /= 1024.0; |
| 223 | units = " GiB"; |
| 224 | } // if |
| 225 | if (sizeInSI > 1024.0) { |
| 226 | sizeInSI /= 1024.0; |
| 227 | units = " TiB"; |
| 228 | } // if |
| 229 | if (sizeInSI > 1024.0) { |
| 230 | sizeInSI /= 1024.0; |
| 231 | units = " PiB"; |
| 232 | } // if |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 233 | theValue.setf(ios::fixed); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 234 | if (units == " bytes") { // in bytes, so no decimal point |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 235 | theValue.precision(0); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 236 | } else { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 237 | theValue.precision(1); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 238 | } // if/else |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 239 | theValue << sizeInSI << units; |
| 240 | return theValue.str(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 241 | } // BlocksToSI() |
| 242 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 243 | // Converts two consecutive characters in the input string into a |
| 244 | // number, interpreting the string as a hexadecimal number, starting |
| 245 | // at the specified position. |
| 246 | unsigned char StrToHex(const string & input, unsigned int position) { |
| 247 | unsigned char retval = 0x00; |
| 248 | unsigned int temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 249 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 250 | if (input.length() >= (position + 2)) { |
| 251 | sscanf(input.substr(position, 2).c_str(), "%x", &temp); |
| 252 | retval = (unsigned char) temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 253 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 254 | return retval; |
| 255 | } // StrToHex() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 256 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 257 | // Returns 1 if input can be interpreted as a hexadecimal number -- |
| 258 | // all characters must be spaces, digits, or letters A-F (upper- or |
| 259 | // lower-case), with at least one valid hexadecimal digit; otherwise |
| 260 | // returns 0. |
| 261 | int IsHex(const string & input) { |
| 262 | int isHex = 1, foundHex = 0, i; |
| 263 | |
| 264 | for (i = 0; i < (int) input.length(); i++) { |
| 265 | if ((input[i] < '0') || (input[i] > '9')) { |
| 266 | if ((input[i] < 'A') || (input[i] > 'F')) { |
| 267 | if ((input[i] < 'a') || (input[i] > 'f')) { |
| 268 | if ((input[i] != ' ') && (input[i] != '\n')) { |
| 269 | isHex = 0; |
| 270 | } |
| 271 | } else foundHex = 1; |
| 272 | } else foundHex = 1; |
| 273 | } else foundHex = 1; |
| 274 | } // for |
| 275 | if (!foundHex) |
| 276 | isHex = 0; |
| 277 | return isHex; |
| 278 | } // IsHex() |
| 279 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 280 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 281 | int IsLittleEndian(void) { |
| 282 | int littleE = 1; // assume little-endian (Intel-style) |
| 283 | union { |
| 284 | uint32_t num; |
| 285 | unsigned char uc[sizeof(uint32_t)]; |
| 286 | } endian; |
| 287 | |
| 288 | endian.num = 1; |
| 289 | if (endian.uc[0] != (unsigned char) 1) { |
| 290 | littleE = 0; |
| 291 | } // if |
| 292 | return (littleE); |
| 293 | } // IsLittleEndian() |
| 294 | |
| 295 | // Reverse the byte order of theValue; numBytes is number of bytes |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 296 | void ReverseBytes(void* theValue, int numBytes) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 297 | char* tempValue = NULL; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 298 | int i; |
| 299 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 300 | tempValue = new char [numBytes]; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 301 | if (tempValue != NULL) { |
| 302 | memcpy(tempValue, theValue, numBytes); |
| 303 | for (i = 0; i < numBytes; i++) |
| 304 | ((char*) theValue)[i] = tempValue[numBytes - i - 1]; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 305 | delete[] tempValue; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 306 | } // if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 307 | } // ReverseBytes() |
| 308 | |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 309 | // Extract integer data from argument string, which should be colon-delimited |
| 310 | uint64_t GetInt(const string & argument, int itemNum) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 311 | uint64_t retval; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 312 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 313 | istringstream inString(GetString(argument, itemNum)); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 314 | inString >> retval; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 315 | return retval; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 316 | } // GetInt() |
| 317 | |
| 318 | // Extract string data from argument string, which should be colon-delimited |
| 319 | string GetString(const string & argument, int itemNum) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 320 | size_t startPos = -1, endPos = -1; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 321 | |
| 322 | while (itemNum-- > 0) { |
| 323 | startPos = endPos + 1; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 324 | endPos = argument.find(':', startPos); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 325 | } |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 326 | if (endPos == string::npos) |
| 327 | endPos = argument.length(); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 328 | endPos--; |
| 329 | |
| 330 | return argument.substr(startPos, endPos - startPos + 1); |
| 331 | } // GetString() |