third_party/incbin: Add public domain incbin

Add the public domain incbin library used to include binary files
directly into C code. The UNLICENSE file confirms the code can be
treated as unencumbered.

BUG=none
BRANCH=none
TEST=make buildall

Signed-off-by: Keith Short <keithshort@chromium.org>
Change-Id: I6107737cdb5a94e71e5325943b2d1d2627659f85
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2289214
Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
This commit is contained in:
Keith Short 2020-07-07 14:40:25 -06:00 committed by Commit Bot
parent cf1ced5987
commit bf7c46cfad
12 changed files with 900 additions and 0 deletions

11
builtin/limits.h Normal file
View File

@ -0,0 +1,11 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef __CROS_EC_LIMITS_H_
#define __CROS_EC_LIMITS_H_
#define ULONG_MAX 0xffffffffu
#endif /* __CROS_EC_LIMITS_H_ */

12
third_party/incbin/.travis.yml vendored Normal file
View File

@ -0,0 +1,12 @@
language: c
os:
- linux
- osx
compiler:
- gcc
- clang
script:
- make -C test test

167
third_party/incbin/README.md vendored Normal file
View File

@ -0,0 +1,167 @@
# incbin
Include binary files in your C/C++ applications with ease
## Example
``` c
#include "incbin.h"
INCBIN(Icon, "icon.png");
// This translation unit now has three symbols
// const unsigned char gIconData[];
// const unsigned char *const gIconEnd; // a marker to the end, take the address to get the ending pointer
// const unsigned int gIconSize;
// Reference in other translation units like this
INCBIN_EXTERN(Icon);
// This translation unit now has three extern symbols
// Use `extern "C"` in case of writing C++ code
// extern const unsigned char gIconData[];
// extern const unsigned char *const gIconEnd; // a marker to the end, take the address to get the ending pointer
// extern const unsigned int gIconSize;
```
## Portability
Known to work on the following compilers
* GCC
* Clang
* PathScale
* Intel
* Solaris & Sun Studio
* Green Hills
* SNC (ProDG)
* Diab C++ (WindRiver)
* XCode
* ArmCC
* RealView
* ImageCraft
* Stratus VOS C
* TinyCC
* cparser & libfirm
* LCC
* MSVC _See MSVC below_
If your compiler is not listed, as long as it supports GCC inline assembler, this
should work.
## MISRA
INCBIN can be used in MISRA C setting. However it should be independently checked
due to its use of inline assembly to achieve what it does. Independent verification
of the header has been done several times based on commit: 7e327a28ba5467c4202ec37874beca7084e4b08c
## Alignment
The data included by this tool will be aligned on the architectures word boundary
unless some variant of SIMD is detected, then it's aligned on a byte boundary that
respects SIMD convention just incase your binary data may be used in vectorized
code. The table of the alignments for SIMD this header recognizes is as follows:
| SIMD | Alignment |
|-----------------------------------------|-----------|
| SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2 | 16 |
| Neon | 16 |
| AVX, AVX2 | 32 |
| AVX512 | 64 |
## Prefix
By default, `incbin.h` emits symbols with a `g` prefix. This can be adjusted by
defining `INCBIN_PREFIX` before including `incbin.h` with a desired prefix. For
instance
``` c
#define INCBIN_PREFIX g_
#include "incbin.h"
INCBIN(test, "test.txt");
// This translation unit now has three symbols
// const unsigned char g_testData[];
// const unsigned char *const g_testEnd;
// const unsigned int g_testSize;
```
You can also choose to have no prefix by defining the prefix with nothing, for example:
``` c
#define INCBIN_PREFIX
#include "incbin.h"
INCBIN(test, "test.txt");
// This translation unit now has three symbols
// const unsigned char testData[];
// const unsigned char *const testEnd;
// const unsigned int testSize;
```
## Style
By default, `incbin.h` emits symbols with `CamelCase` style. This can be adjusted
by defining `INCBIN_STYLE` before including `incbin.h` to change the style. There
are two possible styles to choose from
* INCBIN_STYLE_CAMEL (CamelCase)
* INCBIN_STYLE_SNAKE (snake_case)
For instance:
``` c
#define INCBIN_STYLE INCBIN_STYLE_SNAKE
#include "incbin.h"
INCBIN(test, "test.txt");
// This translation unit now has three symbols
// const unsigned char gtest_data[];
// const unsigned char *const gtest_end;
// const unsigned int gtest_size;
```
Combining both the style and prefix allows for you to adjust `incbin.h` to suite
your existing style and practices.
## Overriding Linker Output section
By default, `incbin.h` emits into the read-only linker output section used on
the detected platform. If you need to override this for whatever reason, you
can manually specify the linker output section.
For example, to emit data into program memory for
[esp8266/Arduino](github.com/esp8266/Arduino):
``` c
#define INCBIN_OUTPUT_SECTION ".irom.text"
#include "incbin.h"
INCBIN(Foo, "foo.txt");
// Data is emitted into program memory that never gets copied to RAM
```
## Explanation
`INCBIN` is a macro which uses the inline assembler provided by almost all
compilers to include binary files. It achieves this by utilizing the `.incbin`
directive of the inline assembler. It then uses the assembler to calculate the
size of the included binary and exports two global symbols that can be externally
referenced in other translation units which contain the data and size of the
included binary data respectively.
## MSVC
Supporting MSVC is slightly harder as MSVC lacks an inline assembler which can
include data. To support this we ship a tool which can process source files
containing `INCBIN` macro usage and generate an external source file containing
the data of all of them combined. This file is named `data.c` by default.
Just include it into your build and use the `incbin.h` to reference data as
needed. It's suggested you integrate this tool as part of your projects's
pre-build events so that this can be automated. A more comprehensive list of
options for this tool can be viewed by invoking the tool with `-help`
If you're using a custom prefix, be sure to specify the prefix on the command
line with `-p <prefix>` so that everything matches up; similarly, if you're
using a custom style, be sure to specify the style on the command line with
`-S <style>` as well.
## Miscellaneous
Documentation for the API is provided by the header using Doxygen notation.
For licensing information see UNLICENSE.

