Skip to content

Commit

Permalink
Add compression version to the encoded FP
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Apr 11, 2024
1 parent ab8a125 commit 85f332b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
22 changes: 12 additions & 10 deletions src/chromaprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,19 @@ int chromaprint_decode_fingerprint(const char *encoded_fp, int encoded_size, uin
if (base64) {
encoded = Base64Decode(encoded);
}
std::vector<uint32_t> uncompressed;
int algo;
auto ok = DecompressFingerprint(encoded, uncompressed, algo);
if (!ok) {
*fp = nullptr;
*size = 0;
if (algorithm) {
*algorithm = 0;
}
return 0;
}
auto uncompressed = DecompressFingerprintV2(encoded, algo);
if (uncompressed.empty()) {
auto ok = DecompressFingerprint(encoded, uncompressed, algo);
if (!ok) {
*fp = nullptr;
*size = 0;
if (algorithm) {
*algorithm = 0;
}
return 0;
}
}
*fp = (uint32_t *) malloc(sizeof(uint32_t) * uncompressed.size());
*size = int(uncompressed.size());
if (algorithm) {
Expand Down
11 changes: 9 additions & 2 deletions src/fingerprint_compressor_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ bool CompressFingerprintV2(const std::vector<uint32_t> &hashes, int algorithm, s

compressed.resize(compressed_size + 4);

compressed[0] = algorithm;
int compression_version = 1;

compressed[0] = (compression_version & 0x3) << 6 | (algorithm & 0x3f);
compressed[1] = num_hashes >> 16;
compressed[2] = num_hashes >> 8;
compressed[3] = num_hashes;
Expand All @@ -48,7 +50,12 @@ std::vector<uint32_t> DecompressFingerprintV2(const std::string &data, int &algo
return std::vector<uint32_t>();
}

algorithm = data[0];
int compression_version = (data[0] >> 6) & 0x3;
if (compression_version != 1) {
return std::vector<uint32_t>();
}

algorithm = data[0] & 0x3f;
size_t num_hashes = (data[1] << 16) | (data[2] << 8) | data[3];

std::string packed;
Expand Down
7 changes: 6 additions & 1 deletion src/fingerprint_decompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ bool FingerprintDecompressor::Decompress(const std::string &input)
return false;
}

m_algorithm = input[0];
int compression_version = (input[0] >> 6) & 0x3;
if (compression_version != 0) {
return false;
}

m_algorithm = input[0] & 0x3f;

const size_t num_values =
((size_t)((unsigned char)(input[1])) << 16) |
Expand Down

0 comments on commit 85f332b

Please sign in to comment.