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 | |
Roderick W. Smith | e3ee733 | 2013-09-24 12:56:11 -0400 | [diff] [blame] | 6 | /* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 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 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 34 | // Reads a string from stdin, returning it as a C++-style string. |
| 35 | // Note that the returned string will NOT include the carriage return |
| 36 | // entered by the user. |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame^] | 37 | #ifdef EFI |
| 38 | extern int __sscanf( const char * str , const char * format , ... ) ; |
| 39 | string ReadString(void) { |
| 40 | string inString; |
| 41 | char efiString[256]; |
| 42 | int stringLength; |
| 43 | |
| 44 | if (fgets(efiString, 255, stdin) != NULL) { |
| 45 | stringLength = strlen(efiString); |
| 46 | if ((stringLength > 0) && (efiString[stringLength - 1] == '\n')) |
| 47 | efiString[stringLength - 1] = '\0'; |
| 48 | inString = efiString; |
| 49 | } else { |
| 50 | inString = ""; |
| 51 | } |
| 52 | return inString; |
| 53 | } // ReadString() |
| 54 | #else |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 55 | string ReadString(void) { |
| 56 | string inString; |
| 57 | |
| 58 | getline(cin, inString); |
srs5694 | a6297b8 | 2012-03-25 16:13:16 -0400 | [diff] [blame] | 59 | if (!cin.good()) |
| 60 | exit(5); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 61 | return inString; |
| 62 | } // ReadString() |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame^] | 63 | #endif |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 64 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 65 | // Get a numeric value from the user, between low and high (inclusive). |
| 66 | // Keeps looping until the user enters a value within that range. |
| 67 | // If user provides no input, def (default value) is returned. |
| 68 | // (If def is outside of the low-high range, an explicit response |
| 69 | // is required.) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 70 | int GetNumber(int low, int high, int def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 71 | int response, num; |
| 72 | char line[255]; |
| 73 | |
| 74 | if (low != high) { // bother only if low and high differ... |
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 << prompt; |
| 77 | cin.getline(line, 255); |
srs5694 | a6297b8 | 2012-03-25 16:13:16 -0400 | [diff] [blame] | 78 | if (!cin.good()) |
| 79 | exit(5); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 80 | num = sscanf(line, "%d", &response); |
| 81 | if (num == 1) { // user provided a response |
| 82 | if ((response < low) || (response > high)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 83 | cout << "Value out of range\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 84 | } else { // user hit enter; return default |
| 85 | response = def; |
| 86 | } // if/else |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 87 | } while ((response < low) || (response > high)); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 88 | } else { // low == high, so return this value |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 89 | cout << "Using " << low << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 90 | response = low; |
| 91 | } // else |
| 92 | return (response); |
| 93 | } // GetNumber() |
| 94 | |
| 95 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 96 | char GetYN(void) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 97 | char response; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 98 | string line; |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 99 | bool again = 0 ; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 100 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 101 | do { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 102 | if ( again ) { cout << "Your option? " ; } |
| 103 | again = 1 ; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 104 | cout << "(Y/N): "; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 105 | line = ReadString(); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 106 | response = toupper(line[0]); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 107 | } while ((response != 'Y') && (response != 'N')); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 108 | return response; |
| 109 | } // GetYN(void) |
| 110 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 111 | // Obtains a sector number, between low and high, from the |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 112 | // user, accepting values prefixed by "+" to add sectors to low, |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 113 | // or the same with "K", "M", "G", "T", or "P" as suffixes to add |
| 114 | // kilobytes, megabytes, gigabytes, terabytes, or petabytes, |
| 115 | // respectively. If a "-" prefix is used, use the high value minus |
| 116 | // the user-specified number of sectors (or KiB, MiB, etc.). Use the |
| 117 | // def value as the default if the user just hits Enter. The sSize is |
| 118 | // the sector size of the device. |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 119 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize, |
| 120 | const string & prompt) { |
| 121 | uint64_t response; |
| 122 | char line[255]; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 123 | |
| 124 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 125 | cout << prompt; |
| 126 | cin.getline(line, 255); |
srs5694 | a6297b8 | 2012-03-25 16:13:16 -0400 | [diff] [blame] | 127 | if (!cin.good()) |
| 128 | exit(5); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 129 | response = IeeeToInt(line, sSize, low, high, def); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 130 | } while ((response < low) || (response > high)); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 131 | return response; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 132 | } // GetSectorNum() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 133 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 134 | // 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] | 135 | // number of sectors. If no units are appended, interprets as the number |
| 136 | // of sectors; otherwise, interprets as number of specified units and |
| 137 | // converts to sectors. For instance, with 512-byte sectors, "1K" converts |
| 138 | // to 2. If value includes a "+", adds low and subtracts 1; if SIValue |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 139 | // inclues a "-", subtracts from high. If IeeeValue is empty, returns def. |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 140 | // Returns final sector value. In case inValue is invalid, returns 0 (a |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 141 | // sector value that's always in use on GPT and therefore invalid); and if |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 142 | // inValue works out to something outside the range low-high, returns the |
| 143 | // computed value; the calling function is responsible for checking the |
| 144 | // validity of this value. |
| 145 | // NOTE: There's a difference in how GCC and VC++ treat oversized values |
| 146 | // (say, "999999999999999999999") read via the ">>" operator; GCC turns |
| 147 | // them into the maximum value for the type, whereas VC++ turns them into |
| 148 | // 0 values. The result is that IeeeToInt() returns UINT64_MAX when |
| 149 | // compiled with GCC (and so the value is rejected), whereas when VC++ |
| 150 | // is used, the default value is returned. |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 151 | uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) { |
| 152 | uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1; |
| 153 | size_t foundAt = 0; |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame^] | 154 | char suffix = ' ', plusFlag = ' '; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 155 | string suffixes = "KMGTPE"; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 156 | int badInput = 0; // flag bad input; once this goes to 1, other values are irrelevant |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 157 | |
| 158 | if (sSize == 0) { |
| 159 | sSize = SECTOR_SIZE; |
Roderick W. Smith | f694803 | 2014-03-29 00:27:33 -0400 | [diff] [blame] | 160 | cerr << "Bug: Sector size invalid in IeeeToInt()!\n"; |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 161 | } // if |
| 162 | |
| 163 | // Remove leading spaces, if present |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 164 | while (inValue[0] == ' ') |
| 165 | inValue.erase(0, 1); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 166 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 167 | // If present, flag and remove leading plus or minus sign |
| 168 | if ((inValue[0] == '+') || (inValue[0] == '-')) { |
| 169 | plusFlag = inValue[0]; |
| 170 | inValue.erase(0, 1); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 171 | } // if |
| 172 | |
| 173 | // Extract numeric response and, if present, suffix |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 174 | istringstream inString(inValue); |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 175 | if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1)) |
| 176 | badInput = 1; |
| 177 | inString >> response >> suffix; |
| 178 | suffix = toupper(suffix); |
| 179 | |
| 180 | // If no response, or if response == 0, use default (def) |
| 181 | if ((inValue.length() == 0) || (response == 0)) { |
| 182 | response = def; |
| 183 | suffix = ' '; |
| 184 | plusFlag = ' '; |
| 185 | } // if |
| 186 | |
| 187 | // Find multiplication and division factors for the suffix |
| 188 | foundAt = suffixes.find(suffix); |
| 189 | if (foundAt != string::npos) { |
| 190 | bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1)); |
| 191 | mult = bytesPerUnit / sSize; |
| 192 | divide = sSize / bytesPerUnit; |
| 193 | } // if |
| 194 | |
| 195 | // Adjust response based on multiplier and plus flag, if present |
| 196 | if (mult > 1) { |
| 197 | if (response > (UINT64_MAX / mult)) |
| 198 | badInput = 1; |
| 199 | else |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 200 | response *= mult; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 201 | } else if (divide > 1) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 202 | response /= divide; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 203 | } // if/elseif |
| 204 | |
| 205 | if (plusFlag == '+') { |
| 206 | // Recompute response based on low part of range (if default == high |
| 207 | // value, which should be the case when prompting for the end of a |
| 208 | // range) or the defaut value (if default != high, which should be |
| 209 | // the case for the first sector of a partition). |
| 210 | if (def == high) { |
| 211 | if (response > 0) |
| 212 | response--; |
| 213 | if (response > (UINT64_MAX - low)) |
| 214 | badInput = 1; |
| 215 | else |
| 216 | response = response + low; |
| 217 | } else { |
| 218 | if (response > (UINT64_MAX - def)) |
| 219 | badInput = 1; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 220 | else |
| 221 | response = response + def; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 222 | } // if/else |
| 223 | } else if (plusFlag == '-') { |
| 224 | if (response > high) |
| 225 | badInput = 1; |
| 226 | else |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 227 | response = high - response; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 228 | } // if |
| 229 | |
| 230 | if (badInput) |
| 231 | response = UINT64_C(0); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 232 | |
| 233 | return response; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 234 | } // IeeeToInt() |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 235 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 236 | // Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB, |
| 237 | // GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in |
| 238 | // units of the sector size or, if that parameter is omitted, in bytes. |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 239 | // (sectorSize defaults to 1). Note that this function uses peculiar |
| 240 | // manual computation of decimal value rather than simply setting |
| 241 | // theValue.precision() because this isn't possible using the available |
| 242 | // EFI library. |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 243 | string BytesToIeee(uint64_t size, uint32_t sectorSize) { |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 244 | uint64_t sizeInIeee; |
| 245 | uint64_t previousIeee; |
| 246 | float decimalIeee; |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame^] | 247 | uint64_t index = 0; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 248 | string units, prefixes = " KMGTPEZ"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 249 | ostringstream theValue; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 250 | |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 251 | sizeInIeee = previousIeee = size * (uint64_t) sectorSize; |
| 252 | while ((sizeInIeee > 1024) && (index < (prefixes.length() - 1))) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 253 | index++; |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 254 | previousIeee = sizeInIeee; |
| 255 | sizeInIeee /= 1024; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 256 | } // while |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 257 | if (prefixes[index] == ' ') { |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 258 | theValue << sizeInIeee << " bytes"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 259 | } else { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 260 | units = " iB"; |
| 261 | units[1] = prefixes[index]; |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 262 | decimalIeee = ((float) previousIeee - |
| 263 | ((float) sizeInIeee * 1024.0) + 51.2) / 102.4; |
| 264 | if (decimalIeee >= 10.0) { |
| 265 | decimalIeee = 0.0; |
| 266 | sizeInIeee++; |
| 267 | } |
| 268 | theValue << sizeInIeee << "." << (uint32_t) decimalIeee << units; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 269 | } // if/else |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 270 | return theValue.str(); |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 271 | } // BytesToIeee() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 272 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 273 | // Converts two consecutive characters in the input string into a |
| 274 | // number, interpreting the string as a hexadecimal number, starting |
| 275 | // at the specified position. |
| 276 | unsigned char StrToHex(const string & input, unsigned int position) { |
| 277 | unsigned char retval = 0x00; |
| 278 | unsigned int temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 279 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 280 | if (input.length() > position) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 281 | sscanf(input.substr(position, 2).c_str(), "%x", &temp); |
| 282 | retval = (unsigned char) temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 283 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 284 | return retval; |
| 285 | } // StrToHex() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 286 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 287 | // Returns 1 if input can be interpreted as a hexadecimal number -- |
| 288 | // all characters must be spaces, digits, or letters A-F (upper- or |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 289 | // lower-case), with at least one valid hexadecimal digit; with the |
| 290 | // exception of the first two characters, which may be "0x"; otherwise |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 291 | // returns 0. |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 292 | int IsHex(string input) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 293 | int isHex = 1, foundHex = 0, i; |
| 294 | |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 295 | if (input.substr(0, 2) == "0x") |
| 296 | input.erase(0, 2); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 297 | for (i = 0; i < (int) input.length(); i++) { |
| 298 | if ((input[i] < '0') || (input[i] > '9')) { |
| 299 | if ((input[i] < 'A') || (input[i] > 'F')) { |
| 300 | if ((input[i] < 'a') || (input[i] > 'f')) { |
| 301 | if ((input[i] != ' ') && (input[i] != '\n')) { |
| 302 | isHex = 0; |
| 303 | } |
| 304 | } else foundHex = 1; |
| 305 | } else foundHex = 1; |
| 306 | } else foundHex = 1; |
| 307 | } // for |
| 308 | if (!foundHex) |
| 309 | isHex = 0; |
| 310 | return isHex; |
| 311 | } // IsHex() |
| 312 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 313 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 314 | int IsLittleEndian(void) { |
| 315 | int littleE = 1; // assume little-endian (Intel-style) |
| 316 | union { |
| 317 | uint32_t num; |
| 318 | unsigned char uc[sizeof(uint32_t)]; |
| 319 | } endian; |
| 320 | |
| 321 | endian.num = 1; |
| 322 | if (endian.uc[0] != (unsigned char) 1) { |
| 323 | littleE = 0; |
| 324 | } // if |
| 325 | return (littleE); |
| 326 | } // IsLittleEndian() |
| 327 | |
| 328 | // Reverse the byte order of theValue; numBytes is number of bytes |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 329 | void ReverseBytes(void* theValue, int numBytes) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 330 | char* tempValue = NULL; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 331 | int i; |
| 332 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 333 | tempValue = new char [numBytes]; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 334 | if (tempValue != NULL) { |
| 335 | memcpy(tempValue, theValue, numBytes); |
| 336 | for (i = 0; i < numBytes; i++) |
| 337 | ((char*) theValue)[i] = tempValue[numBytes - i - 1]; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 338 | delete[] tempValue; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 339 | } else { |
| 340 | cerr << "Could not allocate memory in ReverseBytes()! Terminating\n"; |
| 341 | exit(1); |
| 342 | } // if/else |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 343 | } // ReverseBytes() |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 344 | |
| 345 | // On Windows, display a warning and ask whether to continue. If the user elects |
| 346 | // not to continue, exit immediately. |
| 347 | void WinWarning(void) { |
| 348 | #ifdef _WIN32 |
| 349 | cout << "\a************************************************************************\n" |
Roderick W. Smith | a920398 | 2014-03-29 00:45:59 -0400 | [diff] [blame] | 350 | << "Most versions of Windows cannot boot from a GPT disk except on a UEFI-based\n" |
| 351 | << "computer, and most varieties prior to Vista cannot read GPT disks. Therefore,\n" |
| 352 | << "you should exit now unless you understand the implications of converting MBR\n" |
| 353 | << "to GPT or creating a new GPT disk layout!\n" |
| 354 | << "************************************************************************\n\n"; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 355 | cout << "Are you SURE you want to continue? "; |
| 356 | if (GetYN() != 'Y') |
| 357 | exit(0); |
| 358 | #endif |
| 359 | } // WinWarning() |