iPXE
datauri.h File Reference

Data URIs. More...

#include <stdint.h>
#include <string.h>
#include <ipxe/uri.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static size_t datauri_max_len (struct uri *uri)
 Get maximum length of parsed data URI.
int datauri_parse (struct uri *uri, void *buf)
 Parse data URI.

Detailed Description

Data URIs.

Definition in file datauri.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ datauri_max_len()

size_t datauri_max_len ( struct uri * uri)
inlinestatic

Get maximum length of parsed data URI.

Parameters
uriData URI
Return values
max_lenMaximum length

Definition at line 23 of file datauri.h.

23 {
24
25 /* Neither URI decoding nor base64 decoding can ever expand data */
26 return ( uri->opaque ? strlen ( uri->opaque ) : 0 );
27}
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A Uniform Resource Identifier.
Definition uri.h:65
const char * opaque
Opaque part.
Definition uri.h:71

References uri::opaque, and strlen().

Referenced by datauri_fail_okx(), datauri_okx(), and datauri_open().

◆ datauri_parse()

int datauri_parse ( struct uri * uri,
void * buf )
extern

Parse data URI.

Parameters
uriData URI
bufBuffer to fill in
Return values
lenLength of data, or negative error

The buffer length must be at least the size reported by datauri_max_len().

Definition at line 62 of file datauri.c.

62 {
63 static const char b64marker[7] = ";base64";
64 const char *comma;
65 const char *encoded;
66 size_t prefix_len;
67 int len;
68 int rc;
69
70 /* Sanity check */
71 if ( ! uri->opaque ) {
72 DBGC ( uri, "DATA %p has no opaque part\n", uri );
73 return -EINVAL;
74 }
75
76 /* Locate encoded string */
77 comma = strchr ( uri->opaque, ',' );
78 if ( ! comma ) {
79 DBGC ( uri, "DATA %p has no comma separator\n", uri );
80 return -EINVAL;
81 }
82 prefix_len = ( comma - uri->opaque );
83 encoded = ( comma + 1 );
84 len = strlen ( encoded );
85
86 /* Decode string */
87 if ( ( prefix_len >= sizeof ( b64marker ) ) &&
88 ( strncasecmp ( ( comma - sizeof ( b64marker ) ), b64marker,
89 sizeof ( b64marker ) ) == 0 ) ) {
90
91 /* Decode Base64 string */
92 len = base64_decode ( encoded, buf, len );
93 if ( len < 0 ) {
94 rc = len;
95 DBGC ( uri, "DATA %p could not decode Base64: %s\n",
96 uri, strerror ( rc ) );
97 return rc;
98 }
99
100 } else {
101
102 /* Copy raw string */
103 strcpy ( buf, encoded );
104 }
105
106 DBGC ( uri, "DATA %p decoded \"%s\":\n", uri, uri->opaque );
107 DBGC_HDA ( uri, 0, buf, len );
108 return len;
109}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int base64_decode(const char *encoded, void *data, size_t len)
Base64-decode string.
Definition base64.c:92
ring len
Length.
Definition dwmac.h:226
#define DBGC(...)
Definition compiler.h:530
#define DBGC_HDA(...)
Definition compiler.h:531
#define EINVAL
Invalid argument.
Definition errno.h:429
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
char * strchr(const char *src, int character)
Find character within a string.
Definition string.c:288
int strncasecmp(const char *first, const char *second, size_t max)
Compare case-insensitive strings.
Definition string.c:222
char * strcpy(char *dest, const char *src)
Copy string.
Definition string.c:378

References base64_decode(), DBGC, DBGC_HDA, EINVAL, len, uri::opaque, rc, strchr(), strcpy(), strerror(), strlen(), and strncasecmp().

Referenced by datauri_fail_okx(), datauri_okx(), and datauri_open().