BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlde_base64encoderoptions.h
Go to the documentation of this file.
1/// @file bdlde_base64encoderoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlde_base64encoderoptions.h -*-C++-*-
8#ifndef INCLUDED_BDLDE_BASE64ENCODEROPTIONS
9#define INCLUDED_BDLDE_BASE64ENCODEROPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT_RCSID(bdlde_base64encoderoptions_h,"$Id$ $CSID$")
14
15/// @defgroup bdlde_base64encoderoptions bdlde_base64encoderoptions
16/// @brief Provide a value-semantic attribute class for encoder options.
17/// @addtogroup bdl
18/// @{
19/// @addtogroup bdlde
20/// @{
21/// @addtogroup bdlde_base64encoderoptions
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bdlde_base64encoderoptions-purpose"> Purpose</a>
26/// * <a href="#bdlde_base64encoderoptions-classes"> Classes </a>
27/// * <a href="#bdlde_base64encoderoptions-description"> Description </a>
28/// * <a href="#bdlde_base64encoderoptions-attributes"> Attributes </a>
29/// * <a href="#bdlde_base64encoderoptions-pre-defined-styles"> Pre-Defined Styles </a>
30/// * <a href="#bdlde_base64encoderoptions-standards-references"> Standards References </a>
31/// * <a href="#bdlde_base64encoderoptions-rfc-2045---mime-standard"> RFC-2045 - Mime Standard </a>
32/// * <a href="#bdlde_base64encoderoptions-rfc-4648---the-base16|32|64-encoding-standard"> RFC-4648 - The Base16|32|64 Encoding Standard </a>
33/// * <a href="#bdlde_base64encoderoptions-usage"> Usage </a>
34/// * <a href="#bdlde_base64encoderoptions-example-1-basic-usage"> Example 1: Basic Usage </a>
35/// * <a href="#bdlde_base64encoderoptions-example-2"> Example 2: </a>
36/// * <a href="#bdlde_base64encoderoptions-example-3"> Example 3: </a>
37/// * <a href="#bdlde_base64encoderoptions-example-4"> Example 4: </a>
38///
39/// # Purpose {#bdlde_base64encoderoptions-purpose}
40/// Provide a value-semantic attribute class for encoder options.
41///
42/// # Classes {#bdlde_base64encoderoptions-classes}
43///
44/// - bdlde::Base64EncoderOptions: options for encoder
45///
46/// @see bdlde_base64decoderoptions, bdlde_base64encoder,
47/// bdlde_base64decoder, bdlde_base64alphabet
48///
49/// # Description {#bdlde_base64encoderoptions-description}
50/// This component provides a value-semantic attribute class for
51/// specifying options for `bdlde::Base64Encoder`.
52///
53/// This `class` supports default-generated copy construction and copy
54/// assignment, but the constructor is private. To create an object one must
55/// call one of the class methods, which will return a newly-constructed object
56/// by value. Specialized class methods are provided to create objects
57/// configured for the `mime`, `urlSafe`, and `standard` configurations.
58///
59/// Other configurations may be obtained by specifying arguments to the `custom`
60/// class method, or by calling the setters after the object is created.
61///
62/// ## Attributes {#bdlde_base64encoderoptions-attributes}
63///
64///
65/// @code
66/// Name Type
67/// ------------- --------------------
68/// maxLineLength int
69/// alphabet Base64Alphabet::Enum
70/// isPaded bool
71/// @endcode
72/// * `maxLineLength`: the maximum number of Base64 characters to output before
73/// inserting a '\n'. If this value is 0, maxLineLength is ignored, and no
74/// '\n' characters are output in the Base64 text.
75///
76/// * `alphabet`: describes the set of available characters (or alphabet) that
77/// may appear in the resulting encoded text. Note that Base64 encoding
78/// breaks binary data into 64 bit blocks, where the numeric values 0-61 are
79/// encoded using [0-9a-zA-Z], the alphabet only impacts how the values 62
80/// and 63 are represented:
81///
82/// - `e_BASIC` : defined in standard RFC 2045 section 6.8, and should be the
83/// default choice, uses '+' and '/' for 62 and 63, respectively,
84///
85/// - `e_URL` : defined in RFC 4648 section 5, creates an encoded
86/// representation suitable for use in a URL or filename. The alphabet
87/// avoids characters like '/' that have a special meaning in the context
88/// of a URL or filename. Uses '-' and '_' for 62 and 63, respectively.
89///
90/// * `isPadded` : if true, indicates that output is padded with '=' characters
91/// to a multiple of 4 characters.
92///
93/// ## Pre-Defined Styles {#bdlde_base64encoderoptions-pre-defined-styles}
94///
95///
96/// The constructor is private; the client is expected to use the public class
97/// methods to create 'option's objects. The 'custom' class method can create
98/// any arbitrary value, plus there are 3 pre-defined class methods. The
99/// following table shows the hard-wired attribute values of these class methods
100/// -- if a value is in parentheses, it means the attribute is a parameter of
101/// the method and the value in parentheses is its default value.
102///
103/// | class method | maxLineLength | alphabet | isPadded |
104/// | :----------- | :-----------: | :------: | :------: |
105/// | custom | any | any | any |
106/// | mime | 76 | e_BASIC | true |
107/// | standard | 0 [disabled] | e_BASIC | (true) |
108/// | urlSafe | 0 [disabled] | e_URL | (false) |
109///
110/// Note that in the above table, values in parentheses -- e.g. (true) indicate
111/// a default which may be overrideen by a user-supplied value.
112///
113/// ## Standards References {#bdlde_base64encoderoptions-standards-references}
114///
115///
116/// Below is some background on standards related to base64 encoding.
117///
118/// ### RFC-2045 - Mime Standard {#bdlde_base64encoderoptions-rfc-2045---mime-standard}
119///
120///
121/// The Base64 encoding originated as a way of encoding arbitrary binary data
122/// into the body of ASCII-only emails. This was the MIME standard,
123/// https://datatracker.ietf.org/doc/html/rfc2045#section-6.8
124///
125/// The `mime` factory function creates a Base64Encoder configured to the MIME
126/// specification.
127///
128/// ### RFC-4648 - The Base16|32|64 Encoding Standard {#bdlde_base64encoderoptions-rfc-4648---the-base16|32|64-encoding-standard}
129///
130///
131/// Eventually, Base64 encoding was separately standardized in RFC-4648:
132/// https://datatracker.ietf.org/doc/html/rfc464
133///
134/// This standard differs slightly from the original MIME Base64 encoding in
135/// that the encoding doesn't have line feeds, and the padding at the end of a
136/// line is optional.
137///
138/// This standard provides a couple variants of the alphabet of characters that
139/// can be used to encode the data.
140///
141/// **Standard Alphabet** - the standard alphabet is described in
142/// https://datatracker.ietf.org/doc/html/rfc4648#section-4. A factory function
143/// `standard` is provided to create a `Base64Encoder` configured to use the
144/// standard alphabet.
145///
146/// ** URL and Filename Safe Alphabet** - An alternative alphabet for encoding
147/// data to be used in file names or URLs in
148/// https://datatracker.ietf.org/doc/html/rfc4648#section-5. A factory function
149/// `urlSafe` is provided to create a `Base64Encoder` configured to use the URL
150/// and filename safe alphabet. This alphabet avoids characters, like '/', that
151/// have a special meaning the the context of a file name or URL.
152///
153/// ## Usage {#bdlde_base64encoderoptions-usage}
154///
155///
156/// This section illustrates intended use of this component.
157///
158/// ### Example 1: Basic Usage {#bdlde_base64encoderoptions-example-1-basic-usage}
159///
160///
161/// Suppose we want a `Base64EncoderOptions` object configured for MIME
162/// encoding, meaning `maxLineLength == 76`, `alphabet == e_BASIC`, and
163/// `isPadded == true`.
164///
165/// First, it turns out that those are the default values of the attributes, so
166/// all we have to do is default construct an object, and we're done.
167/// @code
168/// const bdlde::Base64EncoderOptions& mimeOptions =
169/// bdlde::Base64EncoderOptions::mime();
170/// @endcode
171/// Then, we check the attributes:
172/// @code
173/// assert(mimeOptions.maxLineLength() == 76);
174/// assert(mimeOptions.alphabet() == bdlde::Base64Alphabet::e_BASIC);
175/// assert(mimeOptions.isPadded() == true);
176/// @endcode
177/// Now, we stream the object:
178/// @code
179/// mimeOptions.print(cout);
180/// @endcode
181/// Finally, we observe the output:
182/// @code
183/// [
184/// maxLineLength = 76
185/// alphabet = BASIC
186/// isPadded = true
187/// ]
188/// @endcode
189///
190/// ### Example 2: {#bdlde_base64encoderoptions-example-2}
191///
192///
193/// Suppose we want a `Base64EncoderOptions` object configured for translating
194/// URL's. That would mean a `maxLineLength == 0`, `alphabet == e_URL`, and
195/// `isPadded == false`.
196///
197/// First, the class method `urlSafe` returns an object configured exactly that
198/// way, so we simply call it:
199/// @code
200/// const bdlde::Base64EncoderOptions& urlOptions =
201/// bdlde::Base64EncoderOptions::urlSafe();
202/// @endcode
203/// Then, we check the attributes:
204/// @code
205/// assert(urlOptions.maxLineLength() == 0);
206/// assert(urlOptions.alphabet() == bdlde::Base64Alphabet::e_URL);
207/// assert(urlOptions.isPadded() == false);
208/// @endcode
209/// Now, we stream the object:
210/// @code
211/// urlOptions.print(cout);
212/// @endcode
213/// Finally, we observe the output:
214/// @code
215/// [
216/// maxLineLength = 0
217/// alphabet = URL
218/// isPadded = false
219/// ]
220/// @endcode
221///
222/// ### Example 3: {#bdlde_base64encoderoptions-example-3}
223///
224///
225/// Suppose we want an options object configured for standard Base64:
226///
227/// First, we can simply call the `standard` class method:
228/// @code
229/// const bdlde::Base64EncoderOptions& standardOptions =
230/// bdlde::Base64EncoderOptions::standard();
231/// @endcode
232/// Then, we check the attributes:
233/// @code
234/// assert(standardOptions.maxLineLength() == 0);
235/// assert(standardOptions.alphabet() == bdlde::Base64Alphabet::e_BASIC);
236/// assert(standardOptions.isPadded() == true);
237/// @endcode
238/// Now, we stream the object:
239/// @code
240/// standardOptions.print(cout);
241/// @endcode
242/// Finally, we observe the output:
243/// @code
244/// [
245/// maxLineLength = 0
246/// alphabet = BASIC
247/// isPadded = true
248/// ]
249/// @endcode
250///
251/// ### Example 4: {#bdlde_base64encoderoptions-example-4}
252///
253///
254/// Suppose we want a really strangely configured options object with
255/// `maxLineLength == 200`, `alphabet == e_URL`, and padding.
256///
257/// First, we can simply call the `custom` class method:
258/// @code
259/// const bdlde::Base64EncoderOptions& customOptions =
260/// bdlde::Base64EncoderOptions::custom(200,
261/// bdlde::Base64Alphabet::e_URL,
262/// true);
263/// @endcode
264/// Then, we check the attributes:
265/// @code
266/// assert(customOptions.maxLineLength() == 200);
267/// assert(customOptions.alphabet() == bdlde::Base64Alphabet::e_URL);
268/// assert(customOptions.isPadded() == true);
269/// @endcode
270/// Now, we stream the object:
271/// @code
272/// cout << customOptions << endl;
273/// @endcode
274/// Finally, we observe the output:
275/// @code
276/// [ maxLineLength = 200 alphabet = URL isPadded = true ]
277/// @endcode
278/// @}
279/** @} */
280/** @} */
281
282/** @addtogroup bdl
283 * @{
284 */
285/** @addtogroup bdlde
286 * @{
287 */
288/** @addtogroup bdlde_base64encoderoptions
289 * @{
290 */
291
292#include <bdlde_base64alphabet.h>
293
295
296#include <bsls_assert.h>
297
298#include <bsl_iosfwd.h>
299
300
301namespace bdlde {
302
303 // ==========================
304 // class Base64EncoderOptions
305 // ==========================
306
307/// This `class` stores the configuration of a `Base64Encoder`.
308///
309/// See @ref bdlde_base64encoderoptions
311
312 // DATA
313 int d_maxLineLength; // the max line length of output
314 // between CRLF's. A value of
315 // 0 means no CRLF's will be
316 // added
317
318 Base64Alphabet::Enum d_alphabet; // the alphabet to be used --
319 // basic or url
320
321 bool d_isPadded; // is the output to be padded with
322 // '='s
323
324 public:
325 // PUBLIC TYPES
327
328 private:
329 // PRIVATE CREATORS
330
331 /// Create a `Base64EncoderOptions` object having the specified
332 /// `maxLineLength, `alphabet', and `isPadded` attribute values. The
333 /// behavior is unless `0 <= maxLineLength` and `alphabet` is a defined
334 /// value of `Base64Alphabet::Enum`.
337 bool padded);
338
339 public:
340 // CLASS METHODS
341
342 /// Return a `Base64EncoderOptions` object having the specified
343 /// `maxLineLength, alphabet, and `isPadded' attribute values. The
344 /// behavior is unless `0 <= maxLineLength` and 'alphabet is a defined
345 /// value of `Base64Alphabet::Enum`.
346 static
349 bool padded);
350
351 /// Return a `Base64EncoderOptions` object having the attributes
352 /// `maxLineLength == 76`, `alphabet == Base64Alphabet::e_BASIC`, and
353 /// `isPadded == true`. This conforms to RFC 2045.
354 static
356
357 /// Return a `Base64EncoderOptions` object having the attributes
358 /// `maxLineLength == 0`, `alphabet == Base64Alphabet::e_BASIC`, and
359 /// `isPadded == false`. If `padded` is not specified, it defaults to
360 /// `true`. This conforms to RFC 4648 section 4.
361 static
362 Base64EncoderOptions standard(bool padded = true);
363
364 /// Return a `Base64EncoderOptions` object having the attributes
365 /// `maxLineLength == 0`, `alphabet == Base64Alphabet::e_URL`, and
366 /// the specified `padded`. If `padded` is not specified, it defaults
367 /// to `false`. This conforms to RFC 4648 section 5.
368 static
369 Base64EncoderOptions urlSafe(bool padded = false);
370
371 // CREATORS
372
373 /// Default copy constructor.
375
376 /// Destroy this object.
378
379 // MANIPULATORS
380
381 /// Default operator=().
383
384 /// Set the `alphabet` attribute to the specified `value`.
385 ///
386 /// \pre The behavior is undefined unless `value` is either `e_BASIC` or `e_UTL`.
388
389 /// Set the `isPadded` attribute to the specified `value`.
390 void setIsPadded(bool value);
391
392 /// Set the `maxLineLength` attribute to the specified `value`.
393 ///
394 /// \pre The behavior is undefined unless `0 <= value`.
395 void setMaxLineLength(int value);
396
397 // ACCESSORS
398
399 /// Format this object to the specified output `stream` at the
400 /// optionally specified indentation `level` and return a reference to
401 /// the modifiable `stream`. If `level` is specified, optionally
402 /// specify `spacesPerLevel`, the number of spaces per indentation level
403 /// for this and all of its nested objects. Each line is indented by
404 /// the absolute value of `level * spacesPerLevel`. If `level` is
405 /// negative, suppress indentation of the first line. If
406 /// `spacesPerLevel` is negative, suppress line breaks and format the
407 /// entire output on one line. If `stream` is initially invalid, this operation has no effect.
408 ///
409 /// \note Note that a trailing newline is provided
410 /// in multiline mode only.
411 bsl::ostream& print(bsl::ostream& stream,
412 int level = 0,
413 int spacesPerLevel = 4) const;
414
415 /// Return the value of the `alphabet` attribute.
417
418 /// Return the value of the `isPadded` attribute.
419 bool isPadded() const;
420
421 /// Return the value of the `maxLineLength` attribute.
422 int maxLineLength() const;
423};
424
425// FREE OPERATORS
426
427/// Return `true` if the specified `lhs` and `rhs` attribute objects have
428/// the same value, and `false` otherwise. Two attribute objects have the
429/// same value if each respective attribute has the same value.
430bool operator==(const Base64EncoderOptions& lhs,
431 const Base64EncoderOptions& rhs);
432
433/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
434/// have the same value, and `false` otherwise. Two attribute objects do
435/// not have the same value if one or more respective attributes differ in
436/// values.
437bool operator!=(const Base64EncoderOptions& lhs,
438 const Base64EncoderOptions& rhs);
439
440/// Format the specified `rhs` to the specified output `stream` and
441/// return a reference to the modifiable `stream`.
442bsl::ostream& operator<<(bsl::ostream& stream,
443 const Base64EncoderOptions& rhs);
444
445} // close package namespace
446
447
448// TRAITS
449namespace bsl {
450
451template <>
454
455
456} // close namespace bsl
457
458// ============================================================================
459// INLINE FUNCTION DEFINITIONS
460// ============================================================================
461
462
463namespace bdlde {
464
465 // --------------------------
466 // class Base64EncoderOptions
467 // --------------------------
468
469// PRIVATE CREATORS
470inline
471Base64EncoderOptions::Base64EncoderOptions(int maxLineLength,
472 Base64Alphabet::Enum alphabet,
473 bool padded)
474: d_maxLineLength(maxLineLength)
475, d_alphabet(alphabet)
476, d_isPadded(padded)
477{
478 BSLS_ASSERT(0 <= maxLineLength);
479 BSLS_ASSERT(Base64Alphabet::e_BASIC == alphabet ||
480 Base64Alphabet::e_URL == alphabet);
481}
482
483// CLASS METHODS
484inline
485Base64EncoderOptions Base64EncoderOptions::custom(
486 int maxLineLength,
487 Base64Alphabet::Enum alphabet,
488 bool padded)
489{
490 return Base64EncoderOptions(maxLineLength, alphabet, padded);
491}
492
493inline
494Base64EncoderOptions Base64EncoderOptions::mime()
495{
496 return Base64EncoderOptions(k_MIME_MAX_LINE_LENGTH,
497 Base64Alphabet::e_BASIC,
498 true);
499}
500
501inline
502Base64EncoderOptions Base64EncoderOptions::standard(bool padded)
503{
504 return Base64EncoderOptions(0, Base64Alphabet::e_BASIC, padded);
505}
506
507inline
508Base64EncoderOptions Base64EncoderOptions::urlSafe(bool padded)
509{
510 return Base64EncoderOptions(0, Base64Alphabet::e_URL, padded);
511}
512
513// MANIPULATORS
514inline
515void Base64EncoderOptions::setAlphabet(Base64Alphabet::Enum value)
516{
517 BSLS_ASSERT(Base64Alphabet::e_BASIC == value ||
518 Base64Alphabet::e_URL == value);
519
520 d_alphabet = value;
521}
522
523inline
524void Base64EncoderOptions::setIsPadded(bool value)
525{
526 d_isPadded = value;
527}
528
529inline
530void Base64EncoderOptions::setMaxLineLength(int value)
531{
532 BSLS_ASSERT(0 <= value);
533
534 d_maxLineLength = value;
535}
536
537// ACCESSORS
538inline
539Base64Alphabet::Enum Base64EncoderOptions::alphabet() const
540{
541 return d_alphabet;
542}
543
544inline
545bool Base64EncoderOptions::isPadded() const
546{
547 return d_isPadded;
548}
549
550inline
551int Base64EncoderOptions::maxLineLength() const
552{
553 return d_maxLineLength;
554}
555
556} // close package namespace
557
558
559#endif
560
561// ----------------------------------------------------------------------------
562// Copyright 2022 Bloomberg Finance L.P.
563//
564// Licensed under the Apache License, Version 2.0 (the "License");
565// you may not use this file except in compliance with the License.
566// You may obtain a copy of the License at
567//
568// http://www.apache.org/licenses/LICENSE-2.0
569//
570// Unless required by applicable law or agreed to in writing, software
571// distributed under the License is distributed on an "AS IS" BASIS,
572// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
573// See the License for the specific language governing permissions and
574// limitations under the License.
575// ----------------------------- END-OF-FILE ----------------------------------
576
577/** @} */
578/** @} */
579/** @} */
Definition bdlde_base64encoderoptions.h:310
static Base64EncoderOptions custom(int maxLineLength, Base64Alphabet::Enum alphabet, bool padded)
Definition bdlde_base64encoderoptions.h:485
~Base64EncoderOptions()=default
Destroy this object.
static Base64EncoderOptions standard(bool padded=true)
Definition bdlde_base64encoderoptions.h:502
bool isPadded() const
Return the value of the isPadded attribute.
Definition bdlde_base64encoderoptions.h:545
int maxLineLength() const
Return the value of the maxLineLength attribute.
Definition bdlde_base64encoderoptions.h:551
@ k_MIME_MAX_LINE_LENGTH
Definition bdlde_base64encoderoptions.h:326
Base64EncoderOptions & operator=(const Base64EncoderOptions &)=default
Default operator=().
static Base64EncoderOptions urlSafe(bool padded=false)
Definition bdlde_base64encoderoptions.h:508
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
void setIsPadded(bool value)
Set the isPadded attribute to the specified value.
Definition bdlde_base64encoderoptions.h:524
void setAlphabet(Base64Alphabet::Enum value)
Definition bdlde_base64encoderoptions.h:515
static Base64EncoderOptions mime()
Definition bdlde_base64encoderoptions.h:494
void setMaxLineLength(int value)
Definition bdlde_base64encoderoptions.h:530
Base64EncoderOptions(const Base64EncoderOptions &)=default
Default copy constructor.
Base64Alphabet::Enum alphabet() const
Return the value of the alphabet attribute.
Definition bdlde_base64encoderoptions.h:539
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT_RCSID(tag, str)
BSLS_IDENT_RCSID() - insert ident str (specific to platform/compiler)
Definition bsls_ident.h:244
#define BSLS_IDENT_PRAGMA_ONCE
BSLS_IDENT_PRAGMA_ONCE - macro to avoid multiple inclusion
Definition bsls_ident.h:263
Definition bdlde_base64alphabet.h:118
bsl::ostream & operator<<(bsl::ostream &stream, Base64Alphabet::Enum value)
Definition bdlat_valuetypefunctions.h:939
Enum
Definition bdlde_base64alphabet.h:137
Definition bslmf_istriviallycopyable.h:324