24
third_party/incbin/UNLICENSE vendored Normal file
View File

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

289
third_party/incbin/incbin.c vendored Normal file
View File

@ -0,0 +1,289 @@
#ifdef _MSC_VER
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#ifndef PATH_MAX
# define PATH_MAX 260
#endif
#define SEARCH_PATHS_MAX 64
#define FILE_PATHS_MAX 1024
static int fline(char **line, size_t *n, FILE *fp) {
int chr;
char *pos;
if (!line || !n || !fp)
return -1;
if (!*line)
if (!(*line = (char *)malloc((*n=64))))
return -1;
chr = *n;
pos = *line;
for (;;) {
int c = fgetc(fp);
if (chr < 2) {
*n += (*n > 16) ? *n : 64;
chr = *n + *line - pos;
if (!(*line = (char *)realloc(*line,*n)))
return -1;
pos = *n - chr + *line;
}
if (ferror(fp))
return -1;
if (c == EOF) {
if (pos == *line)
return -1;
else
break;
}
*pos++ = c;
chr--;
if (c == '\n')
break;
}
*pos = '\0';
return pos - *line;
}
static FILE *open_file(const char *name, const char *mode, const char (*searches)[PATH_MAX], int count) {
int i;
for (i = 0; i < count; i++) {
char buffer[FILENAME_MAX + PATH_MAX];
FILE *fp;
#ifndef _MSC_VER
snprintf(buffer, sizeof(buffer), "%s/%s", searches[i], name);
#else
_snprintf(buffer, sizeof(buffer), "%s/%s", searches[i], name);
#endif
if ((fp = fopen(buffer, mode)))
return fp;
}
return !count ? fopen(name, mode) : NULL;
}
static int strcicmp(const char *s1, const char *s2) {
const unsigned char *us1 = (const unsigned char *)s1,
*us2 = (const unsigned char *)s2;
while (tolower(*us1) == tolower(*us2)) {
if (*us1++ == '\0')
return 0;
us2++;
}
return tolower(*us1) - tolower(*us2);
}
/* styles */
enum { kCamel, kSnake };
/* identifiers */
enum { kData, kEnd, kSize };
static const char *styled(int style, int ident) {
switch (style) {
case kCamel:
switch (ident) {
case kData: return "Data";
case kEnd: return "End";
case kSize: return "Size";
}
break;
case kSnake:
switch (ident) {
case kData: return "_data";
case kEnd: return "_end";
case kSize: return "_size";
}
break;
}
}
int main(int argc, char **argv) {
int ret, i, paths, files = 0, style = kCamel;
char outfile[FILENAME_MAX] = "data.c";
char search_paths[SEARCH_PATHS_MAX][PATH_MAX];
char prefix[FILENAME_MAX] = "g";
char file_paths[FILE_PATHS_MAX][PATH_MAX];
FILE *out = NULL;
argc--;
argv++;
#define s(IDENT) styled(style, IDENT)
if (argc == 0) {
usage:
fprintf(stderr, "%s [-help] [-Ipath...] | <files> | [-o output] | [-p prefix]\n", argv[-1]);
fprintf(stderr, " -o - output file [default is \"data.c\"]\n");
fprintf(stderr, " -p - specify a prefix for symbol names (default is \"g\")\n");
fprintf(stderr, " -S<style> - specify a style for symbol generation (default is \"camelcase\")\n");
fprintf(stderr, " -I<path> - specify an include path for the tool to use\n");
fprintf(stderr, " -help - this\n");
fprintf(stderr, "example:\n");
fprintf(stderr, " %s icon.png music.mp3 -o file.c\n", argv[-1]);
fprintf(stderr, "styles (for -S):\n");
fprintf(stderr, " camelcase\n");
fprintf(stderr, " snakecase\n");
return 1;
}
for (i = 0, paths = 0; i < argc; i++) {
if (!strcmp(argv[i], "-o")) {
if (i + 1 < argc) {
strcpy(outfile, argv[i + 1]);
memmove(argv+i+1, argv+i+2, (argc-i-2) * sizeof *argv);
argc--;
continue;
}
} else if (!strcmp(argv[i], "-p")) {
/* supports "-p" with no prefix as well as
* "-p -" which is another way of saying "no prefix"
* and "-p <prefix>" for an actual prefix.
*/
if (argv[i+1][0] == '-') {
prefix[0] = '\0';
/* is it just a -? */
if (i + 1 < argc && !strcmp(argv[i+1], "-")) {
memmove(argv+i+1, argv+i+2, (argc-i-2) * sizeof *argv);
argc--;
}
continue;
}
strcpy(prefix, argv[i + 1]);
memmove(argv+i+1, argv+i+2, (argc-i-2) * sizeof *argv);
argc--;
continue;
} else if (!strncmp(argv[i], "-I", 2)) {
char *name = argv[i] + 2; /* skip "-I"; */
if (paths >= SEARCH_PATHS_MAX) {
fprintf(stderr, "maximum search paths exceeded\n");
return 1;
}
strcpy(search_paths[paths++], name);
continue;
} else if (!strncmp(argv[i], "-S", 2)) {
char *name = argv[i] + 2; /* skip "-S"; */
if (!strcicmp(name, "camel") || !strcicmp(name, "camelcase"))
style = kCamel;
else if (!strcicmp(name, "snake") || !strcicmp(name, "snakecase"))
style = kSnake;
else
goto usage;
continue;
} else if (!strcmp(argv[i], "-help")) {
goto usage;
} else {
if (files >= sizeof file_paths / sizeof *file_paths) {
fprintf(stderr, "maximum file paths exceeded\n");
return 1;
}
strcpy(file_paths[files++], argv[i]);
}
}
if (!(out = fopen(outfile, "w"))) {
fprintf(stderr, "failed to open `%s' for output\n", outfile);
return 1;
}
fprintf(out, "/* File automatically generated by incbin */\n");
/* Be sure to define the prefix if we're not using the default */
if (strcmp(prefix, "g"))
fprintf(out, "#define INCBIN_PREFIX %s\n", prefix);
if (style != 0)
fprintf(out, "#define INCBIN_STYLE INCBIN_STYLE_SNAKE\n");
fprintf(out, "#include \"incbin.h\"\n\n");
fprintf(out, "#ifdef __cplusplus\n");
fprintf(out, "extern \"C\" {\n");
fprintf(out, "#endif\n\n");
for (i = 0; i < files; i++) {
FILE *fp = open_file(file_paths[i], "r", search_paths, paths);
char *line = NULL;
size_t size = 0;
if (!fp) {
fprintf(stderr, "failed to open `%s' for reading\n", file_paths[i]);
fclose(out);
return 1;
}
while (fline(&line, &size, fp) != -1) {
char *inc, *beg, *sep, *end, *name, *file;
FILE *f;
if (!(inc = strstr(line, "INCBIN"))) continue;
if (!(beg = strchr(inc, '('))) continue;
if (!(sep = strchr(beg, ','))) continue;
if (!(end = strchr(sep, ')'))) continue;
name = beg + 1;
file = sep + 1;
while (isspace(*name)) name++;
while (isspace(*file)) file++;
sep--;
while (isspace(*sep)) sep--;
*++sep = '\0';
end--;
while (isspace(*end)) end--;
*++end = '\0';
fprintf(out, "/* INCBIN(%s, %s); */\n", name, file);
fprintf(out, "INCBIN_CONST INCBIN_ALIGN unsigned char %s%s%s[] = {\n ", prefix, name, s(kData));
*--end = '\0';
file++;
if (!(f = open_file(file, "rb", search_paths, paths))) {
fprintf(stderr, "failed to include data `%s'\n", file);
goto end;
} else {
long size, i;
unsigned char *data, count = 0;
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
if (!(data = (unsigned char *)malloc(size))) {
fprintf(stderr, "out of memory\n");
fclose(f);
ret = 1;
goto end;
}
if (fread(data, size, 1, f) != 1) {
fprintf(stderr, "failed reading include data `%s'\n", file);
free(data);
fclose(f);
ret = 1;
goto end;
}
for (i = 0; i < size; i++) {
if (count == 12) {
fprintf(out, "\n ");
count = 0;
}
fprintf(out, i != size - 1 ? "0x%02X, " : "0x%02X", data[i]);
count++;
}
free(data);
fclose(f);
}
fprintf(out, "\n};\n");
fprintf(out, "INCBIN_CONST INCBIN_ALIGN unsigned char *const %s%s%s = %s%s%s + sizeof(%s%s%s);\n", prefix, name, s(kEnd), prefix, name, s(kData), prefix, name, s(kData));
fprintf(out, "INCBIN_CONST unsigned int %s%s%s = sizeof(%s%s%s);\n", prefix, name, s(kSize), prefix, name, s(kData));
}
end:
free(line);
fclose(fp);
printf("included `%s'\n", file_paths[i]);
}
if (ret == 0) {
fprintf(out, "\n#ifdef __cplusplus\n");
fprintf(out, "}\n");
fprintf(out, "#endif\n");
fclose(out);
printf("generated `%s'\n", outfile);
return 0;
}
fclose(out);
remove(outfile);
return 1;
}

