Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements #1

Merged
merged 48 commits into from Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
459b257
Edward Rudd's version
turol Apr 28, 2015
2041278
Add simple ddsinfo program
turol Apr 28, 2015
16492e0
Add .gitignore
turol Apr 29, 2015
39aee29
Merge branch 'urkle'
turol Apr 29, 2015
4675978
ddsinfo now shows all miplevel sizes
turol Apr 29, 2015
25055ce
Aaron Melcher's version
turol Apr 29, 2015
ada4059
Add glddstest example program
turol Apr 29, 2015
b3f9b46
Show GL errors in standard out
turol Apr 29, 2015
17ace64
More informative error messages
turol Apr 29, 2015
68c8cba
Fix non-POW2 compressed formats
turol Apr 30, 2015
a9cd79d
Merge branch 'aaronmelcher' into temp2
turol Jul 6, 2021
8a4feaf
Store filename in a helper variable
turol Apr 30, 2015
497bd61
Refactor ddsinfo by moving all interesting bits to a helper function
turol Apr 30, 2015
ef72de3
Refactor glddstest by moving OpenGL initialization earlier
turol Apr 30, 2015
b9146c9
Move glddstest interesting bits to a helper function
turol Apr 30, 2015
5f742bd
Support multiple files at once
turol Apr 30, 2015
3d36db0
Check MOJODDS_getTexture return value and error out if it fails
turol Apr 30, 2015
7875fb6
Add sanity check for miplevels
turol Apr 30, 2015
b39b657
Add AFL helper program
turol Apr 30, 2015
df876b5
Fix incorrect printf format strings
turol Apr 30, 2015
f8b13ca
Check the file contains enough data if the header claims it does
turol Apr 30, 2015
a72b414
Fix crash when width * height would overflow uint32
turol Apr 30, 2015
e84d41f
Fix crash when dwPitchOrLinearSize is incorrect
turol Apr 30, 2015
a097f05
Fix division by zero
turol Apr 30, 2015
88da12b
Fix mip size calculation for uncompressed textures
turol Apr 30, 2015
ec966a6
Also verify calculated size against data size
turol Apr 30, 2015
94b2072
Set blockSize when reading luminance-alpha format
turol Apr 30, 2015
2724ece
Print textureType
turol Apr 30, 2015
95c76f5
Cube maps must be square
turol Apr 30, 2015
891984e
Fix LDFLAGS support in Makefile
turol Apr 30, 2015
8b0dc7c
Add support for fetching specific cube map faces
turol May 8, 2015
834d76a
Fix ddsinfo
turol May 8, 2015
9b78a49
Add support for AFL manual init
turol May 9, 2015
2e4959c
Add filename to GL trace by using GREMEDY_string_marker
turol May 9, 2015
fd717d5
Avoid integer overflow and resulting crash when advertised texture si…
turol May 9, 2015
7094db7
Calculate miplevels from size if header says zero
turol May 9, 2015
63430b0
Also calculate mip level 0 size from width and height instead of trus…
turol May 9, 2015
e5a4ec3
Improve invalid miplevels calculation to too many mips relative to size
turol May 9, 2015
942bc98
GL_LUMINANCE_ALPHA is not a compressed format
turol May 9, 2015
abd77b4
Validate cube map size
turol May 9, 2015
22ab4b6
Respect OpenGL maximum texture size
turol May 9, 2015
1963982
Fix cubemap face offset calculation
turol May 9, 2015
cad881f
Fix format strings
turol May 9, 2015
624b401
Whitespace
turol May 9, 2015
6b44329
Pointer arithmetic on void pointers is not portable
turol May 9, 2015
fc2699d
Remove useless variable
turol May 9, 2015
5b1cc93
Add AFL-generated test corpus
turol May 11, 2015
72c2778
Remove MOJODDS_TEXTURE_NONE
turol Feb 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
*.dds -text
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
*.o
*~
*.trace
ddsinfo
glddstest
afl-mojodds
34 changes: 34 additions & 0 deletions Makefile
@@ -0,0 +1,34 @@

CC?=gcc
CFLAGS?=-Wall -g -O -std=c99
LDFLAGS?=-g

CFLAGS+=$(shell sdl2-config --cflags)
CFLAGS+=$(shell pkg-config glew --cflags)

LDLIBS+=$(shell sdl2-config --libs)
LDLIBS+=$(shell pkg-config glew --libs)


.SUFFIXES: .o

PROGRAMS:=ddsinfo glddstest afl-mojodds

.PHONY: all clean

all: $(PROGRAMS)

clean:
-rm $(PROGRAMS) *.o


ddsinfo: ddsinfo.o mojodds.o
$(CC) $(LDFLAGS) -o $@ $^


glddstest: glddstest.o mojodds.o
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)


afl-mojodds: afl-mojodds.o mojodds.o
$(CC) $(LDFLAGS) -o $@ $^
116 changes: 116 additions & 0 deletions afl-mojodds.c
@@ -0,0 +1,116 @@
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mojodds.h"


// helper for fuzzing with AFL
// if you don't know what this is you don't need it


void __afl_manual_init(void);


int main(int argc, char *argv[]) {
if (argc != 2) {
return 0;
}

#ifdef __AFL_HAVE_MANUAL_INIT
__afl_manual_init();
#endif // __AFL_HAVE_MANUAL_INIT

const char *filename = argv[1];

FILE *f = fopen(filename, "rb");
if (!f) {
return 1;
}

fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);

char *contents = malloc(size);
size_t readbytes = fread(contents, 1, size, f);
if (readbytes != size) {
free(contents);
fclose(f);
return 2;
}

fclose(f);

