Files
qmc/qmc-c/include/qmc_decryptor.h
T
2026-08-12 20:14:16 +08:00

65 lines
1.9 KiB
C

#ifndef QMC_DECRYPTOR_H
#define QMC_DECRYPTOR_H
#include <stdint.h>
#include <stddef.h>
/**
* @brief Identifies the cipher type selected for decryption.
*/
typedef enum {
CIPHER_STATIC,
CIPHER_MAP,
CIPHER_RC4
} cipher_kind_t;
/**
* @brief Polymorphic cipher container.
*
* Holds the appropriate cipher state based on the derived key length:
* empty key uses Static, length 1-300 uses Map, greater than 300 uses RC4.
*/
typedef struct {
cipher_kind_t kind;
union {
void *_unused; /* Static cipher has no state */
void *map;
void *rc4;
} data;
} cipher_type_t;
/**
* @brief Decrypts MFLAC data using the specified ekey.
*
* Derives the cipher key, selects the appropriate cipher, decrypts the
* data, and removes trailing non-audio bytes. The caller is responsible
* for freeing @p *out.
*
* @param data The encrypted MFLAC file data.
* @param data_len The length of @p data.
* @param ekey The base64-encoded ekey string (null-terminated).
* @param out Receives the allocated decrypted FLAC bytes.
* @param out_len Receives the decrypted length (data_len or less after
* trailing trim).
* @return 0 on success, or -1 if key derivation or decryption fails.
*/
int decrypt_mflac(const uint8_t *data, size_t data_len, const char *ekey,
uint8_t **out, size_t *out_len);
/**
* @brief Removes trailing bytes that are not part of a valid FLAC audio
* frame.
*
* Scans for FLAC frame sync markers (0xFF + byte with bits 7-2 set to
* 0x3E) and discards data from the last sync marker with fewer than
* 1000 bytes of trailing data. Operates in-place on @p *data by
* reducing @p *data_len.
*
* @param data Pointer to the FLAC buffer.
* @param data_len Pointer to the buffer length; reduced on truncation.
* @return 0 on success.
*/
int trim_flac_trailing(uint8_t **data, size_t *data_len);
#endif /* QMC_DECRYPTOR_H */