advisory + bonus chapter

This commit is contained in:
dzonerzy 2023-10-21 19:48:18 +02:00
parent f0178961d5
commit 8076777680
5 changed files with 152 additions and 95 deletions

View File

@ -0,0 +1 @@
yet to be assigned

BIN
res/got-root.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

View File

@ -32,21 +32,24 @@ libdzonerzy:
@mkdir -p $(DIST) @mkdir -p $(DIST)
@mkdir -p $(RAW_DIST) @mkdir -p $(RAW_DIST)
@mkdir -p $(SITE_DIST) @mkdir -p $(SITE_DIST)
$(foreach file, $(wildcard res/*.jpg res/*.gif res/*.png), $(CONVERT_TOOL) $(file) $(CONVERT_ARGS) $(file).webp;) @$(foreach file, $(wildcard res/*.jpg res/*.gif res/*.png), $(CONVERT_TOOL) $(file) $(CONVERT_ARGS) $(file).webp;)
@$(foreach file, $(wildcard res/*), $(EMBED_TOOL) -i $(file).webp -o $(SRC_INC_DIR)/$(notdir $(file).webp).h $(EMBED_ARGS);) @$(foreach file, $(wildcard res/*.jpg res/*.gif res/*.png), $(EMBED_TOOL) -i $(file).webp -o $(SRC_INC_DIR)/$(notdir $(file).webp).h $(EMBED_ARGS);)
@$(foreach file, $(wildcard res/advisory/*.txt), $(EMBED_TOOL) -i $(file) -t=advisory -o $(SRC_INC_DIR)/$(notdir $(file)).h $(EMBED_ARGS);)
@rm -f res/*.webp @rm -f res/*.webp
@ls $(SRC_INC_DIR)/*.h | xargs -n1 basename | sed 's/^/#include </' | sed 's/$$/>/' >> $(SRC_DIR)/res.h @ls $(SRC_INC_DIR)/*.h | xargs -n1 basename | sed 's/^/#include </' | sed 's/$$/>/' >> $(SRC_DIR)/res.h
$(CC) $(CFLAGS) $(LDFLAGS) -o $(DIST)/$(OUTPUT) $(SRC_DIR)/libdzonerzy.so.c $(SRC_DIR)/challenge.o $(CC) $(CFLAGS) $(LDFLAGS) -o $(DIST)/$(OUTPUT) $(SRC_DIR)/libdzonerzy.so.c $(SRC_DIR)/challenge.o
libdzonerzy-dump: libdzonerzy-dump:
@$(READELF) -S -W $(DIST)/$(OUTPUT) | $(AWK_UTIL) | xargs dirname {} | sort | uniq | xargs -I{} mkdir -p $(RAW_DIST)/{} $(READELF) -S -W $(DIST)/$(OUTPUT) | $(AWK_UTIL) | xargs dirname {} | sort | uniq | xargs -I{} mkdir -p $(RAW_DIST)/{}
@$(foreach fn, $(shell $(READELF) -S -W $(DIST)/$(OUTPUT) | $(AWK_UTIL_NOCRLF)), $(OBJCOPY) $(DIST)/$(OUTPUT) --dump-section $(fn)=/dev/stdout | cat | head -c -1 > $(RAW_DIST)/$(fn);) @$(foreach fn, $(shell $(READELF) -S -W $(DIST)/$(OUTPUT) | $(AWK_UTIL_NOCRLF)), $(OBJCOPY) $(DIST)/$(OUTPUT) --dump-section $(fn)=/dev/stdout | cat | head -c -1 > $(RAW_DIST)/$(fn);)
libdzonerzy-gen: libdzonerzy-dump libdzonerzy-gen: libdzonerzy-dump
@mkdir -p $(SITE_DIST)/assets/ @mkdir -p $(SITE_DIST)/assets/
@mkdir -p $(SITE_DIST)/articles/ @mkdir -p $(SITE_DIST)/articles/
@mkdir -p $(SITE_DIST)/advisory/
@$(foreach page, $(wildcard $(RAW_DIST)/pages/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/$(notdir $(page)) | sed 's/\.md/\.html/') $(page);) @$(foreach page, $(wildcard $(RAW_DIST)/pages/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/$(notdir $(page)) | sed 's/\.md/\.html/') $(page);)
@$(foreach article, $(wildcard $(RAW_DIST)/articles/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/articles/$(notdir $(article)) | sed 's/\.md/\.html/') $(article);) @$(foreach article, $(wildcard $(RAW_DIST)/articles/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/articles/$(notdir $(article)) | sed 's/\.md/\.html/') $(article);)
@$(foreach advisory, $(wildcard $(RAW_DIST)/advisory/*), cp $(advisory) $(SITE_DIST)/advisory/$(notdir $(advisory));)
@cp $(RAW_DIST)/main.css $(SITE_DIST)/main.css @cp $(RAW_DIST)/main.css $(SITE_DIST)/main.css
@cp $(RAW_DIST)/main.js $(SITE_DIST)/main.js @cp $(RAW_DIST)/main.js $(SITE_DIST)/main.js
@cp $(RAW_DIST)/sitemap.xml $(SITE_DIST)/sitemap.xml @cp $(RAW_DIST)/sitemap.xml $(SITE_DIST)/sitemap.xml

File diff suppressed because one or more lines are too long

View File

@ -24,11 +24,24 @@ Copyright:
#define VERSION "0.0.1" #define VERSION "0.0.1"
typedef enum _format
{
FORMAT_C,
FORMAT_H,
} format_t;
typedef enum _type
{
TYPE_EMBED,
TYPE_ADVISORY,
} type_t;
typedef struct _embed typedef struct _embed
{ {
char *input; char *input;
char *output; char *output;
char *format; format_t format;
type_t type;
int verbose; int verbose;
int quiet; int quiet;
int autowrap; int autowrap;
@ -44,7 +57,7 @@ int main(int argc, char **argv)
int c = 0; int c = 0;
embed_t args = {0}; embed_t args = {0};
while ((c = getopt(argc, argv, "hvi:o:f:Vqw")) != -1) while ((c = getopt(argc, argv, "hvi:o:f:t:Vqw")) != -1)
{ {
switch (c) switch (c)
{ {
@ -72,11 +85,11 @@ int main(int argc, char **argv)
switch (optarg[0]) switch (optarg[0])
{ {
case 'c': case 'c':
args.format = "c"; args.format = FORMAT_C;
break; break;
case 'h': case 'h':
args.format = "h"; args.format = FORMAT_H;
break; break;
default: default:
usage(argv); usage(argv);
@ -84,6 +97,27 @@ int main(int argc, char **argv)
break; break;
} }
break; break;
case 't':
// check -t=embed -t=advisory handle equal sign and skip it
if (optarg[0] == '=')
{
optarg++;
}
switch (optarg[0])
{
case 'e':
args.type = TYPE_EMBED;
break;
case 'a':
args.type = TYPE_ADVISORY;
break;
default:
fprintf(stderr, "Unknown type: %s defaulting to embed\n", optarg);
args.type = TYPE_EMBED;
break;
}
break;
case 'V': case 'V':
args.verbose = 1; args.verbose = 1;
break; break;
@ -124,6 +158,8 @@ void usage(char **argv)
printf(" -i, --input=FILE Input file\n"); printf(" -i, --input=FILE Input file\n");
printf(" -o, --output=FILE Output file\n"); printf(" -o, --output=FILE Output file\n");
printf(" -f, --format=FORMAT Output format (c, h)\n"); printf(" -f, --format=FORMAT Output format (c, h)\n");
printf(" -t, --type=TYPE Output type (embed, advisory)\n");
printf(" -w, --autowrap Autowrap output\n");
printf(" -V, --verbose Verbose output\n"); printf(" -V, --verbose Verbose output\n");
printf(" -q, --quiet Quiet output\n"); printf(" -q, --quiet Quiet output\n");
printf("\n"); printf("\n");
@ -211,17 +247,13 @@ int embed(embed_t *args)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (args->format == NULL) char *formats[] = {"c", "h"};
{
fprintf(stderr, "Output format not specified\n");
return EXIT_FAILURE;
}
if (args->verbose) if (args->verbose)
{ {
printf("Input file: %s\n", args->input); printf("Input file: %s\n", args->input);
printf("Output file: %s\n", args->output); printf("Output file: %s\n", args->output);
printf("Output format: %s\n", args->format); printf("Output format: %s\n", formats[args->format]);
} }
FILE *input = fopen(args->input, "rb"); FILE *input = fopen(args->input, "rb");
@ -274,28 +306,39 @@ int embed(embed_t *args)
fprintf(output, " */\n"); fprintf(output, " */\n");
fprintf(output, "\n"); fprintf(output, "\n");
if (args->format[0] == 'c') char *typ;
switch (args->type)
{ {
case TYPE_EMBED:
typ = "embed";
break;
case TYPE_ADVISORY:
typ = "advisory";
break;
default:
typ = "unknown";
break;
}
if (args->format == FORMAT_C)
{
fprintf(output, "__attribute__((section(\"%s/%s\"))) const unsigned char %s[] = {", typ, name_only, input_name);
if (args->autowrap) if (args->autowrap)
{ {
fprintf(output, "__attribute__((section(\"embed/%s\"))) const unsigned char %s[] = {\n", name_only, input_name); fprintf(output, "\n");
}
else
{
fprintf(output, "__attribute__((section(\"embed/%s\"))) const unsigned char %s[] = {", name_only, input_name);
} }
} }
else if (args->format[0] == 'h') else if (args->format == FORMAT_H)
{ {
fprintf(output, "#pragma once\n"); fprintf(output, "#pragma once\n");
fprintf(output, "#ifndef %s_H\n", input_name); fprintf(output, "#ifndef %s_H\n", input_name);
fprintf(output, "__attribute__((section(\"%s/%s\"))) const unsigned char %s[] = {", typ, name_only, input_name);
if (args->autowrap) if (args->autowrap)
{ {
fprintf(output, "__attribute__((section(\"embed/%s\"))) const unsigned char %s[] = {\n", name_only, input_name); fprintf(output, "\n");
}
else
{
fprintf(output, "__attribute__((section(\"embed/%s\"))) const unsigned char %s[] = {", name_only, input_name);
} }
} }
@ -332,7 +375,9 @@ int embed(embed_t *args)
fprintf(output, "\n"); fprintf(output, "\n");
} }
fprintf(output, " 0x00,\n"); if (args->type == TYPE_EMBED)
fprintf(output, " 0x00,\n");
fprintf(output, "};\n"); fprintf(output, "};\n");
} }
else else
@ -342,11 +387,14 @@ int embed(embed_t *args)
fprintf(output, "0x%02X", c); fprintf(output, "0x%02X", c);
fprintf(output, ", "); fprintf(output, ", ");
} }
fprintf(output, "0x00");
if (args->type == TYPE_EMBED)
fprintf(output, "0x00");
fprintf(output, "};\n\n"); fprintf(output, "};\n\n");
} }
if (args->format[0] == 'h') if (args->format == FORMAT_H)
{ {
fprintf(output, "#endif /* %s_H */\n", input_name); fprintf(output, "#endif /* %s_H */\n", input_name);
} }