#include <errno.h>
#include <glib.h>
+#include <algorithm>
#include <cstring>
+#include <utility>
#include "include/bundle.h"
static const int CHECKSUM_LENGTH = 32;
Bundle::Bundle(unsigned char* raw, int size, bool base64) {
+ if (size < 0)
+ THROW(BUNDLE_ERROR_INVALID_PARAMETER);
+
int ret;
if (base64)
- ret = Decode(raw, size);
+ ret = Decode(raw, static_cast<size_t>(size));
else
- ret = DecodeRaw(raw, size);
+ ret = DecodeRaw(raw, static_cast<size_t>(size));
if (ret != BUNDLE_ERROR_NONE)
THROW(ret);
}
return reinterpret_cast<unsigned char*>(encoded_data);
}
-int Bundle::Decode(unsigned char* raw, int size) {
+int Bundle::Decode(unsigned char* raw, size_t size) {
unsigned char* d_str = new (std::nothrow) unsigned char[(size / 4) * 3 + 3];
if (d_str == nullptr)
return BUNDLE_ERROR_OUT_OF_MEMORY;
std::unique_ptr<unsigned char[]> d_ptr(d_str);
gint state = 0;
guint save = 0;
- unsigned int d_len_raw = g_base64_decode_step(reinterpret_cast<char*>(raw),
- size, d_str, &state, &save);
+ size_t d_len_raw = static_cast<size_t>(g_base64_decode_step(
+ reinterpret_cast<char*>(raw), size, d_str, &state, &save));
if (d_len_raw < CHECKSUM_LENGTH)
return BUNDLE_ERROR_OUT_OF_MEMORY;
return raw;
}
-int Bundle::DecodeRaw(unsigned char* raw, int size) {
+int Bundle::DecodeRaw(unsigned char* raw, size_t size) {
char* extract_checksum = new (std::nothrow) char[CHECKSUM_LENGTH + 1];
if (extract_checksum == nullptr)
return BUNDLE_ERROR_OUT_OF_MEMORY;
std::unique_ptr<char[]> extract_ptr(extract_checksum);
unsigned char* d_str = raw;
- unsigned int d_len_raw = size;
+ size_t d_len_raw = size;
strncpy(extract_checksum, reinterpret_cast<char*>(d_str), CHECKSUM_LENGTH);
extract_checksum[CHECKSUM_LENGTH] = '\0';
return BUNDLE_ERROR_INVALID_PARAMETER;
unsigned char* d_r = d_str + CHECKSUM_LENGTH;
- unsigned int d_len = d_len_raw - CHECKSUM_LENGTH;
+ size_t d_len = d_len_raw - CHECKSUM_LENGTH;
unsigned int reader = 0;
std::vector<unsigned char> bytes(d_r, d_r + d_len);
unsigned char* Encode();
unsigned char* EncodeRaw(int* size);
- int DecodeRaw(unsigned char* raw, int size);
+ int DecodeRaw(unsigned char* raw, size_t size);
const std::unordered_map<std::string, std::shared_ptr<KeyInfo>>& GetMap() const;
std::vector<std::string> Export();
private:
- int Decode(unsigned char* raw, int size);
+ int Decode(unsigned char* raw, size_t size);
int Import(int argc, char** argv);
private: