0031501: Foundation Classes, Message_Printer - remove theToPutEndl argument -- use...
[occt.git] / src / FSD / FSD_Base64Decoder.cxx
CommitLineData
fc552d84 1// Author: Kirill Gavrilov
2// Copyright (c) 2016-2019 OPEN CASCADE SAS
3//
4// This file is part of Open CASCADE Technology software library.
5//
6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
11//
12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
14
15#include <FSD_Base64Decoder.hxx>
16
17#include <Message.hxx>
18#include <Message_Messenger.hxx>
19
20// =======================================================================
21// function : Decode
22// purpose :
23// =======================================================================
24Handle(NCollection_Buffer) FSD_Base64Decoder::Decode (const Standard_Byte* theStr,
25 const Standard_Size theLen)
26{
27 //! Look-up table for decoding base64 stream.
28 static const Standard_Byte THE_BASE64_FROM[128] =
29 {
30 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
31 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
32 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 62, 255, 63,
33 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 0, 255, 255, 255,
34 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
35 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 63,
36 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
37 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255
38 };
39
40 Handle(NCollection_Buffer) aData = new NCollection_Buffer (NCollection_BaseAllocator::CommonBaseAllocator());
41 if (!aData->Allocate (3 * theLen / 4))
42 {
a87b1b37 43 Message::SendFail ("Fail to allocate memory.");
fc552d84 44 return Handle(NCollection_Buffer)();
45 }
46
47 Standard_Byte* aDataPtr = aData->ChangeData();
48 const Standard_Byte* anEnd = theStr + theLen;
49 for (const Standard_Byte* aByteIter = theStr; aByteIter < anEnd; aByteIter += 4)
50 {
51 // get values for each group of four base 64 characters
52 const Standard_Byte b4[4] =
53 {
54 aByteIter + 0 < anEnd && aByteIter[0] <= 'z' ? THE_BASE64_FROM[aByteIter[0]] : Standard_Byte(0xFF),
55 aByteIter + 1 < anEnd && aByteIter[1] <= 'z' ? THE_BASE64_FROM[aByteIter[1]] : Standard_Byte(0xFF),
56 aByteIter + 2 < anEnd && aByteIter[2] <= 'z' ? THE_BASE64_FROM[aByteIter[2]] : Standard_Byte(0xFF),
57 aByteIter + 3 < anEnd && aByteIter[3] <= 'z' ? THE_BASE64_FROM[aByteIter[3]] : Standard_Byte(0xFF)
58 };
59
60 // transform into a group of three bytes
61 const Standard_Byte b3[3] =
62 {
63 Standard_Byte(((b4[0] & 0x3F) << 2) + ((b4[1] & 0x30) >> 4)),
64 Standard_Byte(((b4[1] & 0x0F) << 4) + ((b4[2] & 0x3C) >> 2)),
65 Standard_Byte(((b4[2] & 0x03) << 6) + ((b4[3] & 0x3F) >> 0))
66 };
67
68 // add the byte to the return value if it isn't part of an '=' character (indicated by 0xFF)
69 if (b4[1] != 0xFF)
70 {
71 *aDataPtr = b3[0];
72 ++aDataPtr;
73 }
74 if (b4[2] != 0xFF)
75 {
76 *aDataPtr = b3[1];
77 ++aDataPtr;
78 }
79 if (b4[3] != 0xFF)
80 {
81 *aDataPtr = b3[2];
82 ++aDataPtr;
83 }
84 }
85
86 return aData;
87}