iPXE
efi_strings.c
Go to the documentation of this file.
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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stddef.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <ipxe/vsprintf.h>
31 #include <ipxe/efi/efi_strings.h>
32 
33 /** Context used by efi_vsnprintf() and friends */
35  /** printf context */
37  /** Buffer for formatted string (used by efi_printf_sputc()) */
38  wchar_t *buf;
39  /** Buffer length (used by efi_printf_sputc())
40  *
41  * Note that this is a number of wide characters, not a number
42  * of bytes.
43  */
44  size_t max_wlen;
45 };
46 
47 /**
48  * Write wide character to buffer
49  *
50  * @v ctx Context
51  * @v c Character
52  */
53 static void efi_printf_sputc ( struct printf_context *ctx, unsigned int c ) {
54  struct efi_sputc_context * sctx =
56 
57  if ( ctx->len < sctx->max_wlen )
58  sctx->buf[ctx->len] = c;
59 }
60 
61 /**
62  * Write a formatted string to a wide-character buffer
63  *
64  * @v wbuf Buffer into which to write the string
65  * @v wsize Size of buffer (in wide characters)
66  * @v fmt Format string
67  * @v args Arguments corresponding to the format string
68  * @ret wlen Length of formatted string (in wide characters)
69  *
70  * If the buffer is too small to contain the string, the returned
71  * length is the length that would have been written had enough space
72  * been available.
73  */
74 int efi_vsnprintf ( wchar_t *wbuf, size_t wsize, const char *fmt,
75  va_list args ) {
76  struct efi_sputc_context sctx;
77  size_t wlen;
78  size_t wend;
79 
80  /* Hand off to vcprintf */
82  sctx.buf = wbuf;
83  sctx.max_wlen = wsize;
84  wlen = vcprintf ( &sctx.ctx, fmt, args );
85 
86  /* Add trailing NUL */
87  if ( wsize ) {
88  wend = wsize - 1;
89  if ( wlen < wend )
90  wend = wlen;
91  wbuf[wend] = '\0';
92  }
93 
94  return wlen;
95 }
96 
97 /**
98  * Write a formatted string to a buffer
99  *
100  * @v wbuf Buffer into which to write the string
101  * @v wsize Size of buffer (in wide characters)
102  * @v fmt Format string
103  * @v ... Arguments corresponding to the format string
104  * @ret wlen Length of formatted string (in wide characters)
105  */
106 int efi_snprintf ( wchar_t *wbuf, size_t wsize, const char *fmt, ... ) {
107  va_list args;
108  int i;
109 
110  va_start ( args, fmt );
111  i = efi_vsnprintf ( wbuf, wsize, fmt, args );
112  va_end ( args );
113  return i;
114 }
115 
116 /**
117  * Version of efi_vsnprintf() that accepts a signed buffer size
118  *
119  * @v wbuf Buffer into which to write the string
120  * @v swsize Size of buffer (in wide characters)
121  * @v fmt Format string
122  * @v args Arguments corresponding to the format string
123  * @ret wlen Length of formatted string (in wide characters)
124  */
125 int efi_vssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt,
126  va_list args ) {
127 
128  /* Treat negative buffer size as zero buffer size */
129  if ( swsize < 0 )
130  swsize = 0;
131 
132  /* Hand off to vsnprintf */
133  return efi_vsnprintf ( wbuf, swsize, fmt, args );
134 }
135 
136 /**
137  * Version of efi_vsnprintf() that accepts a signed buffer size
138  *
139  * @v wbuf Buffer into which to write the string
140  * @v swsize Size of buffer (in wide characters)
141  * @v fmt Format string
142  * @v ... Arguments corresponding to the format string
143  * @ret wlen Length of formatted string (in wide characters)
144  */
145 int efi_ssnprintf ( wchar_t *wbuf, ssize_t swsize, const char *fmt, ... ) {
146  va_list args;
147  int len;
148 
149  /* Hand off to vssnprintf */
150  va_start ( args, fmt );
151  len = efi_vssnprintf ( wbuf, swsize, fmt, args );
152  va_end ( args );
153  return len;
154 }
155 
156 /**
157  * Write a formatted string to newly allocated memory
158  *
159  * @v wstrp Pointer to hold allocated string
160  * @v fmt Format string
161  * @v args Arguments corresponding to the format string
162  * @ret len Length of formatted string (in wide characters)
163  */
164 int efi_vasprintf ( wchar_t **wstrp, const char *fmt, va_list args ) {
165  size_t len;
166  va_list args_tmp;
167 
168  /* Calculate length needed for string */
169  va_copy ( args_tmp, args );
170  len = ( efi_vsnprintf ( NULL, 0, fmt, args_tmp ) + 1 );
171  va_end ( args_tmp );
172 
173  /* Allocate and fill string */
174  *wstrp = malloc ( len * sizeof ( **wstrp ) );
175  if ( ! *wstrp )
176  return -ENOMEM;
177  return efi_vsnprintf ( *wstrp, len, fmt, args );
178 }
179 
180 /**
181  * Write a formatted string to newly allocated memory
182  *
183  * @v wstrp Pointer to hold allocated string
184  * @v fmt Format string
185  * @v ... Arguments corresponding to the format string
186  * @ret len Length of formatted string (in wide characters)
187  */
188 int efi_asprintf ( wchar_t **wstrp, const char *fmt, ... ) {
189  va_list args;
190  int len;
191 
192  va_start ( args, fmt );
193  len = efi_vasprintf ( wstrp, fmt, args );
194  va_end ( args );
195  return len;
196 }
uint32_t c
Definition: md4.c:30
struct printf_context ctx
printf context
Definition: efi_strings.c:36
#define va_end(ap)
Definition: stdarg.h:9
void(* handler)(struct printf_context *ctx, unsigned int c)
Character handler.
Definition: vsprintf.h:57
Error codes.
int efi_vsnprintf(wchar_t *wbuf, size_t wsize, const char *fmt, va_list args)
Write a formatted string to a wide-character buffer.
Definition: efi_strings.c:74
EFI strings.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
printf() and friends
wchar_t * buf
Buffer for formatted string (used by efi_printf_sputc())
Definition: efi_strings.c:38
int efi_vasprintf(wchar_t **wstrp, const char *fmt, va_list args)
Write a formatted string to newly allocated memory.
Definition: efi_strings.c:164
#define va_copy(dest, src)
Definition: stdarg.h:10
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
int efi_snprintf(wchar_t *wbuf, size_t wsize, const char *fmt,...)
Write a formatted string to a buffer.
Definition: efi_strings.c:106
static void efi_printf_sputc(struct printf_context *ctx, unsigned int c)
Write wide character to buffer.
Definition: efi_strings.c:53
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
A printf context.
Definition: vsprintf.h:47
size_t max_wlen
Buffer length (used by efi_printf_sputc())
Definition: efi_strings.c:44
uint32_t len
Length.
Definition: ena.h:14
__builtin_va_list va_list
Definition: stdarg.h:6
int efi_vssnprintf(wchar_t *wbuf, ssize_t swsize, const char *fmt, va_list args)
Version of efi_vsnprintf() that accepts a signed buffer size.
Definition: efi_strings.c:125
int ssize_t const char * fmt
Definition: vsprintf.h:72
Context used by efi_vsnprintf() and friends.
Definition: efi_strings.c:34
signed long ssize_t
Definition: stdint.h:7
#define va_start(ap, last)
Definition: stdarg.h:7
int efi_asprintf(wchar_t **wstrp, const char *fmt,...)
Write a formatted string to newly allocated memory.
Definition: efi_strings.c:188
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int efi_ssnprintf(wchar_t *wbuf, ssize_t swsize, const char *fmt,...)
Version of efi_vsnprintf() that accepts a signed buffer size.
Definition: efi_strings.c:145
size_t vcprintf(struct printf_context *ctx, const char *fmt, va_list args)
Write a formatted string to a printf context.
Definition: vsprintf.c:187