diff --git a/src/main.c b/src/main.c index 6aedbc5..4d5c031 100644 --- a/src/main.c +++ b/src/main.c @@ -102,7 +102,6 @@ puts( "Manage virtual disks\n" " Usage: vvd OPERATION [FILE [OPTIONS]]\n" - " vvd PAGE\n" "\nOPERATION\n" " info Get vdisk image information\n" " new Create new empty vdisk\n" @@ -114,8 +113,8 @@ "\nOPTIONS\n" " --raw Open as RAW\n" " --create-raw Create as RAW\n" - " --create-dyn Create dynamic vdisk\n" - " --create-fixed Create fixed vdisk\n" + " --create-dyn Create vdisk as dynamic\n" + " --create-fixed Create vdisk as fixed\n" ); exit(EXIT_SUCCESS); } @@ -184,14 +183,14 @@ #ifdef _WIN32 #define MAIN int wmain(int argc, wchar_t **argv) -#define s(quote) L##quote +#define vstr(quote) L##quote #define PSTR "%ls" int scmp(const wchar_t *s, const wchar_t *t) { return wcscmp(s, t) == 0; } #else #define MAIN int main(int argc, char **argv) -#define s(quote) ##quote +#define vstr(quote) ##quote #define PSTR "%s" int scmp(const char *s, const char *t) { return strcmp(s, t) == 0; @@ -219,22 +218,22 @@ // // Open flags // - if (scmp(arg, s("--raw"))) { + if (scmp(arg, vstr("--raw"))) { oflags |= VDISK_RAW; continue; } // // Create flags // - if (scmp(arg, s("--create-raw"))) { + if (scmp(arg, vstr("--create-raw"))) { cflags |= VDISK_RAW; continue; } - if (scmp(arg, s("--create-dynamic"))) { + if (scmp(arg, vstr("--create-dynamic"))) { cflags |= VDISK_CREATE_DYN; continue; } - if (scmp(arg, s("--create-fixed"))) { + if (scmp(arg, vstr("--create-fixed"))) { cflags |= VDISK_CREATE_FIXED; continue; } @@ -259,7 +258,7 @@ const _vchar *action = argv[1]; // Action time - if (scmp(action, s("info"))) { + if (scmp(action, vstr("info"))) { if (file_input == NULL) { fputs("main: missing vdisk", stderr); return EXIT_FAILURE; @@ -270,7 +269,7 @@ } return vvd_info(&vdin); } - if (scmp(action, s("map"))) { + if (scmp(action, vstr("map"))) { if (file_input == NULL) { fputs("main: missing vdisk", stderr); return EXIT_FAILURE; @@ -281,19 +280,19 @@ } return vvd_map(&vdin); } - if (scmp(action, s("compact"))) { + if (scmp(action, vstr("compact"))) { fputs("main: not implemented", stderr); return EXIT_FAILURE; } - if (scmp(action, s("resize"))) { + if (scmp(action, vstr("resize"))) { fputs("main: not implemented", stderr); return EXIT_FAILURE; } - if (scmp(action, s("defrag"))) { + if (scmp(action, vstr("defrag"))) { fputs("main: not implemented", stderr); return EXIT_FAILURE; } - if (scmp(action, s("new"))) { + if (scmp(action, vstr("new"))) { fputs("main: not implemented", stderr); /*if (argc < 4) // Needs vvd -N TYPE SIZE goto L_MISSING_ARGS; @@ -314,14 +313,14 @@ */ return EXIT_FAILURE; } - if (scmp(action, s("version")) || scmp(action, s("--version"))) + if (scmp(action, vstr("version")) || scmp(action, vstr("--version"))) version(); - if (scmp(action, s("help")) || scmp(action, s("--help"))) + if (scmp(action, vstr("help")) || scmp(action, vstr("--help"))) help(); - if (scmp(action, s("license")) || scmp(action, s("--license"))) + if (scmp(action, vstr("license")) || scmp(action, vstr("--license"))) license(); #ifdef INCLUDE_TESTS - if (scmp(action, s("test"))) + if (scmp(action, vstr("test"))) test(); #endif diff --git a/src/utils.c b/src/utils.c index 5600330..0a3e2cf 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2,6 +2,10 @@ #include #include "utils.h" +// +// fbins +// + void fbins(uint64_t n, char *buf) { // Lazy code 2.0, sorry float f = n; char *fs; @@ -22,6 +26,10 @@ snprintf(buf, 16, fs, f); } +// +// sbinf +// + int sbinf(_vchar *input, uint64_t *size) { float f; char c; @@ -53,28 +61,49 @@ return 0; } +// +// bswap16 +// + uint16_t bswap16(uint16_t s) { return s >> 8 | s << 8; } +// +// bswap32 +// + uint32_t bswap32(uint32_t s) { - return (s & 0x000000ff) << 24 | (s & 0x0000ff00) << 8 | - (s & 0x00ff0000) >> 8 | (s & 0xff000000) >> 24; + v = (v >> 16) | (v << 16); + return ((v & 0xFF00FF00) >> 8) | ((v & 0x00FF00FF) << 8); } +// +// bswap64 +// + uint64_t bswap64(uint64_t s) { - uint32_t *p = (uint32_t*)&s; - return (uint64_t)bswap32(p[0]) << 32 | (uint64_t)bswap32(p[1]); + v = (v >> 32) | (v << 32); + v = ((v & 0xFFFF0000FFFF0000) >> 16) | ((v & 0x0000FFFF0000FFFF) << 16); + return ((v & 0xFF00FF00FF00FF00) >> 8) | ((v & 0x00FF00FF00FF00FF) << 8); } -void print_a(char *p, uint8_t *a, size_t s) { +// +// print_array +// + +void print_array(char *p, uint8_t *a, size_t s) { size_t i = 0; - printl(p); + putout(p); while (--s) printf(" %02X", a[i++]); putchar('\n'); } +// +// extcmp +// + int extcmp(_vchar *s1, const _vchar *s2) { #ifdef _WIN32 wchar_t *ext = wcsrchr(s1, '.'); @@ -95,14 +124,26 @@ #endif } -void printl(const char *s) { +// +// putout +// + +void putout(const char *s) { fputs(s, stdout); } +// +// pow2 +// + int pow2(int n) { return (n & (n - 1)) == 0; } +// +// wstra +// + void wstra(char16 *src, char *dest, int dsize) { size_t bi = 0; // buffer index if (src[bi] == 0) { diff --git a/src/utils.h b/src/utils.h index 3c632c5..afe6797 100644 --- a/src/utils.h +++ b/src/utils.h @@ -26,7 +26,7 @@ /** * Function alias of fputs(*, stdout) to avoid argument bloat. */ -void printl(const char *s); +void putout(const char *s); #define BIN_FLENGTH 16 /** @@ -41,7 +41,7 @@ /** * Print array with prefix string */ -void print_a(char *p, uint8_t *a, size_t s); +void print_array(char *p, uint8_t *a, size_t s); /** * Byte swap a 16-bit (2-Byte) value. @@ -65,6 +65,7 @@ #define EXT_VMDK "vmdk" #define EXT_VHD "vhd" #endif + /** * Compare file path with constant extension string. * @@ -82,6 +83,7 @@ /** * Convert an UTF-16 string to an ASCII string and returns number of charaters * copied with the destination's maximum buffer size. This function fills up - * upto dsize-1 characters and inserts a null terminator. + * upto dsize-1 characters and inserts a null terminator and is useful when + * processing GPT entries. */ void wstra(char16 *src, char *dest, int dsize);