int isDDS = MOJODDS_isDDS(contents, size);
if (!isDDS) {
return 3;
}

const void *tex = NULL;
unsigned long texlen = 0;
unsigned int glfmt = 0, w = 0, h = 0, miplevels = 0;
unsigned int cubemapfacelen = 0;
MOJODDS_textureType textureType = 0;
int retval = MOJODDS_getTexture(contents, size, &tex, &texlen, &glfmt, &w, &h, &miplevels, &cubemapfacelen, &textureType);
if (!retval) {
free(contents);
return 4;
}

uint32_t hash = 0x12345678;
switch (textureType) {
case MOJODDS_TEXTURE_2D:
for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getMipMapTexture(miplevel, glfmt, tex, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
continue;
}

// read every byte to make sure any buffer overflows actually overflow
const char *miptex_ = (const char *) miptex;
for (unsigned int i = 0; i < miptexlen; i++) {
hash = (hash * 65537) ^ miptex_[i];
}
}
break;

case MOJODDS_TEXTURE_CUBE:
for (MOJODDS_cubeFace cubeFace = MOJODDS_CUBEFACE_POSITIVE_X; cubeFace <= MOJODDS_CUBEFACE_NEGATIVE_Z; cubeFace++) {
for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getCubeFace(cubeFace, miplevel, glfmt, tex, cubemapfacelen, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
continue;
}

// read every byte to make sure any buffer overflows actually overflow
const char *miptex_ = (const char *) miptex;
for (unsigned int i = 0; i < miptexlen; i++) {
hash = (hash * 65537) ^ miptex_[i];
}
}
}
break;

case MOJODDS_TEXTURE_VOLUME:
// TODO: do something with the data
break;

}
// do something the optimizer is not allowed to remove
printf("0x%08x\n", hash);

free(contents);

return 0;
}
133 changes: 133 additions & 0 deletions ddsinfo.c
@@ -0,0 +1,133 @@
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mojodds.h"


// from http://graphics.stanford.edu/~seander/bithacks.html
static bool isPow2(unsigned int v) {
return v && !(v & (v - 1));
}


static int ddsinfo(const char *filename) {
printf("%s\n", filename);

FILE *f = fopen(filename, "rb");
if (!f) {
printf("Error opening %s: %s (%d)\n", filename, strerror(errno), errno);
return 1;
}

fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);

char *contents = malloc(size);
size_t readbytes = fread(contents, 1, size, f);
if (readbytes != size) {
printf("Only got %u of %ld bytes: %s\n", (unsigned int) readbytes, size, strerror(errno));
free(contents);
fclose(f);
return 2;
}

fclose(f);

int isDDS = MOJODDS_isDDS(contents, size);
printf("isDDS: %d\n", isDDS);
if (isDDS) {
const void *tex = NULL;
unsigned long texlen = 0;
unsigned int glfmt = 0, w = 0, h = 0, miplevels = 0;
unsigned int cubemapfacelen = 0;
MOJODDS_textureType textureType = 0;
int retval = MOJODDS_getTexture(contents, size, &tex, &texlen, &glfmt, &w, &h, &miplevels, &cubemapfacelen, &textureType);
if (!retval) {
printf("MOJODDS_getTexture failed\n");
free(contents);
return 3;
}

uintptr_t texoffset = ((const char *)(tex)) - contents;
printf("texoffset: %u\n", (unsigned int)(texoffset));
printf("texlen: %lu\n", texlen);
printf("glfmt: 0x%x\n", glfmt);
printf("width x height: %u x %u\n", w, h);
printf("miplevels: %u\n", miplevels);
printf("textureType: ");
switch (textureType) {
case MOJODDS_TEXTURE_2D:
printf("2D\n");
printf("\n");

for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getMipMapTexture(miplevel, glfmt, tex, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
printf("MOJODDS_getMipMapTexture(%u) error: %d\n", miplevel, retval);
continue;
}

uintptr_t miptexoffset = ((const char *)(miptex)) - ((const char *)(tex));
bool npot = !(isPow2(mipW) || isPow2(mipH));
printf("%4u x %4u %s", mipW, mipH, npot ? "NPOT " : " ");
printf("miptexoffset: %8u ", (unsigned int)(miptexoffset));
printf("miptexlen: %8lu\n", miptexlen);
}
break;

case MOJODDS_TEXTURE_CUBE:
printf("cube\n");
printf("cubemapfacelen: %u\n", cubemapfacelen);
printf("\n");

for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
const void *miptex = NULL;
unsigned long miptexlen = 0;
unsigned int mipW = 0, mipH = 0;
retval = MOJODDS_getCubeFace(MOJODDS_CUBEFACE_POSITIVE_X, miplevel, glfmt, tex, cubemapfacelen, w, h, &miptex, &miptexlen, &mipW, &mipH);
if (!retval) {
printf("MOJODDS_getMipMapTexture(%u) error: %d\n", miplevel, retval);
continue;
}

uintptr_t miptexoffset = ((const char *)(miptex)) - ((const char *)(tex));
bool npot = !(isPow2(mipW) || isPow2(mipH));
printf("%4u x %4u %s", mipW, mipH, npot ? "NPOT " : " ");
printf("miptexoffset: %8u ", (unsigned int)(miptexoffset));
printf("miptexlen: %8lu\n", miptexlen);
}
break;

case MOJODDS_TEXTURE_VOLUME:
printf("volume\n");
break;

}
}

free(contents);

return 0;
}


int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s DDS-file ...\n", argv[0]);
return 0;
}

for (int i = 1; i < argc; i++) {
ddsinfo(argv[i]);
}

return 0;
}