368
third_party/incbin/incbin.h vendored Normal file
View File

@ -0,0 +1,368 @@
/**
* @file incbin.h
* @author Dale Weiler
* @brief Utility for including binary files
*
* Facilities for including binary files into the current translation unit and
* making use from them externally in other translation units.
*/
#ifndef INCBIN_HDR
#define INCBIN_HDR
#include <limits.h>
#if defined(__AVX512BW__) || \
defined(__AVX512CD__) || \
defined(__AVX512DQ__) || \
defined(__AVX512ER__) || \
defined(__AVX512PF__) || \
defined(__AVX512VL__) || \
defined(__AVX512F__)
# define INCBIN_ALIGNMENT_INDEX 6
#elif defined(__AVX__) || \
defined(__AVX2__)
# define INCBIN_ALIGNMENT_INDEX 5
#elif defined(__SSE__) || \
defined(__SSE2__) || \
defined(__SSE3__) || \
defined(__SSSE3__) || \
defined(__SSE4_1__) || \
defined(__SSE4_2__) || \
defined(__neon__)
# define INCBIN_ALIGNMENT_INDEX 4
#elif ULONG_MAX != 0xffffffffu
# define INCBIN_ALIGNMENT_INDEX 3
# else
# define INCBIN_ALIGNMENT_INDEX 2
#endif
/* Lookup table of (1 << n) where `n' is `INCBIN_ALIGNMENT_INDEX' */
#define INCBIN_ALIGN_SHIFT_0 1
#define INCBIN_ALIGN_SHIFT_1 2
#define INCBIN_ALIGN_SHIFT_2 4
#define INCBIN_ALIGN_SHIFT_3 8
#define INCBIN_ALIGN_SHIFT_4 16
#define INCBIN_ALIGN_SHIFT_5 32
#define INCBIN_ALIGN_SHIFT_6 64
/* Actual alignment value */
#define INCBIN_ALIGNMENT \
INCBIN_CONCATENATE( \
INCBIN_CONCATENATE(INCBIN_ALIGN_SHIFT, _), \
INCBIN_ALIGNMENT_INDEX)
/* Stringize */
#define INCBIN_STR(X) \
#X
#define INCBIN_STRINGIZE(X) \
INCBIN_STR(X)
/* Concatenate */
#define INCBIN_CAT(X, Y) \
X ## Y
#define INCBIN_CONCATENATE(X, Y) \
INCBIN_CAT(X, Y)
/* Deferred macro expansion */
#define INCBIN_EVAL(X) \
X
#define INCBIN_INVOKE(N, ...) \
INCBIN_EVAL(N(__VA_ARGS__))
/* Green Hills uses a different directive for including binary data */
#if defined(__ghs__)
# if (__ghs_asm == 2)
# define INCBIN_MACRO ".file"
/* Or consider the ".myrawdata" entry in the ld file */
# else
# define INCBIN_MACRO "\tINCBIN"
# endif
#else
# define INCBIN_MACRO ".incbin"
#endif
#ifndef _MSC_VER
# define INCBIN_ALIGN \
__attribute__((aligned(INCBIN_ALIGNMENT)))
#else
# define INCBIN_ALIGN __declspec(align(INCBIN_ALIGNMENT))
#endif
#if defined(__arm__) || /* GNU C and RealView */ \
defined(__arm) || /* Diab */ \
defined(_ARM) /* ImageCraft */
# define INCBIN_ARM
#endif
#ifdef __GNUC__
/* Utilize .balign where supported */
# define INCBIN_ALIGN_HOST ".balign " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
# define INCBIN_ALIGN_BYTE ".balign 1\n"
#elif defined(INCBIN_ARM)
/*
* On arm assemblers, the alignment value is calculated as (1 << n) where `n' is
* the shift count. This is the value passed to `.align'
*/
# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT_INDEX) "\n"
# define INCBIN_ALIGN_BYTE ".align 0\n"
#else
/* We assume other inline assembler's treat `.align' as `.balign' */
# define INCBIN_ALIGN_HOST ".align " INCBIN_STRINGIZE(INCBIN_ALIGNMENT) "\n"
# define INCBIN_ALIGN_BYTE ".align 1\n"
#endif
/* INCBIN_CONST is used by incbin.c generated files */
#if defined(__cplusplus)
# define INCBIN_EXTERNAL extern "C"
# define INCBIN_CONST extern const
#else
# define INCBIN_EXTERNAL extern
# define INCBIN_CONST const
#endif
/**
* @brief Optionally override the linker section into which data is emitted.
*
* @warning If you use this facility, you'll have to deal with platform-specific linker output
* section naming on your own
*
* Overriding the default linker output section, e.g for esp8266/Arduino:
* @code
* #define INCBIN_OUTPUT_SECTION ".irom.text"
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
* // Data is emitted into program memory that never gets copied to RAM
* @endcode
*/
#if !defined(INCBIN_OUTPUT_SECTION)
# if defined(__APPLE__)
# define INCBIN_OUTPUT_SECTION ".const_data"
# else
# define INCBIN_OUTPUT_SECTION ".rodata"
# endif
#endif
#if defined(__APPLE__)
/* The directives are different for Apple branded compilers */
# define INCBIN_SECTION INCBIN_OUTPUT_SECTION "\n"
# define INCBIN_GLOBAL(NAME) ".globl " INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
# define INCBIN_INT ".long "
# define INCBIN_MANGLE "_"
# define INCBIN_BYTE ".byte "
# define INCBIN_TYPE(...)
#else
# define INCBIN_SECTION ".section " INCBIN_OUTPUT_SECTION "\n"
# define INCBIN_GLOBAL(NAME) ".global " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME "\n"
# if defined(__ghs__)
# define INCBIN_INT ".word "
# else
# define INCBIN_INT ".int "
# endif
# if defined(__USER_LABEL_PREFIX__)
# define INCBIN_MANGLE INCBIN_STRINGIZE(__USER_LABEL_PREFIX__)
# else
# define INCBIN_MANGLE ""
# endif
# if defined(INCBIN_ARM)
/* On arm assemblers, `@' is used as a line comment token */
# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", %object\n"
# elif defined(__MINGW32__) || defined(__MINGW64__)
/* Mingw doesn't support this directive either */
# define INCBIN_TYPE(NAME)
# else
/* It's safe to use `@' on other architectures */
# define INCBIN_TYPE(NAME) ".type " INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME ", @object\n"
# endif
# define INCBIN_BYTE ".byte "
#endif
/* List of style types used for symbol names */
#define INCBIN_STYLE_CAMEL 0
#define INCBIN_STYLE_SNAKE 1
/**
* @brief Specify the prefix to use for symbol names.
*
* By default this is `g', producing symbols of the form:
* @code
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
*
* // Now you have the following symbols:
* // const unsigned char gFooData[];
* // const unsigned char *const gFooEnd;
* // const unsigned int gFooSize;
* @endcode
*
* If however you specify a prefix before including: e.g:
* @code
* #define INCBIN_PREFIX incbin
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
*
* // Now you have the following symbols instead:
* // const unsigned char incbinFooData[];
* // const unsigned char *const incbinFooEnd;
* // const unsigned int incbinFooSize;
* @endcode
*/
#if !defined(INCBIN_PREFIX)
# define INCBIN_PREFIX g
#endif
/**
* @brief Specify the style used for symbol names.
*
* Possible options are
* - INCBIN_STYLE_CAMEL "CamelCase"
* - INCBIN_STYLE_SNAKE "snake_case"
*
* Default option is *INCBIN_STYLE_CAMEL* producing symbols of the form:
* @code
* #include "incbin.h"
* INCBIN(Foo, "foo.txt");
*
* // Now you have the following symbols:
* // const unsigned char <prefix>FooData[];
* // const unsigned char *const <prefix>FooEnd;
* // const unsigned int <prefix>FooSize;
* @endcode
*
* If however you specify a style before including: e.g:
* @code
* #define INCBIN_STYLE INCBIN_STYLE_SNAKE
* #include "incbin.h"
* INCBIN(foo, "foo.txt");
*
* // Now you have the following symbols:
* // const unsigned char <prefix>foo_data[];
* // const unsigned char *const <prefix>foo_end;
* // const unsigned int <prefix>foo_size;
* @endcode
*/
#if !defined(INCBIN_STYLE)
# define INCBIN_STYLE INCBIN_STYLE_CAMEL
#endif
/* Style lookup tables */
#define INCBIN_STYLE_0_DATA Data
#define INCBIN_STYLE_0_END End
#define INCBIN_STYLE_0_SIZE Size
#define INCBIN_STYLE_1_DATA _data
#define INCBIN_STYLE_1_END _end
#define INCBIN_STYLE_1_SIZE _size
/* Style lookup: returning identifier */
#define INCBIN_STYLE_IDENT(TYPE) \
INCBIN_CONCATENATE( \
INCBIN_STYLE_, \
INCBIN_CONCATENATE( \
INCBIN_EVAL(INCBIN_STYLE), \
INCBIN_CONCATENATE(_, TYPE)))
/* Style lookup: returning string literal */
#define INCBIN_STYLE_STRING(TYPE) \
INCBIN_STRINGIZE( \
INCBIN_STYLE_IDENT(TYPE)) \
/* Generate the global labels by indirectly invoking the macro with our style
* type and concatenating the name against them. */
#define INCBIN_GLOBAL_LABELS(NAME, TYPE) \
INCBIN_INVOKE( \
INCBIN_GLOBAL, \
INCBIN_CONCATENATE( \
NAME, \
INCBIN_INVOKE( \
INCBIN_STYLE_IDENT, \
TYPE))) \
INCBIN_INVOKE( \
INCBIN_TYPE, \
INCBIN_CONCATENATE( \
NAME, \
INCBIN_INVOKE( \
INCBIN_STYLE_IDENT, \
TYPE)))
/**
* @brief Externally reference binary data included in another translation unit.
*
* Produces three external symbols that reference the binary data included in
* another translation unit.
*
* The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
* "Data", as well as "End" and "Size" after. An example is provided below.
*
* @param NAME The name given for the binary data
*
* @code
* INCBIN_EXTERN(Foo);
*
* // Now you have the following symbols:
* // extern const unsigned char <prefix>FooData[];
* // extern const unsigned char *const <prefix>FooEnd;
* // extern const unsigned int <prefix>FooSize;
* @endcode
*/
#define INCBIN_EXTERN(NAME) \
INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char \
INCBIN_CONCATENATE( \
INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
INCBIN_STYLE_IDENT(DATA))[]; \
INCBIN_EXTERNAL const INCBIN_ALIGN unsigned char *const \
INCBIN_CONCATENATE( \
INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
INCBIN_STYLE_IDENT(END)); \
INCBIN_EXTERNAL const unsigned int \
INCBIN_CONCATENATE( \
INCBIN_CONCATENATE(INCBIN_PREFIX, NAME), \
INCBIN_STYLE_IDENT(SIZE))
/**
* @brief Include a binary file into the current translation unit.
*
* Includes a binary file into the current translation unit, producing three symbols
* for objects that encode the data and size respectively.
*
* The symbol names are a concatenation of `INCBIN_PREFIX' before *NAME*; with
* "Data", as well as "End" and "Size" after. An example is provided below.
*
* @param NAME The name to associate with this binary data (as an identifier.)
* @param FILENAME The file to include (as a string literal.)
*
* @code
* INCBIN(Icon, "icon.png");
*
* // Now you have the following symbols:
* // const unsigned char <prefix>IconData[];
* // const unsigned char *const <prefix>IconEnd;
* // const unsigned int <prefix>IconSize;
* @endcode
*
* @warning This must be used in global scope
* @warning The identifiers may be different if INCBIN_STYLE is not default
*
* To externally reference the data included by this in another translation unit
* please @see INCBIN_EXTERN.
*/
#ifdef _MSC_VER
#define INCBIN(NAME, FILENAME) \
INCBIN_EXTERN(NAME)
#else
#define INCBIN(NAME, FILENAME) \
__asm__(INCBIN_SECTION \
INCBIN_GLOBAL_LABELS(NAME, DATA) \
INCBIN_ALIGN_HOST \
INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) ":\n" \
INCBIN_MACRO " \"" FILENAME "\"\n" \
INCBIN_GLOBAL_LABELS(NAME, END) \
INCBIN_ALIGN_BYTE \
INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) ":\n" \
INCBIN_BYTE "1\n" \
INCBIN_GLOBAL_LABELS(NAME, SIZE) \
INCBIN_ALIGN_HOST \
INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(SIZE) ":\n" \
INCBIN_INT INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(END) " - " \
INCBIN_MANGLE INCBIN_STRINGIZE(INCBIN_PREFIX) #NAME INCBIN_STYLE_STRING(DATA) "\n" \
INCBIN_ALIGN_HOST \
".text\n" \
); \
INCBIN_EXTERN(NAME)
#endif
#endif

