core: only alloc as much as needed when reading in cursor images (#51)

This commit is contained in:
Ikalco 2024-07-04 10:59:59 -05:00 committed by GitHub
parent 66d5b46ff9
commit a5c0d57325
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -634,18 +634,22 @@ std::optional<std::string> CHyprcursorImplementation::loadTheme() {
int errp = 0; int errp = 0;
zip_t* zip = zip_open(cursor.path().string().c_str(), ZIP_RDONLY, &errp); zip_t* zip = zip_open(cursor.path().string().c_str(), ZIP_RDONLY, &errp);
zip_file_t* meta_file = zip_fopen(zip, "meta.hl", ZIP_FL_UNCHANGED); zip_int64_t index = zip_name_locate(zip, "meta.hl", ZIP_FL_ENC_GUESS);
bool metaIsHL = true; bool metaIsHL = true;
if (!meta_file) {
meta_file = zip_fopen(zip, "meta.toml", ZIP_FL_UNCHANGED); if (index == -1) {
metaIsHL = false; index = zip_name_locate(zip, "meta.toml", ZIP_FL_ENC_GUESS);
if (!meta_file) metaIsHL = false;
return "cursor" + cursor.path().string() + "failed to load meta";
} }
char* buffer = new char[1024 * 1024]; /* 1MB should be more than enough */ if (index == -1)
return "cursor" + cursor.path().string() + "failed to load meta";
int readBytes = zip_fread(meta_file, buffer, 1024 * 1024 - 1); zip_file_t* meta_file = zip_fopen_index(zip, index, ZIP_FL_UNCHANGED);
char* buffer = new char[1024 * 1024]; /* 1MB should be more than enough */
int readBytes = zip_fread(meta_file, buffer, 1024 * 1024 - 1);
zip_fclose(meta_file); zip_fclose(meta_file);
@ -670,6 +674,9 @@ std::optional<std::string> CHyprcursorImplementation::loadTheme() {
SHAPE->overrides = meta.parsedData.overrides; SHAPE->overrides = meta.parsedData.overrides;
zip_stat_t sb;
zip_stat_init(&sb);
for (auto& i : SHAPE->images) { for (auto& i : SHAPE->images) {
if (SHAPE->shapeType == SHAPE_INVALID) { if (SHAPE->shapeType == SHAPE_INVALID) {
if (i.filename.ends_with(".svg")) if (i.filename.ends_with(".svg"))
@ -694,14 +701,23 @@ std::optional<std::string> CHyprcursorImplementation::loadTheme() {
IMAGE->delay = i.delay; IMAGE->delay = i.delay;
IMAGE->isSVG = SHAPE->shapeType == SHAPE_SVG; IMAGE->isSVG = SHAPE->shapeType == SHAPE_SVG;
// read from zip index = zip_name_locate(zip, i.filename.c_str(), ZIP_FL_ENC_GUESS);
zip_file_t* image_file = zip_fopen(zip, i.filename.c_str(), ZIP_FL_UNCHANGED); if (index == -1)
if (!image_file)
return "cursor" + cursor.path().string() + "failed to load image_file"; return "cursor" + cursor.path().string() + "failed to load image_file";
IMAGE->data = new char[1024 * 1024]; /* 1MB should be more than enough, again. This probably should be in the spec. */ // read from zip
zip_file_t* image_file = zip_fopen_index(zip, index, ZIP_FL_UNCHANGED);
zip_stat_index(zip, index, ZIP_FL_UNCHANGED, &sb);
IMAGE->dataLen = zip_fread(image_file, IMAGE->data, 1024 * 1024 - 1); if (sb.valid & ZIP_STAT_SIZE) {
IMAGE->data = new char[sb.size + 1];
IMAGE->dataLen = sb.size + 1;
} else {
IMAGE->data = new char[1024 * 1024]; /* 1MB should be more than enough, again. This probably should be in the spec. */
IMAGE->dataLen = 1024 * 1024;
}
IMAGE->dataLen = zip_fread(image_file, IMAGE->data, IMAGE->dataLen - 1);
zip_fclose(image_file); zip_fclose(image_file);