I am currently trying to compile a library for MacOS that was originally written for Windows. This library's source makes use of lots of Windows secure functions like sscanf_s()
Obviously, clang is having a hard time recognising these functions.
Note: This library is still in use by other people in my team so I cannot just haphazardly replace all these functions from the code.
I tried writing macros to replace these functions with more standard fucntions like so:
#define _snprintf_s(buffer, size, count, format, ...) snprintf(buffer, size, format, VA_ARGS)
#define sscanf_s sscanf
but the format args used by scanf functions are weird to write macros for since they are in this pattern:
format_arg1, size[if format_arg1 is string], format_arg2, format_arg3, size[if format_arg3 is string] ... so on ...
I have considered the following solutions:
I am open to other solutions too, Thanks!
Is there any easy way to map windows specific secure C-API funcs to functions that will work on mac?
It depends on which particular functions you want to emulate, and on how closely you want to emulate them.
You seem to recognize that you can use macros to reinterpret members of the printf_s family of functions as corresponding members of the printf family instead. This should get you the same output on Mac as on Windows when the arguments are valid, but of course, you don't this way get the (dubious) advantages of runtime argument checking on Mac. Similar applies to some of the other _s functions.
You also seem to recognize that the scanf_s family of functions is more troublesome, as the arguments to these functions do not, in some cases, align with the arguments to the corresponding scanf family functions. One needs to interpret the format to know what the required differences are, so this cannot be done with macros. You could write wrapper functions that do the interpretation and dispatch appropriate calls, but in no way would I characterize that as "easy".
I have very low regard for Microsoft's so-called "security enhanced" versions of standard library functions, so my first suggestion would be to convert to using the standard functions. That others on your team also use the same library is not an inherent blocker for this, as a version that has been liberated from the security-enhanced functions should still be workable for them. The bigger hurdle might be to get your management's approval for this, especially if they're inclined to believe the "security enhanced" nonsense, but perhaps that's achievable.
If you can't just switch to standard functions, then
for printf_s and others for which it is feasible to do so, I would do as you already imagined: use macros to redefine calls to these functions to call the corresponding standard functions instead.
for other functions, I would wrap each call in a conditional compilation directive, so that the security-enhanced function is called on Windows, and the standard one on Mac (with an appropriately-adjusted argument list).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With