Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bareosbase64.py « util « bareos - github.com/bareos/python-bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21bae65940582a996087ab4c669050a0fe5bddc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Bacula and therefore Bareos specific implementation of a base64 decoder.
This class offers functions to handle this.
"""

class BareosBase64(object):
    '''
    Bacula and therefore Bareos specific implementation of a base64 decoder
    '''

    base64_digits = \
        ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
         'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/']

    def __init__(self):
        '''
        Initialize the Base 64 conversion routines
        '''
        self.base64_map = dict(list(zip(self.base64_digits, list(range(0, 64)))))

    @staticmethod
    def twos_comp(val, bits):
        """compute the 2's compliment of int value val"""
        if (val&(1<<(bits-1))) != 0:
            val = val - (1<<bits)
        return val

    def base64_to_int(self, base64):
        '''
        Convert the Base 64 characters in base64 to a value.
        '''
        value = 0
        first = 0
        neg = False

        if base64[0] == '-':
            neg = True
            first = 1

        for i in range(first, len(base64)):
            value = value << 6
            try:
                value += self.base64_map[base64[i]]
            except KeyError:
                print("KeyError:", i)

        return -value if neg else value

    def int_to_base64(self, value):
        """
        Convert an integer to base 64
        """
        result = ""
        if value < 0:
            result = "-"
            value = -value

        while value:
            charnumber = value % 0x3F
            result += self.base64_digits[charnumber]
            value = value >> 6
        return result

    def string_to_base64(self, string, compatible=False):
        """
        Convert a string to base64
        """
        buf = ""
        reg = 0
        rem = 0
        char = 0
        i = 0
        while i < len(string):
            if rem < 6:
                reg <<= 8
                char = string[i]
                if not compatible:
                    if char >= 128:
                        char = self.twos_comp(char, 8)
                reg |= char
                i += 1
                rem += 8

            save = reg
            reg >>= (rem - 6)
            buf += self.base64_digits[reg & 0x3F]
            reg = save
            rem -= 6

        if rem:
            mask = (1 << rem) - 1
            if compatible:
                buf += self.base64_digits[(reg & mask) << (6 - rem)]
            else:
                buf += self.base64_digits[reg & mask]
        return bytearray(buf, 'utf-8')