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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stddef.h>
  21. #include <stdarg.h>
  22. #include <ipxe/vsprintf.h>
  23. #include <ipxe/efi/efi_strings.h>
  24. /** Context used by efi_vsnprintf() and friends */
  25. struct efi_sputc_context {
  26. /** printf context */
  27. struct printf_context ctx;
  28. /** Buffer for formatted string (used by efi_printf_sputc()) */
  29. wchar_t *buf;
  30. /** Buffer length (used by efi_printf_sputc())
  31. *
  32. * Note that this is a number of wide characters, not a number
  33. * of bytes.
  34. */
  35. size_t max_wlen;
  36. };
  37. /**
  38. * Write wide character to buffer
  39. *
  40. * @v ctx Context
  41. * @v c Character
  42. */
  43. static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) {
  44. struct efi_sputc_context * sctx =
  45. container_of ( ctx, struct efi_sputc_context, ctx );
  46. if ( ctx->len < sctx->max_wlen )
  47. sctx->buf[ctx->len] = c;
  48. }
  49. /**
  50. * Write a formatted string to a wide-character buffer
  51. *
  52. * @v wbuf Buffer into which to write the string
  53. * @v wsize Size of buffer (in wide characters)
  54. * @v fmt Format string
  55. * @v args Arguments corresponding to the format string
  56. * @ret wlen Length of formatted string (in wide characters)
  57. *
  58. * If the buffer is too small to contain the string, the returned
  59. * length is the length that would have been written had enough space
  60. * been available.
  61. */
  62. int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt,
  63. va_list args ) {
  64. struct efi_sputc_context sctx;
  65. size_t wlen;
  66. size_t wend;
  67. /* Hand off to vcprintf */
  68. sctx.ctx.handler = efi_printf_sputc;
  69. sctx.buf = wbuf;
  70. sctx.max_wlen = wsize;
  71. wlen = vcprintf ( &sctx.ctx, fmt, args );
  72. /* Add trailing NUL */
  73. if ( wsize ) {
  74. wend = wsize - 1;
  75. if ( wlen < wend )
  76. wend = wlen;
  77. wbuf[wend] = '\0';
  78. }
  79. return wlen;
  80. }
  81. /**
  82. * Write a formatted string to a buffer
  83. *
  84. * @v wbuf Buffer into which to write the string
  85. * @v wsize Size of buffer (in wide characters)
  86. * @v fmt Format string
  87. * @v ... Arguments corresponding to the format string
  88. * @ret wlen Length of formatted string (in wide characters)
  89. */
  90. int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) {
  91. va_list args;
  92. int i;
  93. va_start ( args, fmt );
  94. i = efi_vsnprintf ( wbuf, wsize, fmt, args );
  95. va_end ( args );
  96. return i;
  97. }
  98. /**
  99. * Version of efi_vsnprintf() that accepts a signed buffer size
  100. *
  101. * @v wbuf Buffer into which to write the string
  102. * @v swsize Size of buffer (in wide characters)
  103. * @v fmt Format string
  104. * @v args Arguments corresponding to the format string
  105. * @ret wlen Length of formatted string (in wide characters)
  106. */
  107. int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
  108. va_list args ) {
  109. /* Treat negative buffer size as zero buffer size */
  110. if ( swsize < 0 )
  111. swsize = 0;
  112. /* Hand off to vsnprintf */
  113. return efi_vsnprintf ( wbuf, swsize, fmt, args );
  114. }
  115. /**
  116. * Version of efi_vsnprintf() that accepts a signed buffer size
  117. *
  118. * @v wbuf Buffer into which to write the string
  119. * @v swsize Size of buffer (in wide characters)
  120. * @v fmt Format string
  121. * @v ... Arguments corresponding to the format string
  122. * @ret wlen Length of formatted string (in wide characters)
  123. */
  124. int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
  125. va_list args;
  126. int len;
  127. /* Hand off to vssnprintf */
  128. va_start ( args, fmt );
  129. len = efi_vssnprintf ( wbuf, swsize, fmt, args );
  130. va_end ( args );
  131. return len;
  132. }