blob: 1994cd24d5dd491cd9a761fe059dcc6328b7c7f8 [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
srs56949a46b042011-03-15 00:34:10 -040034void ReadCString(char *inStr, int numchars) {
35 if (!fgets(inStr, numchars, stdin)) {
srs5694bf8950c2011-03-12 01:23:12 -050036 cerr << "Critical error! Failed fgets() in ReadCString()\n";
37 exit(1);
38 } // if
srs5694bf8950c2011-03-12 01:23:12 -050039} // ReadCString()
40
srs5694e7b4ff92009-08-18 13:16:10 -040041// Get a numeric value from the user, between low and high (inclusive).
42// Keeps looping until the user enters a value within that range.
43// If user provides no input, def (default value) is returned.
44// (If def is outside of the low-high range, an explicit response
45// is required.)
srs5694fed16d02010-01-27 23:03:40 -050046int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040047 int response, num;
48 char line[255];
49
50 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040051 do {
srs5694fed16d02010-01-27 23:03:40 -050052 cout << prompt;
53 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040054 num = sscanf(line, "%d", &response);
55 if (num == 1) { // user provided a response
56 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050057 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040058 } else { // user hit enter; return default
59 response = def;
60 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040061 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040062 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050063 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040064 response = low;
65 } // else
66 return (response);
67} // GetNumber()
68
69// Gets a Y/N response (and converts lowercase to uppercase)
70char GetYN(void) {
71 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040072 char response;
srs5694e7b4ff92009-08-18 13:16:10 -040073
srs56940873e9d2010-10-07 13:00:45 -040074 do {
srs5694fed16d02010-01-27 23:03:40 -050075 cout << "(Y/N): ";
srs56949a46b042011-03-15 00:34:10 -040076 ReadCString(line, sizeof(line));
srs569401f7f082011-03-15 23:53:31 -040077 response = toupper(line[0]);
srs56940873e9d2010-10-07 13:00:45 -040078 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040079 return response;
80} // GetYN(void)
81
srs5694e4ac11e2009-08-31 10:13:04 -040082// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040083// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040084// or the same with "K", "M", "G", "T", or "P" as suffixes to add
85// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
86// respectively. If a "-" prefix is used, use the high value minus
87// the user-specified number of sectors (or KiB, MiB, etc.). Use the
88// def value as the default if the user just hits Enter. The sSize is
89// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050090uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
91 const string & prompt) {
92 uint64_t response;
93 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040094
95 do {
srs5694fed16d02010-01-27 23:03:40 -050096 cout << prompt;
97 cin.getline(line, 255);
srs569401f7f082011-03-15 23:53:31 -040098 response = IeeeToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -040099 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500100 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400101} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400102
srs569401f7f082011-03-15 23:53:31 -0400103// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
srs5694df9d3632011-01-08 18:33:24 -0500104// number of sectors. If no units are appended, interprets as the number
105// of sectors; otherwise, interprets as number of specified units and
106// converts to sectors. For instance, with 512-byte sectors, "1K" converts
107// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
srs569401f7f082011-03-15 23:53:31 -0400108// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
srs5694df9d3632011-01-08 18:33:24 -0500109// Returns integral sector value.
srs569401f7f082011-03-15 23:53:31 -0400110uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
111 uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1;
112 size_t foundAt = 0;
113 char suffix, plusFlag = ' ';
114 string suffixes = "KMGTPE";
srs5694df9d3632011-01-08 18:33:24 -0500115
116 if (sSize == 0) {
117 sSize = SECTOR_SIZE;
118 cerr << "Bug: Sector size invalid in SIToInt()!\n";
119 } // if
120
121 // Remove leading spaces, if present
srs569401f7f082011-03-15 23:53:31 -0400122 while (inValue[0] == ' ')
123 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500124
srs569401f7f082011-03-15 23:53:31 -0400125 // If present, flag and remove leading plus or minus sign
126 if ((inValue[0] == '+') || (inValue[0] == '-')) {
127 plusFlag = inValue[0];
128 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500129 } // if
130
131 // Extract numeric response and, if present, suffix
srs569401f7f082011-03-15 23:53:31 -0400132 istringstream inString(inValue);
133 if (((inString.peek() >= '0') && (inString.peek() <= '9')) || (inString.peek() == -1)) {
134 inString >> response >> suffix;
135 suffix = toupper(suffix);
136
137 // If no response, or if response == 0, use default (def)
138 if ((inValue.length() == 0) || (response == 0)) {
139 response = def;
140 suffix = ' ';
141 plusFlag = 0;
142 } // if
143
144 // Find multiplication and division factors for the suffix
145 foundAt = suffixes.find(suffix);
146 if (foundAt != string::npos) {
147 bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1));
148 mult = bytesPerUnit / sSize;
149 divide = sSize / bytesPerUnit;
150 } // if
151
152 // Adjust response based on multiplier and plus flag, if present
153 if (mult > 1)
154 response *= mult;
155 else if (divide > 1)
156 response /= divide;
157 if (plusFlag == '+') {
158 // Recompute response based on low part of range (if default = high
159 // value, which should be the case when prompting for the end of a
160 // range) or the defaut value (if default != high, which should be
161 // the case for the first sector of a partition).
162 if (def == high)
163 response = response + low - UINT64_C(1);
164 else
165 response = response + def;
166 } else if (plusFlag == '-') {
167 response = high - response;
168 } // if
169 } else { // user input is invalid
srs5694df9d3632011-01-08 18:33:24 -0500170 response = high + UINT64_C(1);
srs569401f7f082011-03-15 23:53:31 -0400171 } // if/else
srs5694df9d3632011-01-08 18:33:24 -0500172
173 return response;
srs569401f7f082011-03-15 23:53:31 -0400174} // IeeeToInt()
srs5694df9d3632011-01-08 18:33:24 -0500175
srs569401f7f082011-03-15 23:53:31 -0400176// Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB,
177// GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in
178// units of the sector size or, if that parameter is omitted, in bytes.
srs56940873e9d2010-10-07 13:00:45 -0400179// (sectorSize defaults to 1).
srs569401f7f082011-03-15 23:53:31 -0400180string BytesToIeee(uint64_t size, uint32_t sectorSize) {
181 float sizeInIeee;
182 uint index = 0;
183 string units, prefixes = " KMGTPE";
srs569408bb0da2010-02-19 17:19:55 -0500184 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400185
srs569401f7f082011-03-15 23:53:31 -0400186 sizeInIeee = size * (float) sectorSize;
187 while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) {
188 index++;
189 sizeInIeee /= 1024.0;
190 } // while
srs569408bb0da2010-02-19 17:19:55 -0500191 theValue.setf(ios::fixed);
srs569401f7f082011-03-15 23:53:31 -0400192 if (prefixes[index] == ' ') {
193 units = " bytes";
srs569408bb0da2010-02-19 17:19:55 -0500194 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500195 } else {
srs569401f7f082011-03-15 23:53:31 -0400196 units = " iB";
197 units[1] = prefixes[index];
srs569408bb0da2010-02-19 17:19:55 -0500198 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500199 } // if/else
srs569401f7f082011-03-15 23:53:31 -0400200 theValue << sizeInIeee << units;
srs569408bb0da2010-02-19 17:19:55 -0500201 return theValue.str();
srs569401f7f082011-03-15 23:53:31 -0400202} // BlocksToIeee()
srs5694e7b4ff92009-08-18 13:16:10 -0400203
srs56946699b012010-02-04 00:55:30 -0500204// Converts two consecutive characters in the input string into a
205// number, interpreting the string as a hexadecimal number, starting
206// at the specified position.
207unsigned char StrToHex(const string & input, unsigned int position) {
208 unsigned char retval = 0x00;
209 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400210
srs56946699b012010-02-04 00:55:30 -0500211 if (input.length() >= (position + 2)) {
212 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
213 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400214 } // if
srs56946699b012010-02-04 00:55:30 -0500215 return retval;
216} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400217
srs56940873e9d2010-10-07 13:00:45 -0400218// Returns 1 if input can be interpreted as a hexadecimal number --
219// all characters must be spaces, digits, or letters A-F (upper- or
220// lower-case), with at least one valid hexadecimal digit; otherwise
221// returns 0.
222int IsHex(const string & input) {
223 int isHex = 1, foundHex = 0, i;
224
225 for (i = 0; i < (int) input.length(); i++) {
226 if ((input[i] < '0') || (input[i] > '9')) {
227 if ((input[i] < 'A') || (input[i] > 'F')) {
228 if ((input[i] < 'a') || (input[i] > 'f')) {
229 if ((input[i] != ' ') && (input[i] != '\n')) {
230 isHex = 0;
231 }
232 } else foundHex = 1;
233 } else foundHex = 1;
234 } else foundHex = 1;
235 } // for
236 if (!foundHex)
237 isHex = 0;
238 return isHex;
239} // IsHex()
240
srs56942a9f5da2009-08-26 00:48:01 -0400241// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
242int IsLittleEndian(void) {
243 int littleE = 1; // assume little-endian (Intel-style)
244 union {
245 uint32_t num;
246 unsigned char uc[sizeof(uint32_t)];
247 } endian;
248
249 endian.num = 1;
250 if (endian.uc[0] != (unsigned char) 1) {
251 littleE = 0;
252 } // if
253 return (littleE);
254} // IsLittleEndian()
255
256// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400257void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500258 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400259 int i;
260
srs5694cb76c672010-02-11 22:22:22 -0500261 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500262 if (tempValue != NULL) {
263 memcpy(tempValue, theValue, numBytes);
264 for (i = 0; i < numBytes; i++)
265 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500266 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500267 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400268} // ReverseBytes()
269
srs56949ddc14b2010-08-22 22:44:42 -0400270// Extract integer data from argument string, which should be colon-delimited
271uint64_t GetInt(const string & argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500272 uint64_t retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400273
srs569464cbd172011-03-01 22:03:54 -0500274 istringstream inString(GetString(argument, itemNum));
srs56949ddc14b2010-08-22 22:44:42 -0400275 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400276 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400277} // GetInt()
278
279// Extract string data from argument string, which should be colon-delimited
srs56949a46b042011-03-15 00:34:10 -0400280// If string begins with a colon, that colon is skipped in the counting. If an
281// invalid itemNum is specified, returns an empty string.
srs5694bf8950c2011-03-12 01:23:12 -0500282string GetString(string argument, int itemNum) {
srs56949a46b042011-03-15 00:34:10 -0400283 size_t startPos = 0, endPos = 0;
284 string retVal = "";
285 int foundLast = 0;
286 int numFound = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400287
srs56949a46b042011-03-15 00:34:10 -0400288 if (argument[0] == ':')
289 argument.erase(0, 1);
290 while ((numFound < itemNum) && (!foundLast)) {
srs569464cbd172011-03-01 22:03:54 -0500291 endPos = argument.find(':', startPos);
srs56949a46b042011-03-15 00:34:10 -0400292 numFound++;
293 if (endPos == string::npos) {
294 foundLast = 1;
295 endPos = argument.length();
296 } else if (numFound < itemNum) {
297 startPos = endPos + 1;
298 } // if/elseif
299 } // while
300 if ((numFound == itemNum) && (numFound > 0))
301 retVal = argument.substr(startPos, endPos - startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400302
srs56949a46b042011-03-15 00:34:10 -0400303 return retVal;
srs569401f7f082011-03-15 23:53:31 -0400304} // GetString()