You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_strings.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <stdarg.h>
  26. #include <ipxe/vsprintf.h>
  27. #include <ipxe/efi/efi_strings.h>
  28. /** Context used by efi_vsnprintf() and friends */
  29. struct efi_sputc_context {
  30. /** printf context */
  31. struct printf_context ctx;
  32. /** Buffer for formatted string (used by efi_printf_sputc()) */
  33. wchar_t *buf;
  34. /** Buffer length (used by efi_printf_sputc())
  35. *
  36. * Note that this is a number of wide characters, not a number
  37. * of bytes.
  38. */
  39. size_t max_wlen;
  40. };
  41. /**
  42. * Write wide character to buffer
  43. *
  44. * @v ctx Context
  45. * @v c Character
  46. */
  47. static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) {
  48. struct efi_sputc_context * sctx =
  49. container_of ( ctx, struct efi_sputc_context, ctx );
  50. if ( ctx->len < sctx->max_wlen )
  51. sctx->buf[ctx->len] = c;
  52. }
  53. /**
  54. * Write a formatted string to a wide-character buffer
  55. *
  56. * @v wbuf Buffer into which to write the string
  57. * @v wsize Size of buffer (in wide characters)
  58. * @v fmt Format string
  59. * @v args Arguments corresponding to the format string
  60. * @ret wlen Length of formatted string (in wide characters)
  61. *
  62. * If the buffer is too small to contain the string, the returned
  63. * length is the length that would have been written had enough space
  64. * been available.
  65. */
  66. int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt,
  67. va_list args ) {
  68. struct efi_sputc_context sctx;
  69. size_t wlen;
  70. size_t wend;
  71. /* Hand off to vcprintf */
  72. sctx.ctx.handler = efi_printf_sputc;
  73. sctx.buf = wbuf;
  74. sctx.max_wlen = wsize;
  75. wlen = vcprintf ( &sctx.ctx, fmt, args );
  76. /* Add trailing NUL */
  77. if ( wsize ) {
  78. wend = wsize - 1;
  79. if ( wlen < wend )
  80. wend = wlen;
  81. wbuf[wend] = '\0';
  82. }
  83. return wlen;
  84. }
  85. /**
  86. * Write a formatted string to a buffer
  87. *
  88. * @v wbuf Buffer into which to write the string
  89. * @v wsize Size of buffer (in wide characters)
  90. * @v fmt Format string
  91. * @v ... Arguments corresponding to the format string
  92. * @ret wlen Length of formatted string (in wide characters)
  93. */
  94. int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) {
  95. va_list args;
  96. int i;
  97. va_start ( args, fmt );
  98. i = efi_vsnprintf ( wbuf, wsize, fmt, args );
  99. va_end ( args );
  100. return i;
  101. }
  102. /**
  103. * Version of efi_vsnprintf() that accepts a signed buffer size
  104. *
  105. * @v wbuf Buffer into which to write the string
  106. * @v swsize Size of buffer (in wide characters)
  107. * @v fmt Format string
  108. * @v args Arguments corresponding to the format string
  109. * @ret wlen Length of formatted string (in wide characters)
  110. */
  111. int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
  112. va_list args ) {
  113. /* Treat negative buffer size as zero buffer size */
  114. if ( swsize < 0 )
  115. swsize = 0;
  116. /* Hand off to vsnprintf */
  117. return efi_vsnprintf ( wbuf, swsize, fmt, args );
  118. }
  119. /**
  120. * Version of efi_vsnprintf() that accepts a signed buffer size
  121. *
  122. * @v wbuf Buffer into which to write the string
  123. * @v swsize Size of buffer (in wide characters)
  124. * @v fmt Format string
  125. * @v ... Arguments corresponding to the format string
  126. * @ret wlen Length of formatted string (in wide characters)
  127. */
  128. int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
  129. va_list args;
  130. int len;
  131. /* Hand off to vssnprintf */
  132. va_start ( args, fmt );
  133. len = efi_vssnprintf ( wbuf, swsize, fmt, args );
  134. va_end ( args );
  135. return len;
  136. }