please corrupt me UwU poggy woggy

This commit is contained in:
XANTRONIX Development 2021-12-03 18:23:04 -05:00
parent aab5ce27a4
commit 771a652245
8 changed files with 128 additions and 128 deletions

View file

@ -9,7 +9,7 @@
#include <errno.h> #include <errno.h>
#include <cammy/photo.h> #include <cammy/photo.h>
#include <cammy/sram.h> #include <cammy/camera.h>
#include "pnglite.h" #include "pnglite.h"
#include "png.h" #include "png.h"
@ -38,7 +38,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
} }
int cammy_export(int argc, char **argv) { int cammy_export(int argc, char **argv) {
cammy_sram *sram; cammy_camera *camera;
int photo = 0; int photo = 0;
cammy_image *image; cammy_image *image;
@ -52,18 +52,18 @@ int cammy_export(int argc, char **argv) {
photo = atoi(argv[3]); photo = atoi(argv[3]);
if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) { if (photo < 1 || photo > CAMMY_CAMERA_PHOTO_COUNT) {
usage(argc, argv, "Invalid photo number"); usage(argc, argv, "Invalid photo number");
} }
if ((sram = cammy_sram_open(argv[2])) == NULL) { if ((camera = cammy_camera_open(argv[2])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n", fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_sram_open()", argv[2], strerror(errno)); argv[0], "cammy_camera_open()", argv[2], strerror(errno));
goto error_sram_open; goto error_camera_open;
} }
if ((image = cammy_photo_export(&sram->data->photos[photo], if ((image = cammy_photo_export(&camera->data->photos[photo],
CAMMY_IMAGE_BITMAP, CAMMY_IMAGE_BITMAP,
CAMMY_IMAGE_24BPP_RGB, CAMMY_IMAGE_24BPP_RGB,
palette)) == NULL) { palette)) == NULL) {
@ -76,7 +76,7 @@ int cammy_export(int argc, char **argv) {
cammy_image_destroy(image); cammy_image_destroy(image);
cammy_sram_close(sram); cammy_camera_close(camera);
return 0; return 0;
@ -84,8 +84,8 @@ error_image_save:
cammy_image_destroy(image); cammy_image_destroy(image);
error_photo_export: error_photo_export:
cammy_sram_close(sram); cammy_camera_close(camera);
error_sram_open: error_camera_open:
return 1; return 1;
} }

View file

@ -5,7 +5,7 @@
#include <errno.h> #include <errno.h>
#include <cammy/photo.h> #include <cammy/photo.h>
#include <cammy/sram.h> #include <cammy/camera.h>
#include "commands.h" #include "commands.h"
@ -25,7 +25,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
} }
int cammy_import(int argc, char **argv) { int cammy_import(int argc, char **argv) {
cammy_sram *sram; cammy_camera *camera;
cammy_image *image; cammy_image *image;
int photo = 0; int photo = 0;
@ -39,15 +39,15 @@ int cammy_import(int argc, char **argv) {
photo = atoi(argv[3]); photo = atoi(argv[3]);
if (photo < 1 || photo > CAMMY_SRAM_PHOTO_COUNT) { if (photo < 1 || photo > CAMMY_CAMERA_PHOTO_COUNT) {
usage(argc, argv, "Invalid photo number"); usage(argc, argv, "Invalid photo number");
} }
if ((sram = cammy_sram_open(argv[2])) == NULL) { if ((camera = cammy_camera_open(argv[2])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n", fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "cammy_sram_open()", argv[2], strerror(errno)); argv[0], "cammy_camera_open()", argv[2], strerror(errno));
goto error_sram_open; goto error_camera_open;
} }
if ((image = cammy_image_open(argv[4])) == NULL) { if ((image = cammy_image_open(argv[4])) == NULL) {
@ -62,13 +62,13 @@ int cammy_import(int argc, char **argv) {
goto error_invalid_dimensions; goto error_invalid_dimensions;
} }
if (cammy_photo_import(&sram->data->photos[photo-1], image) < 0) { if (cammy_photo_import(&camera->data->photos[photo-1], image) < 0) {
goto error_photo_import; goto error_photo_import;
} }
cammy_image_destroy(image); cammy_image_destroy(image);
cammy_sram_close(sram); cammy_camera_close(camera);
return 0; return 0;
@ -77,8 +77,8 @@ error_invalid_dimensions:
cammy_image_destroy(image); cammy_image_destroy(image);
error_image_open: error_image_open:
cammy_sram_close(sram); cammy_camera_close(camera);
error_sram_open: error_camera_open:
return 1; return 1;
} }

View file

@ -3,7 +3,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <cammy/sram.h> #include <cammy/camera.h>
#include "commands.h" #include "commands.h"
@ -41,7 +41,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
} }
void cammy_validate_photo_number(int argc, char **argv, int num) { void cammy_validate_photo_number(int argc, char **argv, int num) {
if (num < 1 || num > CAMMY_SRAM_PHOTO_COUNT) { if (num < 1 || num > CAMMY_CAMERA_PHOTO_COUNT) {
usage(argc, argv, "Invalid photo number"); usage(argc, argv, "Invalid photo number");
} }
} }

42
include/cammy/camera.h Normal file
View file

@ -0,0 +1,42 @@
#ifndef _CAMMY_CAMERA_H
#define _CAMMY_CAMERA_H
#include <stdint.h>
#include <cammy/photo.h>
#define CAMMY_CAMERA_SCRATCH_SIZE 4604
#define CAMMY_CAMERA_SCRATCH_2_SIZE 4
#define CAMMY_CAMERA_PHOTO_COUNT 30
#pragma pack(1)
#pragma pack(push)
typedef struct _cammy_camera_data {
uint8_t scratch[CAMMY_CAMERA_SCRATCH_SIZE];
cammy_tile gameface[CAMMY_PHOTO_TILES_HEIGHT]
[CAMMY_PHOTO_TILES_WIDTH];
uint8_t scratch_2[CAMMY_CAMERA_SCRATCH_2_SIZE];
cammy_photo photos[CAMMY_CAMERA_PHOTO_COUNT];
} cammy_camera_data;
#pragma pack(pop)
typedef struct _cammy_camera {
int fd;
cammy_camera_data *data;
size_t size,
page_size,
mapped_size;
} cammy_camera;
cammy_camera *cammy_camera_open(const char *file);
void cammy_camera_close(cammy_camera *sram);
#endif /* _CAMMY_CAMERA_H */

View file

@ -1,42 +0,0 @@
#ifndef _CAMMY_SRAM_H
#define _CAMMY_SRAM_H
#include <stdint.h>
#include <cammy/photo.h>
#define CAMMY_SRAM_SCRATCH_SIZE 4604
#define CAMMY_SRAM_SCRATCH_2_SIZE 4
#define CAMMY_SRAM_PHOTO_COUNT 30
#pragma pack(1)
#pragma pack(push)
typedef struct _cammy_sram_data {
uint8_t scratch[CAMMY_SRAM_SCRATCH_SIZE];
cammy_tile gameface[CAMMY_PHOTO_TILES_HEIGHT]
[CAMMY_PHOTO_TILES_WIDTH];
uint8_t scratch_2[CAMMY_SRAM_SCRATCH_2_SIZE];
cammy_photo photos[CAMMY_SRAM_PHOTO_COUNT];
} cammy_sram_data;
#pragma pack(pop)
typedef struct _cammy_sram {
int fd;
cammy_sram_data *data;
size_t size,
page_size,
mapped_size;
} cammy_sram;
cammy_sram *cammy_sram_open(const char *file);
void cammy_sram_close(cammy_sram *sram);
#endif /* _CAMMY_SRAM_H */

View file

@ -11,8 +11,8 @@ HEADERS_LOCAL =
HEADERS_BUILD = $(HEADERS_LOCAL) \ HEADERS_BUILD = $(HEADERS_LOCAL) \
$(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/,$(HEADERS)) $(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/,$(HEADERS))
HEADERS = sram.h image.h photo.h tile.h HEADERS = camera.h image.h photo.h tile.h
OBJS = sram.o image.o photo.o OBJS = camera.o image.o photo.o
VERSION_MAJOR = 0 VERSION_MAJOR = 0
VERSION_MINOR = 0.1 VERSION_MINOR = 0.1

62
src/camera.c Normal file
View file

@ -0,0 +1,62 @@
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <cammy/camera.h>
static inline size_t _mapped_size(size_t size, size_t page_size) {
return size + (page_size - (size % page_size));
}
cammy_camera *cammy_camera_open(const char *file) {
cammy_camera *camera;
struct stat st;
if ((camera = malloc(sizeof(*camera))) == NULL) {
goto error_malloc_camera;
}
if ((camera->fd = open(file, O_RDWR)) < 0) {
goto error_open;
}
if (fstat(camera->fd, &st) < 0) {
goto error_stat;
}
if (st.st_size < sizeof(cammy_camera_data)) {
errno = EINVAL;
goto error_invalid_file;
}
camera->size = st.st_size;
camera->page_size = (size_t)sysconf(_SC_PAGESIZE); camera->mapped_size = _mapped_size(st.st_size, camera->page_size);
if ((camera->data = mmap(NULL, camera->mapped_size,
PROT_READ | PROT_WRITE, MAP_SHARED, camera->fd, 0)) == NULL) {
goto error_mmap;
}
return camera;
error_mmap:
error_invalid_file:
error_stat:
close(camera->fd);
error_open:
free(camera);
error_malloc_camera:
return NULL;
}
void cammy_camera_close(cammy_camera *camera) {
munmap(camera->data, camera->mapped_size);
close(camera->fd);
free(camera);
}

View file

@ -1,62 +0,0 @@
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <cammy/sram.h>
static inline size_t _mapped_size(size_t size, size_t page_size) {
return size + (page_size - (size % page_size));
}
cammy_sram *cammy_sram_open(const char *file) {
cammy_sram *sram;
struct stat st;
if ((sram = malloc(sizeof(*sram))) == NULL) {
goto error_malloc_sram;
}
if ((sram->fd = open(file, O_RDWR)) < 0) {
goto error_open;
}
if (fstat(sram->fd, &st) < 0) {
goto error_stat;
}
if (st.st_size < sizeof(cammy_sram_data)) {
errno = EINVAL;
goto error_invalid_file;
}
sram->size = st.st_size;
sram->page_size = (size_t)sysconf(_SC_PAGESIZE); sram->mapped_size = _mapped_size(st.st_size, sram->page_size);
if ((sram->data = mmap(NULL, sram->mapped_size,
PROT_READ | PROT_WRITE, MAP_SHARED, sram->fd, 0)) == NULL) {
goto error_mmap;
}
return sram;
error_mmap:
error_invalid_file:
error_stat:
close(sram->fd);
error_open:
free(sram);
error_malloc_sram:
return NULL;
}
void cammy_sram_close(cammy_sram *sram) {
munmap(sram->data, sram->mapped_size);
close(sram->fd);
free(sram);
}