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