iPXE
efi_strings.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdarg.h>
00028 #include <ipxe/vsprintf.h>
00029 #include <ipxe/efi/efi_strings.h>
00030 
00031 /** Context used by efi_vsnprintf() and friends */
00032 struct efi_sputc_context {
00033         /** printf context */
00034         struct printf_context ctx;
00035         /** Buffer for formatted string (used by efi_printf_sputc()) */
00036         wchar_t *buf;
00037         /** Buffer length (used by efi_printf_sputc())
00038          *
00039          * Note that this is a number of wide characters, not a number
00040          * of bytes.
00041          */
00042         size_t max_wlen;
00043 };
00044 
00045 /**
00046  * Write wide character to buffer
00047  *
00048  * @v ctx               Context
00049  * @v c                 Character
00050  */
00051 static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) {
00052         struct efi_sputc_context * sctx =
00053                 container_of ( ctx, struct efi_sputc_context, ctx );
00054 
00055         if ( ctx->len < sctx->max_wlen )
00056                 sctx->buf[ctx->len] = c;
00057 }
00058 
00059 /**
00060  * Write a formatted string to a wide-character buffer
00061  *
00062  * @v wbuf              Buffer into which to write the string
00063  * @v wsize             Size of buffer (in wide characters)
00064  * @v fmt               Format string
00065  * @v args              Arguments corresponding to the format string
00066  * @ret wlen            Length of formatted string (in wide characters)
00067  *
00068  * If the buffer is too small to contain the string, the returned
00069  * length is the length that would have been written had enough space
00070  * been available.
00071  */
00072 int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt,
00073                     va_list args ) {
00074         struct efi_sputc_context sctx;
00075         size_t wlen;
00076         size_t wend;
00077 
00078         /* Hand off to vcprintf */
00079         sctx.ctx.handler = efi_printf_sputc;
00080         sctx.buf = wbuf;
00081         sctx.max_wlen = wsize;
00082         wlen = vcprintf ( &sctx.ctx, fmt, args );
00083 
00084         /* Add trailing NUL */
00085         if ( wsize ) {
00086                 wend = wsize - 1;
00087                 if ( wlen < wend )
00088                         wend = wlen;
00089                 wbuf[wend] = '\0';
00090         }
00091 
00092         return wlen;
00093 }
00094 
00095 /**
00096  * Write a formatted string to a buffer
00097  *
00098  * @v wbuf              Buffer into which to write the string
00099  * @v wsize             Size of buffer (in wide characters)
00100  * @v fmt               Format string
00101  * @v ...               Arguments corresponding to the format string
00102  * @ret wlen            Length of formatted string (in wide characters)
00103  */
00104 int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) {
00105         va_list args;
00106         int i;
00107 
00108         va_start ( args, fmt );
00109         i = efi_vsnprintf ( wbuf, wsize, fmt, args );
00110         va_end ( args );
00111         return i;
00112 }
00113 
00114 /**
00115  * Version of efi_vsnprintf() that accepts a signed buffer size
00116  *
00117  * @v wbuf              Buffer into which to write the string
00118  * @v swsize            Size of buffer (in wide characters)
00119  * @v fmt               Format string
00120  * @v args              Arguments corresponding to the format string
00121  * @ret wlen            Length of formatted string (in wide characters)
00122  */
00123 int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
00124                      va_list args ) {
00125 
00126         /* Treat negative buffer size as zero buffer size */
00127         if ( swsize < 0 )
00128                 swsize = 0;
00129 
00130         /* Hand off to vsnprintf */
00131         return efi_vsnprintf ( wbuf, swsize, fmt, args );
00132 }
00133 
00134 /**
00135  * Version of efi_vsnprintf() that accepts a signed buffer size
00136  *
00137  * @v wbuf              Buffer into which to write the string
00138  * @v swsize            Size of buffer (in wide characters)
00139  * @v fmt               Format string
00140  * @v ...               Arguments corresponding to the format string
00141  * @ret wlen            Length of formatted string (in wide characters)
00142  */
00143 int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
00144         va_list args;
00145         int len;
00146 
00147         /* Hand off to vssnprintf */
00148         va_start ( args, fmt );
00149         len = efi_vssnprintf ( wbuf, swsize, fmt, args );
00150         va_end ( args );
00151         return len;
00152 }