Thar0

snprintf for decomp

Aug 8th, 2021 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. /*
  2.  * This is free and unencumbered software released into the public domain.
  3.  *
  4.  * Anyone is free to copy, modify, publish, use, compile, sell, or
  5.  * distribute this software, either in source code form or as a compiled
  6.  * binary, for any purpose, commercial or non-commercial, and by any
  7.  * means.
  8.  *
  9.  * In jurisdictions that recognize copyright laws, the author or authors
  10.  * of this software dedicate any and all copyright interest in the
  11.  * software to the public domain. We make this dedication for the benefit
  12.  * of the public at large and to the detriment of our heirs and
  13.  * successors. We intend this dedication to be an overt act of
  14.  * relinquishment in perpetuity of all present and future rights to this
  15.  * software under copyright law.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20.  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.  * OTHER DEALINGS IN THE SOFTWARE.
  24.  *
  25.  * For more information, please refer to <http://unlicense.org>
  26.  */
  27.  
  28. #include "global.h"
  29.  
  30. // Add these two to a header
  31. s32 vsnprintf(char* dst, u32 size, const char* fmt, va_list args);
  32. s32 snprintf(char* dst, u32 size, const char* fmt, ...);
  33.  
  34. typedef struct {
  35.     char* dst;    // current dst
  36.     char* dstEnd; // end dst
  37. } NPrintf;
  38.  
  39. static void* proutNSprintf(void* arg, const char* str, u32 size) {
  40.     NPrintf* np = (NPrintf*)arg;
  41.  
  42.     if (np->dst >= np->dstEnd || np->dst == NULL) {
  43.         // reached or passed the end, don't write out but continue
  44.         // in order to get the full string length
  45.         return np;
  46.     }
  47.     // ensure size does not end up out of bounds
  48.     size = MIN(size, np->dstEnd - np->dst);
  49.     np->dst = (char*)memcpy(np->dst, str, size) + size;
  50.     return np;
  51. }
  52.  
  53. s32 vsnprintf(char* dst, u32 size, const char* fmt, va_list args) {
  54.     NPrintf np;
  55.     s32 len;
  56.  
  57.     // dst can be null
  58.     np.dst = dst;
  59.     // size can be zero
  60.     np.dstEnd = &dst[MAX(size - 1, 0)];
  61.  
  62.     len = _Printf(proutNSprintf, &np, fmt, args);
  63.     if (dst != NULL && size > 0 && len > -1) {
  64.         // calculated length may not always be in the buffer,
  65.         // so terminate at min of the two
  66.         dst[MIN(len, size - 1)] = '\0';
  67.     }
  68.     return len;
  69. }
  70.  
  71. s32 snprintf(char* dst, u32 size, const char* fmt, ...) {
  72.     s32 len;
  73.     va_list args;
  74.     va_start(args, fmt);
  75.  
  76.     len = vsnprintf(dst, size, fmt, args);
  77.  
  78.     va_end(args);
  79.     return len;
  80. }
  81.  
Add Comment
Please, Sign In to add comment