blob: 24bcffbe5c91375b0f725c3f49d00144918decb3 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001// 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
srs5694221e0872009-08-29 15:00:31 -04006/* 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
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
srs5694e7b4ff92009-08-18 13:16:10 -040012#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdint.h>
14#include <errno.h>
srs5694e4ac11e2009-08-31 10:13:04 -040015#include <fcntl.h>
srs5694fed16d02010-01-27 23:03:40 -050016#include <string.h>
srs5694e35eb1b2009-09-14 00:29:34 -040017#include <sys/stat.h>
srs5694fed16d02010-01-27 23:03:40 -050018#include <string>
19#include <iostream>
srs569408bb0da2010-02-19 17:19:55 -050020#include <sstream>
srs5694e7b4ff92009-08-18 13:16:10 -040021#include "support.h"
22
23#include <sys/types.h>
24
srs56945d58fe02010-01-03 20:57:08 -050025// 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
srs5694e7b4ff92009-08-18 13:16:10 -040032using namespace std;
33
srs56945a608532011-03-17 13:53:01 -040034// 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.
37string ReadString(void) {
38 string inString;
39
40 getline(cin, inString);
41 return inString;
42} // ReadString()
srs5694bf8950c2011-03-12 01:23:12 -050043
srs5694e7b4ff92009-08-18 13:16:10 -040044// Get a numeric value from the user, between low and high (inclusive).
45// Keeps looping until the user enters a value within that range.
46// If user provides no input, def (default value) is returned.
47// (If def is outside of the low-high range, an explicit response
48// is required.)
srs5694fed16d02010-01-27 23:03:40 -050049int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040050 int response, num;
51 char line[255];
52
53 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040054 do {
srs5694fed16d02010-01-27 23:03:40 -050055 cout << prompt;
56 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040057 num = sscanf(line, "%d", &response);
58 if (num == 1) { // user provided a response
59 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050060 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040061 } else { // user hit enter; return default
62 response = def;
63 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040064 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040065 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050066 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040067 response = low;
68 } // else
69 return (response);
70} // GetNumber()
71
72// Gets a Y/N response (and converts lowercase to uppercase)
73char GetYN(void) {
srs56940873e9d2010-10-07 13:00:45 -040074 char response;
srs56945a608532011-03-17 13:53:01 -040075 string line;
srs5694e7b4ff92009-08-18 13:16:10 -040076
srs56940873e9d2010-10-07 13:00:45 -040077 do {
srs5694fed16d02010-01-27 23:03:40 -050078 cout << "(Y/N): ";
srs56945a608532011-03-17 13:53:01 -040079 line = ReadString();
srs569401f7f082011-03-15 23:53:31 -040080 response = toupper(line[0]);
srs56940873e9d2010-10-07 13:00:45 -040081 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040082 return response;
83} // GetYN(void)
84
srs5694e4ac11e2009-08-31 10:13:04 -040085// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040086// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040087// or the same with "K", "M", "G", "T", or "P" as suffixes to add
88// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
89// respectively. If a "-" prefix is used, use the high value minus
90// the user-specified number of sectors (or KiB, MiB, etc.). Use the
91// def value as the default if the user just hits Enter. The sSize is
92// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050093uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
94 const string & prompt) {
95 uint64_t response;
96 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040097
98 do {
srs5694fed16d02010-01-27 23:03:40 -050099 cout << prompt;
100 cin.getline(line, 255);
srs569401f7f082011-03-15 23:53:31 -0400101 response = IeeeToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -0400102 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500103 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400104} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400105
srs569401f7f082011-03-15 23:53:31 -0400106// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
srs5694df9d3632011-01-08 18:33:24 -0500107// number of sectors. If no units are appended, interprets as the number
108// of sectors; otherwise, interprets as number of specified units and
109// converts to sectors. For instance, with 512-byte sectors, "1K" converts
110// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
srs569401f7f082011-03-15 23:53:31 -0400111// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
srs56946aae2a92011-06-10 01:16:51 -0400112// Returns final sector value. In case inValue is invalid, returns 0 (a
113// sector value that's always is use on GPT and therefore invalid); and if
114// inValue works out to something outside the range low-high, returns the
115// computed value; the calling function is responsible for checking the
116// validity of this value.
117// NOTE: There's a difference in how GCC and VC++ treat oversized values
118// (say, "999999999999999999999") read via the ">>" operator; GCC turns
119// them into the maximum value for the type, whereas VC++ turns them into
120// 0 values. The result is that IeeeToInt() returns UINT64_MAX when
121// compiled with GCC (and so the value is rejected), whereas when VC++
122// is used, the default value is returned.
srs569401f7f082011-03-15 23:53:31 -0400123uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
124 uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1;
125 size_t foundAt = 0;
126 char suffix, plusFlag = ' ';
127 string suffixes = "KMGTPE";
srs56946aae2a92011-06-10 01:16:51 -0400128 int badInput = 0; // flag bad input; once this goes to 1, other values are irrelevant
srs5694df9d3632011-01-08 18:33:24 -0500129
130 if (sSize == 0) {
131 sSize = SECTOR_SIZE;
132 cerr << "Bug: Sector size invalid in SIToInt()!\n";
133 } // if
134
135 // Remove leading spaces, if present
srs569401f7f082011-03-15 23:53:31 -0400136 while (inValue[0] == ' ')
137 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500138
srs569401f7f082011-03-15 23:53:31 -0400139 // If present, flag and remove leading plus or minus sign
140 if ((inValue[0] == '+') || (inValue[0] == '-')) {
141 plusFlag = inValue[0];
142 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500143 } // if
144
145 // Extract numeric response and, if present, suffix
srs569401f7f082011-03-15 23:53:31 -0400146 istringstream inString(inValue);
srs56946aae2a92011-06-10 01:16:51 -0400147 if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
148 badInput = 1;
149 inString >> response >> suffix;
150 suffix = toupper(suffix);
151
152 // If no response, or if response == 0, use default (def)
153 if ((inValue.length() == 0) || (response == 0)) {
154 response = def;
155 suffix = ' ';
156 plusFlag = ' ';
157 } // if
158
159 // Find multiplication and division factors for the suffix
160 foundAt = suffixes.find(suffix);
161 if (foundAt != string::npos) {
162 bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1));
163 mult = bytesPerUnit / sSize;
164 divide = sSize / bytesPerUnit;
165 } // if
166
167 // Adjust response based on multiplier and plus flag, if present
168 if (mult > 1) {
169 if (response > (UINT64_MAX / mult))
170 badInput = 1;
171 else
srs569401f7f082011-03-15 23:53:31 -0400172 response *= mult;
srs56946aae2a92011-06-10 01:16:51 -0400173 } else if (divide > 1) {
srs569401f7f082011-03-15 23:53:31 -0400174 response /= divide;
srs56946aae2a92011-06-10 01:16:51 -0400175 } // if/elseif
176
177 if (plusFlag == '+') {
178 // Recompute response based on low part of range (if default == high
179 // value, which should be the case when prompting for the end of a
180 // range) or the defaut value (if default != high, which should be
181 // the case for the first sector of a partition).
182 if (def == high) {
183 if (response > 0)
184 response--;
185 if (response > (UINT64_MAX - low))
186 badInput = 1;
187 else
188 response = response + low;
189 } else {
190 if (response > (UINT64_MAX - def))
191 badInput = 1;
srs569401f7f082011-03-15 23:53:31 -0400192 else
193 response = response + def;
srs56946aae2a92011-06-10 01:16:51 -0400194 } // if/else
195 } else if (plusFlag == '-') {
196 if (response > high)
197 badInput = 1;
198 else
srs569401f7f082011-03-15 23:53:31 -0400199 response = high - response;
srs56946aae2a92011-06-10 01:16:51 -0400200 } // if
201
202 if (badInput)
203 response = UINT64_C(0);
srs5694df9d3632011-01-08 18:33:24 -0500204
205 return response;
srs569401f7f082011-03-15 23:53:31 -0400206} // IeeeToInt()
srs5694df9d3632011-01-08 18:33:24 -0500207
srs569401f7f082011-03-15 23:53:31 -0400208// Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB,
209// GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in
210// units of the sector size or, if that parameter is omitted, in bytes.
srs56940873e9d2010-10-07 13:00:45 -0400211// (sectorSize defaults to 1).
srs569401f7f082011-03-15 23:53:31 -0400212string BytesToIeee(uint64_t size, uint32_t sectorSize) {
213 float sizeInIeee;
srs56945a608532011-03-17 13:53:01 -0400214 unsigned int index = 0;
srs56946aae2a92011-06-10 01:16:51 -0400215 string units, prefixes = " KMGTPEZ";
srs569408bb0da2010-02-19 17:19:55 -0500216 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400217
srs569401f7f082011-03-15 23:53:31 -0400218 sizeInIeee = size * (float) sectorSize;
219 while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) {
220 index++;
221 sizeInIeee /= 1024.0;
222 } // while
srs569408bb0da2010-02-19 17:19:55 -0500223 theValue.setf(ios::fixed);
srs569401f7f082011-03-15 23:53:31 -0400224 if (prefixes[index] == ' ') {
225 units = " bytes";
srs569408bb0da2010-02-19 17:19:55 -0500226 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500227 } else {
srs569401f7f082011-03-15 23:53:31 -0400228 units = " iB";
229 units[1] = prefixes[index];
srs569408bb0da2010-02-19 17:19:55 -0500230 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500231 } // if/else
srs569401f7f082011-03-15 23:53:31 -0400232 theValue << sizeInIeee << units;
srs569408bb0da2010-02-19 17:19:55 -0500233 return theValue.str();
srs569401f7f082011-03-15 23:53:31 -0400234} // BlocksToIeee()
srs5694e7b4ff92009-08-18 13:16:10 -0400235
srs56946699b012010-02-04 00:55:30 -0500236// Converts two consecutive characters in the input string into a
237// number, interpreting the string as a hexadecimal number, starting
238// at the specified position.
239unsigned char StrToHex(const string & input, unsigned int position) {
240 unsigned char retval = 0x00;
241 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400242
srs56945a608532011-03-17 13:53:01 -0400243 if (input.length() > position) {
srs56946699b012010-02-04 00:55:30 -0500244 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
245 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400246 } // if
srs56946699b012010-02-04 00:55:30 -0500247 return retval;
248} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400249
srs56940873e9d2010-10-07 13:00:45 -0400250// Returns 1 if input can be interpreted as a hexadecimal number --
251// all characters must be spaces, digits, or letters A-F (upper- or
srs56946aae2a92011-06-10 01:16:51 -0400252// lower-case), with at least one valid hexadecimal digit; with the
253// exception of the first two characters, which may be "0x"; otherwise
srs56940873e9d2010-10-07 13:00:45 -0400254// returns 0.
srs56946aae2a92011-06-10 01:16:51 -0400255int IsHex(string input) {
srs56940873e9d2010-10-07 13:00:45 -0400256 int isHex = 1, foundHex = 0, i;
257
srs56946aae2a92011-06-10 01:16:51 -0400258 if (input.substr(0, 2) == "0x")
259 input.erase(0, 2);
srs56940873e9d2010-10-07 13:00:45 -0400260 for (i = 0; i < (int) input.length(); i++) {
261 if ((input[i] < '0') || (input[i] > '9')) {
262 if ((input[i] < 'A') || (input[i] > 'F')) {
263 if ((input[i] < 'a') || (input[i] > 'f')) {
264 if ((input[i] != ' ') && (input[i] != '\n')) {
265 isHex = 0;
266 }
267 } else foundHex = 1;
268 } else foundHex = 1;
269 } else foundHex = 1;
270 } // for
271 if (!foundHex)
272 isHex = 0;
273 return isHex;
274} // IsHex()
275
srs56942a9f5da2009-08-26 00:48:01 -0400276// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
277int IsLittleEndian(void) {
278 int littleE = 1; // assume little-endian (Intel-style)
279 union {
280 uint32_t num;
281 unsigned char uc[sizeof(uint32_t)];
282 } endian;
283
284 endian.num = 1;
285 if (endian.uc[0] != (unsigned char) 1) {
286 littleE = 0;
287 } // if
288 return (littleE);
289} // IsLittleEndian()
290
291// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400292void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500293 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400294 int i;
295
srs5694cb76c672010-02-11 22:22:22 -0500296 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500297 if (tempValue != NULL) {
298 memcpy(tempValue, theValue, numBytes);
299 for (i = 0; i < numBytes; i++)
300 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500301 delete[] tempValue;
srs56946aae2a92011-06-10 01:16:51 -0400302 } else {
303 cerr << "Could not allocate memory in ReverseBytes()! Terminating\n";
304 exit(1);
305 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -0400306} // ReverseBytes()