blob: 4e7c874f50aaf8c033fd2832879526d7e2d5d054 [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>
srs56945a608532011-03-17 13:53:01 -040018#include <unicode/ustdio.h>
srs5694fed16d02010-01-27 23:03:40 -050019#include <string>
20#include <iostream>
srs569408bb0da2010-02-19 17:19:55 -050021#include <sstream>
srs5694e7b4ff92009-08-18 13:16:10 -040022#include "support.h"
23
24#include <sys/types.h>
25
srs56945d58fe02010-01-03 20:57:08 -050026// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
27// it's not already defined. This should become unnecessary in the future.
28// Note that this is a Linux-only ioctl....
29#ifndef BLKPBSZGET
30#define BLKPBSZGET _IO(0x12,123)
31#endif
32
srs5694e7b4ff92009-08-18 13:16:10 -040033using namespace std;
34
srs56945a608532011-03-17 13:53:01 -040035// Reads a string from stdin, returning it as a C++-style string.
36// Note that the returned string will NOT include the carriage return
37// entered by the user.
38string ReadString(void) {
39 string inString;
40
41 getline(cin, inString);
42 return inString;
43} // ReadString()
srs5694bf8950c2011-03-12 01:23:12 -050044
srs5694e7b4ff92009-08-18 13:16:10 -040045// Get a numeric value from the user, between low and high (inclusive).
46// Keeps looping until the user enters a value within that range.
47// If user provides no input, def (default value) is returned.
48// (If def is outside of the low-high range, an explicit response
49// is required.)
srs5694fed16d02010-01-27 23:03:40 -050050int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040051 int response, num;
52 char line[255];
53
54 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040055 do {
srs5694fed16d02010-01-27 23:03:40 -050056 cout << prompt;
57 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040058 num = sscanf(line, "%d", &response);
59 if (num == 1) { // user provided a response
60 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050061 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040062 } else { // user hit enter; return default
63 response = def;
64 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040065 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040066 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050067 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040068 response = low;
69 } // else
70 return (response);
71} // GetNumber()
72
73// Gets a Y/N response (and converts lowercase to uppercase)
74char GetYN(void) {
srs56940873e9d2010-10-07 13:00:45 -040075 char response;
srs56945a608532011-03-17 13:53:01 -040076 string line;
srs5694e7b4ff92009-08-18 13:16:10 -040077
srs56940873e9d2010-10-07 13:00:45 -040078 do {
srs5694fed16d02010-01-27 23:03:40 -050079 cout << "(Y/N): ";
srs56945a608532011-03-17 13:53:01 -040080 line = ReadString();
srs569401f7f082011-03-15 23:53:31 -040081 response = toupper(line[0]);
srs56940873e9d2010-10-07 13:00:45 -040082 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040083 return response;
84} // GetYN(void)
85
srs5694e4ac11e2009-08-31 10:13:04 -040086// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040087// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040088// or the same with "K", "M", "G", "T", or "P" as suffixes to add
89// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
90// respectively. If a "-" prefix is used, use the high value minus
91// the user-specified number of sectors (or KiB, MiB, etc.). Use the
92// def value as the default if the user just hits Enter. The sSize is
93// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050094uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
95 const string & prompt) {
96 uint64_t response;
97 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040098
99 do {
srs5694fed16d02010-01-27 23:03:40 -0500100 cout << prompt;
101 cin.getline(line, 255);
srs569401f7f082011-03-15 23:53:31 -0400102 response = IeeeToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -0400103 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500104 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400105} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400106
srs569401f7f082011-03-15 23:53:31 -0400107// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
srs5694df9d3632011-01-08 18:33:24 -0500108// number of sectors. If no units are appended, interprets as the number
109// of sectors; otherwise, interprets as number of specified units and
110// converts to sectors. For instance, with 512-byte sectors, "1K" converts
111// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
srs569401f7f082011-03-15 23:53:31 -0400112// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
srs5694df9d3632011-01-08 18:33:24 -0500113// Returns integral sector value.
srs569401f7f082011-03-15 23:53:31 -0400114uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
115 uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1;
116 size_t foundAt = 0;
117 char suffix, plusFlag = ' ';
118 string suffixes = "KMGTPE";
srs5694df9d3632011-01-08 18:33:24 -0500119
120 if (sSize == 0) {
121 sSize = SECTOR_SIZE;
122 cerr << "Bug: Sector size invalid in SIToInt()!\n";
123 } // if
124
125 // Remove leading spaces, if present
srs569401f7f082011-03-15 23:53:31 -0400126 while (inValue[0] == ' ')
127 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500128
srs569401f7f082011-03-15 23:53:31 -0400129 // If present, flag and remove leading plus or minus sign
130 if ((inValue[0] == '+') || (inValue[0] == '-')) {
131 plusFlag = inValue[0];
132 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500133 } // if
134
135 // Extract numeric response and, if present, suffix
srs569401f7f082011-03-15 23:53:31 -0400136 istringstream inString(inValue);
137 if (((inString.peek() >= '0') && (inString.peek() <= '9')) || (inString.peek() == -1)) {
138 inString >> response >> suffix;
139 suffix = toupper(suffix);
140
141 // If no response, or if response == 0, use default (def)
142 if ((inValue.length() == 0) || (response == 0)) {
143 response = def;
144 suffix = ' ';
145 plusFlag = 0;
146 } // if
147
148 // Find multiplication and division factors for the suffix
149 foundAt = suffixes.find(suffix);
150 if (foundAt != string::npos) {
151 bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1));
152 mult = bytesPerUnit / sSize;
153 divide = sSize / bytesPerUnit;
154 } // if
155
156 // Adjust response based on multiplier and plus flag, if present
157 if (mult > 1)
158 response *= mult;
159 else if (divide > 1)
160 response /= divide;
161 if (plusFlag == '+') {
162 // Recompute response based on low part of range (if default = high
163 // value, which should be the case when prompting for the end of a
164 // range) or the defaut value (if default != high, which should be
165 // the case for the first sector of a partition).
166 if (def == high)
167 response = response + low - UINT64_C(1);
168 else
169 response = response + def;
170 } else if (plusFlag == '-') {
171 response = high - response;
172 } // if
173 } else { // user input is invalid
srs5694df9d3632011-01-08 18:33:24 -0500174 response = high + UINT64_C(1);
srs569401f7f082011-03-15 23:53:31 -0400175 } // if/else
srs5694df9d3632011-01-08 18:33:24 -0500176
177 return response;
srs569401f7f082011-03-15 23:53:31 -0400178} // IeeeToInt()
srs5694df9d3632011-01-08 18:33:24 -0500179
srs569401f7f082011-03-15 23:53:31 -0400180// Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB,
181// GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in
182// units of the sector size or, if that parameter is omitted, in bytes.
srs56940873e9d2010-10-07 13:00:45 -0400183// (sectorSize defaults to 1).
srs569401f7f082011-03-15 23:53:31 -0400184string BytesToIeee(uint64_t size, uint32_t sectorSize) {
185 float sizeInIeee;
srs56945a608532011-03-17 13:53:01 -0400186 unsigned int index = 0;
srs569401f7f082011-03-15 23:53:31 -0400187 string units, prefixes = " KMGTPE";
srs569408bb0da2010-02-19 17:19:55 -0500188 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400189
srs569401f7f082011-03-15 23:53:31 -0400190 sizeInIeee = size * (float) sectorSize;
191 while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) {
192 index++;
193 sizeInIeee /= 1024.0;
194 } // while
srs569408bb0da2010-02-19 17:19:55 -0500195 theValue.setf(ios::fixed);
srs569401f7f082011-03-15 23:53:31 -0400196 if (prefixes[index] == ' ') {
197 units = " bytes";
srs569408bb0da2010-02-19 17:19:55 -0500198 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500199 } else {
srs569401f7f082011-03-15 23:53:31 -0400200 units = " iB";
201 units[1] = prefixes[index];
srs569408bb0da2010-02-19 17:19:55 -0500202 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500203 } // if/else
srs569401f7f082011-03-15 23:53:31 -0400204 theValue << sizeInIeee << units;
srs569408bb0da2010-02-19 17:19:55 -0500205 return theValue.str();
srs569401f7f082011-03-15 23:53:31 -0400206} // BlocksToIeee()
srs5694e7b4ff92009-08-18 13:16:10 -0400207
srs56946699b012010-02-04 00:55:30 -0500208// Converts two consecutive characters in the input string into a
209// number, interpreting the string as a hexadecimal number, starting
210// at the specified position.
211unsigned char StrToHex(const string & input, unsigned int position) {
212 unsigned char retval = 0x00;
213 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400214
srs56945a608532011-03-17 13:53:01 -0400215 if (input.length() > position) {
srs56946699b012010-02-04 00:55:30 -0500216 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
217 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400218 } // if
srs56946699b012010-02-04 00:55:30 -0500219 return retval;
220} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400221
srs56940873e9d2010-10-07 13:00:45 -0400222// Returns 1 if input can be interpreted as a hexadecimal number --
223// all characters must be spaces, digits, or letters A-F (upper- or
224// lower-case), with at least one valid hexadecimal digit; otherwise
225// returns 0.
226int IsHex(const string & input) {
227 int isHex = 1, foundHex = 0, i;
228
229 for (i = 0; i < (int) input.length(); i++) {
230 if ((input[i] < '0') || (input[i] > '9')) {
231 if ((input[i] < 'A') || (input[i] > 'F')) {
232 if ((input[i] < 'a') || (input[i] > 'f')) {
233 if ((input[i] != ' ') && (input[i] != '\n')) {
234 isHex = 0;
235 }
236 } else foundHex = 1;
237 } else foundHex = 1;
238 } else foundHex = 1;
239 } // for
240 if (!foundHex)
241 isHex = 0;
242 return isHex;
243} // IsHex()
244
srs56942a9f5da2009-08-26 00:48:01 -0400245// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
246int IsLittleEndian(void) {
247 int littleE = 1; // assume little-endian (Intel-style)
248 union {
249 uint32_t num;
250 unsigned char uc[sizeof(uint32_t)];
251 } endian;
252
253 endian.num = 1;
254 if (endian.uc[0] != (unsigned char) 1) {
255 littleE = 0;
256 } // if
257 return (littleE);
258} // IsLittleEndian()
259
260// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400261void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500262 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400263 int i;
264
srs5694cb76c672010-02-11 22:22:22 -0500265 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500266 if (tempValue != NULL) {
267 memcpy(tempValue, theValue, numBytes);
268 for (i = 0; i < numBytes; i++)
269 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500270 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500271 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400272} // ReverseBytes()