CIRCT  19.0.0git
Utils.cpp
Go to the documentation of this file.
1 //===- Utils.cpp - implementations ESI utility code -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // DO NOT EDIT!
10 // This file is distributed as part of an ESI package. The source for this file
11 // should always be modified within CIRCT
12 // (lib/dialect/ESI/runtime/cpp/lib/backends/Cosim.cpp).
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "esi/Utils.h"
17 
18 static constexpr char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
19  "abcdefghijklmnopqrstuvwxyz"
20  "0123456789+/";
21 
22 // Copied then modified slightly from llvm/lib/Support/Base64.cpp.
23 void esi::utils::encodeBase64(const void *dataIn, size_t size,
24  std::string &buffer) {
25  const char *data = static_cast<const char *>(dataIn);
26  buffer.resize(((size + 2) / 3) * 4);
27 
28  size_t i = 0, j = 0;
29  for (size_t n = size / 3 * 3; i < n; i += 3, j += 4) {
30  uint32_t x = ((unsigned char)data[i] << 16) |
31  ((unsigned char)data[i + 1] << 8) | (unsigned char)data[i + 2];
32  buffer[j + 0] = Table[(x >> 18) & 63];
33  buffer[j + 1] = Table[(x >> 12) & 63];
34  buffer[j + 2] = Table[(x >> 6) & 63];
35  buffer[j + 3] = Table[x & 63];
36  }
37  if (i + 1 == size) {
38  uint32_t x = ((unsigned char)data[i] << 16);
39  buffer[j + 0] = Table[(x >> 18) & 63];
40  buffer[j + 1] = Table[(x >> 12) & 63];
41  buffer[j + 2] = '=';
42  buffer[j + 3] = '=';
43  } else if (i + 2 == size) {
44  uint32_t x =
45  ((unsigned char)data[i] << 16) | ((unsigned char)data[i + 1] << 8);
46  buffer[j + 0] = Table[(x >> 18) & 63];
47  buffer[j + 1] = Table[(x >> 12) & 63];
48  buffer[j + 2] = Table[(x >> 6) & 63];
49  buffer[j + 3] = '=';
50  }
51 }
static constexpr char Table[]
Definition: Utils.cpp:18
void encodeBase64(const void *data, size_t size, std::string &out)
Definition: Utils.cpp:23