|
9 | 9 | #include <stdio.h> |
10 | 10 | #include <string.h> |
11 | 11 |
|
| 12 | +#if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 13 | +#define HIDAPI_SSCANF sscanf_s |
| 14 | +#define HIDAPI_FSCANF fscanf_s |
| 15 | +#define HIDAPI_SCANSET_SIZE(buf) , (unsigned)_countof(buf) |
| 16 | +#else |
| 17 | +#define HIDAPI_SSCANF sscanf |
| 18 | +#define HIDAPI_FSCANF fscanf |
| 19 | +#define HIDAPI_SCANSET_SIZE(buf) |
| 20 | +#endif |
| 21 | + |
| 22 | +#define sscanf HIDAPI_SSCANF |
| 23 | + |
| 24 | +static const char* hidapi_strerror_compat(int errnum, char* buf, size_t buf_size) |
| 25 | +{ |
| 26 | +#if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 27 | + if (strerror_s(buf, buf_size, errnum) == 0) { |
| 28 | + return buf; |
| 29 | + } |
| 30 | + return "Unknown error"; |
| 31 | +#else |
| 32 | + (void)buf; |
| 33 | + (void)buf_size; |
| 34 | + return strerror(errnum); |
| 35 | +#endif |
| 36 | +} |
| 37 | + |
12 | 38 | static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename) |
13 | 39 | { |
14 | 40 | FILE* file; |
15 | 41 | errno_t err = fopen_s(&file, filename, "r"); |
16 | 42 |
|
17 | 43 | if (err != 0) { |
18 | | - fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, strerror(err)); |
| 44 | + char err_buf[128]; |
| 45 | + fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, hidapi_strerror_compat((int)err, err_buf, sizeof(err_buf))); |
19 | 46 | return NULL; |
20 | 47 | } |
21 | 48 |
|
@@ -49,8 +76,8 @@ static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename) |
49 | 76 | if (sscanf(line, "dev->product_id = 0x%04hX\n", &product_id)) continue; |
50 | 77 | if (sscanf(line, "dev->usage_page = 0x%04hX\n", &usage_page)) continue; |
51 | 78 | if (sscanf(line, "dev->usage = 0x%04hX\n", &usage)) continue; |
52 | | - if (sscanf(line, "dev->manufacturer_string = \"%127[^\"\n]", manufacturer_string)) continue; |
53 | | - if (sscanf(line, "dev->product_string = \"%127[^\"\n]", product_string)) continue; |
| 79 | + if (sscanf(line, "dev->manufacturer_string = \"%127[^\"\n]", manufacturer_string HIDAPI_SCANSET_SIZE(manufacturer_string))) continue; |
| 80 | + if (sscanf(line, "dev->product_string = \"%127[^\"\n]", product_string HIDAPI_SCANSET_SIZE(product_string))) continue; |
54 | 81 | if (sscanf(line, "dev->release_number = 0x%04hX\n", &release_number)) continue; |
55 | 82 | if (sscanf(line, "dev->interface_number = %d\n", &interface_number)) continue; |
56 | 83 | // if (sscanf(line, "dev->path = \"%127[^\"]\n", path)) continue; |
@@ -456,14 +483,15 @@ static BOOLEAN read_hex_data_from_text_file(const char *filename, unsigned char |
456 | 483 | FILE* file = NULL; |
457 | 484 | errno_t err = fopen_s(&file, filename, "r"); |
458 | 485 | if (err != 0) { |
459 | | - fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, strerror(err)); |
| 486 | + char err_buf[128]; |
| 487 | + fprintf(stderr, "ERROR: Couldn't open file '%s' for reading: %s\n", filename, hidapi_strerror_compat((int)err, err_buf, sizeof(err_buf))); |
460 | 488 | return FALSE; |
461 | 489 | } |
462 | 490 |
|
463 | 491 | BOOLEAN result = TRUE; |
464 | 492 | unsigned int val; |
465 | 493 | char buf[16]; |
466 | | - while (fscanf(file, "%15s", buf) == 1) { |
| 494 | + while (HIDAPI_FSCANF(file, "%15s", buf HIDAPI_SCANSET_SIZE(buf)) == 1) { |
467 | 495 | if (sscanf(buf, "0x%X", &val) != 1) { |
468 | 496 | fprintf(stderr, "Invalid HEX text ('%s') file, got %s\n", filename, buf); |
469 | 497 | result = FALSE; |
|
0 commit comments