summaryrefslogtreecommitdiff
path: root/support.cc
blob: 3cbabf7b2e8b8f057408cbeb89fad5da6d3437c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// support.cc
// Non-class support functions for gdisk program.
// Primarily by Rod Smith, February 2009, but with a few functions
// copied from other sources (see attributions below).

/* This program is copyright (c) 2009-2022 by Roderick W. Smith. It is distributed
  under the terms of the GNU GPL version 2, as detailed in the COPYING file. */

#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <sstream>
#include "support.h"

#include <sys/types.h>

// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
// it's not already defined. This should become unnecessary in the future.
// Note that this is a Linux-only ioctl....
#ifndef BLKPBSZGET
#define BLKPBSZGET _IO(0x12,123)
#endif

using namespace std;

// Reads a string from stdin, returning it as a C++-style string.
// Note that the returned string will NOT include the carriage return
// entered by the user.
#ifdef EFI
extern int __sscanf( const char * str , const char * format , ... ) ;
string ReadString(void) {
   string inString;
   char efiString[256];
   int stringLength;

   if (fgets(efiString, 255, stdin) != NULL) {
      stringLength = strlen(efiString);
      if ((stringLength > 0) && (efiString[stringLength - 1] == '\n'))
          efiString[stringLength - 1] = '\0';
      inString = efiString;
   } else {
      inString = "";
   }
   return inString;
} // ReadString()
#else
string ReadString(void) {
   string inString;

   cout << flush;
   getline(cin, inString);
   if (!cin.good())
      exit(5);
   return inString;
} // ReadString()
#endif

// Get a numeric value from the user, between low and high (inclusive).
// Keeps looping until the user enters a value within that range.
// If user provides no input, def (default value) is returned.
// (If def is outside of the low-high range, an explicit response
// is required.)
uint64_t GetNumber(uint64_t low, uint64_t high, uint64_t def, const string & prompt) {
   uint64_t response, num;
   char line[255];

   if (low != high) { // bother only if low and high differ...
      do {
         cout << prompt << flush;
         cin.getline(line, 255);
         if (!cin.good())
            exit(5);
         num = sscanf(line, "%" PRIu64, &response);
         if (num == 1) { // user provided a response
            if ((response < low) || (response > high))
               cout << "Value out of range\n";
         } else { // user hit enter; return default
            response = def;
         } // if/else
      } while ((response < low) || (response > high));
   } else { // low == high, so return this value
      cout << "Using " << low << "\n";
      response = low;
   } // else
   return (response);
} // GetNumber()

// Gets a Y/N response (and converts lowercase to uppercase)
char GetYN(void) {
   char response;
   string line;
   bool again = 0 ;

   do {
      if ( again ) { cout << "Your option? " ; }
      again = 1 ;
      cout << "(Y/N): " << flush;
      line = ReadString();
      response = toupper(line[0]);
   } while ((response != 'Y') && (response != 'N'));
   return response;
} // GetYN(void)

// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
// number of sectors. If no units are appended, interprets as the number
// of sectors; otherwise, interprets as number of specified units and
// converts to sectors. For instance, with 512-byte sectors, "1K" converts
// to 2. If value includes a "+", adds low and subtracts 1; if inValue
// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
// Returns final sector value. In case inValue is invalid, returns 0 (a
// sector value that's always in use on GPT and therefore invalid); and if
// inValue works out to something outside the range low-high, returns the
// computed value; the calling function is responsible for checking the
// validity of this value.
// If inValue contains a decimal number (e.g., "9.5G"), quietly truncate it
// (to "9G" in this example).
// NOTE: There's a difference in how GCC and VC++ treat oversized values
// (say, "999999999999999999999") read via the ">>" operator; GCC turns
// them into the maximum value for the type, whereas VC++ turns them into
// 0 values. The result is that IeeeToInt() returns UINT64_MAX when
// compiled with GCC (and so the value is rejected), whereas when VC++
// is used, the default value is returned.
uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint32_t sectorAlignment, uint64_t def) {
   uint64_t response = def, bytesPerUnit, mult = 1, divide = 1;
   size_t foundAt = 0;
   char suffix = ' ', plusFlag = ' ';
   string suffixes = "KMGTPE";
   int badInput = 0; // flag bad input; once this goes to 1, other values are irrelevant

   if (sSize == 0) {
      sSize = SECTOR_SIZE;
      cerr << "Bug: Sector size invalid in IeeeToInt()!\n";
   } // if

   // Remove leading spaces, if present
   while (inValue[0] == ' ')
      inValue.erase(0, 1);

   // If present, flag and remove leading plus or minus sign
   if ((inValue[0] == '+') || (inValue[0] == '-')) {
      plusFlag = inValue[0];
      inValue.erase(0, 1);
   } // if

   // Extract numeric response and, if present, suffix
   istringstream inString(inValue);
   if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
      badInput = 1;
   inString >> response >> suffix;
   suffix = toupper(suffix);
   foundAt = suffixes.find(suffix);
   // If suffix is invalid, try to find a valid one. Done because users
   // sometimes enter decimal numbers; when they do, suffix becomes
   // '.', and we need to truncate the number and find the real suffix.
   while (foundAt > (suffixes.length() - 1) && inString.peek() != -1) {
      inString >> suffix;
      foundAt = suffixes.find(suffix);
      suffix = toupper(suffix);
   }

   // If no response, or if response == 0, use default (def)
   if ((inValue.length() == 0) || (response == 0)) {
      response = def;
      suffix = ' ';
      plusFlag = ' ';
   } // if

   // Find multiplication and division factors for the suffix
   if (foundAt != string::npos) {
      bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1));
      mult = bytesPerUnit / sSize;
      divide = sSize / bytesPerUnit;
   } // if

   // Adjust response based on multiplier and plus flag, if present
   if (mult > 1) {
      if (response > (UINT64_MAX / mult))
         badInput = 1;
      else
         response *= mult;
   } else if (divide > 1) {
         response /= divide;
   } // if/elseif

   if (plusFlag == '+') {
      // Recompute response based on low part of range (if default is within
      // sectorAlignment sectors of high, which should be the case when
      // prompting for the end of a range) or the defaut value (if default is
      // further away from the high value, which should be the case for the
      // first sector of a partition).
      if ((high - def) < sectorAlignment) {
         if (response > 0)
            response--;
         if (response > (UINT64_MAX - low))
            badInput = 1;
         else
            response = response + low;
      } else {
         if (response > (UINT64_MAX - def))
            badInput = 1;
         else
            response = response + def;
      } // if/else
   } else if (plusFlag == '-') {
      if (response > high)
         badInput = 1;
      else
         response = high - response;
   } // if   

   if (badInput)
      response = UINT64_C(0);

   return response;
} // IeeeToInt()

// Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB,
// GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in
// units of the sector size or, if that parameter is omitted, in bytes.
// (sectorSize defaults to 1). Note that this function uses peculiar
// manual computation of decimal value rather than simply setting
// theValue.precision() because this isn't possible using the available
// EFI library.
string BytesToIeee(uint64_t size, uint32_t sectorSize) {
   uint64_t sizeInIeee;
   uint64_t previousIeee;
   float decimalIeee;
   uint64_t index = 0;
   string units, prefixes = " KMGTPEZ";
   ostringstream theValue;

   sizeInIeee = previousIeee = size * (uint64_t) sectorSize;
   while ((sizeInIeee > 1024) && (index < (prefixes.length() - 1))) {
      index++;
      previousIeee = sizeInIeee;
      sizeInIeee /= 1024;
   } // while
   if (prefixes[index] == ' ') {
      theValue << sizeInIeee << " bytes";
   } else {
      units = "  iB";
      units[1] = prefixes[index];
      decimalIeee = ((float) previousIeee -
                     ((float) sizeInIeee * 1024.0) + 51.2) / 102.4;
      if (decimalIeee >= 10.0) {
         decimalIeee = 0.0;
         sizeInIeee++;
      }
      theValue << sizeInIeee << "." << (uint32_t) decimalIeee << units;
   } // if/else
   return theValue.str();
} // BytesToIeee()

// Converts two consecutive characters in the input string into a
// number, interpreting the string as a hexadecimal number, starting
// at the specified position.
unsigned char StrToHex(const string & input, unsigned int position) {
   unsigned char retval = 0x00;
   unsigned int temp;

   if (input.length() > position) {
      sscanf(input.substr(position, 2).c_str(), "%x", &temp);
      retval = (unsigned char) temp;
   } // if
   return retval;
} // StrToHex()

// Returns 1 if input can be interpreted as a hexadecimal number --
// all characters must be spaces, digits, or letters A-F (upper- or
// lower-case), with at least one valid hexadecimal digit; with the
// exception of the first two characters, which may be "0x"; otherwise
// returns 0.
int IsHex(string input) {
   int isHex = 1, foundHex = 0, i;

   if (input.substr(0, 2) == "0x")
      input.erase(0, 2);
   for (i = 0; i < (int) input.length(); i++) {
      if ((input[i] < '0') || (input[i] > '9')) {
         if ((input[i] < 'A') || (input[i] > 'F')) {
            if ((input[i] < 'a') || (input[i] > 'f')) {
               if ((input[i] != ' ') && (input[i] != '\n')) {
                  isHex = 0;
               }
            } else foundHex = 1;
         } else foundHex = 1;
      } else foundHex = 1;
   } // for
   if (!foundHex)
      isHex = 0;
   return isHex;
} // IsHex()

// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
int IsLittleEndian(void) {
   int littleE = 1; // assume little-endian (Intel-style)
   union {
      uint32_t num;
      unsigned char uc[sizeof(uint32_t)];
   } endian;

   endian.num = 1;
   if (endian.uc[0] != (unsigned char) 1) {
      littleE = 0;
   } // if
   return (littleE);
} // IsLittleEndian()

// Reverse the byte order of theValue; numBytes is number of bytes
void ReverseBytes(void* theValue, int numBytes) {
   char* tempValue = NULL;
   int i;

   tempValue = new char [numBytes];
   if (tempValue != NULL) {
      memcpy(tempValue, theValue, numBytes);
      for (i = 0; i < numBytes; i++)
         ((char*) theValue)[i] = tempValue[numBytes - i - 1];
      delete[] tempValue;
   } else {
      cerr << "Could not allocate memory in ReverseBytes()! Terminating\n";
      exit(1);
   } // if/else
} // ReverseBytes()

// On Windows, display a warning and ask whether to continue. If the user elects
// not to continue, exit immediately.
void WinWarning(void) {
   #ifdef _WIN32
   cout << "\a************************************************************************\n"
        << "Most versions of Windows cannot boot from a GPT disk except on a UEFI-based\n"
        << "computer, and most varieties prior to Vista cannot read GPT disks. Therefore,\n"
        << "you should exit now unless you understand the implications of converting MBR\n"
        << "to GPT or creating a new GPT disk layout!\n"
        << "************************************************************************\n\n";
   cout << "Are you SURE you want to continue? ";
   if (GetYN() != 'Y')
      exit(0);
   #endif
} // WinWarning()

// Returns the input string in lower case
string ToLower(const string& input) {
   string lower = input; // allocate correct size through copy

   transform(input.begin(), input.end(), lower.begin(), ::tolower);
   return lower;
} // ToLower()