iPXE
Data Structures | Functions
efi_strings.c File Reference
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <ipxe/vsprintf.h>
#include <ipxe/efi/efi_strings.h>

Go to the source code of this file.

Data Structures

struct  efi_sputc_context
 Context used by efi_vsnprintf() and friends. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void efi_printf_sputc (struct printf_context *ctx, unsigned int c)
 Write wide character to buffer. More...
 
int efi_vsnprintf (wchar_t *wbuf, size_t wsize, const char *fmt, va_list args)
 Write a formatted string to a wide-character buffer. More...
 
int efi_snprintf (wchar_t *wbuf, size_t wsize, const char *fmt,...)
 Write a formatted string to a buffer. More...
 
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. More...
 
int efi_ssnprintf (wchar_t *wbuf, ssize_t swsize, const char *fmt,...)
 Version of efi_vsnprintf() that accepts a signed buffer size. More...
 
int efi_vasprintf (wchar_t **wstrp, const char *fmt, va_list args)
 Write a formatted string to newly allocated memory. More...
 
int efi_asprintf (wchar_t **wstrp, const char *fmt,...)
 Write a formatted string to newly allocated memory. More...
 

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ efi_printf_sputc()

static void efi_printf_sputc ( struct printf_context ctx,
unsigned int  c 
)
static

Write wide character to buffer.

Parameters
ctxContext
cCharacter

Definition at line 53 of file efi_strings.c.

53  {
54  struct efi_sputc_context * sctx =
56 
57  if ( ctx->len < sctx->max_wlen )
58  sctx->buf[ctx->len] = c;
59 }
uint32_t c
Definition: md4.c:30
wchar_t * buf
Buffer for formatted string (used by efi_printf_sputc())
Definition: efi_strings.c:38
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
size_t max_wlen
Buffer length (used by efi_printf_sputc())
Definition: efi_strings.c:44
Context used by efi_vsnprintf() and friends.
Definition: efi_strings.c:34

References efi_sputc_context::buf, c, container_of, ctx, and efi_sputc_context::max_wlen.

Referenced by efi_vsnprintf().

◆ efi_vsnprintf()

int efi_vsnprintf ( wchar_t wbuf,
size_t  wsize,
const char *  fmt,
va_list  args 
)

Write a formatted string to a wide-character buffer.

Parameters
wbufBuffer into which to write the string
wsizeSize of buffer (in wide characters)
fmtFormat string
argsArguments corresponding to the format string
Return values
wlenLength of formatted string (in wide characters)

If the buffer is too small to contain the string, the returned length is the length that would have been written had enough space been available.

Definition at line 74 of file efi_strings.c.

75  {
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 }
struct printf_context ctx
printf context
Definition: efi_strings.c:36
void(* handler)(struct printf_context *ctx, unsigned int c)
Character handler.
Definition: vsprintf.h:57
static void efi_printf_sputc(struct printf_context *ctx, unsigned int c)
Write wide character to buffer.
Definition: efi_strings.c:53
int ssize_t const char * fmt
Definition: vsprintf.h:72
Context used by efi_vsnprintf() and friends.
Definition: efi_strings.c:34
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

References efi_sputc_context::buf, efi_sputc_context::ctx, efi_printf_sputc(), fmt, printf_context::handler, efi_sputc_context::max_wlen, and vcprintf().

Referenced by efi_ifr_string(), efi_snprintf(), efi_vasprintf(), efi_vsprintf(), and efi_vssnprintf().

◆ efi_snprintf()

int efi_snprintf ( wchar_t wbuf,
size_t  wsize,
const char *  fmt,
  ... 
)

Write a formatted string to a buffer.

Parameters
wbufBuffer into which to write the string
wsizeSize of buffer (in wide characters)
fmtFormat string
...Arguments corresponding to the format string
Return values
wlenLength of formatted string (in wide characters)

Definition at line 106 of file efi_strings.c.

106  {
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 }
#define va_end(ap)
Definition: stdarg.h:9
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
__builtin_va_list va_list
Definition: stdarg.h:6
int ssize_t const char * fmt
Definition: vsprintf.h:72
#define va_start(ap, last)
Definition: stdarg.h:7

References efi_vsnprintf(), fmt, va_end, and va_start.

Referenced by efi_block_filename(), efi_file_varlen(), efi_image_path(), efi_local_open_resolved(), efi_snp_hii_append(), efi_snp_probe(), and efivars_fetch().

◆ efi_vssnprintf()

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.

Parameters
wbufBuffer into which to write the string
swsizeSize of buffer (in wide characters)
fmtFormat string
argsArguments corresponding to the format string
Return values
wlenLength of formatted string (in wide characters)

Definition at line 125 of file efi_strings.c.

126  {
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 }
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
int ssize_t const char * fmt
Definition: vsprintf.h:72

References efi_vsnprintf(), and fmt.

Referenced by efi_ssnprintf().

◆ efi_ssnprintf()

int efi_ssnprintf ( wchar_t wbuf,
ssize_t  swsize,
const char *  fmt,
  ... 
)

Version of efi_vsnprintf() that accepts a signed buffer size.

Parameters
wbufBuffer into which to write the string
swsizeSize of buffer (in wide characters)
fmtFormat string
...Arguments corresponding to the format string
Return values
wlenLength of formatted string (in wide characters)

Definition at line 145 of file efi_strings.c.

145  {
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 }
#define va_end(ap)
Definition: stdarg.h:9
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
#define va_start(ap, last)
Definition: stdarg.h:7

References efi_vssnprintf(), fmt, len, va_end, and va_start.

◆ efi_vasprintf()

int efi_vasprintf ( wchar_t **  wstrp,
const char *  fmt,
va_list  args 
)

Write a formatted string to newly allocated memory.

Parameters
wstrpPointer to hold allocated string
fmtFormat string
argsArguments corresponding to the format string
Return values
lenLength of formatted string (in wide characters)

Definition at line 164 of file efi_strings.c.

164  {
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 }
#define va_end(ap)
Definition: stdarg.h:9
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
#define va_copy(dest, src)
Definition: stdarg.h:10
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint32_t len
Length.
Definition: ena.h:14
__builtin_va_list va_list
Definition: stdarg.h:6
int ssize_t const char * fmt
Definition: vsprintf.h:72
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References efi_vsnprintf(), ENOMEM, fmt, len, malloc(), NULL, va_copy, and va_end.

Referenced by efi_asprintf().

◆ efi_asprintf()

int efi_asprintf ( wchar_t **  wstrp,
const char *  fmt,
  ... 
)

Write a formatted string to newly allocated memory.

Parameters
wstrpPointer to hold allocated string
fmtFormat string
...Arguments corresponding to the format string
Return values
lenLength of formatted string (in wide characters)

Definition at line 188 of file efi_strings.c.

188  {
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 }
#define va_end(ap)
Definition: stdarg.h:9
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
uint32_t len
Length.
Definition: ena.h:14
__builtin_va_list va_list
Definition: stdarg.h:6
int ssize_t const char * fmt
Definition: vsprintf.h:72
#define va_start(ap, last)
Definition: stdarg.h:7

References efi_vasprintf(), fmt, len, va_end, and va_start.

Referenced by efi_image_cmdline(), and efi_shim_cmdline().