Mercurial > msvpvf
comparison src/common.c @ 88:af4ed765c1ac
*: add IUP GUI
a lot simpler than win32, but I assume there's more going on in
the background that I don't know about :)
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 14 Jul 2025 02:39:24 -0400 |
| parents | 8f90d5addda9 |
| children |
comparison
equal
deleted
inserted
replaced
| 87:2e819b84d7c0 | 88:af4ed765c1ac |
|---|---|
| 1 #include "common.h" | 1 #include "common.h" |
| 2 | 2 |
| 3 #include <string.h> | 3 #include <string.h> |
| 4 #include <stdlib.h> | |
| 4 | 5 |
| 5 /* hardcoded magic values; stored at 0x18... */ | 6 /* hardcoded magic values; stored at 0x18... */ |
| 6 static const uint8_t magic_veg[16] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A}; | 7 static const uint8_t magic_veg[16] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A}; |
| 7 static const uint8_t magic_vf[16] = {0xF6, 0x1B, 0x3C, 0x53, 0x35, 0xD6, 0xF3, 0x43, 0x8A, 0x90, 0x64, 0xB8, 0x87, 0x23, 0x1F, 0x7F}; | 8 static const uint8_t magic_vf[16] = {0xF6, 0x1B, 0x3C, 0x53, 0x35, 0xD6, 0xF3, 0x43, 0x8A, 0x90, 0x64, 0xB8, 0x87, 0x23, 0x1F, 0x7F}; |
| 8 | 9 |
| 9 int set_file_information(FILE* target, uint8_t version, enum types type) { | 10 int set_file_information(FILE *target, uint8_t version, enum types type) |
| 11 { | |
| 10 const uint8_t* magic = (type == TYPES_VF) ? magic_vf : magic_veg; | 12 const uint8_t* magic = (type == TYPES_VF) ? magic_vf : magic_veg; |
| 11 | 13 |
| 12 if (fseek(target, 0x46, SEEK_SET)) | 14 if (fseek(target, 0x46, SEEK_SET)) |
| 13 return -1; | 15 return -1; |
| 14 | 16 |
| 22 return -1; | 24 return -1; |
| 23 | 25 |
| 24 return 0; | 26 return 0; |
| 25 } | 27 } |
| 26 | 28 |
| 27 int get_file_information(FILE* input, uint8_t* version, enum types* type) { | 29 int get_file_information(FILE *input, uint8_t *version, enum types *type) |
| 30 { | |
| 28 uint8_t magic[16] = {0}; | 31 uint8_t magic[16] = {0}; |
| 29 | 32 |
| 30 if (fseek(input, 0x46, SEEK_SET)) | 33 if (fseek(input, 0x46, SEEK_SET)) |
| 31 return -1; | 34 return -1; |
| 32 | 35 |
| 34 | 37 |
| 35 if (fseek(input, 0x18, SEEK_SET)) | 38 if (fseek(input, 0x18, SEEK_SET)) |
| 36 return -1; | 39 return -1; |
| 37 | 40 |
| 38 /* read the WHOLE magic, then memcmp */ | 41 /* read the WHOLE magic, then memcmp */ |
| 39 if (fread(magic, sizeof(*magic), ARRAYSIZE(magic), input) < ARRAYSIZE(magic)) | 42 if (fread(magic, sizeof(*magic), ARRAY_SIZE(magic), input) < ARRAY_SIZE(magic)) |
| 40 return -1; | 43 return -1; |
| 41 | 44 |
| 42 if (!memcmp(magic, magic_veg, ARRAYSIZE(magic))) { | 45 if (!memcmp(magic, magic_veg, ARRAY_SIZE(magic))) { |
| 43 *type = TYPES_VEG; | 46 *type = TYPES_VEG; |
| 44 } else if (!memcmp(magic, magic_vf, ARRAYSIZE(magic))) { | 47 } else if (!memcmp(magic, magic_vf, ARRAY_SIZE(magic))) { |
| 45 *type = TYPES_VF; | 48 *type = TYPES_VF; |
| 46 } else { | 49 } else { |
| 47 *type = TYPES_UNKNOWN; | 50 *type = TYPES_UNKNOWN; |
| 48 } | 51 } |
| 49 | 52 |
| 50 return 0; | 53 return 0; |
| 51 } | 54 } |
| 55 | |
| 56 /* source needs read permissions, target needs write permissions, both must be in binary mode */ | |
| 57 int copy_file(FILE *source, FILE *target) | |
| 58 { | |
| 59 char ch[4096]; | |
| 60 | |
| 61 while (!feof(source)) { | |
| 62 size_t b = fread(ch, 1, sizeof(ch), source); | |
| 63 if (b && (fwrite(ch, 1, b, target) < b)) | |
| 64 return -1; | |
| 65 } | |
| 66 | |
| 67 return 0; | |
| 68 } | |
| 69 | |
| 70 /* ------------------------------------------------------------------------ */ | |
| 71 | |
| 72 char *str_ndup(const char *str, size_t sz) | |
| 73 { | |
| 74 char *x; | |
| 75 | |
| 76 x = malloc(sz + 1); | |
| 77 if (!x) | |
| 78 return NULL; | |
| 79 | |
| 80 memcpy(x, str, sz); | |
| 81 x[sz] = 0; | |
| 82 | |
| 83 return x; | |
| 84 } | |
| 85 | |
| 86 char *str_dup(const char *str) | |
| 87 { | |
| 88 return str_ndup(str, strlen(str)); | |
| 89 } | |
| 90 | |
| 91 /* ------------------------------------------------------------------------ */ | |
| 92 | |
| 93 const char *type_names[] = { | |
| 94 [TYPES_VF] = "Movie Studio", | |
| 95 [TYPES_VEG] = "Vegas Pro", | |
| 96 [TYPES_UNKNOWN] = "Unknown", | |
| 97 }; | |
| 98 | |
| 99 /* ------------------------------------------------------------------------ */ | |
| 100 | |
| 101 char *msvpvf_vasprintf(const char *format, va_list ap) | |
| 102 { | |
| 103 va_list ap2; | |
| 104 int needed_size; | |
| 105 char *output = NULL; | |
| 106 | |
| 107 va_copy(ap2, ap); | |
| 108 | |
| 109 needed_size = vsnprintf(NULL, 0, format, ap); | |
| 110 if (needed_size < 0) { | |
| 111 va_end(ap2); | |
| 112 return NULL; | |
| 113 } | |
| 114 | |
| 115 output = malloc((needed_size + 1) * sizeof(char)); | |
| 116 if (!output) { | |
| 117 va_end(ap2); | |
| 118 return NULL; | |
| 119 } | |
| 120 | |
| 121 needed_size = vsnprintf(output, needed_size + 1, format, ap2); | |
| 122 if (needed_size < 0) { | |
| 123 free(output); | |
| 124 va_end(ap2); | |
| 125 return NULL; | |
| 126 } | |
| 127 | |
| 128 va_end(ap2); | |
| 129 | |
| 130 return output; | |
| 131 } | |
| 132 | |
| 133 char *msvpvf_asprintf(const char *format, ...) | |
| 134 { | |
| 135 char *x; | |
| 136 va_list ap; | |
| 137 | |
| 138 va_start(ap, format); | |
| 139 x = msvpvf_vasprintf(format, ap); | |
| 140 va_end(ap); | |
| 141 | |
| 142 return x; | |
| 143 } |
