iPXE
datauri.c File Reference

Data URIs. More...

#include <string.h>
#include <strings.h>
#include <errno.h>
#include <ipxe/base64.h>
#include <ipxe/blob.h>
#include <ipxe/open.h>
#include <ipxe/datauri.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int datauri_parse (struct uri *uri, void *buf)
 Parse data URI.
static int datauri_open (struct interface *xfer, struct uri *uri)
 Open data URI.

Variables

struct uri_opener data_uri_opener __uri_opener
 Data URI opener.

Detailed Description

Data URIs.

The "data" URI scheme is defined in RFC 2397. In the interest of reducing code size, we support only a practical subset of the specification. Media types will be ignored (and must not contain a literal comma character). Base64 encoding is supported.

URI encoding will already have been stripped by the URI parser. Special characters may therefore be used in the URI string (e.g. "hello%20world"). However, URI-encoded NUL bytes (%00) will not work as expected, since they will be interpreted as terminating the URI string. If NUL bytes are required, then Base64 encoding must be used instead.

Definition in file datauri.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ datauri_parse()

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

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
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 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().

◆ datauri_open()

int datauri_open ( struct interface * xfer,
struct uri * uri )
static

Open data URI.

Parameters
xferData transfer interface
uriURI
Return values
rcReturn status code

Definition at line 118 of file datauri.c.

118 {
119 void *data;
120 int len;
121 int rc;
122
123 /* Allocate space for parsed data */
124 data = malloc ( datauri_max_len ( uri ) );
125 if ( ! data ) {
126 rc = -ENOMEM;
127 goto err_alloc;
128 }
129
130 /* Parse data */
131 len = datauri_parse ( uri, data );
132 if ( len < 0 ) {
133 rc = len;
134 goto err_parse;
135 }
136
137 /* Open downloadable blob */
138 if ( ( rc = blob_open ( xfer, data, len ) ) != 0 )
139 goto err_open;
140
141 err_open:
142 err_parse:
143 free ( data );
144 err_alloc:
145 return rc;
146}
int blob_open(struct interface *xfer, const void *data, size_t len)
Open data blob.
Definition blob.c:110
int datauri_parse(struct uri *uri, void *buf)
Parse data URI.
Definition datauri.c:62
static size_t datauri_max_len(struct uri *uri)
Get maximum length of parsed data URI.
Definition datauri.h:23
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define ENOMEM
Not enough space.
Definition errno.h:535
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:621
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55

References blob_open(), data, datauri_max_len(), datauri_parse(), ENOMEM, free, len, malloc(), and rc.

Variable Documentation

◆ __uri_opener

struct uri_opener data_uri_opener __uri_opener
Initial value:
= {
.scheme = "data",
.open = datauri_open,
}
static int datauri_open(struct interface *xfer, struct uri *uri)
Open data URI.
Definition datauri.c:118

Data URI opener.

Definition at line 149 of file datauri.c.

149 {
150 .scheme = "data",
151 .open = datauri_open,
152};