Boolset Encoding and Decoding
Boolset is an encoding method used to represent a set of boolean values. It converts a set of boolean values into a string for easy storage and transmission. In Cloudreve's API, Boolset is typically used for permission settings, such as user group permissions and file permissions.
Internal Representation
The BooleanSet
type is essentially a byte slice ([]byte
). In this slice, each bit represents a boolean value (permission switch).
- The
0
th flag bit corresponds to the0
th bit of the0
th byte in the byte slice. - The
1
st flag bit corresponds to the1
st bit of the0
th byte in the byte slice. - ...
- The
7
th flag bit corresponds to the7
th bit of the0
th byte in the byte slice. - The
8
th flag bit corresponds to the0
th bit of the1
st byte in the byte slice. - And so on...
A bit value of 1
indicates the corresponding flag is true
(enabled), while 0
indicates false
(disabled).
Encoding Process
- Determine Maximum Index: Find the maximum index
max_n
among all flags to be represented. - Calculate Byte Count: Determine the number of bytes required. At least
floor(max_n / 8) + 1
bytes are needed to accommodate all flags. - Initialize Byte Sequence: Create a byte sequence with the required number of bytes, initializing all bytes to
0
. - Set Flags: For each flag
n
that needs to be set totrue
:- Locate the
byte_index = floor(n / 8)
th byte in the byte sequence. - Calculate the bit index within that byte:
bit_index = n % 8
. - Set the
bit_index
th bit of that byte to1
. This is typically done using a Bitwise OR operation:byte[byte_index] = byte[byte_index] | (1 << bit_index)
.
- Locate the
- Base64 Encode: Encode the final byte sequence into a Base64 string.
Decoding Process
- Base64 Decode: First, decode the Base64 string back into the original byte sequence.
- Check Flag: To check the status (
true
orfalse
) of a specific flagn
:- Calculate the byte index:
byte_index = floor(n / 8)
. - Boundary Check: Check if
byte_index
is within the valid range of the byte sequence. If it's out of range, the flag is implicitlyfalse
(because it was never set). - If
byte_index
is valid, locate thebyte_index
th byte in the sequence. - Calculate the bit index:
bit_index = n % 8
. - Check the value of the
bit_index
th bit of that byte. This is typically done using a Bitwise AND operation:is_set = (byte[byte_index] & (1 << bit_index)) != 0
. - If the result is non-zero, flag
n
istrue
; otherwise, it isfalse
.
- Calculate the byte index:
Programming Language Implementation
Golang
go
// Load from string
bs, err := boolset.FromString("/f8B")
if err != nil {
return nil, err
}
// Read the 5th flag bit
fmt.Println(bs.Enabled(5))
// Set the 1st flag bit
boolset.Set(1, true, bs)
// Save to string
encoded, err := bs.String()
if err != nil {
return nil, err
}
TypeScript
typescript
// Load from string
const bs = new Boolset("/f8B");
// Read the 5th flag bit
console.log(bs.enabled(5));
// Set the 1st flag bit
bs.set(1, true);
// Save to string
const encoded = bs.toString();
Flag Definition
User Group Permissions
Flag Bit | Description |
---|---|
0 | Is administrator |
1 | Is anonymous user group |
2 | Can share files |
3 | Can access WebDAV |
4 | Can perform server-side batch download |
5 | Can execute archive compression tasks |
6 | Can enable WebDAV proxy |
7 | Can download shares from others |
8 | Can download shares for free |
9 | Can perform remote downloads |
10 | Can relocate storage policy |
11 | Use redirect direct link |
12 | Can use advanced delete options |
13 | Can select specific node to process tasks |
14 | Can set higher share permissions for anonymous users |
File Permissions
Flag Bit | Description |
---|---|
0 | Read |
1 | Update |
2 | Create |
3 | Delete |
Node Functions
Flag Bit | Description |
---|---|
0 | Reserved, do not use |
1 | Create compressed file |
2 | Decompress file |
3 | Remote download |
4 | Transfer storage policy |