Coverage Report

Created: 2026-03-31 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rdkit/Code/DataStructs/BitVect.cpp
Line
Count
Source
1
// $Id$
2
//
3
// Copyright (c) 2003-2008 greg Landrum and Rational Discovery LLC
4
//
5
//  @@ All Rights Reserved @@
6
//  This file is part of the RDKit.
7
//  The contents are covered by the terms of the BSD license
8
//  which is included in the file license.txt, found at the root
9
//  of the RDKit source tree.
10
//
11
#include "BitVect.h"
12
#include <sstream>
13
#include <limits>
14
#include <RDGeneral/StreamOps.h>
15
#include "base64.h"
16
#ifdef WIN32
17
#include <ios>
18
#endif
19
#include <cstdint>
20
21
0
BitVect::~BitVect() {};  // must always implement virtual destructors
22
23
void BitVect::initFromText(const char *data, const unsigned int dataLen,
24
0
                           bool isBase64, bool allowOldFormat) {
25
0
  std::stringstream ss(std::ios_base::binary | std::ios_base::in |
26
0
                       std::ios_base::out);
27
0
  if (isBase64) {
28
0
    unsigned int actualLen;
29
0
    char *decoded;
30
0
    decoded = Base64Decode((const char *)data, &actualLen);
31
0
    ss.write(decoded, actualLen);
32
0
    delete[] decoded;
33
0
  } else {
34
0
    ss.write(data, dataLen);
35
0
  }
36
37
0
  std::int32_t format = 0;
38
0
  std::int32_t version = 0;
39
0
  std::uint32_t nOn = 0;
40
0
  std::int32_t size;
41
42
  // earlier versions of the code did not have the version number encoded, so
43
  //  we'll use that to distinguish version 0
44
0
  RDKit::streamRead(ss, size);
45
0
  if (size < 0) {
46
0
    version = -1 * size;
47
0
    switch (version) {
48
0
      case 16:
49
0
        format = 1;
50
0
        break;
51
0
      case 32:
52
0
        format = 2;
53
0
        break;
54
0
      default:
55
0
        throw ValueErrorException("bad version in BitVect pickle");
56
0
        break;
57
0
    }
58
0
    RDKit::streamRead(ss, size);
59
0
  } else if (!allowOldFormat) {
60
0
    throw ValueErrorException("invalid BitVect pickle");
61
0
  }
62
63
0
  RDKit::streamRead(ss, nOn);
64
0
  _initForSize(static_cast<int>(size));
65
66
  // if the either have older version or or version 16 with ints for on bits
67
0
  if ((format == 0) ||
68
0
      ((format == 1) && (size >= std::numeric_limits<unsigned short>::max()))) {
69
0
    std::uint32_t tmp;
70
0
    for (unsigned int i = 0; i < nOn; i++) {
71
0
      RDKit::streamRead(ss, tmp);
72
0
      setBit(tmp);
73
0
    }
74
0
  } else if (format == 1) {  // version 16 and on bits stored as short ints
75
0
    std::uint16_t tmp;
76
0
    for (unsigned int i = 0; i < nOn; i++) {
77
0
      RDKit::streamRead(ss, tmp);
78
0
      setBit(tmp);
79
0
    }
80
0
  } else if (format == 2) {  // run length encoded format
81
0
    std::uint32_t curr = 0;
82
0
    for (unsigned int i = 0; i < nOn; i++) {
83
0
      curr += RDKit::readPackedIntFromStream(ss);
84
0
      setBit(curr);
85
0
      curr++;
86
0
    }
87
0
  }
88
0
}