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