srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // 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 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 6 | /* 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 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | #define __STDC_LIMIT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 12 | #include <stdio.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <errno.h> |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 15 | #include <fcntl.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 16 | #include <string.h> |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 17 | #include <sys/stat.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 18 | #include <string> |
| 19 | #include <iostream> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 20 | #include "support.h" |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 24 | // As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if |
| 25 | // it's not already defined. This should become unnecessary in the future. |
| 26 | // Note that this is a Linux-only ioctl.... |
| 27 | #ifndef BLKPBSZGET |
| 28 | #define BLKPBSZGET _IO(0x12,123) |
| 29 | #endif |
| 30 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 31 | using namespace std; |
| 32 | |
| 33 | // Get a numeric value from the user, between low and high (inclusive). |
| 34 | // Keeps looping until the user enters a value within that range. |
| 35 | // If user provides no input, def (default value) is returned. |
| 36 | // (If def is outside of the low-high range, an explicit response |
| 37 | // is required.) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 38 | int GetNumber(int low, int high, int def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 39 | int response, num; |
| 40 | char line[255]; |
| 41 | |
| 42 | if (low != high) { // bother only if low and high differ... |
| 43 | response = low - 1; // force one loop by setting response outside range |
| 44 | while ((response < low) || (response > high)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 45 | cout << prompt; |
| 46 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 47 | num = sscanf(line, "%d", &response); |
| 48 | if (num == 1) { // user provided a response |
| 49 | if ((response < low) || (response > high)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 50 | cout << "Value out of range\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 51 | } else { // user hit enter; return default |
| 52 | response = def; |
| 53 | } // if/else |
| 54 | } // while |
| 55 | } else { // low == high, so return this value |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 56 | cout << "Using " << low << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 57 | response = low; |
| 58 | } // else |
| 59 | return (response); |
| 60 | } // GetNumber() |
| 61 | |
| 62 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 63 | char GetYN(void) { |
| 64 | char line[255]; |
| 65 | char response = '\0'; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 66 | char *junk; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 67 | |
| 68 | while ((response != 'Y') && (response != 'N')) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 69 | cout << "(Y/N): "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 70 | junk = fgets(line, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 71 | sscanf(line, "%c", &response); |
| 72 | if (response == 'y') response = 'Y'; |
| 73 | if (response == 'n') response = 'N'; |
| 74 | } // while |
| 75 | return response; |
| 76 | } // GetYN(void) |
| 77 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 78 | // Obtains a sector number, between low and high, from the |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | // user, accepting values prefixed by "+" to add sectors to low, |
| 80 | // or the same with "K", "M", "G", or "T" as suffixes to add |
| 81 | // kilobytes, megabytes, gigabytes, or terabytes, respectively. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 82 | // If a "-" prefix is used, use the high value minus the user- |
| 83 | // specified number of sectors (or KiB, MiB, etc.). Use the def |
| 84 | //value as the default if the user just hits Enter |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 85 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 86 | unsigned long long response; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 87 | int num, plusFlag = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 88 | uint64_t mult = 1; |
| 89 | char suffix; |
| 90 | char line[255]; |
| 91 | |
| 92 | response = low - 1; // Ensure one pass by setting a too-low initial value |
| 93 | while ((response < low) || (response > high)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 94 | cout << prompt; |
| 95 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 96 | |
| 97 | // Remove leading spaces, if present |
| 98 | while (line[0] == ' ') |
| 99 | strcpy(line, &line[1]); |
| 100 | |
| 101 | // If present, flag and remove leading plus sign |
| 102 | if (line[0] == '+') { |
| 103 | plusFlag = 1; |
| 104 | strcpy(line, &line[1]); |
| 105 | } // if |
| 106 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 107 | // If present, flag and remove leading minus sign |
| 108 | if (line[0] == '-') { |
| 109 | plusFlag = -1; |
| 110 | strcpy(line, &line[1]); |
| 111 | } // if |
| 112 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 113 | // Extract numeric response and, if present, suffix |
| 114 | num = sscanf(line, "%llu%c", &response, &suffix); |
| 115 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 116 | // If no response, use default (def) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 117 | if (num <= 0) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 118 | response = (unsigned long long) def; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 119 | suffix = ' '; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 120 | plusFlag = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 121 | } // if |
| 122 | |
| 123 | // Set multiplier based on suffix |
| 124 | switch (suffix) { |
| 125 | case 'K': |
| 126 | case 'k': |
| 127 | mult = (uint64_t) 1024 / SECTOR_SIZE; |
| 128 | break; |
| 129 | case 'M': |
| 130 | case 'm': |
| 131 | mult = (uint64_t) 1048576 / SECTOR_SIZE; |
| 132 | break; |
| 133 | case 'G': |
| 134 | case 'g': |
| 135 | mult = (uint64_t) 1073741824 / SECTOR_SIZE; |
| 136 | break; |
| 137 | case 'T': |
| 138 | case 't': |
| 139 | mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE; |
| 140 | break; |
| 141 | default: |
| 142 | mult = 1; |
| 143 | } // switch |
| 144 | |
| 145 | // Adjust response based on multiplier and plus flag, if present |
| 146 | response *= (unsigned long long) mult; |
| 147 | if (plusFlag == 1) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 148 | // Recompute response based on low part of range (if default = high |
| 149 | // value, which should be the case when prompting for the end of a |
| 150 | // range) or the defaut value (if default != high, which should be |
| 151 | // the case for the first sector of a partition). |
| 152 | if (def == high) |
| 153 | response = response + (unsigned long long) low - UINT64_C(1); |
| 154 | else |
| 155 | response = response + (unsigned long long) def - UINT64_C(1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 156 | } // if |
| 157 | if (plusFlag == -1) { |
| 158 | response = (unsigned long long) high - response; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 159 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 160 | } // while |
| 161 | return ((uint64_t) response); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 162 | } // GetSectorNum() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 163 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 164 | // Takes a size in bytes (in size) and converts this to a size in |
| 165 | // SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string |
| 166 | // form |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 167 | string BytesToSI(uint64_t size) { |
| 168 | string units; |
| 169 | char theValue[99]; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 170 | float sizeInSI; |
| 171 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 172 | theValue[0] = '\0'; |
| 173 | sizeInSI = (float) size; |
| 174 | units = " bytes"; |
| 175 | if (sizeInSI > 1024.0) { |
| 176 | sizeInSI /= 1024.0; |
| 177 | units = " KiB"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 178 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 179 | if (sizeInSI > 1024.0) { |
| 180 | sizeInSI /= 1024.0; |
| 181 | units = " MiB"; |
| 182 | } // if |
| 183 | if (sizeInSI > 1024.0) { |
| 184 | sizeInSI /= 1024.0; |
| 185 | units = " GiB"; |
| 186 | } // if |
| 187 | if (sizeInSI > 1024.0) { |
| 188 | sizeInSI /= 1024.0; |
| 189 | units = " TiB"; |
| 190 | } // if |
| 191 | if (sizeInSI > 1024.0) { |
| 192 | sizeInSI /= 1024.0; |
| 193 | units = " PiB"; |
| 194 | } // if |
| 195 | if (units == " bytes") { // in bytes, so no decimal point |
| 196 | sprintf(theValue, "%1.0f%s", sizeInSI, units.c_str()); |
| 197 | } else { |
| 198 | sprintf(theValue, "%1.1f%s", sizeInSI, units.c_str()); |
| 199 | } // if/else |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 200 | return theValue; |
| 201 | } // BlocksToSI() |
| 202 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 203 | // Return a plain-text name for a partition type. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 204 | // Convert a GUID to a string representation, suitable for display |
| 205 | // to humans.... |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 206 | string GUIDToStr(struct GUIDData theGUID) { |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 207 | unsigned long long blocks[11], block; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 208 | char theString[40]; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 209 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 210 | theString[0] = '\0';; |
| 211 | blocks[0] = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF)); |
| 212 | blocks[1] = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32; |
| 213 | blocks[2] = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48; |
| 214 | blocks[3] = (theGUID.data2 & UINT64_C(0x00000000000000FF)); |
| 215 | blocks[4] = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8; |
| 216 | blocks[5] = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16; |
| 217 | blocks[6] = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24; |
| 218 | blocks[7] = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32; |
| 219 | blocks[8] = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40; |
| 220 | blocks[9] = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48; |
| 221 | blocks[10] = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56; |
| 222 | sprintf(theString, |
| 223 | "%08llX-%04llX-%04llX-%02llX%02llX-%02llX%02llX%02llX%02llX%02llX%02llX", |
| 224 | blocks[0], blocks[1], blocks[2], blocks[3], blocks[4], blocks[5], |
| 225 | blocks[6], blocks[7], blocks[8], blocks[9], blocks[10]); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 226 | return theString; |
| 227 | } // GUIDToStr() |
| 228 | |
| 229 | // Get a GUID from the user |
| 230 | GUIDData GetGUID(void) { |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 231 | unsigned long long part1, part2, part3, part4, part5; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 232 | int entered = 0; |
| 233 | char temp[255], temp2[255]; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 234 | char* junk; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 235 | GUIDData theGUID; |
| 236 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 237 | cout << "\nA GUID is entered in five segments of from two to six bytes, with\n" |
| 238 | << "dashes between segments.\n"; |
| 239 | cout << "Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n" |
| 240 | << "'R' to generate the entire GUID randomly: "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 241 | junk = fgets(temp, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 242 | |
| 243 | // If user entered 'r' or 'R', generate GUID randomly.... |
| 244 | if ((temp[0] == 'r') || (temp[0] == 'R')) { |
| 245 | theGUID.data1 = (uint64_t) rand() * (uint64_t) rand(); |
| 246 | theGUID.data2 = (uint64_t) rand() * (uint64_t) rand(); |
| 247 | entered = 1; |
| 248 | } // if user entered 'R' or 'r' |
| 249 | |
| 250 | // If string length is right for whole entry, try to parse it.... |
| 251 | if ((strlen(temp) == 37) && (entered == 0)) { |
| 252 | strncpy(temp2, &temp[0], 8); |
| 253 | temp2[8] = '\0'; |
| 254 | sscanf(temp2, "%llx", &part1); |
| 255 | strncpy(temp2, &temp[9], 4); |
| 256 | temp2[4] = '\0'; |
| 257 | sscanf(temp2, "%llx", &part2); |
| 258 | strncpy(temp2, &temp[14], 4); |
| 259 | temp2[4] = '\0'; |
| 260 | sscanf(temp2, "%llx", &part3); |
| 261 | theGUID.data1 = (part3 << 48) + (part2 << 32) + part1; |
| 262 | strncpy(temp2, &temp[19], 4); |
| 263 | temp2[4] = '\0'; |
| 264 | sscanf(temp2, "%llx", &part4); |
| 265 | strncpy(temp2, &temp[24], 12); |
| 266 | temp2[12] = '\0'; |
| 267 | sscanf(temp2, "%llx", &part5); |
| 268 | theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) + |
| 269 | ((part4 & UINT64_C(0x00000000000000FF)) << 8) + |
| 270 | ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) + |
| 271 | ((part5 & UINT64_C(0x000000FF00000000)) >> 8) + |
| 272 | ((part5 & UINT64_C(0x00000000FF000000)) << 8) + |
| 273 | ((part5 & UINT64_C(0x0000000000FF0000)) << 24) + |
| 274 | ((part5 & UINT64_C(0x000000000000FF00)) << 40) + |
| 275 | ((part5 & UINT64_C(0x00000000000000FF)) << 56); |
| 276 | entered = 1; |
| 277 | } // if |
| 278 | |
| 279 | // If neither of the above methods of entry was used, use prompted |
| 280 | // entry.... |
| 281 | if (entered == 0) { |
| 282 | sscanf(temp, "%llx", &part1); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 283 | cout << "Enter a two-byte hexadecimal number for the second segment: "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 284 | junk = fgets(temp, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 285 | sscanf(temp, "%llx", &part2); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 286 | cout << "Enter a two-byte hexadecimal number for the third segment: "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 287 | junk = fgets(temp, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 288 | sscanf(temp, "%llx", &part3); |
| 289 | theGUID.data1 = (part3 << 48) + (part2 << 32) + part1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 290 | cout << "Enter a two-byte hexadecimal number for the fourth segment: "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 291 | junk = fgets(temp, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 292 | sscanf(temp, "%llx", &part4); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 293 | cout << "Enter a six-byte hexadecimal number for the fifth segment: "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 294 | junk = fgets(temp, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 295 | sscanf(temp, "%llx", &part5); |
| 296 | theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) + |
| 297 | ((part4 & UINT64_C(0x00000000000000FF)) << 8) + |
| 298 | ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) + |
| 299 | ((part5 & UINT64_C(0x000000FF00000000)) >> 8) + |
| 300 | ((part5 & UINT64_C(0x00000000FF000000)) << 8) + |
| 301 | ((part5 & UINT64_C(0x0000000000FF0000)) << 24) + |
| 302 | ((part5 & UINT64_C(0x000000000000FF00)) << 40) + |
| 303 | ((part5 & UINT64_C(0x00000000000000FF)) << 56); |
| 304 | entered = 1; |
| 305 | } // if/else |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 306 | cout << "New GUID: " << GUIDToStr(theGUID) << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 307 | return theGUID; |
| 308 | } // GetGUID() |
| 309 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 310 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 311 | int IsLittleEndian(void) { |
| 312 | int littleE = 1; // assume little-endian (Intel-style) |
| 313 | union { |
| 314 | uint32_t num; |
| 315 | unsigned char uc[sizeof(uint32_t)]; |
| 316 | } endian; |
| 317 | |
| 318 | endian.num = 1; |
| 319 | if (endian.uc[0] != (unsigned char) 1) { |
| 320 | littleE = 0; |
| 321 | } // if |
| 322 | return (littleE); |
| 323 | } // IsLittleEndian() |
| 324 | |
| 325 | // Reverse the byte order of theValue; numBytes is number of bytes |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 326 | void ReverseBytes(void* theValue, int numBytes) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 327 | char* tempValue = NULL; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 328 | int i; |
| 329 | |
| 330 | tempValue = (char*) malloc(numBytes); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 331 | if (tempValue != NULL) { |
| 332 | memcpy(tempValue, theValue, numBytes); |
| 333 | for (i = 0; i < numBytes; i++) |
| 334 | ((char*) theValue)[i] = tempValue[numBytes - i - 1]; |
| 335 | free(tempValue); |
| 336 | } // if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 337 | } // ReverseBytes() |
| 338 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 339 | // Compute (2 ^ value). Given the return type, value must be 63 or less. |
| 340 | // Used in some bit-fiddling functions |
| 341 | uint64_t PowerOf2(int value) { |
| 342 | uint64_t retval = 1; |
| 343 | int i; |
| 344 | |
| 345 | if ((value < 64) && (value >= 0)) { |
| 346 | for (i = 0; i < value; i++) { |
| 347 | retval *= 2; |
| 348 | } // for |
| 349 | } else retval = 0; |
| 350 | return retval; |
| 351 | } // PowerOf2() |