blob: f51973a5ea1c33d7f416d0502b247f883c3de914 [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)) {
srs5694c2f6e0c2011-03-16 02:42:33 -040036 cerr << "Error! Failed fgets() in ReadCString()\n";
37 if ((numchars > 0) && (inStr != NULL))
38 inStr[0] = '\0';
srs5694bf8950c2011-03-12 01:23:12 -050039 } // if
srs5694bf8950c2011-03-12 01:23:12 -050040} // ReadCString()
41
srs5694e7b4ff92009-08-18 13:16:10 -040042// Get a numeric value from the user, between low and high (inclusive).
43// Keeps looping until the user enters a value within that range.
44// If user provides no input, def (default value) is returned.
45// (If def is outside of the low-high range, an explicit response
46// is required.)
srs5694fed16d02010-01-27 23:03:40 -050047int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040048 int response, num;
49 char line[255];
50
51 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040052 do {
srs5694fed16d02010-01-27 23:03:40 -050053 cout << prompt;
54 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040055 num = sscanf(line, "%d", &response);
56 if (num == 1) { // user provided a response
57 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050058 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040059 } else { // user hit enter; return default
60 response = def;
61 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040062 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040063 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050064 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040065 response = low;
66 } // else
67 return (response);
68} // GetNumber()
69
70// Gets a Y/N response (and converts lowercase to uppercase)
71char GetYN(void) {
72 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040073 char response;
srs5694e7b4ff92009-08-18 13:16:10 -040074
srs56940873e9d2010-10-07 13:00:45 -040075 do {
srs5694fed16d02010-01-27 23:03:40 -050076 cout << "(Y/N): ";
srs56949a46b042011-03-15 00:34:10 -040077 ReadCString(line, sizeof(line));
srs569401f7f082011-03-15 23:53:31 -040078 response = toupper(line[0]);
srs56940873e9d2010-10-07 13:00:45 -040079 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040080 return response;
81} // GetYN(void)
82
srs5694e4ac11e2009-08-31 10:13:04 -040083// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040084// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040085// or the same with "K", "M", "G", "T", or "P" as suffixes to add
86// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
87// respectively. If a "-" prefix is used, use the high value minus
88// the user-specified number of sectors (or KiB, MiB, etc.). Use the
89// def value as the default if the user just hits Enter. The sSize is
90// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050091uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
92 const string & prompt) {
93 uint64_t response;
94 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040095
96 do {
srs5694fed16d02010-01-27 23:03:40 -050097 cout << prompt;
98 cin.getline(line, 255);
srs569401f7f082011-03-15 23:53:31 -040099 response = IeeeToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -0400100 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500101 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400102} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400103
srs569401f7f082011-03-15 23:53:31 -0400104// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
srs5694df9d3632011-01-08 18:33:24 -0500105// number of sectors. If no units are appended, interprets as the number
106// of sectors; otherwise, interprets as number of specified units and
107// converts to sectors. For instance, with 512-byte sectors, "1K" converts
108// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
srs569401f7f082011-03-15 23:53:31 -0400109// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
srs5694df9d3632011-01-08 18:33:24 -0500110// Returns integral sector value.
srs569401f7f082011-03-15 23:53:31 -0400111uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
112 uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1;
113 size_t foundAt = 0;
114 char suffix, plusFlag = ' ';
115 string suffixes = "KMGTPE";
srs5694df9d3632011-01-08 18:33:24 -0500116
117 if (sSize == 0) {
118 sSize = SECTOR_SIZE;
119 cerr << "Bug: Sector size invalid in SIToInt()!\n";
120 } // if
121
122 // Remove leading spaces, if present
srs569401f7f082011-03-15 23:53:31 -0400123 while (inValue[0] == ' ')
124 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500125
srs569401f7f082011-03-15 23:53:31 -0400126 // If present, flag and remove leading plus or minus sign
127 if ((inValue[0] == '+') || (inValue[0] == '-')) {
128 plusFlag = inValue[0];
129 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500130 } // if
131
132 // Extract numeric response and, if present, suffix
srs569401f7f082011-03-15 23:53:31 -0400133 istringstream inString(inValue);
134 if (((inString.peek() >= '0') && (inString.peek() <= '9')) || (inString.peek() == -1)) {
135 inString >> response >> suffix;
136 suffix = toupper(suffix);
137
138 // If no response, or if response == 0, use default (def)
139 if ((inValue.length() == 0) || (response == 0)) {
140 response = def;
141 suffix = ' ';
142 plusFlag = 0;
143 } // if
144
145 // Find multiplication and division factors for the suffix
146 foundAt = suffixes.find(suffix);
147 if (foundAt != string::npos) {
148 bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1));
149 mult = bytesPerUnit / sSize;
150 divide = sSize / bytesPerUnit;
151 } // if
152
153 // Adjust response based on multiplier and plus flag, if present
154 if (mult > 1)
155 response *= mult;
156 else if (divide > 1)
157 response /= divide;
158 if (plusFlag == '+') {
159 // Recompute response based on low part of range (if default = high
160 // value, which should be the case when prompting for the end of a
161 // range) or the defaut value (if default != high, which should be
162 // the case for the first sector of a partition).
163 if (def == high)
164 response = response + low - UINT64_C(1);
165 else
166 response = response + def;
167 } else if (plusFlag == '-') {
168 response = high - response;
169 } // if
170 } else { // user input is invalid
srs5694df9d3632011-01-08 18:33:24 -0500171 response = high + UINT64_C(1);
srs569401f7f082011-03-15 23:53:31 -0400172 } // if/else
srs5694df9d3632011-01-08 18:33:24 -0500173
174 return response;
srs569401f7f082011-03-15 23:53:31 -0400175} // IeeeToInt()
srs5694df9d3632011-01-08 18:33:24 -0500176
srs569401f7f082011-03-15 23:53:31 -0400177// Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB,
178// GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in
179// units of the sector size or, if that parameter is omitted, in bytes.
srs56940873e9d2010-10-07 13:00:45 -0400180// (sectorSize defaults to 1).
srs569401f7f082011-03-15 23:53:31 -0400181string BytesToIeee(uint64_t size, uint32_t sectorSize) {
182 float sizeInIeee;
183 uint index = 0;
184 string units, prefixes = " KMGTPE";
srs569408bb0da2010-02-19 17:19:55 -0500185 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400186
srs569401f7f082011-03-15 23:53:31 -0400187 sizeInIeee = size * (float) sectorSize;
188 while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) {
189 index++;
190 sizeInIeee /= 1024.0;
191 } // while
srs569408bb0da2010-02-19 17:19:55 -0500192 theValue.setf(ios::fixed);
srs569401f7f082011-03-15 23:53:31 -0400193 if (prefixes[index] == ' ') {
194 units = " bytes";
srs569408bb0da2010-02-19 17:19:55 -0500195 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500196 } else {
srs569401f7f082011-03-15 23:53:31 -0400197 units = " iB";
198 units[1] = prefixes[index];
srs569408bb0da2010-02-19 17:19:55 -0500199 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500200 } // if/else
srs569401f7f082011-03-15 23:53:31 -0400201 theValue << sizeInIeee << units;
srs569408bb0da2010-02-19 17:19:55 -0500202 return theValue.str();
srs569401f7f082011-03-15 23:53:31 -0400203} // BlocksToIeee()
srs5694e7b4ff92009-08-18 13:16:10 -0400204
srs56946699b012010-02-04 00:55:30 -0500205// Converts two consecutive characters in the input string into a
206// number, interpreting the string as a hexadecimal number, starting
207// at the specified position.
208unsigned char StrToHex(const string & input, unsigned int position) {
209 unsigned char retval = 0x00;
210 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400211
srs56946699b012010-02-04 00:55:30 -0500212 if (input.length() >= (position + 2)) {
213 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
214 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400215 } // if
srs56946699b012010-02-04 00:55:30 -0500216 return retval;
217} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400218
srs56940873e9d2010-10-07 13:00:45 -0400219// Returns 1 if input can be interpreted as a hexadecimal number --
220// all characters must be spaces, digits, or letters A-F (upper- or
221// lower-case), with at least one valid hexadecimal digit; otherwise
222// returns 0.
223int IsHex(const string & input) {
224 int isHex = 1, foundHex = 0, i;
225
226 for (i = 0; i < (int) input.length(); i++) {
227 if ((input[i] < '0') || (input[i] > '9')) {
228 if ((input[i] < 'A') || (input[i] > 'F')) {
229 if ((input[i] < 'a') || (input[i] > 'f')) {
230 if ((input[i] != ' ') && (input[i] != '\n')) {
231 isHex = 0;
232 }
233 } else foundHex = 1;
234 } else foundHex = 1;
235 } else foundHex = 1;
236 } // for
237 if (!foundHex)
238 isHex = 0;
239 return isHex;
240} // IsHex()
241
srs56942a9f5da2009-08-26 00:48:01 -0400242// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
243int IsLittleEndian(void) {
244 int littleE = 1; // assume little-endian (Intel-style)
245 union {
246 uint32_t num;
247 unsigned char uc[sizeof(uint32_t)];
248 } endian;
249
250 endian.num = 1;
251 if (endian.uc[0] != (unsigned char) 1) {
252 littleE = 0;
253 } // if
254 return (littleE);
255} // IsLittleEndian()
256
257// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400258void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500259 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400260 int i;
261
srs5694cb76c672010-02-11 22:22:22 -0500262 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500263 if (tempValue != NULL) {
264 memcpy(tempValue, theValue, numBytes);
265 for (i = 0; i < numBytes; i++)
266 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500267 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500268 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400269} // ReverseBytes()
270
srs56949ddc14b2010-08-22 22:44:42 -0400271// Extract integer data from argument string, which should be colon-delimited
272uint64_t GetInt(const string & argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500273 uint64_t retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400274
srs569464cbd172011-03-01 22:03:54 -0500275 istringstream inString(GetString(argument, itemNum));
srs56949ddc14b2010-08-22 22:44:42 -0400276 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400277 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400278} // GetInt()
279
280// Extract string data from argument string, which should be colon-delimited
srs56949a46b042011-03-15 00:34:10 -0400281// If string begins with a colon, that colon is skipped in the counting. If an
282// invalid itemNum is specified, returns an empty string.
srs5694bf8950c2011-03-12 01:23:12 -0500283string GetString(string argument, int itemNum) {
srs56949a46b042011-03-15 00:34:10 -0400284 size_t startPos = 0, endPos = 0;
285 string retVal = "";
286 int foundLast = 0;
287 int numFound = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400288
srs56949a46b042011-03-15 00:34:10 -0400289 if (argument[0] == ':')
290 argument.erase(0, 1);
291 while ((numFound < itemNum) && (!foundLast)) {
srs569464cbd172011-03-01 22:03:54 -0500292 endPos = argument.find(':', startPos);
srs56949a46b042011-03-15 00:34:10 -0400293 numFound++;
294 if (endPos == string::npos) {
295 foundLast = 1;
296 endPos = argument.length();
297 } else if (numFound < itemNum) {
298 startPos = endPos + 1;
299 } // if/elseif
300 } // while
301 if ((numFound == itemNum) && (numFound > 0))
302 retVal = argument.substr(startPos, endPos - startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400303
srs56949a46b042011-03-15 00:34:10 -0400304 return retVal;
srs569401f7f082011-03-15 23:53:31 -0400305} // GetString()