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