size32_t ThorCompress(const void * src, size32_t srcSz, MemoryBuffer & dest, size32_t threshold) { size32_t prev = dest.length(); size32_t dSz = srcSz + sizeof(size32_t); void * d = dest.reserve(dSz); size32_t ret = ThorCompress(src, srcSz, d, dSz, threshold); dest.setLength(prev+ret); return ret; }
void LZMACompressToBuffer(MemoryBuffer & out, size32_t len, const void * src) { CLZMA lzma; size32_t outbase = out.length(); size32_t *sz = (size32_t *)out.reserve(len+sizeof(size32_t)*2); *sz = len; sz++; *sz = lzma.compress(src,len,sz+1); if (*sz>len) { *sz = len; memcpy(sz+1,src,len); } else out.setLength(outbase+sizeof(size32_t)*2+*sz); }
size32_t aesDecrypt(MemoryBuffer &out, size32_t inSz, const void *inBytes, size32_t keyLen, const char *key, const char *iv) { if (0 == inSz) return 0; OwnedEVPCipherCtx ctx(EVP_CIPHER_CTX_new()); if (!ctx) throw makeEVPException(0, "Failed EVP_CIPHER_CTX_new"); const size32_t cipherBlockSz = 128; // from man page - "should have sufficient room for (inl + cipher_block_size) bytes unless the cipher block size is 1 in which case inl bytes is sufficient" size32_t outMaxSz = (cipherBlockSz==1) ? inSz : (inSz + cipherBlockSz/8); size32_t startSz = out.length(); byte *outPtr = (byte *)out.reserveTruncate(outMaxSz); /* Initialise the decryption operation. IMPORTANT - ensure you use a key * and IV size appropriate for your cipher * In this example we are using 256 bit AES (i.e. a 256 bit key). The * IV size for *most* modes is the same as the block size. For AES this * is 128 bits * */ if (!iv) iv = staticAesIV; if (1 != EVP_DecryptInit_ex(ctx, getAesCipher(keyLen), nullptr, (const unsigned char *)key, (const unsigned char *)iv)) throw makeEVPException(0, "Failed EVP_DecryptInit_ex"); /* Provide the message to be decrypted, and obtain the plaintext output. * EVP_DecryptUpdate can be called multiple times if necessary */ int outSz; if (1 != EVP_DecryptUpdate(ctx, outPtr, &outSz, (const unsigned char *)inBytes, inSz)) throw makeEVPException(0, "Failed EVP_DecryptUpdate"); int plaintext_len = outSz; /* Finalise the decryption. Further plaintext bytes may be written at * this stage. */ if (1 != EVP_DecryptFinal_ex(ctx, outPtr + outSz, &outSz)) throw makeEVPException(0, "Failed EVP_DecryptFinal_ex"); plaintext_len += outSz; out.setLength(startSz+plaintext_len); // truncate length of 'out' to final size return (size32_t)plaintext_len; }
size32_t gatherData(MemoryBuffer &mb) { CriticalBlock b(crit); if (progressEnabled) { MemoryBuffer progressData; { CriticalBlock b(crit); ForEachItemIn(g, activeGraphs) // NB: 1 for each slavesPerProcess { CGraphBase &graph = activeGraphs.item(g); progressData.append((unsigned)graph.queryJobChannel().queryMyRank()-1); if (!graph.serializeStats(progressData)) progressData.setLength(progressData.length()-sizeof(unsigned)); } } size32_t sz = progressData.length(); if (sz) { ThorCompress(progressData, mb, 0x200); return sz; } }