# base/dataurl (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/dataurl/

Decodes a "data:" URL. (in the original, fetch and the browser accept data: URLs as is)
- Decodes percent forms in the body first, then decodes base64 if the header ends with ;base64 (case-insensitive). (the same order as fetch's data: URL handling)
- The type is between "data:" and ;base64. If empty, it is text/plain;charset=US-ASCII like fetch.
- Used by desktop asset loading (Engine::request_asset_bytes, load_image, load_audio_asset) and the timeline's data: images.
  On the web, the browser's fetch decodes data: URLs. (the asset base URL is not prepended)

## DataUrl (struct)

A decoded data: URL. (type and bytes)

Example: Decode a base64 data: URL and read its type and bytes

```rust
use vanilla::base::dataurl::parse_data_url;

let data_url = parse_data_url("data:text/plain;base64,SGVsbG8=").unwrap();
assert_eq!(data_url.mime_type, "text/plain");
assert_eq!(data_url.bytes, b"Hello".to_vec());
```

- `pub mime_type: String` (field, since 0.4.0): Type. (the text between "data:" and ;base64 with leading and trailing whitespace trimmed, case kept. text/plain;charset=US-ASCII if empty)
  - returns String
- `pub bytes: Vec<u8>` (field, since 0.4.0): Decoded bytes of the body. (percent forms decoded, and base64 decoded too if ;base64)
  - returns Vec<u8>

## Functions and constants

- `pub fn is_data_url(location: &str) -> bool` (method, since 0.4.0): Whether it is a data: URL. (checks the leading "data:" case-insensitively)
  - `location` &str: URL or asset path to check.
  - returns bool: true if the first 5 bytes are "data:". (false if shorter than 5 bytes)
- `pub fn parse_data_url(data_url: &str) -> Option<DataUrl>` (method, since 0.4.0): Decodes a data: URL. (None if there is no comma or the base64 is invalid)
  - `data_url` &str: data: URL to decode. (`data:type[;base64],body`)
  - returns Option<DataUrl>: Type and bytes. None if it is not a data: URL, has no comma, or is ;base64 and the percent-decoded body is not UTF-8 or the base64 has invalid characters.

  Example: A URL with an empty type and a URL that cannot be decoded

  ```rust
  use vanilla::base::dataurl::parse_data_url;
  
  let text_url = parse_data_url("data:,a%20b").unwrap();
  assert_eq!(text_url.mime_type, "text/plain;charset=US-ASCII");
  assert_eq!(text_url.bytes, b"a b".to_vec());
  assert!(parse_data_url("data:audio/wav;base64").is_none());
  assert!(parse_data_url("data:audio/wav;base64,@@@@").is_none());
  assert!(parse_data_url("sounds/hit.wav").is_none());
  ```

- `pub fn decode_base64(text: &str) -> Option<Vec<u8>>` (method, since 0.3.2): Decodes base64. (standard alphabet, = padding and whitespace are skipped. None if there are invalid characters)
  - The URL-safe alphabet's - and _ are also accepted like + and /. = is skipped wherever it appears, and trailing bits that do not make 8 bits are discarded.
  - `text` &str: base64 string. (whitespace is space, tab and newline characters)
  - returns Option<Vec<u8>>: Decoded bytes. None if there are characters other than the alphabet, = and whitespace.

  Example: Decodes with or without padding, invalid characters give None

  ```rust
  use vanilla::base::dataurl::decode_base64;
  
  assert_eq!(decode_base64("SGVsbG8="), Some(b"Hello".to_vec()));
  assert_eq!(decode_base64("SGk"), Some(b"Hi".to_vec()));
  assert_eq!(decode_base64("S*"), None);
  ```

- `pub fn decode_percent_encoding(text: &str) -> Vec<u8>` (method, since 0.4.0): Decodes percent forms. (replaces %XX with that byte, and leaves a % without two hex digits after it as is)
  - `text` &str: String to decode. (hex digits are case-insensitive)
  - returns Vec<u8>: Decoded bytes. (returned as bytes rather than a string since it may not be UTF-8)

  Example: A % that cannot be decoded stays as is

  ```rust
  use vanilla::base::dataurl::decode_percent_encoding;
  
  assert_eq!(decode_percent_encoding("a%20b%zz%4"), b"a b%zz%4".to_vec());
  ```

