+static bool verify_png_image(const char *image_path) {
+ if (!image_path) {
+ return false;
+ }
+
+ /* Check file exists and has correct PNG header */
+ FILE *png_file = fopen(image_path, "r");
+ if (png_file == NULL) {
+ fprintf(stderr, "Image file path \"%s\" cannot be opened: %s\n", image_path, strerror(errno));
+ return false;
+ }
+ unsigned char png_header[8];
+ memset(png_header, '\0', sizeof(png_header));
+ int bytes_read = fread(png_header, 1, sizeof(png_header), png_file);
+ fclose(png_file);
+ if (bytes_read != sizeof(png_header)) {
+ fprintf(stderr, "Could not read PNG header from \"%s\"\n", image_path);
+ return false;
+ }
+
+ // Check PNG header according to the specification, available at:
+ // https://www.w3.org/TR/2003/REC-PNG-20031110/#5PNG-file-signature
+ static unsigned char PNG_REFERENCE_HEADER[8] = {137, 80, 78, 71, 13, 10, 26, 10};
+ if (memcmp(PNG_REFERENCE_HEADER, png_header, sizeof(png_header)) != 0) {
+ fprintf(stderr, "File \"%s\" does not start with a PNG header. i3lock currently only supports loading PNG files.\n", image_path);
+ return false;
+ }
+ return true;
+}
+
+#ifndef __OpenBSD__