|
| 1 | +--- |
| 2 | +sidebar_position: 9 |
| 3 | +--- |
| 4 | +[//]: # (This file is auto-generated. Please do not modify it yourself.) |
| 5 | + |
| 6 | +# base64_encoded/3 |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +`base64_encoded/3` is a predicate that unifies a string to a base64 encoded string as specified by [RFC 4648](<https://rfc-editor.org/rfc/rfc4648.html>). |
| 11 | + |
| 12 | +The signature is as follows: |
| 13 | + |
| 14 | +```text |
| 15 | +base64_encoded(+Plain, -Encoded, +Options) is det |
| 16 | +base64_encoded(-Plain, +Encoded, +Options) is det |
| 17 | +base64_encoded(+Plain, +Encoded, +Options) is det |
| 18 | +``` |
| 19 | + |
| 20 | +Where: |
| 21 | + |
| 22 | +- Plain is an atom, a list of character codes, or list of characters containing the unencoded \(plain\) text. |
| 23 | +- Encoded is an atom or string containing the base64 encoded text. |
| 24 | +- Options is a list of options that can be used to control the encoding process. |
| 25 | + |
| 26 | +## Options |
| 27 | + |
| 28 | +The following options are supported: |
| 29 | + |
| 30 | +- padding\(\+Boolean\) |
| 31 | + |
| 32 | +If true \(default\), the output is padded with = characters. |
| 33 | + |
| 34 | +- charset\(\+Charset\) |
| 35 | + |
| 36 | +Define the encoding character set to use. The \(default\) 'classic' uses the classical rfc2045 characters. The value 'url' uses URL and file name friendly characters. |
| 37 | + |
| 38 | +- as\(\+Type\) |
| 39 | + |
| 40 | +Defines the type of the output. One of string \(default\) or atom. |
| 41 | + |
| 42 | +- encoding\(\+Encoding\) |
| 43 | + |
| 44 | +Encoding to use for translation between \(Unicode\) text and bytes \(Base64 is an encoding for bytes\). Default is utf8. |
| 45 | + |
| 46 | +## Examples |
| 47 | + |
| 48 | +### Encode a string into a Base64 encoded string (with default options) |
| 49 | + |
| 50 | +This scenario demonstrates how to encode a plain string into its Base64 representation using the `base64_encoded/3` |
| 51 | +predicate. The default options are used, meaning: |
| 52 | + |
| 53 | +- The output is returned as a list of characters (`as(string)`). |
| 54 | +- Padding characters (`=`) are included (`padding(true)`). |
| 55 | +- The classic Base64 character set is used (`charset(classic)`), not the URL-safe variant. |
| 56 | + |
| 57 | +Here are the steps of the scenario: |
| 58 | + |
| 59 | +- **Given** the query: |
| 60 | + |
| 61 | +``` prolog |
| 62 | +base64_encoded('Hello World', X, []). |
| 63 | +``` |
| 64 | + |
| 65 | +- **When** the query is run |
| 66 | +- **Then** the answer we get is: |
| 67 | + |
| 68 | +``` yaml |
| 69 | +height: 42 |
| 70 | +gas_used: 3975 |
| 71 | +answer: |
| 72 | + has_more: false |
| 73 | + variables: ["X"] |
| 74 | + results: |
| 75 | + - substitutions: |
| 76 | + - variable: X |
| 77 | + expression: "['S','G','V',s,b,'G','8',g,'V','2','9',y,b,'G','Q',=]" |
| 78 | +``` |
| 79 | +
|
| 80 | +### Encode a string into a Base64 encoded atom |
| 81 | +
|
| 82 | +This scenario demonstrates how to encode a plain string into a Base64-encoded atom using the `base64_encoded/3` |
| 83 | +predicate. The `as(atom)` option is specified, so the result is returned as a Prolog atom instead of a character |
| 84 | +list. All other options use their default values. |
| 85 | + |
| 86 | +Here are the steps of the scenario: |
| 87 | + |
| 88 | +- **Given** the query: |
| 89 | + |
| 90 | +``` prolog |
| 91 | +base64_encoded('Hello World', X, [as(atom)]). |
| 92 | +``` |
| 93 | + |
| 94 | +- **When** the query is run |
| 95 | +- **Then** the answer we get is: |
| 96 | + |
| 97 | +``` yaml |
| 98 | +height: 42 |
| 99 | +gas_used: 3975 |
| 100 | +answer: |
| 101 | + has_more: false |
| 102 | + variables: ["X"] |
| 103 | + results: |
| 104 | + - substitutions: |
| 105 | + - variable: X |
| 106 | + expression: "'SGVsbG8gV29ybGQ='" |
| 107 | +``` |
| 108 | + |
| 109 | +### Encode a string into a Base64 encoded atom without padding |
| 110 | + |
| 111 | +This scenario demonstrates how to encode a plain string into a Base64-encoded atom using the `base64_encoded/3` predicate |
| 112 | +with custom options. The following options are used: |
| 113 | + |
| 114 | +- `as(atom)` – the result is returned as a Prolog atom. |
| 115 | +- `padding(false)` – padding characters (`=`) are omitted. |
| 116 | +- The classic Base64 character set is used by default (`charset(classic)`). |
| 117 | + |
| 118 | +Here are the steps of the scenario: |
| 119 | + |
| 120 | +- **Given** the query: |
| 121 | + |
| 122 | +``` prolog |
| 123 | +base64_encoded('Hello World', X, [as(atom), padding(false)]). |
| 124 | +``` |
| 125 | + |
| 126 | +- **When** the query is run |
| 127 | +- **Then** the answer we get is: |
| 128 | + |
| 129 | +``` yaml |
| 130 | +height: 42 |
| 131 | +gas_used: 3975 |
| 132 | +answer: |
| 133 | + has_more: false |
| 134 | + variables: ["X"] |
| 135 | + results: |
| 136 | + - substitutions: |
| 137 | + - variable: X |
| 138 | + expression: "'SGVsbG8gV29ybGQ'" |
| 139 | +``` |
| 140 | + |
| 141 | +### Encode a String into a Base64 encoded atom in URL-Safe mode |
| 142 | + |
| 143 | +This scenario demonstrates how to encode a plain string into a Base64-encoded atom using the `base64_encoded/3` predicate |
| 144 | +with URL-safe encoding. The following options are used: |
| 145 | + |
| 146 | +- `as(atom)` – the result is returned as a Prolog atom. |
| 147 | +- `charset(url)` – the URL-safe Base64 alphabet is used (e.g., `-` and `_` instead of `+` and `/`). |
| 148 | +- Padding characters are included by default (`padding(true)`). |
| 149 | + |
| 150 | +Here are the steps of the scenario: |
| 151 | + |
| 152 | +- **Given** the query: |
| 153 | + |
| 154 | +``` prolog |
| 155 | +base64_encoded('<<???>>', Classic, [as(atom), charset(classic)]), |
| 156 | +base64_encoded('<<???>>', UrlSafe, [as(atom), charset(url)]). |
| 157 | +``` |
| 158 | + |
| 159 | +- **When** the query is run |
| 160 | +- **Then** the answer we get is: |
| 161 | + |
| 162 | +``` yaml |
| 163 | +height: 42 |
| 164 | +gas_used: 3976 |
| 165 | +answer: |
| 166 | + has_more: false |
| 167 | + variables: ["Classic", "UrlSafe"] |
| 168 | + results: |
| 169 | + - substitutions: |
| 170 | + - variable: Classic |
| 171 | + expression: "'PDw/Pz8+Pg=='" |
| 172 | + - variable: UrlSafe |
| 173 | + expression: "'PDw_Pz8-Pg=='" |
| 174 | +``` |
| 175 | + |
| 176 | +### Decode a Base64 encoded String into plain text |
| 177 | + |
| 178 | +This scenario demonstrates how to decode a Base64-encoded value back into plain text using the `base64_encoded/3` predicate. |
| 179 | +The encoded input can be provided as a character list or an atom. In this example, default options are used: |
| 180 | +• The result (plain text) is returned as a character list (`as(string)`). |
| 181 | +• Padding characters in the input are allowed (`padding(true)`). |
| 182 | +• The classic Base64 character set is used (`charset(classic)`). |
| 183 | + |
| 184 | +Here are the steps of the scenario: |
| 185 | + |
| 186 | +- **Given** the query: |
| 187 | + |
| 188 | +``` prolog |
| 189 | +base64_encoded(X, 'SGVsbG8gV29ybGQ=', []). |
| 190 | +``` |
| 191 | + |
| 192 | +- **When** the query is run |
| 193 | +- **Then** the answer we get is: |
| 194 | + |
| 195 | +``` yaml |
| 196 | +height: 42 |
| 197 | +gas_used: 3975 |
| 198 | +answer: |
| 199 | + has_more: false |
| 200 | + variables: ["X"] |
| 201 | + results: |
| 202 | + - substitutions: |
| 203 | + - variable: X |
| 204 | + expression: "['H',e,l,l,o,' ','W',o,r,l,d]" |
| 205 | +``` |
| 206 | + |
| 207 | +### Decode a Base64 Encoded string into a plain atom |
| 208 | + |
| 209 | +This scenario demonstrates how to decode a Base64-encoded value back into plain text using the `base64_encoded/3` predicate, |
| 210 | +with the result returned as a Prolog atom. The following options are used: |
| 211 | + |
| 212 | +- `as(atom)` – the decoded plain text is returned as an atom. |
| 213 | +- `padding(true)` – padding characters in the input are allowed (default). |
| 214 | +- `charset(classic)` – the classic Base64 character set is used (default). |
| 215 | + |
| 216 | +Here are the steps of the scenario: |
| 217 | + |
| 218 | +- **Given** the query: |
| 219 | + |
| 220 | +``` prolog |
| 221 | +base64_encoded(X, 'SGVsbG8gV29ybGQ=', [as(atom)]). |
| 222 | +``` |
| 223 | + |
| 224 | +- **When** the query is run |
| 225 | +- **Then** the answer we get is: |
| 226 | + |
| 227 | +``` yaml |
| 228 | +height: 42 |
| 229 | +gas_used: 3975 |
| 230 | +answer: |
| 231 | + has_more: false |
| 232 | + variables: ["X"] |
| 233 | + results: |
| 234 | + - substitutions: |
| 235 | + - variable: X |
| 236 | + expression: "'Hello World'" |
| 237 | +``` |
| 238 | + |
| 239 | +### Error on incorrect charset option |
| 240 | + |
| 241 | +This scenario demonstrates how the `base64_encoded/3` predicate behaves when an invalid value is provided for the |
| 242 | +`charset` option. |
| 243 | + |
| 244 | +Here are the steps of the scenario: |
| 245 | + |
| 246 | +- **Given** the query: |
| 247 | + |
| 248 | +``` prolog |
| 249 | +base64_encoded('Hello World', X, [charset(bad)]). |
| 250 | +``` |
| 251 | + |
| 252 | +- **When** the query is run |
| 253 | +- **Then** the answer we get is: |
| 254 | + |
| 255 | +``` yaml |
| 256 | +height: 42 |
| 257 | +gas_used: 3975 |
| 258 | +answer: |
| 259 | + has_more: false |
| 260 | + variables: ["X"] |
| 261 | + results: |
| 262 | + - error: "error(domain_error(charset,bad),base64_encoded/3)" |
| 263 | + substitutions: |
| 264 | +``` |
| 265 | + |
| 266 | +### Error on incorrect padding option |
| 267 | + |
| 268 | +This scenario demonstrates how the `base64_encoded/3` predicate behaves when an invalid value is provided for the |
| 269 | +`padding` option. |
| 270 | + |
| 271 | +Here are the steps of the scenario: |
| 272 | + |
| 273 | +- **Given** the query: |
| 274 | + |
| 275 | +``` prolog |
| 276 | +base64_encoded('Hello World', X, [padding(bad)]). |
| 277 | +``` |
| 278 | + |
| 279 | +- **When** the query is run |
| 280 | +- **Then** the answer we get is: |
| 281 | + |
| 282 | +``` yaml |
| 283 | +height: 42 |
| 284 | +gas_used: 3975 |
| 285 | +answer: |
| 286 | + has_more: false |
| 287 | + variables: ["X"] |
| 288 | + results: |
| 289 | + - error: "error(domain_error(padding,bad),base64_encoded/3)" |
| 290 | + substitutions: |
| 291 | +``` |
| 292 | + |
| 293 | +### Error on incorrect as option |
| 294 | + |
| 295 | +This scenario demonstrates how the `base64_encoded/3` predicate behaves when an invalid value is provided for the |
| 296 | +`as` option. |
| 297 | + |
| 298 | +Here are the steps of the scenario: |
| 299 | + |
| 300 | +- **Given** the query: |
| 301 | + |
| 302 | +``` prolog |
| 303 | +base64_encoded('Hello World', X, [as(bad)]). |
| 304 | +``` |
| 305 | + |
| 306 | +- **When** the query is run |
| 307 | +- **Then** the answer we get is: |
| 308 | + |
| 309 | +``` yaml |
| 310 | +height: 42 |
| 311 | +gas_used: 3975 |
| 312 | +answer: |
| 313 | + has_more: false |
| 314 | + variables: ["X"] |
| 315 | + results: |
| 316 | + - error: "error(domain_error(as,bad),base64_encoded/3)" |
| 317 | + substitutions: |
| 318 | +``` |
| 319 | + |
| 320 | +### Error on incorrect encoding option |
| 321 | + |
| 322 | +This scenario demonstrates how the `base64_encoded/3` predicate behaves when an invalid value is provided for the |
| 323 | +`encoding` option. |
| 324 | + |
| 325 | +Here are the steps of the scenario: |
| 326 | + |
| 327 | +- **Given** the query: |
| 328 | + |
| 329 | +``` prolog |
| 330 | +base64_encoded(X, 'SGVsbG8gV29ybGQ=', [as(atom), encoding(unknown)]). |
| 331 | +``` |
| 332 | + |
| 333 | +- **When** the query is run |
| 334 | +- **Then** the answer we get is: |
| 335 | + |
| 336 | +``` yaml |
| 337 | +height: 42 |
| 338 | +gas_used: 3975 |
| 339 | +answer: |
| 340 | + has_more: false |
| 341 | + variables: ["X"] |
| 342 | + results: |
| 343 | + - error: "error(type_error(charset,unknown),base64_encoded/3)" |
| 344 | + substitutions: |
| 345 | +``` |
| 346 | + |
| 347 | +### Error on incorrect encoding option (2) |
| 348 | + |
| 349 | +This scenario demonstrates how the `base64_encoded/3` predicate behaves when an invalid type is provided for the |
| 350 | +`encoding` option. |
| 351 | + |
| 352 | +Here are the steps of the scenario: |
| 353 | + |
| 354 | +- **Given** the query: |
| 355 | + |
| 356 | +``` prolog |
| 357 | +base64_encoded(X, 'SGVsbG8gV29ybGQ=', [encoding(bad, 'very bad')]). |
| 358 | +``` |
| 359 | + |
| 360 | +- **When** the query is run |
| 361 | +- **Then** the answer we get is: |
| 362 | + |
| 363 | +``` yaml |
| 364 | +height: 42 |
| 365 | +gas_used: 3975 |
| 366 | +answer: |
| 367 | + has_more: false |
| 368 | + variables: ["X"] |
| 369 | + results: |
| 370 | + - error: "error(type_error(option,encoding(bad,very bad)),base64_encoded/3)" |
| 371 | + substitutions: |
| 372 | +``` |
0 commit comments