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 | 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. |
| 37 | string ReadString(void) { |
| 38 | string inString; |
| 39 | |
| 40 | getline(cin, inString); |
srs5694 | 0541b56 | 2011-12-18 16:35:25 -0500 | [diff] [blame] | 41 | if (!cin) |
| 42 | cin.clear(); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 43 | return inString; |
| 44 | } // ReadString() |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 45 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 46 | // Get a numeric value from the user, between low and high (inclusive). |
| 47 | // Keeps looping until the user enters a value within that range. |
| 48 | // If user provides no input, def (default value) is returned. |
| 49 | // (If def is outside of the low-high range, an explicit response |
| 50 | // is required.) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 51 | int GetNumber(int low, int high, int def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 52 | int response, num; |
| 53 | char line[255]; |
| 54 | |
| 55 | if (low != high) { // bother only if low and high differ... |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 56 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 57 | cout << prompt; |
| 58 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 59 | num = sscanf(line, "%d", &response); |
| 60 | if (num == 1) { // user provided a response |
| 61 | if ((response < low) || (response > high)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 62 | cout << "Value out of range\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 63 | } else { // user hit enter; return default |
| 64 | response = def; |
| 65 | } // if/else |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 66 | } while ((response < low) || (response > high)); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 67 | } else { // low == high, so return this value |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 68 | cout << "Using " << low << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 69 | response = low; |
| 70 | } // else |
| 71 | return (response); |
| 72 | } // GetNumber() |
| 73 | |
| 74 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 75 | char GetYN(void) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 76 | char response; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 77 | string line; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 78 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 79 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 80 | cout << "(Y/N): "; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 81 | line = ReadString(); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 82 | response = toupper(line[0]); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 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 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 103 | response = IeeeToInt(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 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 108 | // 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] | 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 |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 113 | // inclues a "-", subtracts from high. If IeeeValue is empty, returns def. |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 114 | // Returns final sector value. In case inValue is invalid, returns 0 (a |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 115 | // 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] | 116 | // inValue works out to something outside the range low-high, returns the |
| 117 | // computed value; the calling function is responsible for checking the |
| 118 | // validity of this value. |
| 119 | // NOTE: There's a difference in how GCC and VC++ treat oversized values |
| 120 | // (say, "999999999999999999999") read via the ">>" operator; GCC turns |
| 121 | // them into the maximum value for the type, whereas VC++ turns them into |
| 122 | // 0 values. The result is that IeeeToInt() returns UINT64_MAX when |
| 123 | // compiled with GCC (and so the value is rejected), whereas when VC++ |
| 124 | // is used, the default value is returned. |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 125 | uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) { |
| 126 | uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1; |
| 127 | size_t foundAt = 0; |
| 128 | char suffix, plusFlag = ' '; |
| 129 | string suffixes = "KMGTPE"; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 130 | 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] | 131 | |
| 132 | if (sSize == 0) { |
| 133 | sSize = SECTOR_SIZE; |
| 134 | cerr << "Bug: Sector size invalid in SIToInt()!\n"; |
| 135 | } // if |
| 136 | |
| 137 | // Remove leading spaces, if present |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 138 | while (inValue[0] == ' ') |
| 139 | inValue.erase(0, 1); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 140 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 141 | // If present, flag and remove leading plus or minus sign |
| 142 | if ((inValue[0] == '+') || (inValue[0] == '-')) { |
| 143 | plusFlag = inValue[0]; |
| 144 | inValue.erase(0, 1); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 145 | } // if |
| 146 | |
| 147 | // Extract numeric response and, if present, suffix |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 148 | istringstream inString(inValue); |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 149 | if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1)) |
| 150 | badInput = 1; |
| 151 | inString >> response >> suffix; |
| 152 | suffix = toupper(suffix); |
| 153 | |
| 154 | // If no response, or if response == 0, use default (def) |
| 155 | if ((inValue.length() == 0) || (response == 0)) { |
| 156 | response = def; |
| 157 | suffix = ' '; |
| 158 | plusFlag = ' '; |
| 159 | } // if |
| 160 | |
| 161 | // Find multiplication and division factors for the suffix |
| 162 | foundAt = suffixes.find(suffix); |
| 163 | if (foundAt != string::npos) { |
| 164 | bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1)); |
| 165 | mult = bytesPerUnit / sSize; |
| 166 | divide = sSize / bytesPerUnit; |
| 167 | } // if |
| 168 | |
| 169 | // Adjust response based on multiplier and plus flag, if present |
| 170 | if (mult > 1) { |
| 171 | if (response > (UINT64_MAX / mult)) |
| 172 | badInput = 1; |
| 173 | else |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 174 | response *= mult; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 175 | } else if (divide > 1) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 176 | response /= divide; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 177 | } // if/elseif |
| 178 | |
| 179 | if (plusFlag == '+') { |
| 180 | // Recompute response based on low part of range (if default == high |
| 181 | // value, which should be the case when prompting for the end of a |
| 182 | // range) or the defaut value (if default != high, which should be |
| 183 | // the case for the first sector of a partition). |
| 184 | if (def == high) { |
| 185 | if (response > 0) |
| 186 | response--; |
| 187 | if (response > (UINT64_MAX - low)) |
| 188 | badInput = 1; |
| 189 | else |
| 190 | response = response + low; |
| 191 | } else { |
| 192 | if (response > (UINT64_MAX - def)) |
| 193 | badInput = 1; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 194 | else |
| 195 | response = response + def; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 196 | } // if/else |
| 197 | } else if (plusFlag == '-') { |
| 198 | if (response > high) |
| 199 | badInput = 1; |
| 200 | else |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 201 | response = high - response; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 202 | } // if |
| 203 | |
| 204 | if (badInput) |
| 205 | response = UINT64_C(0); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 206 | |
| 207 | return response; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 208 | } // IeeeToInt() |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 209 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 210 | // Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB, |
| 211 | // GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in |
| 212 | // units of the sector size or, if that parameter is omitted, in bytes. |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 213 | // (sectorSize defaults to 1). |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 214 | string BytesToIeee(uint64_t size, uint32_t sectorSize) { |
| 215 | float sizeInIeee; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 216 | unsigned int index = 0; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 217 | string units, prefixes = " KMGTPEZ"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 218 | ostringstream theValue; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 219 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 220 | sizeInIeee = size * (float) sectorSize; |
| 221 | while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) { |
| 222 | index++; |
| 223 | sizeInIeee /= 1024.0; |
| 224 | } // while |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 225 | theValue.setf(ios::fixed); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 226 | if (prefixes[index] == ' ') { |
| 227 | units = " bytes"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 228 | theValue.precision(0); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 229 | } else { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 230 | units = " iB"; |
| 231 | units[1] = prefixes[index]; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 232 | theValue.precision(1); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 233 | } // if/else |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 234 | theValue << sizeInIeee << units; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 235 | return theValue.str(); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 236 | } // BlocksToIeee() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 237 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 238 | // Converts two consecutive characters in the input string into a |
| 239 | // number, interpreting the string as a hexadecimal number, starting |
| 240 | // at the specified position. |
| 241 | unsigned char StrToHex(const string & input, unsigned int position) { |
| 242 | unsigned char retval = 0x00; |
| 243 | unsigned int temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 244 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 245 | if (input.length() > position) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 246 | sscanf(input.substr(position, 2).c_str(), "%x", &temp); |
| 247 | retval = (unsigned char) temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 248 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 249 | return retval; |
| 250 | } // StrToHex() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 251 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 252 | // Returns 1 if input can be interpreted as a hexadecimal number -- |
| 253 | // all characters must be spaces, digits, or letters A-F (upper- or |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 254 | // lower-case), with at least one valid hexadecimal digit; with the |
| 255 | // exception of the first two characters, which may be "0x"; otherwise |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 256 | // returns 0. |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 257 | int IsHex(string input) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 258 | int isHex = 1, foundHex = 0, i; |
| 259 | |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 260 | if (input.substr(0, 2) == "0x") |
| 261 | input.erase(0, 2); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 262 | for (i = 0; i < (int) input.length(); i++) { |
| 263 | if ((input[i] < '0') || (input[i] > '9')) { |
| 264 | if ((input[i] < 'A') || (input[i] > 'F')) { |
| 265 | if ((input[i] < 'a') || (input[i] > 'f')) { |
| 266 | if ((input[i] != ' ') && (input[i] != '\n')) { |
| 267 | isHex = 0; |
| 268 | } |
| 269 | } else foundHex = 1; |
| 270 | } else foundHex = 1; |
| 271 | } else foundHex = 1; |
| 272 | } // for |
| 273 | if (!foundHex) |
| 274 | isHex = 0; |
| 275 | return isHex; |
| 276 | } // IsHex() |
| 277 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 278 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 279 | int IsLittleEndian(void) { |
| 280 | int littleE = 1; // assume little-endian (Intel-style) |
| 281 | union { |
| 282 | uint32_t num; |
| 283 | unsigned char uc[sizeof(uint32_t)]; |
| 284 | } endian; |
| 285 | |
| 286 | endian.num = 1; |
| 287 | if (endian.uc[0] != (unsigned char) 1) { |
| 288 | littleE = 0; |
| 289 | } // if |
| 290 | return (littleE); |
| 291 | } // IsLittleEndian() |
| 292 | |
| 293 | // Reverse the byte order of theValue; numBytes is number of bytes |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 294 | void ReverseBytes(void* theValue, int numBytes) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 295 | char* tempValue = NULL; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 296 | int i; |
| 297 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 298 | tempValue = new char [numBytes]; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 299 | if (tempValue != NULL) { |
| 300 | memcpy(tempValue, theValue, numBytes); |
| 301 | for (i = 0; i < numBytes; i++) |
| 302 | ((char*) theValue)[i] = tempValue[numBytes - i - 1]; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 303 | delete[] tempValue; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 304 | } else { |
| 305 | cerr << "Could not allocate memory in ReverseBytes()! Terminating\n"; |
| 306 | exit(1); |
| 307 | } // if/else |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 308 | } // ReverseBytes() |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 309 | |
| 310 | // On Windows, display a warning and ask whether to continue. If the user elects |
| 311 | // not to continue, exit immediately. |
| 312 | void WinWarning(void) { |
| 313 | #ifdef _WIN32 |
| 314 | cout << "\a************************************************************************\n" |
| 315 | << "Most versions of Windows cannot boot from a GPT disk, and most varieties\n" |
| 316 | << "prior to Vista cannot read GPT disks. Therefore, you should exit now\n" |
| 317 | << "unless you understand the implications of converting MBR to GPT or creating\n" |
| 318 | << "a new GPT disk layout!\n" |
| 319 | << "************************************************************************\n\n"; |
| 320 | cout << "Are you SURE you want to continue? "; |
| 321 | if (GetYN() != 'Y') |
| 322 | exit(0); |
| 323 | #endif |
| 324 | } // WinWarning() |