1 | /* $Id: string-base64.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - UTF-8 and UTF-16 string classes, BASE64 bits.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "VBox/com/string.h"
|
---|
19 |
|
---|
20 | #include <iprt/base64.h>
|
---|
21 | #include <iprt/errcore.h>
|
---|
22 |
|
---|
23 |
|
---|
24 | namespace com
|
---|
25 | {
|
---|
26 |
|
---|
27 | HRESULT Bstr::base64Encode(const void *pvData, size_t cbData, bool fLineBreaks /*= false*/)
|
---|
28 | {
|
---|
29 | uint32_t const fFlags = fLineBreaks ? RTBASE64_FLAGS_EOL_LF : RTBASE64_FLAGS_NO_LINE_BREAKS;
|
---|
30 | size_t cwcEncoded = RTBase64EncodedUtf16LengthEx(cbData, fFlags);
|
---|
31 | HRESULT hrc = reserveNoThrow(cwcEncoded + 1);
|
---|
32 | if (SUCCEEDED(hrc))
|
---|
33 | {
|
---|
34 | int vrc = RTBase64EncodeUtf16Ex(pvData, cbData, fFlags, mutableRaw(), cwcEncoded + 1, &cwcEncoded);
|
---|
35 | AssertRCReturnStmt(vrc, setNull(), E_FAIL);
|
---|
36 | hrc = joltNoThrow(cwcEncoded);
|
---|
37 | }
|
---|
38 | return hrc;
|
---|
39 | }
|
---|
40 |
|
---|
41 | int Bstr::base64Decode(void *pvData, size_t cbData, size_t *pcbActual /*= NULL*/, PRTUTF16 *ppwszEnd /*= NULL*/)
|
---|
42 | {
|
---|
43 | return RTBase64DecodeUtf16Ex(raw(), RTSTR_MAX, pvData, cbData, pcbActual, ppwszEnd);
|
---|
44 | }
|
---|
45 |
|
---|
46 | ssize_t Bstr::base64DecodedSize(PRTUTF16 *ppwszEnd /*= NULL*/)
|
---|
47 | {
|
---|
48 | return RTBase64DecodedUtf16SizeEx(raw(), RTSTR_MAX, ppwszEnd);
|
---|
49 | }
|
---|
50 |
|
---|
51 | } /* namespace com */
|
---|