Skip to content

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 0th flag bit corresponds to the 0th bit of the 0th byte in the byte slice.
  • The 1st flag bit corresponds to the 1st bit of the 0th byte in the byte slice.
  • ...
  • The 7th flag bit corresponds to the 7th bit of the 0th byte in the byte slice.
  • The 8th flag bit corresponds to the 0th bit of the 1st 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

  1. Determine Maximum Index: Find the maximum index max_n among all flags to be represented.
  2. Calculate Byte Count: Determine the number of bytes required. At least floor(max_n / 8) + 1 bytes are needed to accommodate all flags.
  3. Initialize Byte Sequence: Create a byte sequence with the required number of bytes, initializing all bytes to 0.
  4. Set Flags: For each flag n that needs to be set to true:
    • 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_indexth bit of that byte to 1. This is typically done using a Bitwise OR operation: byte[byte_index] = byte[byte_index] | (1 << bit_index).
  5. Base64 Encode: Encode the final byte sequence into a Base64 string.

Decoding Process

  1. Base64 Decode: First, decode the Base64 string back into the original byte sequence.
  2. Check Flag: To check the status (true or false) of a specific flag n:
    • 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 implicitly false (because it was never set).
    • If byte_index is valid, locate the byte_indexth byte in the sequence.
    • Calculate the bit index: bit_index = n % 8.
    • Check the value of the bit_indexth 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 is true; otherwise, it is false.

Programming Language Implementation

Golang

boolset.go

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

boolset.ts

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 BitDescription
0Is administrator
1Is anonymous user group
2Can share files
3Can access WebDAV
4Can perform server-side batch download
5Can execute archive compression tasks
6Can enable WebDAV proxy
7Can download shares from others
8Can download shares for free
9Can perform remote downloads
10Can relocate storage policy
11Use redirect direct link
12Can use advanced delete options
13Can select specific node to process tasks
14Can set higher share permissions for anonymous users

File Permissions

Flag BitDescription
0Read
1Update
2Create
3Delete

Node Functions

Flag BitDescription
0Reserved, do not use
1Create compressed file
2Decompress file
3Remote download
4Transfer storage policy