1
third_party/incbin/test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/asserts

5
third_party/incbin/test/Makefile vendored Normal file
View File

@ -0,0 +1,5 @@
test: asserts
./asserts
asserts: asserts.c ../incbin.h
$(CC) -o asserts asserts.c

20
third_party/incbin/test/asserts.c vendored Normal file
View File

@ -0,0 +1,20 @@
#include <assert.h>
#include <stdlib.h>
#include "../incbin.h"
INCBIN(Lorem, "loremipsum.txt");
INCBIN(Onebyte, "onebyte.txt");
INCBIN(Sevenbytes, "sevenbytes.txt");
int main(int argc, char **argv)
{
assert(gLoremSize==962);
assert(&gLoremData[gLoremSize] == (const unsigned char*) &gLoremEnd);
assert(gOnebyteSize == 1);
assert(&gOnebyteData[gOnebyteSize] == (const unsigned char*) &gOnebyteEnd);
assert(gSevenbytesSize==7);
assert(&gSevenbytesData[gSevenbytesSize] == (const unsigned char*) &gSevenbytesEnd);
exit(0);
}

View File

@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.

1
third_party/incbin/test/onebyte.txt vendored Normal file
View File

@ -0,0 +1 @@
X

View File

@ -0,0 +1 @@
XXXXXXX