├── .gitignore ├── LICENSE ├── Makefile ├── Makefile_Debug ├── README ├── TODO ├── buildNumber.cpp ├── data └── bg.jpg ├── include ├── boot2.h ├── ecc.h ├── errorcodes.h ├── errorhandler.h ├── errorstrings.h ├── filemanager.h ├── flash.h ├── gecko.h ├── gpio.h ├── haxx.h ├── hbc.h ├── hbc │ ├── hbc_certs.h │ ├── hbc_content0.h │ ├── hbc_content1.h │ ├── hbc_tik.h │ └── hbc_tmd.h ├── hollywood.h ├── installer.h ├── jpeglib │ ├── jchuff.h │ ├── jconfig.h │ ├── jdct.h │ ├── jdhuff.h │ ├── jerror.h │ ├── jinclude.h │ ├── jmemsys.h │ ├── jmorecfg.h │ ├── jpegint.h │ ├── jpeglib.h │ ├── jpgogc.h │ └── jversion.h ├── menu.h ├── prodinfo.h ├── runtimeiospatch.h ├── seeprom.h ├── sha256.h └── tools.h └── src ├── boot2.c ├── ecc.c ├── errorhandler.c ├── filemanager.c ├── flash.c ├── gecko.c ├── haxx.c ├── hbc.c ├── hbc ├── hbc_certs.c ├── hbc_content0.c ├── hbc_content1.c ├── hbc_tik.c └── hbc_tmd.c ├── installer.c ├── jpeglib ├── jcapimin.c ├── jcapistd.c ├── jccoefct.c ├── jccolor.c ├── jcdctmgr.c ├── jchuff.c ├── jcinit.c ├── jcmainct.c ├── jcmarker.c ├── jcmaster.c ├── jcomapi.c ├── jcparam.c ├── jcphuff.c ├── jcprepct.c ├── jcsample.c ├── jctrans.c ├── jdapimin.c ├── jdapistd.c ├── jdatadst.c ├── jdatasrc.c ├── jdcoefct.c ├── jdcolor.c ├── jddctmgr.c ├── jdhuff.c ├── jdinput.c ├── jdmainct.c ├── jdmarker.c ├── jdmaster.c ├── jdmerge.c ├── jdphuff.c ├── jdpostct.c ├── jdsample.c ├── jdtrans.c ├── jerror.c ├── jfdctflt.c ├── jfdctfst.c ├── jfdctint.c ├── jidctflt.c ├── jidctfst.c ├── jidctint.c ├── jidctred.c ├── jmemmgr.c ├── jmemnobs.c ├── jmemsrc.c ├── jpgogc.c ├── jquant1.c ├── jquant2.c └── jutils.c ├── main.c ├── menu.c ├── prodinfo.c ├── runtimeiospatch.c ├── seeprom.c ├── sha256.c └── tools.c /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | rgdboot-installer* 3 | include/version.h 4 | std.cfg 5 | rgdboot.ld 6 | pack-it.sh 7 | cppcheck.cppcheck 8 | connect.txt 9 | compile.log 10 | START DEBUGGING - TCP.launch 11 | START DEBUGGING - USB.launch 12 | .project 13 | .cproject 14 | .settings/ 15 | cppcheck-cppcheck-build-dir/ 16 | build_debug/ 17 | buildnumber 18 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | # Clear the implicit built in rules 3 | #--------------------------------------------------------------------------------- 4 | .SUFFIXES: 5 | #--------------------------------------------------------------------------------- 6 | ifeq ($(strip $(DEVKITPPC)),) 7 | $(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") 8 | endif 9 | 10 | include $(DEVKITPPC)/wii_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET is the name of the output 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # INCLUDES is a list of directories containing extra header files 17 | #--------------------------------------------------------------------------------- 18 | 19 | # [nitr8]: change this to "boot" 20 | #TARGET := $(notdir $(CURDIR)) 21 | TARGET := boot 22 | 23 | BUILD := build 24 | SOURCES := src src/hbc src/jpeglib 25 | DATA := data 26 | INCLUDES := include 27 | 28 | #--------------------------------------------------------------------------------- 29 | # options for code generation 30 | #--------------------------------------------------------------------------------- 31 | 32 | # [nitr8]: be really aggressive, so add this - then fix even more compiler warnings 33 | #WARNPLUS = -Wpedantic 34 | 35 | # [nitr8]: be more specific, so add this - then fix compiler warnings 36 | WARNFLAGS = -Wextra $(WARNPLUS) 37 | 38 | # [nitr8]: Add support for realtime debugging using a USB-Gecko (-g) */ 39 | CFLAGS = -g -O2 -Wall $(WARNFLAGS) $(MACHDEP) $(INCLUDE) 40 | 41 | # [nitr8]: Reworked 42 | ifndef NO_DOLPHIN_CHECK 43 | # [nitr8]: Reworked 44 | # CFLAGS = -O2 -Wall $(WARNFLAGS) $(MACHDEP) $(INCLUDE) 45 | CFLAGS += -DDOLPHIN_CHECK 46 | else 47 | TARGET = rgdboot-installer_noDolphinCheck 48 | endif 49 | 50 | # Skipping the version clear will cause a brick on Wiis with a boot2 version higher than 0 - use for testing if you have a flash programmer only 51 | ifdef NO_VERSION_CLEAR 52 | # [nitr8]: Reworked 53 | # CFLAGS = -O2 -Wall $(WARNFLAGS) $(MACHDEP) $(INCLUDE) 54 | CFLAGS += -DNO_VERSION_CLEAR 55 | TARGET = rgdboot-installer_noVersionClear 56 | endif 57 | 58 | CXXFLAGS = $(CFLAGS) 59 | 60 | LDFLAGS = $(MACHDEP) -T$(CURDIR)/../rgdboot.ld -Wl,-Map,$(notdir $@).map 61 | 62 | #--------------------------------------------------------------------------------- 63 | # any extra libraries we wish to link with the project 64 | #--------------------------------------------------------------------------------- 65 | 66 | # [nitr8]: Add support for realtime debugging using a USB-Gecko (libdb) */ 67 | #LIBS := -lfat -lwiiuse -lbte -logc -lm 68 | LIBS := -lfat -lwiiuse -lbte -logc -ldb -lm 69 | 70 | #--------------------------------------------------------------------------------- 71 | # list of directories containing libraries, this must be the top level containing 72 | # include and lib 73 | #--------------------------------------------------------------------------------- 74 | LIBDIRS := 75 | 76 | #--------------------------------------------------------------------------------- 77 | # no real need to edit anything past this point unless you need to add additional 78 | # rules for different file extensions 79 | #--------------------------------------------------------------------------------- 80 | ifneq ($(BUILD),$(notdir $(CURDIR))) 81 | #--------------------------------------------------------------------------------- 82 | 83 | export OUTPUT := $(CURDIR)/$(TARGET) 84 | 85 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 86 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 87 | 88 | export DEPSDIR := $(CURDIR)/$(BUILD) 89 | 90 | #--------------------------------------------------------------------------------- 91 | # automatically build a list of object files for our project 92 | #--------------------------------------------------------------------------------- 93 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 94 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 95 | sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 96 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) 97 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 98 | 99 | #--------------------------------------------------------------------------------- 100 | # use CXX for linking C++ projects, CC for standard C 101 | #--------------------------------------------------------------------------------- 102 | ifeq ($(strip $(CPPFILES)),) 103 | export LD := $(CC) 104 | else 105 | export LD := $(CXX) 106 | endif 107 | 108 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) 109 | export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o) 110 | export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) 111 | 112 | export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) 113 | 114 | #--------------------------------------------------------------------------------- 115 | # build a list of include paths 116 | #--------------------------------------------------------------------------------- 117 | export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \ 118 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 119 | -I$(CURDIR)/$(BUILD) \ 120 | -I$(LIBOGC_INC) \ 121 | -iquote $(CURDIR)/$(INCLUDES)/hbc \ 122 | -iquote $(CURDIR)/$(INCLUDES)/jpeglib 123 | 124 | #--------------------------------------------------------------------------------- 125 | # build a list of library paths 126 | #--------------------------------------------------------------------------------- 127 | export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 128 | 129 | export OUTPUT := $(CURDIR)/$(TARGET) 130 | .PHONY: $(BUILD) clean 131 | 132 | #--------------------------------------------------------------------------------- 133 | #buildNumber = $(shell git rev-list --count HEAD) 134 | # [nitr8]: get rid of warnings when variables are not used at all 135 | $(BUILD): 136 | # @echo "static char *buildNumber = \"$(buildNumber)\";" > $(INCLUDES)/version.h 137 | # @echo "char *buildNumber = \"$(buildNumber)\";" > $(INCLUDES)/version.h 138 | 139 | @gcc -s -Os -Wall -Wextra -o buildnumber buildNumber.cpp 140 | @./buildnumber $(INCLUDES)/version.h 141 | @[ -d $@ ] || mkdir -p $@ 142 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 143 | 144 | #--------------------------------------------------------------------------------- 145 | clean: 146 | @echo clean ... 147 | @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol 148 | @rm -fr $(BUILD) rgdboot-installer_noDolphinCheck.elf rgdboot-installer_noDolphinCheck.dol 149 | 150 | #--------------------------------------------------------------------------------- 151 | run: 152 | wiiload $(TARGET).dol 153 | 154 | 155 | #--------------------------------------------------------------------------------- 156 | else 157 | 158 | DEPENDS := $(OFILES:.o=.d) 159 | 160 | #--------------------------------------------------------------------------------- 161 | # main targets 162 | #--------------------------------------------------------------------------------- 163 | $(OUTPUT).dol: $(OUTPUT).elf 164 | $(OUTPUT).elf: $(OFILES) 165 | 166 | $(OFILES_SOURCES) : $(HFILES) 167 | 168 | #--------------------------------------------------------------------------------- 169 | # This rule links in binary data with the .jpg extension 170 | #--------------------------------------------------------------------------------- 171 | %.jpg.o %_jpg.h : %.jpg 172 | #--------------------------------------------------------------------------------- 173 | @echo $(notdir $<) 174 | @$(bin2o) 175 | 176 | -include $(DEPENDS) 177 | 178 | #--------------------------------------------------------------------------------- 179 | endif 180 | #--------------------------------------------------------------------------------- 181 | -------------------------------------------------------------------------------- /Makefile_Debug: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | # Clear the implicit built in rules 3 | #--------------------------------------------------------------------------------- 4 | .SUFFIXES: 5 | #--------------------------------------------------------------------------------- 6 | ifeq ($(strip $(DEVKITPPC)),) 7 | $(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") 8 | endif 9 | 10 | include $(DEVKITPPC)/wii_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET_DEBUG is the name of the output 14 | # BUILD_DEBUG is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # INCLUDES is a list of directories containing extra header files 17 | #--------------------------------------------------------------------------------- 18 | 19 | # [nitr8]: change this to "boot" 20 | #TARGET_DEBUG := $(notdir $(CURDIR)) 21 | TARGET_DEBUG := boot_debug 22 | 23 | BUILD_DEBUG := build_debug 24 | SOURCES := src src/hbc src/jpeglib 25 | DATA := data 26 | INCLUDES := include 27 | 28 | #--------------------------------------------------------------------------------- 29 | # options for code generation 30 | #--------------------------------------------------------------------------------- 31 | 32 | # [nitr8]: be really aggressive, so add this - then fix even more compiler warnings 33 | #WARNPLUS = -Wpedantic 34 | 35 | # [nitr8]: be more specific, so add this - then fix even more compiler warnings 36 | WARNFLAGS = -Wextra $(WARNPLUS) 37 | 38 | CFLAGS = -g -O0 -Wall $(WARNFLAGS) $(MACHDEP) $(INCLUDE) -DDOLPHIN_CHECK -D_DEBUG 39 | 40 | ifdef NO_DOLPHIN_CHECK 41 | CFLAGS = -g -O0 -Wall $(WARNFLAGS) $(MACHDEP) $(INCLUDE) -D_DEBUG 42 | TARGET_DEBUG = rgdboot-installer_noDolphinCheck 43 | endif 44 | 45 | # Skipping the version clear will cause a brick on Wiis with a boot2 version higher than 0 - use for testing if you have a flash programmer only 46 | ifdef NO_VERSION_CLEAR 47 | CFLAGS = -g -O0 -Wall $(WARNFLAGS) $(MACHDEP) $(INCLUDE) -DNO_VERSION_CLEAR -D_DEBUG 48 | TARGET_DEBUG = rgdboot-installer_noVersionClear 49 | endif 50 | 51 | CXXFLAGS = $(CFLAGS) 52 | 53 | LDFLAGS = $(MACHDEP) -Wl,-Map,$(notdir $@).map 54 | 55 | #--------------------------------------------------------------------------------- 56 | # any extra libraries we wish to link with the project 57 | #--------------------------------------------------------------------------------- 58 | 59 | # [nitr8]: Add support for realtime debugging using a USB-Gecko (libdb) */ 60 | #LIBS := -lfat -lwiiuse -lbte -logc -lm 61 | LIBS := -lfat -lwiiuse -lbte -logc -ldb -lm 62 | 63 | #--------------------------------------------------------------------------------- 64 | # list of directories containing libraries, this must be the top level containing 65 | # include and lib 66 | #--------------------------------------------------------------------------------- 67 | LIBDIRS := 68 | 69 | #--------------------------------------------------------------------------------- 70 | # no real need to edit anything past this point unless you need to add additional 71 | # rules for different file extensions 72 | #--------------------------------------------------------------------------------- 73 | ifneq ($(BUILD_DEBUG),$(notdir $(CURDIR))) 74 | #--------------------------------------------------------------------------------- 75 | 76 | export OUTPUT_DEBUG := $(CURDIR)/$(TARGET_DEBUG) 77 | 78 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 79 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 80 | 81 | export DEPSDIR := $(CURDIR)/$(BUILD_DEBUG) 82 | 83 | #--------------------------------------------------------------------------------- 84 | # automatically build a list of object files for our project 85 | #--------------------------------------------------------------------------------- 86 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 87 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 88 | sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 89 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) 90 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 91 | 92 | #--------------------------------------------------------------------------------- 93 | # use CXX for linking C++ projects, CC for standard C 94 | #--------------------------------------------------------------------------------- 95 | ifeq ($(strip $(CPPFILES)),) 96 | export LD := $(CC) 97 | else 98 | export LD := $(CXX) 99 | endif 100 | 101 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) 102 | export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o) 103 | export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) 104 | 105 | export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) 106 | 107 | #--------------------------------------------------------------------------------- 108 | # build a list of include paths 109 | #--------------------------------------------------------------------------------- 110 | export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \ 111 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 112 | -I$(CURDIR)/$(BUILD_DEBUG) \ 113 | -I$(LIBOGC_INC) \ 114 | -iquote $(CURDIR)/$(INCLUDES)/hbc \ 115 | -iquote $(CURDIR)/$(INCLUDES)/jpeglib 116 | 117 | #--------------------------------------------------------------------------------- 118 | # build a list of library paths 119 | #--------------------------------------------------------------------------------- 120 | export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 121 | 122 | export OUTPUT_DEBUG := $(CURDIR)/$(TARGET_DEBUG) 123 | .PHONY: $(BUILD_DEBUG) clean_debug 124 | 125 | #--------------------------------------------------------------------------------- 126 | #buildNumber = $(shell git rev-list --count HEAD) 127 | # [nitr8]: get rid of warnings when variables are not used at all 128 | $(BUILD_DEBUG): 129 | # @echo "static char *buildNumber = \"$(buildNumber)\";" > $(INCLUDES)/version.h 130 | # @echo "char *buildNumber = \"$(buildNumber)\";" > $(INCLUDES)/version.h 131 | 132 | @gcc -s -Os -Wall -Wextra -o buildnumber buildNumber.cpp 133 | @./buildnumber $(INCLUDES)/version.h 134 | @[ -d $@ ] || mkdir -p $@ 135 | @$(MAKE) --no-print-directory -C $(BUILD_DEBUG) -f $(CURDIR)/Makefile_Debug 136 | 137 | #--------------------------------------------------------------------------------- 138 | clean_debug: 139 | @echo clean_debug ... 140 | @rm -fr $(BUILD_DEBUG) $(OUTPUT_DEBUG).elf 141 | @rm -fr $(BUILD_DEBUG) rgdboot-installer_noDolphinCheck.elf 142 | 143 | #--------------------------------------------------------------------------------- 144 | run_debug: 145 | wiiload $(TARGET_DEBUG).elf 146 | 147 | 148 | #--------------------------------------------------------------------------------- 149 | else 150 | 151 | DEPENDS := $(OFILES:.o=.d) 152 | 153 | #--------------------------------------------------------------------------------- 154 | # main targets 155 | #--------------------------------------------------------------------------------- 156 | $(OUTPUT_DEBUG).elf: $(OFILES) 157 | 158 | $(OFILES_SOURCES) : $(HFILES) 159 | 160 | #--------------------------------------------------------------------------------- 161 | # This rule links in binary data with the .jpg extension 162 | #--------------------------------------------------------------------------------- 163 | %.jpg.o %_jpg.h : %.jpg 164 | #--------------------------------------------------------------------------------- 165 | @echo $(notdir $<) 166 | $(bin2o) 167 | 168 | -include $(DEPENDS) 169 | 170 | #--------------------------------------------------------------------------------- 171 | endif 172 | #--------------------------------------------------------------------------------- 173 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | RGD SDBoot Installer 2 | 3 | By root1024, DeadlyFoez, RedBees, Larsenv -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | -- Refinements/Bugs -- 2 | 3 | * Implement bad block checking 4 | * SD file hash checks 5 | * Fix build on latest toolchain 6 | * Set boot2 version in seeprom to 0 after boot2 wad installation 7 | 8 | -- UI -- 9 | - New UI (images/maybe GUI) 10 | -------------------------------------------------------------------------------- /buildNumber.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // BuildNumber.cpp : Defines the entry point for the console application. 3 | // 4 | // BuildNumber 1.0 - © S. Gregory 2016 5 | // BuildNumber 1.1 - © S. nitr8 2024 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #define UNUSED(x) ((x) = (x)) 16 | 17 | #define ALTERNATIVE 0 18 | 19 | 20 | int main(int argc, char* argv[]) 21 | { 22 | FILE *fp; 23 | char *linebuf; 24 | 25 | #if ALTERNATIVE 26 | const char *magic = "#define BUILDNUMBER"; 27 | #else 28 | const char *magic = "char *buildNumber = "; 29 | #endif 30 | 31 | int nBuild = 1; 32 | size_t len = 0; 33 | ssize_t read = 0; 34 | char *line = NULL; 35 | 36 | UNUSED(argc); 37 | 38 | if (argc < 2) 39 | { 40 | printf("[ERROR]: No filename given\n"); 41 | 42 | return -6; 43 | } 44 | 45 | fp = fopen(argv[1], "r+"); 46 | 47 | if (fp) 48 | { 49 | fseek(fp, 0, SEEK_END); 50 | len = ftell(fp); 51 | fseek(fp, 0, SEEK_SET); 52 | linebuf = (char *)malloc(len); 53 | 54 | if (linebuf) 55 | { 56 | memset(linebuf, 0, len); 57 | 58 | while ((read = getline(&line, &len, fp)) != -1) 59 | { 60 | len = strlen(magic); 61 | 62 | if (strncmp(line, magic, len) == 0) 63 | { 64 | nBuild = atol(line + len + 1) + 1; 65 | fclose(fp); 66 | fp = fopen(argv[1], "w"); 67 | 68 | if (fp) 69 | { 70 | fprintf(fp, "#ifndef __VERSION_H__\r\n"); 71 | fprintf(fp, "#define __VERSION_H__\r\n\r\n\r\n"); 72 | #if ALTERNATIVE 73 | fprintf(fp, "%s %d\r\n", magic, nBuild); 74 | #else 75 | fprintf(fp, "%s\"%d\";\r\n\r\n\r\n", magic, nBuild); 76 | #endif 77 | 78 | #if ALTERNATIVE 79 | fprintf(fp, "%s_STR \"%04d\"\r\n\r\n\r\n", magic, nBuild); 80 | #endif 81 | fprintf(fp, "#endif /* __VERSION_H__ */\r\n\r\n"); 82 | 83 | fclose(fp); 84 | 85 | return 0; 86 | } 87 | else 88 | { 89 | printf("[ERROR]: Couldn't open file %s\n", argv[1]); 90 | 91 | return -1; 92 | } 93 | } 94 | else 95 | { 96 | const char *str = "#define __VERSION_H__"; 97 | 98 | if (strncmp(line, str, strlen(str)) == 0) 99 | { 100 | if (argv[2]) 101 | { 102 | if (atoi(argv[2]) == 1) 103 | { 104 | if (access(argv[1], F_OK) == 0) 105 | { 106 | remove(argv[1]); 107 | goto recreate; 108 | } 109 | } 110 | } 111 | } 112 | 113 | continue; 114 | } 115 | } 116 | 117 | free(linebuf); 118 | } 119 | else 120 | { 121 | printf("[ERROR]: Couldn't allocate %lu bytes of memory for file %s\n", len, argv[1]); 122 | 123 | return -5; 124 | } 125 | 126 | printf("[ERROR]: File %s is not valid\n", argv[1]); 127 | printf(" Recreation can be done by calling 'buildnumber %s 1'\n", argv[1]); 128 | fclose(fp); 129 | 130 | return -2; 131 | } 132 | else 133 | { 134 | recreate: 135 | fp = fopen(argv[1], "w"); 136 | 137 | if (fp) 138 | { 139 | fprintf(fp, "#ifndef __VERSION_H__\r\n"); 140 | fprintf(fp, "#define __VERSION_H__\r\n\r\n\r\n"); 141 | #if ALTERNATIVE 142 | fprintf(fp, "%s %d\r\n", magic, nBuild); 143 | #else 144 | fprintf(fp, "%s\"%d\";\r\n\r\n\r\n", magic, nBuild); 145 | #endif 146 | 147 | #if ALTERNATIVE 148 | fprintf(fp, "%s_STR \"%04d\"\r\n\r\n\r\n", magic, nBuild); 149 | #endif 150 | fprintf(fp, "#endif /* __VERSION_H__ */\r\n\r\n"); 151 | 152 | fclose(fp); 153 | } 154 | else 155 | { 156 | printf("[ERROR]: Couldn't open file %s\n", argv[1]); 157 | 158 | return -4; 159 | } 160 | 161 | return 0; 162 | } 163 | 164 | return -3; 165 | } 166 | 167 | -------------------------------------------------------------------------------- /data/bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedBeesRGD/rgdboot-installer/ac71e661aae277526ba63334540fe35749c9c36b/data/bg.jpg -------------------------------------------------------------------------------- /include/boot2.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __BOOT2_H__ 4 | #define __BOOT2_H__ 5 | 6 | #include "tools.h" 7 | 8 | /* [nitr8]: Moved to "errorcodes.h" */ 9 | /* #define MISSING_FILE -1 10 | #define BOOT2_DOLPHIN -4 11 | #define HASH_MISMATCH -1022 12 | #define CANNOT_DOWNGRADE -1031 */ 13 | 14 | #define RAWBOOT2SIZE 0x21000 15 | 16 | /* [nitr8]: Added WAD header size */ 17 | #define WAD_HEADER_SIZE 0x40 18 | 19 | typedef struct { 20 | signed_blob *ca_cert; /* offset 0x20 (memset OK) */ 21 | signed_blob *cp_cert; /* offset 0x420 (memset OK) */ 22 | signed_blob *xs_cert; /* offset 0x720 (memset OK) */ 23 | 24 | // u8 padding[0x40]; /* offset 0x14 */ 25 | 26 | signed_blob *tik_cert; /* offset 0xA20 (memset OK) */ 27 | signed_blob *TMD_cert /* (memset OK) */; 28 | 29 | } certificates; 30 | 31 | typedef struct { 32 | u32 headerLen; /* offset 0x0 */ 33 | u32 dataOffset; /* offset 0x4 */ 34 | u32 certsLen; /* offset 0x8 */ 35 | u32 tikLen; /* offset 0xC */ 36 | u32 TMDLen; /* offset 0x10 */ 37 | 38 | /* [nitr8]: Was this forgotten? 39 | Let's just assume we're using this struct to assign 40 | it to the contents of a WAD file with no ECC data: 41 | You would point the "certs" struct to offset 0x14 42 | in the WAD which is simply NOT where the certificates 43 | start. Instead, they start at offset 0x20 which is 44 | offset 0x14 + 0xC (12) bytes. Now an "integer" makes 45 | use of 4 bytes, so 12 / 4 = 3. Let's put some padding 46 | here... */ 47 | // u8 padding_1[3]; /* offset 0x14 */ 48 | 49 | certificates *certs; /* offset 0x20 */ 50 | signed_blob *tik; /* offset 0x0 */ 51 | signed_blob *TMD; 52 | 53 | // u8 padding_2[0x34]; /* offset 0x14 */ 54 | 55 | u8 *content; 56 | u32 contentSize; 57 | } boot2; 58 | 59 | typedef struct { 60 | u32 headerLen; 61 | u16 wadType; 62 | u16 wadVersion; 63 | u32 certsLen; 64 | u8 reserved[4]; 65 | u32 tikLen; 66 | u32 TMDLen; 67 | u32 contentSize; 68 | u32 footerSize; 69 | u8 padding[0x20]; 70 | 71 | certificates *certs; 72 | signed_blob *tik; 73 | signed_blob *TMD; 74 | u8 *content; 75 | } WAD; 76 | 77 | #ifdef __cplusplus 78 | extern "C" { 79 | #endif 80 | 81 | /* [nitr8]: Make static */ 82 | /* WAD *ReadWAD(const char *filename); */ 83 | 84 | /* [nitr8]: Make static */ 85 | /* boot2 *ReadBoot2(const char *filename); */ 86 | 87 | /* [nitr8]: Make static */ 88 | /* s32 InstallRawBoot2(const char* filename); */ 89 | 90 | s32 InstallWADBoot2(const char* filename); 91 | s32 InstallSDBoot(const char* filename); 92 | s32 InstallNANDBoot(const char* filename, const char* payload); 93 | s32 BackupBoot2Blocks(const char* filename); 94 | s32 RestoreBoot2Blocks(const char* filename); 95 | 96 | /* [nitr8]: Add function to read ALL the boot2 blocks in a row - with the ECC data and blockmap data included */ 97 | s32 ReadBoot2Blocks(u8 *buffer, int start_block, int end_block); 98 | 99 | /* [nitr8]: Added */ 100 | #ifdef __cplusplus 101 | } 102 | #endif /* __cplusplus */ 103 | 104 | #endif /* __BOOT2_H__ */ 105 | 106 | -------------------------------------------------------------------------------- /include/ecc.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* [nitr8]: New file */ 4 | 5 | #ifndef __ECC_H__ 6 | #define __ECC_H__ 7 | 8 | #include 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | u8 *calc_page_ecc(u8 *data); 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif /* __cplusplus */ 19 | 20 | #endif /* __ECC_H__ */ 21 | 22 | -------------------------------------------------------------------------------- /include/errorcodes.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __ERRORCODES_H__ 4 | #define __ERRORCODES_H__ 5 | 6 | #define ALL_OK 0 7 | 8 | /* [nitr8]: Moved error codes here */ 9 | #define MISSING_FILE -1 10 | #define ERASE_ERROR -2 11 | #define SEEK_ERROR -3 12 | #define BOOT2_DOLPHIN -4 13 | #define BAD_BOOT_BLOCKS -5 14 | 15 | /* [nitr8]: Added */ 16 | #define ALLOC_ERROR -6 17 | 18 | /* [nitr8]: Added */ 19 | #define READ_LENGTH_ERROR -7 20 | 21 | #define BAD_BLOCK -13 22 | #define HASH_MISMATCH -1022 23 | #define CANNOT_DOWNGRADE -1031 24 | 25 | /* [nitr8]: Moved to new file "errorstrings.h" */ 26 | #if 0 27 | typedef enum { 28 | ErrStr_NeedPerms = 0, 29 | ErrStr_InDolphin, 30 | ErrStr_InCafe, 31 | ErrStr_DevFlashErr, 32 | ErrStr_BadFile, 33 | ErrStr_SettingTooBig, 34 | ErrStr_SettingInvalid, 35 | ErrStr_SDCard, 36 | ErrStr_BadBoot2Ver, 37 | ErrStr_WrongVersion, 38 | ErrStr_MissingFiles, 39 | ErrStr_BadBlocks, 40 | ErrStr_Generic, 41 | ErrStr_Count /* Number of values supported by this enum. */ 42 | } ErrStr; 43 | 44 | const char *errorStrings[ErrStr_Count] = { 45 | "AHBPROT is enabled, so the RGD SDBoot Installer can't run properly. To fix this, make sure that you are running the RGD SDBoot Installer\nfrom the Homebrew Channel with the correct meta.xml file in the same folder on your SD card, or from Wiiload.", 46 | "The RGD SDBoot Installer cannot run in Dolphin, as it relies on hardware\nfeatures which Dolphin does not emulate.\n\nThis can be bypassed with the compiler flag -NO_DOLPHIN_CHECK\nfor testing purposes.", 47 | "The RGD SDBoot Installer cannot run on a Wii U, as\ninstalling a custom boot2 has no effect on the Wii U.", 48 | "The RGD SDBoot Installer cannot access /dev/flash.\nThis might be caused by using incompatible IOS.", 49 | "The boot2 file (or the payload) on the SD card is invalid.\nNo data has been written yet, so your system will still boot.", 50 | "The setting.txt file is too big.", 51 | "The setting.txt file is invalid.", 52 | "The RGD SDBoot Installer cannot mount your SD card. Make sure it's inserted and try again.", 53 | "The boot2 version couldn't be obtained.", 54 | "The version of the RGD SDBoot Installer with Dolphin checking disabled will not run on a regular Wii with a boot2 version higher than v0, as it would cause a brick.", 55 | "One or more required files are missing.", 56 | "Unfortunately, there is at least one bad block present in the boot2 area.\nFor safety reasons, RGDBoot Installer cannot install NANDBoot...", 57 | "Error code:" 58 | }; 59 | #endif 60 | 61 | #endif /* __ERRORCODES_H__ */ 62 | 63 | -------------------------------------------------------------------------------- /include/errorhandler.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __ERRORHANDLER_H__ 4 | #define __ERRORHANDLER_H__ 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | extern void ThrowError(const char* errorString); 11 | extern void ThrowErrorEx(const char* errorString, s32 errorCode); 12 | 13 | /* [nitr8]: Added */ 14 | #ifdef __cplusplus 15 | } 16 | #endif /* __cplusplus */ 17 | 18 | #endif /* __ERRORHANDLER_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /include/errorstrings.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* [nitr8]: New file */ 4 | 5 | #ifndef __ERRORSTRINGS_H__ 6 | #define __ERRORSTRINGS_H__ 7 | 8 | typedef enum { 9 | ErrStr_NeedPerms = 0, 10 | ErrStr_InDolphin, 11 | ErrStr_InCafe, 12 | ErrStr_DevFlashErr, 13 | ErrStr_BadFile, 14 | ErrStr_SettingTooBig, 15 | ErrStr_SettingInvalid, 16 | ErrStr_SDCard, 17 | ErrStr_BadBoot2Ver, 18 | ErrStr_WrongVersion, 19 | ErrStr_MissingFiles, 20 | ErrStr_BadBlocks, 21 | 22 | /* [nitr8]: Added these */ 23 | ErrStr_BadBlockError, 24 | ErrStr_EraseError, 25 | ErrStr_SeekError, 26 | ErrStr_AllocError, 27 | ErrStr_ReadLengthError, 28 | ErrStr_DowngradeError, 29 | 30 | ErrStr_Generic, 31 | ErrStr_Count /* Number of values supported by this enum. */ 32 | } ErrStr; 33 | 34 | static const char *errorStrings[ErrStr_Count] = { 35 | "AHBPROT is enabled, so the RGD SDBoot Installer can't run properly. To fix this, make sure that you are running the RGD SDBoot Installer\nfrom the Homebrew Channel with the correct meta.xml file in the same folder on your SD card, or from Wiiload.", 36 | "The RGD SDBoot Installer cannot run in Dolphin, as it relies on hardware\nfeatures which Dolphin does not emulate.\n\nThis can be bypassed with the compiler flag -NO_DOLPHIN_CHECK\nfor testing purposes.", 37 | "The RGD SDBoot Installer cannot run on a Wii U, as\ninstalling a custom boot2 has no effect on the Wii U.", 38 | "The RGD SDBoot Installer cannot access /dev/flash.\nThis might be caused by using incompatible IOS.", 39 | "The boot2 file (or the payload) on the SD card is invalid.\nNo data has been written yet, so your system will still boot.", 40 | "The setting.txt file is too big.", 41 | "The setting.txt file is invalid.", 42 | "The RGD SDBoot Installer cannot mount your SD card. Make sure it's inserted and try again.", 43 | "The boot2 version couldn't be obtained.", 44 | "The version of the RGD SDBoot Installer with Dolphin checking disabled will not run on a regular Wii with a boot2 version higher than v0, as it would cause a brick.", 45 | "One or more required files are missing.", 46 | "Unfortunately, there are multiple bad blocks present in the boot2 area.\nFor safety reasons, RGDBoot Installer cannot install NANDBoot...", 47 | 48 | /* [nitr8]: Added these */ 49 | "Unfortunately, there is a bad block present in the boot2 area.\nFor safety reasons, RGDBoot Installer cannot install NANDBoot...", 50 | "Couldn't erase block", 51 | "Couldn't seek in file", 52 | "Couldn't allocate enough memory", 53 | "Couldn't read from file (bad length)", 54 | "Error: Cannot downgrade boot2", 55 | 56 | "Error code:" 57 | }; 58 | 59 | #endif /* __ERRORSTRINGS_H__ */ 60 | 61 | -------------------------------------------------------------------------------- /include/filemanager.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __FILEMANAGER_H__ 4 | #define __FILEMANAGER_H__ 5 | 6 | #include 7 | #include 8 | 9 | #include "tools.h" 10 | 11 | /* [nitr8]: Added */ 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | char* FileSelect(const char* dir); 17 | 18 | /* [nitr8]: Added */ 19 | #ifdef __cplusplus 20 | } 21 | #endif /* __cplusplus */ 22 | 23 | #endif /* __FILEMANAGER_H__ */ 24 | 25 | -------------------------------------------------------------------------------- /include/flash.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __FLASH_H__ 4 | #define __FLASH_H__ 5 | 6 | #include "tools.h" 7 | 8 | /* [nitr8]: Moved to "errorcodes.h" */ 9 | /* #define MISSING_FILE -1 10 | #define ERASE_ERROR -2 11 | #define SEEK_ERROR -3 12 | #define BAD_BLOCK -13 13 | #define BAD_BOOT_BLOCKS -5 */ 14 | 15 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 16 | #define NAND_ECC_DATA_SIZE 0x40 17 | 18 | /* [nitr8]: Moved here */ 19 | #define PAGE_SIZE_NO_ECC 0x800 20 | #define NAND_PAGE_SIZE (PAGE_SIZE_NO_ECC + NAND_ECC_DATA_SIZE) 21 | #define NAND_BLOCK_SIZE (NAND_PAGE_SIZE * 64) 22 | 23 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 24 | #define ECC_BUFFER_ALLOC (NAND_ECC_DATA_SIZE + 32) 25 | 26 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 27 | #define NAND_ECC_OK 0 28 | #define NAND_ECC_CORRECTED 1 29 | #define NAND_ECC_UNCORRECTABLE -1 30 | 31 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 32 | #define BLOCKMAP_SIGNATURE 0x26f29a401ee684cfULL 33 | 34 | /* [nitr8]: With SDBoot being "installed" in NAND block 1, mark blocks 1 and 2 as "INVALID" 35 | by skipping them as that would end up in running into an endless loop due to MINI 36 | (as of BootMii) recognizing those as VALID blocks and MINI then trying to use them 37 | as a boot2 copy for further booting. Block 2 is deactivated here as well due to boot2 38 | (because of it's size) ALWAYS using "paired" blocks (like 1 & 2, 3 & 4, 7 & 6...). */ 39 | /* #define BOOT2_START 1 */ 40 | #define BOOT2_START 3 41 | 42 | #define BOOT2_END 7 43 | 44 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 45 | #define BLOCK_SIZE 64 46 | 47 | struct Simulation{ 48 | u8* blocksStatus; 49 | int toBeWritten; 50 | }; 51 | 52 | typedef struct{ 53 | char version[10]; 54 | bool isBootMii; 55 | char bootMiiVer[4]; 56 | u8 blockSize; 57 | } Boot2Block; 58 | 59 | /* [nitr8]: Added */ 60 | #ifdef __cplusplus 61 | extern "C" { 62 | #endif 63 | 64 | s32 NANDFlashInit(void); 65 | 66 | /* [nitr8]: Unused */ 67 | /* void NANDFlashClose(void); */ 68 | 69 | s32 flashFile(const char* fileName, int firstBlock, int lastBlock, struct Simulation* sim); 70 | struct Simulation flashFileSim(const char* fileName, int firstBlock, int lastBlock); 71 | 72 | /* [nitr8]: Make static */ 73 | /* s32 dumpPages(const char* fileName, int firstPage, int lastPage); */ 74 | 75 | s32 dumpBlocks(const char* fileName, int firstBlock, int lastBlock); 76 | s32 eraseBlocks(int firstBlock, int lastBlock); 77 | u32 checkBlocks(int firstBlock, int lastBlock); 78 | 79 | void setMinBlock(u32 blockno); 80 | 81 | char identifyBoot1(void); 82 | Boot2Block identifyBoot2(u8 copy); 83 | 84 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 85 | u32 TestNANDBlockmaps(void); 86 | 87 | /* [nitr8]: Added */ 88 | extern s32 flash_fd; 89 | 90 | /* [nitr8]: Added */ 91 | #ifdef __cplusplus 92 | } 93 | #endif /* __cplusplus */ 94 | 95 | #endif /* __FLASH_H__ */ 96 | 97 | -------------------------------------------------------------------------------- /include/gecko.h: -------------------------------------------------------------------------------- 1 | /* 2 | mini - a Free Software replacement for the Nintendo/BroadOn IOS. 3 | USBGecko support code 4 | 5 | Copyright (c) 2008 Nuke - 6 | Copyright (C) 2008, 2009 Hector Martin "marcan" 7 | Copyright (C) 2008, 2009 Sven Peter 8 | Copyright (C) 2009 Andre Heider "dhewg" 9 | 10 | # This code is licensed to you under the terms of the GNU GPL, version 2; 11 | # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 12 | */ 13 | 14 | #ifndef __GECKO_H__ 15 | #define __GECKO_H__ 16 | 17 | /* [nitr8]: Added */ 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | void gecko_init(int port); 23 | int gecko_printf(const char *str, ...); 24 | 25 | /* [nitr8]: Added */ 26 | #ifdef __cplusplus 27 | } 28 | #endif /* __cplusplus */ 29 | 30 | #endif /* __GECKO_H__ */ 31 | 32 | -------------------------------------------------------------------------------- /include/gpio.h: -------------------------------------------------------------------------------- 1 | /* 2 | mini - a Free Software replacement for the Nintendo/BroadOn IOS. 3 | GPIO pin-out constants 4 | 5 | Copyright (C) 2008, 2009 Hector Martin "marcan" 6 | 7 | # This code is licensed to you under the terms of the GNU GPL, version 2; 8 | # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 9 | */ 10 | 11 | #ifndef __GPIO_H__ 12 | #define __GPIO_H__ 13 | 14 | enum { 15 | GP_POWER = 0x000001, 16 | GP_SHUTDOWN = 0x000002, 17 | GP_FAN = 0x000004, 18 | GP_DCDC = 0x000008, 19 | GP_DISPIN = 0x000010, 20 | GP_SLOTLED = 0x000020, 21 | GP_EJECTBTN = 0x000040, 22 | GP_SLOTIN = 0x000080, 23 | GP_SENSORBAR = 0x000100, 24 | GP_DOEJECT = 0x000200, 25 | GP_EEP_CS = 0x000400, 26 | GP_EEP_CLK = 0x000800, 27 | GP_EEP_MOSI = 0x001000, 28 | GP_EEP_MISO = 0x002000, 29 | GP_AVE_SCL = 0x004000, 30 | GP_AVE_SDA = 0x008000, 31 | GP_DEBUG0 = 0x010000, 32 | GP_DEBUG1 = 0x020000, 33 | GP_DEBUG2 = 0x040000, 34 | GP_DEBUG3 = 0x080000, 35 | GP_DEBUG4 = 0x100000, 36 | GP_DEBUG5 = 0x200000, 37 | GP_DEBUG6 = 0x400000, 38 | GP_DEBUG7 = 0x800000, 39 | }; 40 | 41 | #define GP_DEBUG_SHIFT 16 42 | #define GP_DEBUG_MASK 0xFF0000 43 | 44 | #define GP_ALL 0xFFFFFF 45 | #define GP_OWNER_PPC (GP_AVE_SDA | GP_AVE_SCL | GP_DOEJECT | GP_SENSORBAR | GP_SLOTIN | GP_SLOTLED) 46 | #define GP_OWNER_ARM (GP_ALL ^ GP_OWNER_PPC) 47 | #define GP_INPUTS (GP_POWER | GP_EJECTBTN | GP_SLOTIN | GP_EEP_MISO | GP_AVE_SDA) 48 | #define GP_OUTPUTS (GP_ALL ^ GP_INPUTS) 49 | #define GP_ARM_INPUTS (GP_INPUTS & GP_OWNER_ARM) 50 | #define GP_PPC_INPUTS (GP_INPUTS & GP_OWNER_PPC) 51 | #define GP_ARM_OUTPUTS (GP_OUTPUTS & GP_OWNER_ARM) 52 | #define GP_PPC_OUTPUTS (GP_OUTPUTS & GP_OWNER_PPC) 53 | #define GP_DEFAULT_ON (GP_AVE_SCL | GP_DCDC | GP_FAN) 54 | #define GP_ARM_DEFAULT_ON (GP_DEFAULT_ON & GP_OWNER_ARM) 55 | #define GP_PPC_DEFAULT_ON (GP_DEFAULT_ON & GP_OWNER_PPC) 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /include/haxx.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* haxx.h - PowerPC privilege elevation 4 | Written by Palapeli 5 | 6 | Copyright (C) 2022 Palapeli 7 | SPDX-License-Identifier: MIT */ 8 | 9 | /* [nitr8]: Added */ 10 | #ifndef __HAXX_H__ 11 | #define __HAXX_H__ 12 | 13 | /* [nitr8]: just "NO!" 14 | #pragma once */ 15 | 16 | #include 17 | #include 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | /* Performs an IOS exploit and branches to the entrypoint in system mode. */ 24 | 25 | /* [nitr8]: Make static */ 26 | /* s32 Haxx_IOSExploit(u32 entrypoint); */ 27 | 28 | /* Flushes data on PowerPC and invalidates on ARM. */ 29 | 30 | /* [nitr8]: Make static */ 31 | /* void Haxx_FlushRange(const void* data, u32 length); */ 32 | 33 | /* Checks if the PPC has full bus access. */ 34 | 35 | /* [nitr8]: Make static */ 36 | /* bool Haxx_CheckBusAccess(void); */ 37 | 38 | /* Attempts to get full PPC bus access. Will perform an IOS exploit if needed. */ 39 | bool Haxx_GetBusAccess(void); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | /* [nitr8]: Added */ 46 | #endif /* __HAXX_H__ */ 47 | 48 | -------------------------------------------------------------------------------- /include/hbc.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* [nitr8]: Added */ 4 | #ifndef __HBC_H__ 5 | #define __HBC_H__ 6 | 7 | /* [nitr8]: Added */ 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | s32 InstallHBC(void); 13 | 14 | /* [nitr8]: Added */ 15 | #ifdef __cplusplus 16 | } 17 | #endif /* __cplusplus */ 18 | 19 | /* [nitr8]: Added */ 20 | #endif /* __HBC_H__ */ 21 | 22 | -------------------------------------------------------------------------------- /include/hbc/hbc_certs.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* 4 | This file was autogenerated by raw2c. 5 | Visit http://www.devkitpro.org 6 | */ 7 | 8 | /* -------------------------------------------------------------------------------- */ 9 | #ifndef __HBC_CERTS_H__ 10 | #define __HBC_CERTS_H__ 11 | /* -------------------------------------------------------------------------------- */ 12 | 13 | extern const unsigned char hbc_certs[]; 14 | extern const int hbc_certs_size; 15 | 16 | /* -------------------------------------------------------------------------------- */ 17 | #endif /* __HBC_CERTS_H__ */ 18 | /* -------------------------------------------------------------------------------- */ 19 | 20 | -------------------------------------------------------------------------------- /include/hbc/hbc_content0.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* 4 | This file was autogenerated by raw2c. 5 | Visit http://www.devkitpro.org 6 | */ 7 | 8 | /* -------------------------------------------------------------------------------- */ 9 | #ifndef __HBC_CONTENT0_H__ 10 | #define __HBC_CONTENT0_H__ 11 | /* -------------------------------------------------------------------------------- */ 12 | 13 | extern const unsigned char hbc_content0[]; 14 | extern const int hbc_content0_size; 15 | 16 | /* -------------------------------------------------------------------------------- */ 17 | #endif /* __HBC_CONTENT0_H__ */ 18 | /* -------------------------------------------------------------------------------- */ 19 | 20 | -------------------------------------------------------------------------------- /include/hbc/hbc_content1.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* 4 | This file was autogenerated by raw2c. 5 | Visit http://www.devkitpro.org 6 | */ 7 | 8 | /* -------------------------------------------------------------------------------- */ 9 | #ifndef __HBC_CONTENT1_H__ 10 | #define __HBC_CONTENT1_H__ 11 | /* -------------------------------------------------------------------------------- */ 12 | 13 | extern const unsigned char hbc_content1[]; 14 | extern const int hbc_content1_size; 15 | 16 | /* -------------------------------------------------------------------------------- */ 17 | #endif /* __HBC_CONTENT1_H__ */ 18 | /* -------------------------------------------------------------------------------- */ 19 | 20 | -------------------------------------------------------------------------------- /include/hbc/hbc_tik.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* 4 | This file was autogenerated by raw2c. 5 | Visit http://www.devkitpro.org 6 | */ 7 | 8 | /* -------------------------------------------------------------------------------- */ 9 | #ifndef __HBC_TIK_H__ 10 | #define __HBC_TIK_H__ 11 | /* -------------------------------------------------------------------------------- */ 12 | 13 | extern const unsigned char hbc_tik[]; 14 | extern const int hbc_tik_size; 15 | 16 | /* -------------------------------------------------------------------------------- */ 17 | #endif /* __HBC_TIK_H__ */ 18 | /* -------------------------------------------------------------------------------- */ 19 | 20 | -------------------------------------------------------------------------------- /include/hbc/hbc_tmd.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* 4 | This file was autogenerated by raw2c. 5 | Visit http://www.devkitpro.org 6 | */ 7 | 8 | /* -------------------------------------------------------------------------------- */ 9 | #ifndef __HBC_TMD_H__ 10 | #define __HBC_TMD_H__ 11 | /* -------------------------------------------------------------------------------- */ 12 | 13 | extern const unsigned char hbc_tmd[]; 14 | extern const int hbc_tmd_size; 15 | 16 | /* -------------------------------------------------------------------------------- */ 17 | #endif /* __HBC_TMD_H__ */ 18 | /* -------------------------------------------------------------------------------- */ 19 | 20 | -------------------------------------------------------------------------------- /include/hollywood.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* 4 | mini - a Free Software replacement for the Nintendo/BroadOn IOS. 5 | Hollywood register definitions 6 | 7 | Copyright (C) 2008, 2009 Haxx Enterprises 8 | Copyright (C) 2008, 2009 Sven Peter 9 | Copyright (C) 2008, 2009 Hector Martin "marcan" 10 | Copyright (C) 2008, 2009 John Kelley 11 | 12 | # This code is licensed to you under the terms of the GNU GPL, version 2; 13 | # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 14 | */ 15 | 16 | #ifndef __HOLLYWOOD_H__ 17 | #define __HOLLYWOOD_H__ 18 | 19 | /* Hollywood Registers */ 20 | 21 | #define HW_PPC_REG_BASE 0xd000000 22 | #define HW_REG_BASE 0xd800000 23 | 24 | /* The PPC can only see the first three IPC registers */ 25 | #define HW_IPC_PPCMSG (HW_REG_BASE + 0x000) 26 | #define HW_IPC_PPCCTRL (HW_REG_BASE + 0x004) 27 | #define HW_IPC_ARMMSG (HW_REG_BASE + 0x008) 28 | #define HW_IPC_ARMCTRL (HW_REG_BASE + 0x00c) 29 | 30 | #define HW_TIMER (HW_REG_BASE + 0x010) 31 | #define HW_ALARM (HW_REG_BASE + 0x014) 32 | 33 | #define HW_PPCIRQFLAG (HW_REG_BASE + 0x030) 34 | #define HW_PPCIRQMASK (HW_REG_BASE + 0x034) 35 | 36 | #define HW_ARMIRQFLAG (HW_REG_BASE + 0x038) 37 | #define HW_ARMIRQMASK (HW_REG_BASE + 0x03c) 38 | 39 | #define HW_MEMMIRR (HW_REG_BASE + 0x060) 40 | #define HW_AHBPROT (HW_REG_BASE + 0x064) 41 | 42 | /* something to do with PPCBOOT 43 | and legacy DI it seems ?!? */ 44 | #define HW_EXICTRL (HW_REG_BASE + 0x070) 45 | #define EXICTRL_ENABLE_EXI 1 46 | 47 | /* PPC side of GPIO1 (Starlet can access this too) 48 | Output state */ 49 | #define HW_GPIO1BOUT (HW_REG_BASE + 0x0c0) 50 | 51 | /* Direction (1=output) */ 52 | #define HW_GPIO1BDIR (HW_REG_BASE + 0x0c4) 53 | 54 | /* Input state */ 55 | #define HW_GPIO1BIN (HW_REG_BASE + 0x0c8) 56 | 57 | /* Interrupt level */ 58 | #define HW_GPIO1BINTLVL (HW_REG_BASE + 0x0cc) 59 | 60 | /* Interrupt flags (write 1 to clear) */ 61 | #define HW_GPIO1BINTFLAG (HW_REG_BASE + 0x0d0) 62 | 63 | /* Interrupt propagation enable 64 | Do these interrupts go anywhere??? */ 65 | #define HW_GPIO1BINTENABLE (HW_REG_BASE + 0x0d4) 66 | 67 | /* ??? seems to be a mirror of inputs at some point... power-up state? */ 68 | #define HW_GPIO1BINMIR (HW_REG_BASE + 0x0d8) 69 | 70 | /* 0xFFFFFF by default, if cleared disables respective outputs. Top bits non-settable. */ 71 | #define HW_GPIO1ENABLE (HW_REG_BASE + 0x0dc) 72 | 73 | #define HW_GPIO1_SLOT 0x000020 74 | #define HW_GPIO1_DEBUG 0xFF0000 75 | #define HW_GPIO1_DEBUG_SH 16 76 | 77 | /* Starlet side of GPIO1 78 | Output state */ 79 | #define HW_GPIO1OUT (HW_REG_BASE + 0x0e0) 80 | 81 | /* Direction (1=output) */ 82 | #define HW_GPIO1DIR (HW_REG_BASE + 0x0e4) 83 | 84 | /* Input state */ 85 | #define HW_GPIO1IN (HW_REG_BASE + 0x0e8) 86 | 87 | /* Interrupt level */ 88 | #define HW_GPIO1INTLVL (HW_REG_BASE + 0x0ec) 89 | 90 | /* Interrupt flags (write 1 to clear) */ 91 | #define HW_GPIO1INTFLAG (HW_REG_BASE + 0x0f0) 92 | 93 | /* Interrupt propagation enable (interrupts go to main interrupt 0x800) */ 94 | #define HW_GPIO1INTENABLE (HW_REG_BASE + 0x0f4) 95 | 96 | /*??? seems to be a mirror of inputs at some point... power-up state? */ 97 | #define HW_GPIO1INMIR (HW_REG_BASE + 0x0f8) 98 | 99 | /* Owner of each GPIO bit. If 1, GPIO1B registers assume control. If 0, GPIO1 registers assume control. */ 100 | #define HW_GPIO1OWNER (HW_REG_BASE + 0x0fc) 101 | 102 | /* ???? */ 103 | #define HW_DIFLAGS (HW_REG_BASE + 0x180) 104 | #define DIFLAGS_BOOT_CODE 0x100000 105 | 106 | /* maybe a GPIO??? */ 107 | #define HW_RESETS (HW_REG_BASE + 0x194) 108 | 109 | #define HW_CLOCKS (HW_REG_BASE + 0x1b4) 110 | 111 | #define HW_GPIO2OUT (HW_REG_BASE + 0x1c8) 112 | #define HW_GPIO2DIR (HW_REG_BASE + 0x1cc) 113 | #define HW_GPIO2IN (HW_REG_BASE + 0x1d0) 114 | 115 | #define HW_OTPCMD (HW_REG_BASE + 0x1ec) 116 | #define HW_OTPDATA (HW_REG_BASE + 0x1f0) 117 | #define HW_VERSION (HW_REG_BASE + 0x214) 118 | 119 | /* NAND Registers */ 120 | 121 | #define NAND_REG_BASE 0xd010000 122 | 123 | #define NAND_CMD (NAND_REG_BASE + 0x000) 124 | #define NAND_STATUS NAND_CMD 125 | #define NAND_CONF (NAND_REG_BASE + 0x004) 126 | #define NAND_ADDR0 (NAND_REG_BASE + 0x008) 127 | #define NAND_ADDR1 (NAND_REG_BASE + 0x00c) 128 | #define NAND_DATA (NAND_REG_BASE + 0x010) 129 | #define NAND_ECC (NAND_REG_BASE + 0x014) 130 | #define NAND_UNK1 (NAND_REG_BASE + 0x018) 131 | #define NAND_UNK2 (NAND_REG_BASE + 0x01c) 132 | 133 | /* AES Registers */ 134 | 135 | #define AES_REG_BASE 0xd020000 136 | 137 | #define AES_CMD (AES_REG_BASE + 0x000) 138 | #define AES_SRC (AES_REG_BASE + 0x004) 139 | #define AES_DEST (AES_REG_BASE + 0x008) 140 | #define AES_KEY (AES_REG_BASE + 0x00c) 141 | #define AES_IV (AES_REG_BASE + 0x010) 142 | 143 | /* SHA-1 Registers */ 144 | 145 | #define SHA_REG_BASE 0xd030000 146 | 147 | #define SHA_CMD (SHA_REG_BASE + 0x000) 148 | #define SHA_SRC (SHA_REG_BASE + 0x004) 149 | #define SHA_H0 (SHA_REG_BASE + 0x008) 150 | #define SHA_H1 (SHA_REG_BASE + 0x00c) 151 | #define SHA_H2 (SHA_REG_BASE + 0x010) 152 | #define SHA_H3 (SHA_REG_BASE + 0x014) 153 | #define SHA_H4 (SHA_REG_BASE + 0x018) 154 | 155 | /* SD Host Controller Registers */ 156 | 157 | #define SDHC_REG_BASE 0xd070000 158 | 159 | /* EXI Registers */ 160 | 161 | #define EXI_REG_BASE 0xd806800 162 | #define EXI0_REG_BASE (EXI_REG_BASE+0x000) 163 | #define EXI1_REG_BASE (EXI_REG_BASE+0x014) 164 | #define EXI2_REG_BASE (EXI_REG_BASE+0x028) 165 | 166 | #define EXI0_CSR (EXI0_REG_BASE+0x000) 167 | #define EXI0_MAR (EXI0_REG_BASE+0x004) 168 | #define EXI0_LENGTH (EXI0_REG_BASE+0x008) 169 | #define EXI0_CR (EXI0_REG_BASE+0x00c) 170 | #define EXI0_DATA (EXI0_REG_BASE+0x010) 171 | 172 | #define EXI1_CSR (EXI1_REG_BASE+0x000) 173 | #define EXI1_MAR (EXI1_REG_BASE+0x004) 174 | #define EXI1_LENGTH (EXI1_REG_BASE+0x008) 175 | #define EXI1_CR (EXI1_REG_BASE+0x00c) 176 | #define EXI1_DATA (EXI1_REG_BASE+0x010) 177 | 178 | #define EXI2_CSR (EXI2_REG_BASE+0x000) 179 | #define EXI2_MAR (EXI2_REG_BASE+0x004) 180 | #define EXI2_LENGTH (EXI2_REG_BASE+0x008) 181 | #define EXI2_CR (EXI2_REG_BASE+0x00c) 182 | #define EXI2_DATA (EXI2_REG_BASE+0x010) 183 | 184 | #define EXI_BOOT_BASE (EXI_REG_BASE+0x040) 185 | 186 | /* MEMORY CONTROLLER Registers */ 187 | 188 | #define MEM_REG_BASE 0xd8b4000 189 | #define MEM_PROT (MEM_REG_BASE+0x20a) 190 | #define MEM_PROT_START (MEM_REG_BASE+0x20c) 191 | #define MEM_PROT_END (MEM_REG_BASE+0x20e) 192 | #define MEM_FLUSHREQ (MEM_REG_BASE+0x228) 193 | #define MEM_FLUSHACK (MEM_REG_BASE+0x22a) 194 | 195 | #endif /* __HOLLYWOOD_H__ */ 196 | 197 | -------------------------------------------------------------------------------- /include/installer.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __INSTALLER_H__ 4 | #define __INSTALLER_H__ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /* [nitr8]: Added */ 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | void SDBootInstaller(void); 21 | void NANDBootInstaller(void); 22 | void Boot2WADInstaller(void); 23 | void Boot2BackupInstaller(void); 24 | void Boot2BackupMake(void); 25 | 26 | /* [nitr8]: get rid of warning for implicit declaration of function */ 27 | void HBCInstaller(void); 28 | 29 | /* [nitr8]: get rid of warning for implicit declaration of function */ 30 | void BootSysCheck(void); 31 | 32 | /* [nitr8]: get rid of warning for implicit declaration of function */ 33 | void RestoreNAND(void); 34 | 35 | /* [nitr8]: get rid of warning for implicit declaration of function */ 36 | void EraseNANDFS(void); 37 | 38 | /* [nitr8]: Added */ 39 | #ifdef __cplusplus 40 | } 41 | #endif /* __cplusplus */ 42 | 43 | #endif /* __INSTALLER_H__ */ 44 | 45 | -------------------------------------------------------------------------------- /include/jpeglib/jchuff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * jchuff.h 3 | * 4 | * Copyright (C) 1991-1997, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains declarations for Huffman entropy encoding routines 9 | * that are shared between the sequential encoder (jchuff.c) and the 10 | * progressive encoder (jcphuff.c). No other modules need to see these. 11 | */ 12 | 13 | /* The legal range of a DCT coefficient is 14 | * -1024 .. +1023 for 8-bit data; 15 | * -16384 .. +16383 for 12-bit data. 16 | * Hence the magnitude should always fit in 10 or 14 bits respectively. 17 | */ 18 | 19 | #if BITS_IN_JSAMPLE == 8 20 | #define MAX_COEF_BITS 10 21 | #else 22 | #define MAX_COEF_BITS 14 23 | #endif 24 | 25 | /* Derived data constructed for each Huffman table */ 26 | 27 | typedef struct { 28 | unsigned int ehufco[256]; /* code for each symbol */ 29 | char ehufsi[256]; /* length of code for each symbol */ 30 | /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */ 31 | } c_derived_tbl; 32 | 33 | /* Short forms of external names for systems with brain-damaged linkers. */ 34 | 35 | #ifdef NEED_SHORT_EXTERNAL_NAMES 36 | #define jpeg_make_c_derived_tbl jMkCDerived 37 | #define jpeg_gen_optimal_table jGenOptTbl 38 | #endif /* NEED_SHORT_EXTERNAL_NAMES */ 39 | 40 | /* Expand a Huffman table definition into the derived format */ 41 | EXTERN(void) jpeg_make_c_derived_tbl 42 | JPP((j_compress_ptr cinfo, boolean isDC, int tblno, 43 | c_derived_tbl ** pdtbl)); 44 | 45 | /* Generate an optimal table definition given the specified counts */ 46 | EXTERN(void) jpeg_gen_optimal_table 47 | JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])); 48 | -------------------------------------------------------------------------------- /include/jpeglib/jconfig.h: -------------------------------------------------------------------------------- 1 | /* jconfig.h. Generated automatically by configure. */ 2 | /* jconfig.cfg --- source file edited by configure script */ 3 | /* see jconfig.doc for explanations */ 4 | 5 | #define HAVE_PROTOTYPES 6 | #define HAVE_UNSIGNED_CHAR 7 | #define HAVE_UNSIGNED_SHORT 8 | #undef void 9 | #undef const 10 | #undef CHAR_IS_UNSIGNED 11 | //#define HAVE_STDDEF_H 12 | //#define HAVE_STDLIB_H 13 | #ifdef HAVE_CONFIG_H 14 | #include 15 | #endif 16 | #undef NEED_BSD_STRINGS 17 | #undef NEED_SYS_TYPES_H 18 | #undef NEED_FAR_POINTERS 19 | #undef NEED_SHORT_EXTERNAL_NAMES 20 | /* Define this if you get warnings about undefined structures. */ 21 | #undef INCOMPLETE_TYPES_BROKEN 22 | 23 | #ifdef JPEG_INTERNALS 24 | 25 | #undef RIGHT_SHIFT_IS_UNSIGNED 26 | #define INLINE __inline 27 | /* These are for configuring the JPEG memory manager. */ 28 | #undef DEFAULT_MAX_MEM 29 | #undef NO_MKTEMP 30 | 31 | #endif /* JPEG_INTERNALS */ 32 | 33 | #ifdef JPEG_CJPEG_DJPEG 34 | 35 | #define BMP_SUPPORTED /* BMP image file format */ 36 | #define GIF_SUPPORTED /* GIF image file format */ 37 | #define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */ 38 | #undef RLE_SUPPORTED /* Utah RLE image file format */ 39 | #define TARGA_SUPPORTED /* Targa image file format */ 40 | 41 | #undef TWO_FILE_COMMANDLINE 42 | #undef NEED_SIGNAL_CATCHER 43 | #undef DONT_USE_B_MODE 44 | 45 | /* Define this if you want percent-done progress reports from cjpeg/djpeg. */ 46 | #undef PROGRESS_REPORT 47 | 48 | #endif /* JPEG_CJPEG_DJPEG */ 49 | -------------------------------------------------------------------------------- /include/jpeglib/jdct.h: -------------------------------------------------------------------------------- 1 | /* 2 | * jdct.h 3 | * 4 | * Copyright (C) 1994-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This include file contains common declarations for the forward and 9 | * inverse DCT modules. These declarations are private to the DCT managers 10 | * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms. 11 | * The individual DCT algorithms are kept in separate files to ease 12 | * machine-dependent tuning (e.g., assembly coding). 13 | */ 14 | 15 | 16 | /* 17 | * A forward DCT routine is given a pointer to a work area of type DCTELEM[]; 18 | * the DCT is to be performed in-place in that buffer. Type DCTELEM is int 19 | * for 8-bit samples, INT32 for 12-bit samples. (NOTE: Floating-point DCT 20 | * implementations use an array of type FAST_FLOAT, instead.) 21 | * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE). 22 | * The DCT outputs are returned scaled up by a factor of 8; they therefore 23 | * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This 24 | * convention improves accuracy in integer implementations and saves some 25 | * work in floating-point ones. 26 | * Quantization of the output coefficients is done by jcdctmgr.c. 27 | */ 28 | 29 | #if BITS_IN_JSAMPLE == 8 30 | typedef int DCTELEM; /* 16 or 32 bits is fine */ 31 | #else 32 | typedef INT32 DCTELEM; /* must have 32 bits */ 33 | #endif 34 | 35 | typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data)); 36 | typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data)); 37 | 38 | 39 | /* 40 | * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer 41 | * to an output sample array. The routine must dequantize the input data as 42 | * well as perform the IDCT; for dequantization, it uses the multiplier table 43 | * pointed to by compptr->dct_table. The output data is to be placed into the 44 | * sample array starting at a specified column. (Any row offset needed will 45 | * be applied to the array pointer before it is passed to the IDCT code.) 46 | * Note that the number of samples emitted by the IDCT routine is 47 | * DCT_scaled_size * DCT_scaled_size. 48 | */ 49 | 50 | /* typedef inverse_DCT_method_ptr is declared in jpegint.h */ 51 | 52 | /* 53 | * Each IDCT routine has its own ideas about the best dct_table element type. 54 | */ 55 | 56 | typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */ 57 | #if BITS_IN_JSAMPLE == 8 58 | typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */ 59 | #define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */ 60 | #else 61 | typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */ 62 | #define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */ 63 | #endif 64 | typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */ 65 | 66 | 67 | /* 68 | * Each IDCT routine is responsible for range-limiting its results and 69 | * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could 70 | * be quite far out of range if the input data is corrupt, so a bulletproof 71 | * range-limiting step is required. We use a mask-and-table-lookup method 72 | * to do the combined operations quickly. See the comments with 73 | * prepare_range_limit_table (in jdmaster.c) for more info. 74 | */ 75 | 76 | #define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE) 77 | 78 | #define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */ 79 | 80 | 81 | /* Short forms of external names for systems with brain-damaged linkers. */ 82 | 83 | #ifdef NEED_SHORT_EXTERNAL_NAMES 84 | #define jpeg_fdct_islow jFDislow 85 | #define jpeg_fdct_ifast jFDifast 86 | #define jpeg_fdct_float jFDfloat 87 | #define jpeg_idct_islow jRDislow 88 | #define jpeg_idct_ifast jRDifast 89 | #define jpeg_idct_float jRDfloat 90 | #define jpeg_idct_4x4 jRD4x4 91 | #define jpeg_idct_2x2 jRD2x2 92 | #define jpeg_idct_1x1 jRD1x1 93 | #endif /* NEED_SHORT_EXTERNAL_NAMES */ 94 | 95 | /* Extern declarations for the forward and inverse DCT routines. */ 96 | 97 | EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data)); 98 | EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data)); 99 | EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data)); 100 | 101 | EXTERN(void) jpeg_idct_islow 102 | JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, 103 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); 104 | EXTERN(void) jpeg_idct_ifast 105 | JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, 106 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); 107 | EXTERN(void) jpeg_idct_float 108 | JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, 109 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); 110 | EXTERN(void) jpeg_idct_4x4 111 | JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, 112 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); 113 | EXTERN(void) jpeg_idct_2x2 114 | JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, 115 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); 116 | EXTERN(void) jpeg_idct_1x1 117 | JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr, 118 | JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)); 119 | 120 | 121 | /* 122 | * Macros for handling fixed-point arithmetic; these are used by many 123 | * but not all of the DCT/IDCT modules. 124 | * 125 | * All values are expected to be of type INT32. 126 | * Fractional constants are scaled left by CONST_BITS bits. 127 | * CONST_BITS is defined within each module using these macros, 128 | * and may differ from one module to the next. 129 | */ 130 | 131 | #define ONE ((INT32) 1) 132 | #define CONST_SCALE (ONE << CONST_BITS) 133 | 134 | /* Convert a positive real constant to an integer scaled by CONST_SCALE. 135 | * Caution: some C compilers fail to reduce "FIX(constant)" at compile time, 136 | * thus causing a lot of useless floating-point operations at run time. 137 | */ 138 | 139 | #define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5)) 140 | 141 | /* Descale and correctly round an INT32 value that's scaled by N bits. 142 | * We assume RIGHT_SHIFT rounds towards minus infinity, so adding 143 | * the fudge factor is correct for either sign of X. 144 | */ 145 | 146 | #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n) 147 | 148 | /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. 149 | * This macro is used only when the two inputs will actually be no more than 150 | * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a 151 | * full 32x32 multiply. This provides a useful speedup on many machines. 152 | * Unfortunately there is no way to specify a 16x16->32 multiply portably 153 | * in C, but some C compilers will do the right thing if you provide the 154 | * correct combination of casts. 155 | */ 156 | 157 | #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ 158 | #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const))) 159 | #endif 160 | #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */ 161 | #define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const))) 162 | #endif 163 | 164 | #ifndef MULTIPLY16C16 /* default definition */ 165 | #define MULTIPLY16C16(var,const) ((var) * (const)) 166 | #endif 167 | 168 | /* Same except both inputs are variables. */ 169 | 170 | #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */ 171 | #define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2))) 172 | #endif 173 | 174 | #ifndef MULTIPLY16V16 /* default definition */ 175 | #define MULTIPLY16V16(var1,var2) ((var1) * (var2)) 176 | #endif 177 | -------------------------------------------------------------------------------- /include/jpeglib/jdhuff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * jdhuff.h 3 | * 4 | * Copyright (C) 1991-1997, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains declarations for Huffman entropy decoding routines 9 | * that are shared between the sequential decoder (jdhuff.c) and the 10 | * progressive decoder (jdphuff.c). No other modules need to see these. 11 | */ 12 | 13 | /* Short forms of external names for systems with brain-damaged linkers. */ 14 | 15 | #ifdef NEED_SHORT_EXTERNAL_NAMES 16 | #define jpeg_make_d_derived_tbl jMkDDerived 17 | #define jpeg_fill_bit_buffer jFilBitBuf 18 | #define jpeg_huff_decode jHufDecode 19 | #endif /* NEED_SHORT_EXTERNAL_NAMES */ 20 | 21 | 22 | /* Derived data constructed for each Huffman table */ 23 | 24 | #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */ 25 | 26 | typedef struct { 27 | /* Basic tables: (element [0] of each array is unused) */ 28 | INT32 maxcode[18]; /* largest code of length k (-1 if none) */ 29 | /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ 30 | INT32 valoffset[17]; /* huffval[] offset for codes of length k */ 31 | /* valoffset[k] = huffval[] index of 1st symbol of code length k, less 32 | * the smallest code of length k; so given a code of length k, the 33 | * corresponding symbol is huffval[code + valoffset[k]] 34 | */ 35 | 36 | /* Link to public Huffman table (needed only in jpeg_huff_decode) */ 37 | JHUFF_TBL *pub; 38 | 39 | /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of 40 | * the input data stream. If the next Huffman code is no more 41 | * than HUFF_LOOKAHEAD bits long, we can obtain its length and 42 | * the corresponding symbol directly from these tables. 43 | */ 44 | int look_nbits[1< 32 bits on your machine, and shifting/masking longs is 76 | * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE 77 | * appropriately should be a win. Unfortunately we can't define the size 78 | * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) 79 | * because not all machines measure sizeof in 8-bit bytes. 80 | */ 81 | 82 | typedef struct { /* Bitreading state saved across MCUs */ 83 | bit_buf_type get_buffer; /* current bit-extraction buffer */ 84 | int bits_left; /* # of unused bits in it */ 85 | } bitread_perm_state; 86 | 87 | typedef struct { /* Bitreading working state within an MCU */ 88 | /* Current data source location */ 89 | /* We need a copy, rather than munging the original, in case of suspension */ 90 | const JOCTET * next_input_byte; /* => next byte to read from source */ 91 | size_t bytes_in_buffer; /* # of bytes remaining in source buffer */ 92 | /* Bit input buffer --- note these values are kept in register variables, 93 | * not in this struct, inside the inner loops. 94 | */ 95 | bit_buf_type get_buffer; /* current bit-extraction buffer */ 96 | int bits_left; /* # of unused bits in it */ 97 | /* Pointer needed by jpeg_fill_bit_buffer. */ 98 | j_decompress_ptr cinfo; /* back link to decompress master record */ 99 | } bitread_working_state; 100 | 101 | /* Macros to declare and load/save bitread local variables. */ 102 | #define BITREAD_STATE_VARS \ 103 | register bit_buf_type get_buffer; \ 104 | register int bits_left; \ 105 | bitread_working_state br_state 106 | 107 | #define BITREAD_LOAD_STATE(cinfop,permstate) \ 108 | br_state.cinfo = cinfop; \ 109 | br_state.next_input_byte = cinfop->src->next_input_byte; \ 110 | br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \ 111 | get_buffer = permstate.get_buffer; \ 112 | bits_left = permstate.bits_left; 113 | 114 | #define BITREAD_SAVE_STATE(cinfop,permstate) \ 115 | cinfop->src->next_input_byte = br_state.next_input_byte; \ 116 | cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \ 117 | permstate.get_buffer = get_buffer; \ 118 | permstate.bits_left = bits_left 119 | 120 | /* 121 | * These macros provide the in-line portion of bit fetching. 122 | * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer 123 | * before using GET_BITS, PEEK_BITS, or DROP_BITS. 124 | * The variables get_buffer and bits_left are assumed to be locals, 125 | * but the state struct might not be (jpeg_huff_decode needs this). 126 | * CHECK_BIT_BUFFER(state,n,action); 127 | * Ensure there are N bits in get_buffer; if suspend, take action. 128 | * val = GET_BITS(n); 129 | * Fetch next N bits. 130 | * val = PEEK_BITS(n); 131 | * Fetch next N bits without removing them from the buffer. 132 | * DROP_BITS(n); 133 | * Discard next N bits. 134 | * The value N should be a simple variable, not an expression, because it 135 | * is evaluated multiple times. 136 | */ 137 | 138 | #define CHECK_BIT_BUFFER(state,nbits,action) \ 139 | { if (bits_left < (nbits)) { \ 140 | if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \ 141 | { action; } \ 142 | get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } 143 | 144 | #define GET_BITS(nbits) \ 145 | (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1)) 146 | 147 | #define PEEK_BITS(nbits) \ 148 | (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1)) 149 | 150 | #define DROP_BITS(nbits) \ 151 | (bits_left -= (nbits)) 152 | 153 | /* Load up the bit buffer to a depth of at least nbits */ 154 | EXTERN(boolean) jpeg_fill_bit_buffer 155 | JPP((bitread_working_state * state, register bit_buf_type get_buffer, 156 | register int bits_left, int nbits)); 157 | 158 | 159 | /* 160 | * Code for extracting next Huffman-coded symbol from input bit stream. 161 | * Again, this is time-critical and we make the main paths be macros. 162 | * 163 | * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits 164 | * without looping. Usually, more than 95% of the Huffman codes will be 8 165 | * or fewer bits long. The few overlength codes are handled with a loop, 166 | * which need not be inline code. 167 | * 168 | * Notes about the HUFF_DECODE macro: 169 | * 1. Near the end of the data segment, we may fail to get enough bits 170 | * for a lookahead. In that case, we do it the hard way. 171 | * 2. If the lookahead table contains no entry, the next code must be 172 | * more than HUFF_LOOKAHEAD bits long. 173 | * 3. jpeg_huff_decode returns -1 if forced to suspend. 174 | */ 175 | 176 | #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ 177 | { register int nb, look; \ 178 | if (bits_left < HUFF_LOOKAHEAD) { \ 179 | if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ 180 | get_buffer = state.get_buffer; bits_left = state.bits_left; \ 181 | if (bits_left < HUFF_LOOKAHEAD) { \ 182 | nb = 1; goto slowlabel; \ 183 | } \ 184 | } \ 185 | look = PEEK_BITS(HUFF_LOOKAHEAD); \ 186 | if ((nb = htbl->look_nbits[look]) != 0) { \ 187 | DROP_BITS(nb); \ 188 | result = htbl->look_sym[look]; \ 189 | } else { \ 190 | nb = HUFF_LOOKAHEAD+1; \ 191 | slowlabel: \ 192 | if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \ 193 | { failaction; } \ 194 | get_buffer = state.get_buffer; bits_left = state.bits_left; \ 195 | } \ 196 | } 197 | 198 | /* Out-of-line case for Huffman code fetching */ 199 | EXTERN(int) jpeg_huff_decode 200 | JPP((bitread_working_state * state, register bit_buf_type get_buffer, 201 | register int bits_left, d_derived_tbl * htbl, int min_bits)); 202 | -------------------------------------------------------------------------------- /include/jpeglib/jinclude.h: -------------------------------------------------------------------------------- 1 | /* 2 | * jinclude.h 3 | * 4 | * Copyright (C) 1991-1994, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file exists to provide a single place to fix any problems with 9 | * including the wrong system include files. (Common problems are taken 10 | * care of by the standard jconfig symbols, but on really weird systems 11 | * you may have to edit this file.) 12 | * 13 | * NOTE: this file is NOT intended to be included by applications using the 14 | * JPEG library. Most applications need only include jpeglib.h. 15 | */ 16 | 17 | 18 | /* Include auto-config file to find out which system include files we need. */ 19 | 20 | #include "jconfig.h" /* auto configuration options */ 21 | #define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */ 22 | 23 | /* 24 | * We need the NULL macro and size_t typedef. 25 | * On an ANSI-conforming system it is sufficient to include . 26 | * Otherwise, we get them from or ; we may have to 27 | * pull in as well. 28 | * Note that the core JPEG library does not require ; 29 | * only the default error handler and data source/destination modules do. 30 | * But we must pull it in because of the references to FILE in jpeglib.h. 31 | * You can remove those references if you want to compile without . 32 | */ 33 | 34 | #ifdef HAVE_STDDEF_H 35 | #include 36 | #endif 37 | 38 | #ifdef HAVE_STDLIB_H 39 | #include 40 | #endif 41 | 42 | #ifdef NEED_SYS_TYPES_H 43 | #include 44 | #endif 45 | 46 | #include 47 | 48 | /* 49 | * We need memory copying and zeroing functions, plus strncpy(). 50 | * ANSI and System V implementations declare these in . 51 | * BSD doesn't have the mem() functions, but it does have bcopy()/bzero(). 52 | * Some systems may declare memset and memcpy in . 53 | * 54 | * NOTE: we assume the size parameters to these functions are of type size_t. 55 | * Change the casts in these macros if not! 56 | */ 57 | 58 | #ifdef NEED_BSD_STRINGS 59 | 60 | #include 61 | #define MEMZERO(target,size) bzero((void *)(target), (size_t)(size)) 62 | #define MEMCOPY(dest,src,size) bcopy((const void *)(src), (void *)(dest), (size_t)(size)) 63 | 64 | #else /* not BSD, assume ANSI/SysV string lib */ 65 | 66 | #include 67 | #define MEMZERO(target,size) memset((void *)(target), 0, (size_t)(size)) 68 | #define MEMCOPY(dest,src,size) memcpy((void *)(dest), (const void *)(src), (size_t)(size)) 69 | 70 | #endif 71 | 72 | /* 73 | * In ANSI C, and indeed any rational implementation, size_t is also the 74 | * type returned by sizeof(). However, it seems there are some irrational 75 | * implementations out there, in which sizeof() returns an int even though 76 | * size_t is defined as long or unsigned long. To ensure consistent results 77 | * we always use this SIZEOF() macro in place of using sizeof() directly. 78 | */ 79 | 80 | #define SIZEOF(object) ((size_t) sizeof(object)) 81 | 82 | /* 83 | * The modules that use fread() and fwrite() always invoke them through 84 | * these macros. On some systems you may need to twiddle the argument casts. 85 | * CAUTION: argument order is different from underlying functions! 86 | */ 87 | 88 | #define JFREAD(file,buf,sizeofbuf) \ 89 | ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) 90 | #define JFWRITE(file,buf,sizeofbuf) \ 91 | ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) 92 | -------------------------------------------------------------------------------- /include/jpeglib/jmemsys.h: -------------------------------------------------------------------------------- 1 | /* 2 | * jmemsys.h 3 | * 4 | * Copyright (C) 1992-1997, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This include file defines the interface between the system-independent 9 | * and system-dependent portions of the JPEG memory manager. No other 10 | * modules need include it. (The system-independent portion is jmemmgr.c; 11 | * there are several different versions of the system-dependent portion.) 12 | * 13 | * This file works as-is for the system-dependent memory managers supplied 14 | * in the IJG distribution. You may need to modify it if you write a 15 | * custom memory manager. If system-dependent changes are needed in 16 | * this file, the best method is to #ifdef them based on a configuration 17 | * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR 18 | * and USE_MAC_MEMMGR. 19 | */ 20 | 21 | 22 | /* Short forms of external names for systems with brain-damaged linkers. */ 23 | 24 | #ifdef NEED_SHORT_EXTERNAL_NAMES 25 | #define jpeg_get_small jGetSmall 26 | #define jpeg_free_small jFreeSmall 27 | #define jpeg_get_large jGetLarge 28 | #define jpeg_free_large jFreeLarge 29 | #define jpeg_mem_available jMemAvail 30 | #define jpeg_open_backing_store jOpenBackStore 31 | #define jpeg_mem_init jMemInit 32 | #define jpeg_mem_term jMemTerm 33 | #endif /* NEED_SHORT_EXTERNAL_NAMES */ 34 | 35 | 36 | /* 37 | * These two functions are used to allocate and release small chunks of 38 | * memory. (Typically the total amount requested through jpeg_get_small is 39 | * no more than 20K or so; this will be requested in chunks of a few K each.) 40 | * Behavior should be the same as for the standard library functions malloc 41 | * and free; in particular, jpeg_get_small must return NULL on failure. 42 | * On most systems, these ARE malloc and free. jpeg_free_small is passed the 43 | * size of the object being freed, just in case it's needed. 44 | * On an 80x86 machine using small-data memory model, these manage near heap. 45 | */ 46 | 47 | EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject)); 48 | EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object, 49 | size_t sizeofobject)); 50 | 51 | /* 52 | * These two functions are used to allocate and release large chunks of 53 | * memory (up to the total free space designated by jpeg_mem_available). 54 | * The interface is the same as above, except that on an 80x86 machine, 55 | * far pointers are used. On most other machines these are identical to 56 | * the jpeg_get/free_small routines; but we keep them separate anyway, 57 | * in case a different allocation strategy is desirable for large chunks. 58 | */ 59 | 60 | EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo, 61 | size_t sizeofobject)); 62 | EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object, 63 | size_t sizeofobject)); 64 | 65 | /* 66 | * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may 67 | * be requested in a single call to jpeg_get_large (and jpeg_get_small for that 68 | * matter, but that case should never come into play). This macro is needed 69 | * to model the 64Kb-segment-size limit of far addressing on 80x86 machines. 70 | * On those machines, we expect that jconfig.h will provide a proper value. 71 | * On machines with 32-bit flat address spaces, any large constant may be used. 72 | * 73 | * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type 74 | * size_t and will be a multiple of sizeof(align_type). 75 | */ 76 | 77 | #ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */ 78 | #define MAX_ALLOC_CHUNK 1000000000L 79 | #endif 80 | 81 | /* 82 | * This routine computes the total space still available for allocation by 83 | * jpeg_get_large. If more space than this is needed, backing store will be 84 | * used. NOTE: any memory already allocated must not be counted. 85 | * 86 | * There is a minimum space requirement, corresponding to the minimum 87 | * feasible buffer sizes; jmemmgr.c will request that much space even if 88 | * jpeg_mem_available returns zero. The maximum space needed, enough to hold 89 | * all working storage in memory, is also passed in case it is useful. 90 | * Finally, the total space already allocated is passed. If no better 91 | * method is available, cinfo->mem->max_memory_to_use - already_allocated 92 | * is often a suitable calculation. 93 | * 94 | * It is OK for jpeg_mem_available to underestimate the space available 95 | * (that'll just lead to more backing-store access than is really necessary). 96 | * However, an overestimate will lead to failure. Hence it's wise to subtract 97 | * a slop factor from the true available space. 5% should be enough. 98 | * 99 | * On machines with lots of virtual memory, any large constant may be returned. 100 | * Conversely, zero may be returned to always use the minimum amount of memory. 101 | */ 102 | 103 | EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo, 104 | long min_bytes_needed, 105 | long max_bytes_needed, 106 | long already_allocated)); 107 | 108 | 109 | /* 110 | * This structure holds whatever state is needed to access a single 111 | * backing-store object. The read/write/close method pointers are called 112 | * by jmemmgr.c to manipulate the backing-store object; all other fields 113 | * are private to the system-dependent backing store routines. 114 | */ 115 | 116 | #define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */ 117 | 118 | 119 | #ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */ 120 | 121 | typedef unsigned short XMSH; /* type of extended-memory handles */ 122 | typedef unsigned short EMSH; /* type of expanded-memory handles */ 123 | 124 | typedef union { 125 | short file_handle; /* DOS file handle if it's a temp file */ 126 | XMSH xms_handle; /* handle if it's a chunk of XMS */ 127 | EMSH ems_handle; /* handle if it's a chunk of EMS */ 128 | } handle_union; 129 | 130 | #endif /* USE_MSDOS_MEMMGR */ 131 | 132 | #ifdef USE_MAC_MEMMGR /* Mac-specific junk */ 133 | #include 134 | #endif /* USE_MAC_MEMMGR */ 135 | 136 | 137 | typedef struct backing_store_struct * backing_store_ptr; 138 | 139 | typedef struct backing_store_struct { 140 | /* Methods for reading/writing/closing this backing-store object */ 141 | JMETHOD(void, read_backing_store, (j_common_ptr cinfo, 142 | backing_store_ptr info, 143 | void FAR * buffer_address, 144 | long file_offset, long byte_count)); 145 | JMETHOD(void, write_backing_store, (j_common_ptr cinfo, 146 | backing_store_ptr info, 147 | void FAR * buffer_address, 148 | long file_offset, long byte_count)); 149 | JMETHOD(void, close_backing_store, (j_common_ptr cinfo, 150 | backing_store_ptr info)); 151 | 152 | /* Private fields for system-dependent backing-store management */ 153 | #ifdef USE_MSDOS_MEMMGR 154 | /* For the MS-DOS manager (jmemdos.c), we need: */ 155 | handle_union handle; /* reference to backing-store storage object */ 156 | char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ 157 | #else 158 | #ifdef USE_MAC_MEMMGR 159 | /* For the Mac manager (jmemmac.c), we need: */ 160 | short temp_file; /* file reference number to temp file */ 161 | FSSpec tempSpec; /* the FSSpec for the temp file */ 162 | char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */ 163 | #else 164 | /* For a typical implementation with temp files, we need: */ 165 | FILE * temp_file; /* stdio reference to temp file */ 166 | char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */ 167 | #endif 168 | #endif 169 | } backing_store_info; 170 | 171 | 172 | /* 173 | * Initial opening of a backing-store object. This must fill in the 174 | * read/write/close pointers in the object. The read/write routines 175 | * may take an error exit if the specified maximum file size is exceeded. 176 | * (If jpeg_mem_available always returns a large value, this routine can 177 | * just take an error exit.) 178 | */ 179 | 180 | EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo, 181 | backing_store_ptr info, 182 | long total_bytes_needed)); 183 | 184 | 185 | /* 186 | * These routines take care of any system-dependent initialization and 187 | * cleanup required. jpeg_mem_init will be called before anything is 188 | * allocated (and, therefore, nothing in cinfo is of use except the error 189 | * manager pointer). It should return a suitable default value for 190 | * max_memory_to_use; this may subsequently be overridden by the surrounding 191 | * application. (Note that max_memory_to_use is only important if 192 | * jpeg_mem_available chooses to consult it ... no one else will.) 193 | * jpeg_mem_term may assume that all requested memory has been freed and that 194 | * all opened backing-store objects have been closed. 195 | */ 196 | 197 | EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo)); 198 | EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo)); 199 | -------------------------------------------------------------------------------- /include/jpeglib/jpgogc.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * libjpeg - 6b wrapper 3 | * 4 | * The version of libjpeg used in libOGC has been modified to include a memory 5 | * source data manager (jmemsrc.c). 6 | * 7 | * softdev November 2006 8 | ****************************************************************************/ 9 | #ifndef __JPGLIB__ 10 | #define __JPGLIB__ 11 | 12 | #include "jpeglib.h" 13 | 14 | typedef struct { 15 | char *inbuffer; 16 | char *outbuffer; 17 | int inbufferlength; 18 | int outbufferlength; 19 | int width; 20 | int height; 21 | int num_colours; 22 | int dct_method; 23 | int dither_mode; 24 | int greyscale; 25 | } JPEGIMG; 26 | 27 | int JPEG_Decompress(JPEGIMG * jpgimg); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /include/jpeglib/jversion.h: -------------------------------------------------------------------------------- 1 | /* 2 | * jversion.h 3 | * 4 | * Copyright (C) 1991-1998, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains software version identification. 9 | */ 10 | 11 | 12 | #define JVERSION "6b 27-Mar-1998" 13 | 14 | #define JCOPYRIGHT "Copyright (C) 1998, Thomas G. Lane" 15 | -------------------------------------------------------------------------------- /include/menu.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __MENU_H__ 4 | #define __MENU_H__ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define CURSOR "-> " 16 | 17 | #define DOWN 0 18 | #define UP 1 19 | 20 | /* [nitr8]: Extended */ 21 | typedef enum { 22 | MenuStr_InstallSDBoot = 0, 23 | MenuStr_InstallNANDBoot, 24 | MenuStr_InstallHBC, 25 | MenuStr_InstallBoot2WAD, 26 | MenuStr_InstallBoot2Backup, 27 | MenuStr_MakeBoot2Backup, 28 | MenuStr_BootSysCheck, 29 | MenuStr_RestoreNANDBackup, 30 | MenuStr_Credits, 31 | MenuStr_Exit, 32 | 33 | /* [nitr8]: Added (this is a SUB-menu) */ 34 | MenuStr_DoDebugMenu, 35 | 36 | MenuStr_Count /* Number of values supported by this enum. */ 37 | } MenuStr; 38 | 39 | #if 0 40 | typedef enum { 41 | DebugMenuStr_EraseNANDFS = MenuStr_Count, 42 | DebugMenuStr_Count /* Number of values supported by this enum. */ 43 | } DebugMenuStr; 44 | #endif 45 | 46 | /* [nitr8]: Added */ 47 | typedef enum { 48 | DebugMenuStr_BackupSEEPROMBoot2Info = 0, 49 | DebugMenuStr_RestoreSEEPROMBoot2Info, 50 | DebugMenuStr_CompareSEEPROMBoot2Info, 51 | DebugMenuStr_ClearSEEPROMBoot2Info, 52 | DebugMenuStr_TestNANDBlockmaps, 53 | DebugMenuStr_EraseNANDFS, 54 | DebugMenuStr_Count 55 | } debugMenuStr; 56 | 57 | /* [nitr8]: get rid of warnings when arrays are not used at all (MOVED) 58 | static const char *menuStrings[MenuStr_Count] = { 59 | "Install SDBoot", 60 | "Install NANDBoot", 61 | "Install HBC", 62 | "Install a boot2 WAD", 63 | "Restore a boot2 backup", 64 | "Perform a boot2 backup", 65 | "Boot SysCheck", 66 | "Restore a NAND backup", 67 | "View Credits", 68 | "Exit to HBC" 69 | }; 70 | 71 | [nitr8]: get rid of warnings when arrays are not used at all 72 | static const char *debugMenuStrings[DebugMenuStr_Count] = { 73 | "Erase NAND blocks 8-4095" 74 | }; 75 | 76 | [nitr8]: get rid of warnings when variables are not used at all (MOVED) 77 | static bool enableDebugMenu = false; 78 | */ 79 | 80 | /* [nitr8]: Added */ 81 | #ifdef __cplusplus 82 | extern "C" { 83 | #endif 84 | 85 | void ClearScreen(void); 86 | 87 | /* [nitr8]: Make static */ 88 | /* void EnterOption(void); */ 89 | 90 | /* [nitr8]: Make static */ 91 | /* void PrintCursor(void); */ 92 | 93 | /* [nitr8]: Make static */ 94 | /* void Move(u8 direction); */ 95 | 96 | /* [nitr8]: Add sub-menu support */ 97 | /* void PrintMenu(void); */ 98 | 99 | /* [nitr8]: Make static */ 100 | /* void PrintMenu(int which); */ 101 | 102 | u8 EnterMenu(bool enableDebug); 103 | 104 | /* [nitr8]: Added */ 105 | #ifdef __cplusplus 106 | } 107 | #endif /* __cplusplus */ 108 | 109 | #endif /* __MENU_H__ */ 110 | 111 | -------------------------------------------------------------------------------- /include/prodinfo.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef _PRODINFO_H__ 4 | #define _PRODINFO_H__ 5 | 6 | /* [nitr8]: Added */ 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | extern u8 IsMini(void); 12 | 13 | /* [nitr8]: Added */ 14 | #ifdef __cplusplus 15 | } 16 | #endif /* __cplusplus */ 17 | 18 | #endif /* __PRODINFO_H__ */ 19 | 20 | -------------------------------------------------------------------------------- /include/runtimeiospatch.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* This program is free software: you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation, version 2.0. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License 2.0 for more details. 11 | 12 | Copyright (C) 2010 Joseph Jordan 13 | Copyright (C) 2012-2013 damysteryman 14 | Copyright (C) 2012-2015 Christopher Bratusek 15 | Copyright (C) 2013 DarkMatterCore 16 | Copyright (C) 2014 megazig 17 | Copyright (C) 2015 FIX94 */ 18 | 19 | /** 20 | * NOTE: this library has been modified to only include the patches necessary 21 | * for the boot2 installer. 22 | */ 23 | 24 | #ifndef __RUNTIMEIOSPATCH_H__ 25 | #define __RUNTIMEIOSPATCH_H__ 26 | 27 | /** 28 | * Version information for Libruntimeiospatch. 29 | */ 30 | #define LIB_RUNTIMEIOSPATCH_VERSION "1.5.4" 31 | 32 | /*============================================================================== 33 | HW_RVL header 34 | ============================================================================== */ 35 | #if defined(HW_RVL) /* defined(HW_RVL) */ 36 | 37 | /** 38 | *Returns true when HW_AHBPROT access can be applied 39 | */ 40 | #define AHBPROT_DISABLED (*(vu32*)0xcd800064 == 0xFFFFFFFF) 41 | 42 | /*============================================================================== 43 | Error code definitions: 44 | ============================================================================== */ 45 | #define ERROR_AHBPROT -5 46 | #define ERROR_PATCH -7 47 | 48 | /*============================================================================== 49 | C++ header 50 | ============================================================================== */ 51 | #ifdef __cplusplus 52 | extern "C" { 53 | #endif 54 | /* __cplusplus */ 55 | 56 | /*============================================================================== 57 | Patchsets: 58 | ============================================================================== */ 59 | /* 60 | Wii: 61 | * /dev/flash access 62 | * ES_ImportBoot 63 | */ 64 | /*============================================================================== 65 | Functions: 66 | ============================================================================== */ 67 | 68 | /** 69 | * This function can be used to keep HW_AHBPROT access when going to reload IOS 70 | * @param verbose Flag determing whether or not to print messages on-screen 71 | * @example 72 | * if(AHBPROT_DISABLED) { 73 | * s32 ret; 74 | * ret = IosPatch_AHBPROT(false); 75 | * if (ret) { 76 | * IOS_ReloadIOS(36); 77 | * } else { 78 | * printf("IosPatch_AHBPROT failed."); 79 | * } 80 | * } 81 | * @return Signed 32bit integer representing code 82 | * > 0 : Success - return equals to number of applied patches 83 | * ERROR_AHBPROT : Error - No HW_AHBPROT access 84 | */ 85 | 86 | /* [nitr8]: Unused */ 87 | /*s32 IosPatch_AHBPROT(bool verbose); */ 88 | 89 | 90 | s32 Fix_ES_ImportBoot(void); 91 | s32 Enable_DevFlash(void); 92 | 93 | /* [nitr8]: get rid of warning for implicit declaration of function */ 94 | s32 Restore_Trucha(void); 95 | 96 | s32 Enable_DevBoot2(void); 97 | 98 | /* [nitr8]: Added */ 99 | s32 Disable_FlashECCCheck(void); 100 | s32 Enable_FlashECCCheck(void); 101 | 102 | /* [nitr8]: Added */ 103 | s32 Disable_UIDCheck(void); 104 | s32 Enable_UIDCheck(void); 105 | 106 | /* [nitr8]: Added */ 107 | s32 Disable_IOSOpen_ACCESS_prevention(void); 108 | s32 Enable_IOSOpen_ACCESS_prevention(void); 109 | 110 | /* [nitr8]: Added */ 111 | s32 Disable_IOS_Debug_Output(void); 112 | s32 Enable_IOS_Debug_Output(void); 113 | 114 | /** 115 | * This function applies patches on current IOS 116 | * @see Patchsets 117 | * @param verbose Flag determing whether or not to print messages on-screen. 118 | * @example if(AHBPROT_DISABLED) IosPatch_FULL(true, false, false, false); 119 | * @return Signed 32bit integer representing code 120 | * > 0 : Success - return equals to number of applied patches 121 | * ERROR_AHBPROT : Error - No HW_AHBPROT access 122 | * ERROR_PATCH : Error - Patching HW_AHBPROT access failed 123 | */ 124 | 125 | /* [nitr8]: Unused */ 126 | /* s32 IosPatch_RUNTIME(bool verbose); */ 127 | 128 | 129 | /** 130 | * This function combines IosPatch_AHBPROT + IOS_ReloadIOS + IosPatch_RUNTIME 131 | * @see Patchsets 132 | * @param verbose Flag determing whether or not to print messages on-screen. 133 | * @param IOS Which IOS to reload into. 134 | * @example if(AHBPROT_DISABLED) IosPatch_FULL(true, false, false, false, 58); 135 | * @return Signed 32bit integer representing code 136 | * > 0 : Success - return equals to number of applied patches 137 | * ERROR_AHBPROT : Error - No HW_AHBPROT access 138 | * ERROR_PATCH : Error - Patching HW_AHBPROT access failed 139 | */ 140 | 141 | /* [nitr8]: Unused */ 142 | /* s32 IosPatch_FULL(bool verbose, int IOS); */ 143 | 144 | /*============================================================================== 145 | C++ footer 146 | ============================================================================== */ 147 | #ifdef __cplusplus 148 | } 149 | #endif /* __cplusplus */ 150 | 151 | /*============================================================================== 152 | HW_RVL footer 153 | ============================================================================== */ 154 | #endif /* defined(HW_RVL) */ 155 | 156 | #endif /* __RUNTIMEIOSPATCH_H__ */ 157 | 158 | -------------------------------------------------------------------------------- /include/seeprom.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __SEEPROM_H__ 4 | #define __SEEPROM_H__ 5 | 6 | /* [nitr8]: Added */ 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | /* [nitr8]: Added */ 12 | int SEEPROMClearStep(void); 13 | 14 | /* [nitr8]: Added */ 15 | int SEEPROMBackupVersion(void); 16 | 17 | /* [nitr8]: Added */ 18 | int SEEPROMRestoreVersion(void); 19 | 20 | /* [nitr8]: Added */ 21 | int SEEPROMCompareVersion(int state); 22 | 23 | /* [nitr8]: Added */ 24 | int SEEPROMClearVersion(void); 25 | 26 | /* [root1024]: Added */ 27 | void SEEPROMDisplayInfo(void); 28 | 29 | /* [nitr8]: Added */ 30 | #ifdef __cplusplus 31 | } 32 | #endif /* __cplusplus */ 33 | 34 | #endif /* __SEEPROM_H__ */ 35 | 36 | -------------------------------------------------------------------------------- /include/sha256.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /********************************************************************* 4 | * Filename: sha256.h 5 | * Author: Brad Conte (brad AT bradconte.com) 6 | * Copyright: 7 | * Disclaimer: This code is presented "as is" without any guarantees. 8 | * Details: Defines the API for the corresponding SHA1 implementation. 9 | *********************************************************************/ 10 | 11 | #ifndef __SHA256_H__ 12 | #define __SHA256_H__ 13 | 14 | /*************************** HEADER FILES ***************************/ 15 | #include 16 | 17 | /****************************** MACROS ******************************/ 18 | #define SHA256_BLOCK_SIZE 32 /* SHA256 outputs a 32 byte digest */ 19 | 20 | /**************************** DATA TYPES ****************************/ 21 | typedef unsigned char BYTE; /* 8-bit byte */ 22 | typedef unsigned int WORD; /* 32-bit word, change to "long" for 16-bit machines */ 23 | 24 | typedef struct { 25 | BYTE data[64]; 26 | WORD datalen; 27 | unsigned long long bitlen; 28 | WORD state[8]; 29 | } SHA256_CTX; 30 | 31 | /* [nitr8]: Added */ 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | /*********************** FUNCTION DECLARATIONS **********************/ 37 | void sha256_init(SHA256_CTX *ctx); 38 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); 39 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]); 40 | 41 | /* [nitr8]: Added */ 42 | #ifdef __cplusplus 43 | } 44 | #endif /* __cplusplus */ 45 | 46 | #endif /* __SHA256_H__ */ 47 | 48 | -------------------------------------------------------------------------------- /include/tools.h: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #ifndef __TOOLS_H__ 4 | #define __TOOLS_H__ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /* [nitr8]: Added */ 16 | #include "gecko.h" 17 | 18 | #define RGDSDB_PAD_BUTTON_UP 0x777 19 | 20 | /* [nitr8]: Disables warning about unused function parameters where desired */ 21 | #define UNUSED(x) ((x) = (x)) 22 | 23 | /* [nitr8]: Added */ 24 | #ifndef UINT_MAX 25 | #define UINT_MAX ((u32)((s32) - 1)) 26 | #endif 27 | 28 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 29 | #define DATA_CHUNK_LEN 0x200 30 | 31 | /* [nitr8]: Added - at least used for calculation of the ECC data in a NAND block but it's used everywhere in the code */ 32 | #define ALIGNED(x) __attribute__((aligned(x))) 33 | 34 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 35 | #define PACKED __attribute__((packed)) 36 | 37 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 38 | #define MEM2_BSS __attribute__((section(".bss.mem2"))) 39 | 40 | /* [nitr8]: Added - Fix menu bugs with button matches between a Wiimote and a GC Controller */ 41 | typedef struct 42 | { 43 | u32 button; 44 | u32 is_pressed; 45 | u32 is_released; 46 | bool is_gc_controller; 47 | } pad_t; 48 | 49 | /* [nitr8]: Added */ 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | /* [nitr8]: Reworked: Fix menu bug where button press of "WPAD_BUTTON_B" matches GC "PAD_BUTTON_DOWN" */ 55 | /* u32 WaitForPad(void); */ 56 | pad_t WaitForPad(void); 57 | 58 | u8 IsDolphin(void); 59 | u8 IsWiiU(void); 60 | void WaitExit(void); 61 | 62 | /* [nitr8]: Renamed */ 63 | /* void *alloc(u32 size); */ 64 | void *alloc_aligned(u32 size); 65 | 66 | /* [nitr8]: Renamed and return type changed */ 67 | /* u32 filesize(FILE* fp); */ 68 | int GetSizeOfFile(FILE* fp); 69 | 70 | int CheckFile(FILE* fp, const char *filename); 71 | 72 | /* [nitr8]: remove variable expectedSize size right here and move it's amount directly into CheckFileHash itself */ 73 | /* int CheckFileHash(const char *filename, u8 expectedHash[], int expectedSize); */ 74 | int CheckFileHash(const char *filename, u8 expectedHash[]); 75 | 76 | u32 GetBoot2Version(void); 77 | 78 | /* [nitr8]: Added */ 79 | void hexdump(u32 addr, const void *d, int len); 80 | 81 | /* [root1024]: Added - simply replaced "gecko_printf" with "printf" */ 82 | void hexdump_graphical(u32 addr, const void *d, int len); 83 | 84 | /* [nitr8]: Added */ 85 | void memstats(int reset); 86 | 87 | /* [nitr8]: Added */ 88 | int exit_on_error(int check_value, int check_amount, char *string, int error_code); 89 | 90 | /* [nitr8]: Added - used for easier RESET when it comes to restarting the app */ 91 | void console_reset(void); 92 | 93 | /* [nitr8]: Added - used for calculation of the ECC data in a NAND block */ 94 | u32 swap32(u32 val); 95 | 96 | /* [nitr8]: Added - Why is this missing??? */ 97 | /* Basically - trying to write a file into a directory which is non-existent will always FAIL 98 | So for a BARE setup once this app was started, ALWAYS create the required directories */ 99 | void create_directory(char *dir); 100 | 101 | /* [nitr8]: Added */ 102 | #ifdef __cplusplus 103 | } 104 | #endif /* __cplusplus */ 105 | 106 | /* [nitr8]: Added */ 107 | extern int sd_initialized; 108 | 109 | /* [nitr8]: Added - used for easier RESET when it comes to restarting the app */ 110 | extern int console_reload; 111 | 112 | #endif /* __TOOLS_H__ */ 113 | 114 | -------------------------------------------------------------------------------- /src/ecc.c: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | /* [nitr8]: New file */ 4 | 5 | /* Simple ECC verification code, originally by Segher */ 6 | 7 | #include 8 | #include 9 | 10 | #include "ecc.h" 11 | 12 | static u8 parity(u8 x) 13 | { 14 | u8 y = 0; 15 | 16 | while (x) { 17 | y ^= (x & 1); 18 | x >>= 1; 19 | } 20 | 21 | return y; 22 | } 23 | 24 | static void calc_ecc(u8 *data, u8 *ecc) 25 | { 26 | u8 a[12][2]; 27 | int i, j; 28 | u32 a0, a1; 29 | u8 x; 30 | 31 | memset(a, 0, sizeof a); 32 | for (i = 0; i < 512; i++) { 33 | x = data[i]; 34 | for (j = 0; j < 9; j++) 35 | a[3+j][(i >> j) & 1] ^= x; 36 | } 37 | 38 | x = a[3][0] ^ a[3][1]; 39 | a[0][0] = x & 0x55; 40 | a[0][1] = x & 0xaa; 41 | a[1][0] = x & 0x33; 42 | a[1][1] = x & 0xcc; 43 | a[2][0] = x & 0x0f; 44 | a[2][1] = x & 0xf0; 45 | 46 | for (j = 0; j < 12; j++) { 47 | a[j][0] = parity(a[j][0]); 48 | a[j][1] = parity(a[j][1]); 49 | } 50 | 51 | a0 = a1 = 0; 52 | for (j = 0; j < 12; j++) { 53 | a0 |= a[j][0] << j; 54 | a1 |= a[j][1] << j; 55 | } 56 | 57 | ecc[0] = a0; 58 | ecc[1] = a0 >> 8; 59 | ecc[2] = a1; 60 | ecc[3] = a1 >> 8; 61 | } 62 | 63 | u8 * calc_page_ecc(u8 *data) 64 | { 65 | static u8 ecc[16]; 66 | 67 | calc_ecc(data, ecc); 68 | calc_ecc(data + 512, ecc + 4); 69 | calc_ecc(data + 1024, ecc + 8); 70 | calc_ecc(data + 1536, ecc + 12); 71 | return ecc; 72 | } 73 | 74 | /* [nitr8]: UNUSED */ 75 | #if 0 76 | int check_ecc(u8 *page) { 77 | u8 *stored_ecc = page + 2048 + 48; 78 | if (page[2048]!=0xFF) return ECC_INVALID; 79 | if (stored_ecc[0] == 0xFF && stored_ecc[1] == 0xFF) return ECC_BLANK; 80 | 81 | if (memcmp(stored_ecc, calc_page_ecc(page), 16)) return ECC_WRONG; 82 | return ECC_OK; 83 | } 84 | #endif 85 | 86 | -------------------------------------------------------------------------------- /src/errorhandler.c: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "errorhandler.h" 9 | #include "tools.h" 10 | 11 | void ThrowError(const char* errorString) 12 | { 13 | printf("\x1b[2;0H\033[2J"); 14 | 15 | printf("An error has occurred and the RGD SDBoot Installer can't continue.\nThe details of the error are:\n\n"); 16 | printf("%s", errorString); 17 | 18 | WaitExit(); 19 | } 20 | 21 | void ThrowErrorEx(const char* errorString, s32 errorCode) 22 | { 23 | printf("\x1b[2;0H\033[2J"); 24 | 25 | printf("An error has occurred and the RGD SDBoot Installer can't continue.\nThe details of the error are:\n\n"); 26 | printf("%s %i", errorString, errorCode); 27 | 28 | WaitExit(); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/gecko.c: -------------------------------------------------------------------------------- 1 | /* 2 | mini - a Free Software replacement for the Nintendo/BroadOn IOS. 3 | USBGecko support code 4 | 5 | Copyright (c) 2008 Nuke - 6 | Copyright (C) 2008, 2009 Hector Martin "marcan" 7 | Copyright (C) 2008, 2009 Sven Peter 8 | Copyright (C) 2009 Andre Heider "dhewg" 9 | Copyright (C) 2024 nitr8 10 | 11 | # This code is licensed to you under the terms of the GNU GPL, version 2; 12 | # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "hollywood.h" 20 | #include "gecko.h" 21 | #include "tools.h" 22 | 23 | 24 | /* #define GECKO_PRINT_ALT 1 */ 25 | /* #define GECKO_LFCR 0 */ 26 | /* #define GECKO_SAFE 0 */ 27 | 28 | /* [nitr8]: Not working yet... */ 29 | /* #define SCREEN_TO_CONSOLE_OUTPUT 0 */ 30 | 31 | 32 | static u8 gecko_found = 0; 33 | static u8 gecko_console_enabled = 0; 34 | 35 | 36 | static u32 _gecko_command(u32 command) 37 | { 38 | u32 i; 39 | 40 | /* Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock)) */ 41 | write32(EXI1_CSR, 0xd0); 42 | write32(EXI1_DATA, command); 43 | write32(EXI1_CR, 0x19); 44 | 45 | while (read32(EXI1_CR) & 1); 46 | 47 | i = read32(EXI1_DATA); 48 | write32(EXI1_CSR, 0); 49 | 50 | return i; 51 | } 52 | 53 | static u32 _gecko_getid(void) 54 | { 55 | u32 i; 56 | 57 | /* Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock)) */ 58 | write32(EXI1_CSR, 0xd0); 59 | write32(EXI1_DATA, 0); 60 | write32(EXI1_CR, 0x19); 61 | 62 | while (read32(EXI1_CR) & 1); 63 | 64 | write32(EXI1_CR, 0x39); 65 | 66 | while (read32(EXI1_CR) & 1); 67 | 68 | i = read32(EXI1_DATA); 69 | write32(EXI1_CSR, 0); 70 | 71 | return i; 72 | } 73 | 74 | #ifndef NDEBUG 75 | static u32 _gecko_sendbyte(u8 sendbyte) 76 | { 77 | u32 i = _gecko_command(0xB0000000 | (sendbyte<<20)); 78 | 79 | if (i&0x04000000) 80 | return 1; /* Return 1 if byte was sent */ 81 | 82 | return 0; 83 | } 84 | #endif 85 | 86 | static u32 _gecko_recvbyte(u8 *recvbyte) 87 | { 88 | u32 i = 0; 89 | *recvbyte = 0; 90 | 91 | i = _gecko_command(0xA0000000); 92 | 93 | if (i&0x08000000) 94 | { 95 | /* Return 1 if byte was received */ 96 | *recvbyte = (i>>16)&0xff; 97 | 98 | return 1; 99 | } 100 | 101 | return 0; 102 | } 103 | 104 | #if (!defined(NDEBUG) && (defined(GECKO_SAFE) || defined(GECKO_PRINT_ALT))) 105 | static u32 _gecko_checksend(void) 106 | { 107 | u32 i = 0; 108 | i = _gecko_command(0xC0000000); 109 | 110 | if (i&0x04000000) 111 | return 1; /* Return 1 if safe to send */ 112 | 113 | return 0; 114 | } 115 | #endif 116 | 117 | static int gecko_isalive(void) 118 | { 119 | u32 i; 120 | 121 | i = _gecko_getid(); 122 | 123 | if (i != 0x00000000) 124 | return 0; 125 | 126 | i = _gecko_command(0x90000000); 127 | 128 | if ((i & 0xFFFF0000) != 0x04700000) 129 | return 0; 130 | 131 | return 1; 132 | } 133 | 134 | static void gecko_flush(void) 135 | { 136 | u8 tmp; 137 | 138 | while (_gecko_recvbyte(&tmp)); 139 | } 140 | 141 | #if (!defined(NDEBUG) && !defined(GECKO_SAFE) && !defined(GECKO_PRINT_ALT)) 142 | static int gecko_sendbuffer(const void *buffer, u32 size) 143 | { 144 | u32 left = size; 145 | char *ptr = (char*)buffer; 146 | 147 | while (left > 0) 148 | { 149 | if (!_gecko_sendbyte(*ptr)) 150 | break; 151 | 152 | if (*ptr == '\n') 153 | { 154 | #ifdef GECKO_LFCR 155 | _gecko_sendbyte('\r'); 156 | #endif 157 | } 158 | 159 | ptr++; 160 | left--; 161 | } 162 | 163 | return (size - left); 164 | } 165 | #endif 166 | 167 | #if (!defined(NDEBUG) && defined(GECKO_SAFE) && !defined(GECKO_PRINT_ALT)) 168 | static int gecko_sendbuffer_safe(const void *buffer, u32 size) 169 | { 170 | u32 left = size; 171 | char *ptr = (char*)buffer; 172 | 173 | if ((read32(HW_EXICTRL) & EXICTRL_ENABLE_EXI) == 0) 174 | return left; 175 | 176 | while (left > 0) 177 | { 178 | if (_gecko_checksend()) 179 | { 180 | if (!_gecko_sendbyte(*ptr)) 181 | break; 182 | 183 | if (*ptr == '\n') 184 | { 185 | #ifdef GECKO_LFCR 186 | _gecko_sendbyte('\r'); 187 | #endif 188 | } 189 | 190 | ptr++; 191 | left--; 192 | } 193 | } 194 | 195 | return (size - left); 196 | } 197 | #endif 198 | 199 | #ifdef GECKO_PRINT_ALT 200 | 201 | static int gecko_putc(const char s) 202 | { 203 | char *ptr = (char *)&s; 204 | int tries = 10000; /* about 200ms of tries; if we fail, fail gracefully instead of just hanging */ 205 | 206 | if (!gecko_found) 207 | return -1; 208 | 209 | if ((read32(HW_EXICTRL) & EXICTRL_ENABLE_EXI) == 0) 210 | return 0; 211 | 212 | if (_gecko_checksend()) 213 | { 214 | if (!_gecko_sendbyte(*ptr)) 215 | return 0; 216 | 217 | if (*ptr == '\n') 218 | { 219 | #ifdef GECKO_LFCR 220 | _gecko_sendbyte('\r'); 221 | #endif 222 | } 223 | } 224 | else 225 | { 226 | /* if gecko is hung, time out and disable further attempts 227 | only affects gecko users without an active terminal */ 228 | if (tries-- == 0) 229 | { 230 | gecko_found = 0; 231 | return 0; 232 | } 233 | } 234 | 235 | return 1; 236 | } 237 | 238 | /* [nitr8]: Thanks bushing */ 239 | int gecko_printf(const char *str, ...) 240 | { 241 | va_list arp; 242 | u8 c; 243 | u8 f; 244 | u8 r; 245 | u32 val; 246 | u32 pos; 247 | char s[16]; 248 | s32 i; 249 | s32 w; 250 | 251 | /* [nitr8]: Support for USB-Gecko debug output must be 252 | put here due to Haxx_GetBusAccess() FAIL */ 253 | gecko_init(1); 254 | 255 | /* [nitr8]: Return early if the console isn't enabled at all */ 256 | if (!gecko_console_enabled) 257 | return 0; 258 | 259 | va_start(arp, str); 260 | 261 | for (pos = 0;; ) 262 | { 263 | c = *str++; 264 | 265 | /* End of string */ 266 | if (c == 0) 267 | break; 268 | 269 | /* Non escape cahracter */ 270 | if (c != '%') 271 | { 272 | gecko_putc(c); 273 | pos++; 274 | continue; 275 | } 276 | 277 | w = 0; 278 | f = 0; 279 | c = *str++; 280 | 281 | /* Flag: '0' padding */ 282 | if (c == '0') 283 | { 284 | f = 1; 285 | c = *str++; 286 | } 287 | 288 | /* Precision */ 289 | while (c >= '0' && c <= '9') 290 | { 291 | w = w * 10 + (c - '0'); 292 | c = *str++; 293 | } 294 | 295 | /* Type is string */ 296 | if (c == 's') 297 | { 298 | char *param = va_arg(arp, char*); 299 | 300 | for (i = 0; param[i]; i++) 301 | { 302 | gecko_putc(param[i]); 303 | pos++; 304 | } 305 | 306 | continue; 307 | } 308 | 309 | /* [nitr8]: Add a case for checking chars ('c') */ 310 | /* Type is char */ 311 | if (c == 'c') 312 | { 313 | char param = va_arg(arp, int); 314 | 315 | gecko_putc(param); 316 | pos++; 317 | 318 | continue; 319 | } 320 | 321 | r = 0; 322 | 323 | /* Type is signed decimal */ 324 | if (c == 'd') 325 | r = 10; 326 | 327 | /* Type is unsigned decimal */ 328 | if (c == 'u') 329 | r = 10; 330 | 331 | /* [nitr8]: Add a case for checking pointers ('p') */ 332 | /* Type is unsigned hexdecimal */ 333 | if (c == 'p' || c == 'x') 334 | r = 16; 335 | 336 | /* Unknown type */ 337 | if (r == 0) 338 | break; 339 | 340 | val = (c == 'd') ? (u32)(long)va_arg(arp, int) : 341 | (u32)va_arg(arp, unsigned int); 342 | 343 | /* Put numeral string */ 344 | if (c == 'd') 345 | { 346 | if (val & 0x80000000) 347 | { 348 | val = 0 - val; 349 | f |= 4; 350 | } 351 | } 352 | 353 | i = sizeof(s) - 1; 354 | s[i] = 0; 355 | 356 | do 357 | { 358 | c = (u8)(val % r + '0'); 359 | 360 | if (c > '9') 361 | c += 7; 362 | 363 | s[--i] = c; 364 | val /= r; 365 | } while (i && val); 366 | 367 | if (i && (f & 4)) 368 | s[--i] = '-'; 369 | 370 | w = sizeof(s) - 1 - w; 371 | 372 | while (i && i > w) 373 | s[--i] = (f & 1) ? '0' : ' '; 374 | 375 | for (; s[i] ; i++) 376 | { 377 | gecko_putc(s[i]); 378 | pos++; 379 | } 380 | } 381 | 382 | va_end(arp); 383 | 384 | return pos; 385 | } 386 | 387 | #else 388 | 389 | int gecko_printf(const char *fmt, ...) 390 | { 391 | va_list args; 392 | char buffer[256]; 393 | int i; 394 | 395 | if (!gecko_console_enabled) 396 | return 0; 397 | 398 | va_start(args, fmt); 399 | i = vsprintf(buffer, fmt, args); 400 | va_end(args); 401 | #ifdef GECKO_SAFE 402 | return gecko_sendbuffer_safe(buffer, i); 403 | #else 404 | return gecko_sendbuffer(buffer, i); 405 | #endif 406 | } 407 | 408 | #endif 409 | 410 | /* [nitr8]: Add support for output to a specific EXI port */ 411 | void gecko_init(int port) 412 | { 413 | /* [nitr8]: Not working yet... */ 414 | #ifndef SCREEN_TO_CONSOLE_OUTPUT 415 | UNUSED(port); 416 | #else 417 | 418 | /* [nitr8]: Not working yet... */ 419 | #ifdef GECKO_SAFE 420 | int safe = 1; 421 | #else 422 | int safe = 0; 423 | #endif 424 | 425 | #endif 426 | 427 | /* [nitr8]: Return early if the console isn't enabled at all */ 428 | if (gecko_console_enabled) 429 | return; 430 | 431 | write32(EXI0_CSR, 0); 432 | write32(EXI1_CSR, 0); 433 | write32(EXI2_CSR, 0); 434 | write32(EXI0_CSR, 0x2000); 435 | write32(EXI0_CSR, 3<<10); 436 | write32(EXI1_CSR, 3<<10); 437 | 438 | if (!gecko_isalive()) 439 | return; 440 | 441 | gecko_found = 1; 442 | 443 | gecko_flush(); 444 | gecko_console_enabled = 1; 445 | 446 | /* [nitr8]: Not working yet... */ 447 | #ifdef SCREEN_TO_CONSOLE_OUTPUT 448 | CON_EnableGecko(port, safe); 449 | #endif 450 | } 451 | 452 | -------------------------------------------------------------------------------- /src/haxx.c: -------------------------------------------------------------------------------- 1 | /* haxx.c - PowerPC privilege elevation 2 | Written by Palapeli 3 | 4 | Copyright (C) 2022 Palapeli 5 | SPDX-License-Identifier: MIT */ 6 | 7 | #include "haxx.h" 8 | #include 9 | #include 10 | 11 | /* [nitr8]: Don't just ignore warnings! */ 12 | /* #pragma GCC diagnostic push 13 | #pragma GCC diagnostic ignored "-Wpedantic" */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | /* [nitr8]: Added */ 21 | #include "hollywood.h" 22 | 23 | /* [nitr8]: Don't just ignore warnings! 24 | #pragma GCC diagnostic pop */ 25 | 26 | /* Performs an IOS exploit and branches to the entrypoint in system mode. 27 | 28 | Exploit summary: 29 | - IOS does not check validation of vectors with length 0. 30 | - All memory regions mapped as readable are executable (ARMv5 has no 31 | 'no execute' flag). 32 | - NULL/0 points to the beginning of MEM1. 33 | - The /dev/sha resource manager, part of IOSC, runs in system mode. 34 | - It's obvious basically none of the code was audited at all. 35 | 36 | IOCTL 0 (SHA1_Init) writes to the context vector (1) without checking the 37 | length at all. Two of the 32-bit values it initializes are zero. 38 | 39 | Common approach: Point the context vector to the LR on the stack and then 40 | take control after return. 41 | A much more stable approach taken here: Overwrite the PC of the idle thread, 42 | which should always have its context start at 0xFFFE0000 in memory (across 43 | IOS versions). */ 44 | 45 | /* [nitr8]: Make static */ 46 | /* s32 Haxx_IOSExploit(u32 entrypoint) */ 47 | static s32 Haxx_IOSExploit(u32 entrypoint) 48 | { 49 | u32* mem1; 50 | s32 ret; 51 | ioctlv vec[4] ATTRIBUTE_ALIGN(32); 52 | 53 | s32 fd = IOS_Open("/dev/sha", 0); 54 | 55 | if (fd < 0) 56 | return fd; 57 | 58 | printf("Exploit: Setting up MEM1\n"); 59 | 60 | mem1 = (u32*)0x80000000; 61 | mem1[0] = 0x4903468D; /* ldr r1, =0x10100000; mov sp, r1; */ 62 | mem1[1] = 0x49034788; /* ldr r1, =entrypoint; blx r1; */ 63 | /* Overwrite reserved handler to loop infinitely */ 64 | mem1[2] = 0x49036209; /* ldr r1, =0xFFFF0014; str r1, [r1, #0x20]; */ 65 | mem1[3] = 0x47080000; /* bx r1 */ 66 | mem1[4] = 0x10100000; /* temporary stack */ 67 | mem1[5] = entrypoint; 68 | mem1[6] = 0xFFFF0014; /* reserved handler */ 69 | 70 | vec[0].data = NULL; 71 | vec[0].len = 0; 72 | vec[1].data = (void*)0xFFFE0028; 73 | vec[1].len = 0; 74 | 75 | /* Unused vector utilized for cache safety */ 76 | vec[2].data = (void*)0x80000000; 77 | vec[2].len = 32; 78 | 79 | printf("Exploit: Doing exploit call\n"); 80 | 81 | ret = IOS_Ioctlv(fd, 0, 1, 2, vec); 82 | 83 | IOS_Close(fd); 84 | 85 | return ret; 86 | } 87 | 88 | /* Flushes data on PowerPC and invalidates on ARM. */ 89 | 90 | /* [nitr8]: Make static */ 91 | /* void Haxx_FlushRange(const void* data, u32 length) */ 92 | static void Haxx_FlushRange(const void* data, u32 length) 93 | { 94 | /* The IPC function flushes the cache here on PPC, and then IOS invalidates 95 | its own cache. 96 | Note: Neither libogc or IOS check for the invalid fd before they do what 97 | we want, but libogc _could_ change. Keep an eye on this. */ 98 | IOS_Write(-1, data, length); 99 | } 100 | 101 | /* Checks if the PPC has full bus access. */ 102 | 103 | /* [nitr8]: Make static */ 104 | /* bool Haxx_CheckBusAccess(void) */ 105 | static bool Haxx_CheckBusAccess(void) 106 | { 107 | if ((read32(HW_AHBPROT) & 0x80000DFE) != 0x80000DFE) 108 | return false; 109 | 110 | if ((read32(HW_MEMMIRR) & 0x08) != 0x08) 111 | return false; 112 | 113 | return true; 114 | } 115 | 116 | static const u32 armCode[] = { 117 | 0xE3A00536, /* mov r0, #0x0D800000 */ 118 | 0xE5901060, /* ldr r1, [r0, #0x60] */ 119 | 0xE3811008, /* orr r1, #0x08 */ 120 | 0xE5801060, /* str r1, [r0, #0x60] */ 121 | 0xE5901064, /* ldr r1, [r0, #0x64] */ 122 | 0xE381113A, /* orr r1, #0x8000000E */ 123 | 0xE3811EDF, /* orr r1, #0x00000DF0 */ 124 | 0xE5801064, /* str r1, [r0, #0x64] */ 125 | 0xE12FFF1E, /* bx lr */ 126 | }; 127 | 128 | /* Attempts to get full PPC bus access. Will perform an IOS exploit if needed. */ 129 | bool Haxx_GetBusAccess(void) 130 | { 131 | s32 ret; 132 | 133 | /* Do nothing if we already have bus access */ 134 | if (Haxx_CheckBusAccess()) 135 | { 136 | printf("Already have bus access, skipping exploit\n"); 137 | 138 | return true; 139 | } 140 | 141 | /* If just the PPCKERN bit is set then we can just set the other bits */ 142 | if (read32(HW_AHBPROT) & 0x80000000) 143 | { 144 | printf("Already have PPCKERN, setting other bits\n"); 145 | 146 | mask32(HW_AHBPROT, 0, 0x80000DFE); 147 | mask32(HW_MEMMIRR, 0, 0x08); 148 | 149 | return true; 150 | } 151 | 152 | /* No access so we must run the IOS exploit */ 153 | Haxx_FlushRange(armCode, sizeof(armCode)); 154 | 155 | ret = Haxx_IOSExploit((u32)armCode & ~0xC0000000); 156 | printf("IOS exploit call result: %d\n", ret); 157 | 158 | /* The exploit realistically couldn't have run if the result is less than 159 | zero. */ 160 | if (ret >= 0) 161 | { 162 | printf("Waiting for bus access...\n"); 163 | 164 | /* XXX timeout */ 165 | while (!Haxx_CheckBusAccess()) 166 | { 167 | usleep(1000); 168 | } 169 | } 170 | 171 | return Haxx_CheckBusAccess(); 172 | } 173 | 174 | -------------------------------------------------------------------------------- /src/hbc.c: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "errorhandler.h" 10 | 11 | /* [nitr8]: get rid of warnings when arrays are not used at all 12 | #include "errorcodes.h" */ 13 | 14 | #include "seeprom.h" 15 | #include "boot2.h" 16 | #include "tools.h" 17 | #include "prodinfo.h" 18 | #include "runtimeiospatch.h" 19 | #include "menu.h" 20 | #include "flash.h" 21 | 22 | /* [nitr8]: warning: multiple definition of `buildNumber' 23 | #include "version.h" */ 24 | 25 | #include "haxx.h" 26 | #include "hbc.h" 27 | 28 | #include "hbc_content0.h" 29 | #include "hbc_content1.h" 30 | #include "hbc_certs.h" 31 | #include "hbc_tik.h" 32 | #include "hbc_tmd.h" 33 | 34 | s32 InstallHBC(void) 35 | { 36 | s32 ret, cfd; 37 | s32 BLK_SIZE = 1024, offset = 0, size; 38 | u8 *contentBuffer = (u8 *)memalign(32, BLK_SIZE); 39 | 40 | /* Add Ticket */ 41 | ret = ES_AddTicket((signed_blob *)hbc_tik, hbc_tik_size, (signed_blob *)hbc_certs, hbc_certs_size, NULL, 0); 42 | 43 | printf("ES_AddTicket returned: %d\n", ret); 44 | 45 | /* Start adding Title */ 46 | ret = ES_AddTitleStart((signed_blob *)hbc_tmd, hbc_tmd_size, (signed_blob *)hbc_certs, hbc_certs_size, NULL, 0); 47 | 48 | printf("ES_AddTitleStart returned: %d\n", ret); 49 | 50 | /* Start adding content 0 */ 51 | cfd = ES_AddContentStart(0x000100014F484243, 0); 52 | 53 | printf("ES_AddContentStart(0) returned: %d\n", cfd); 54 | 55 | /* Add content 0, with 1024-byte blocks */ 56 | while (offset < hbc_content0_size) 57 | { 58 | size = (offset + BLK_SIZE <= hbc_content0_size) ? BLK_SIZE : hbc_content0_size - offset; 59 | 60 | memcpy(contentBuffer, (u8 *)(hbc_content0+offset), size); 61 | 62 | ret = ES_AddContentData(cfd, contentBuffer, size); 63 | 64 | offset += BLK_SIZE; 65 | } 66 | 67 | printf("ES_AddContentData(0) returned: %d\n", ret); 68 | 69 | /* Finish adding content 0 */ 70 | ret = ES_AddContentFinish(cfd); 71 | 72 | printf("ES_AddContentFinish(0) returned: %d\n", ret); 73 | 74 | /* ------------------------------------------ */ 75 | 76 | cfd = ES_AddContentStart(0x000100014F484243, 1); 77 | 78 | printf("ES_AddContentStart(1) returned: %d\n", cfd); 79 | 80 | offset = 0; 81 | 82 | /* Add content 1, with 1024-byte blocks */ 83 | while (offset < hbc_content1_size) 84 | { 85 | size = (offset + BLK_SIZE <= hbc_content1_size) ? BLK_SIZE : hbc_content1_size - offset; 86 | 87 | memcpy(contentBuffer, (u8 *)(hbc_content1+offset), size); 88 | 89 | ret = ES_AddContentData(cfd, contentBuffer, size); 90 | 91 | offset += BLK_SIZE; 92 | } 93 | 94 | printf("ES_AddContentData(1) returned: %d\n", ret); 95 | 96 | ret = ES_AddContentFinish(cfd); 97 | 98 | printf("ES_AddContentFinish(1) returned: %d\n", ret); 99 | 100 | ret = ES_AddTitleFinish(); 101 | 102 | printf("ES_AddTitleFinish returned: %d\n", ret); 103 | 104 | if (contentBuffer != NULL) 105 | free(contentBuffer); 106 | 107 | WaitForPad(); 108 | 109 | return ret; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /src/hbc/hbc_tik.c: -------------------------------------------------------------------------------- 1 | /* 2 | This file was autogenerated by raw2c. 3 | Visit http://www.devkitpro.org 4 | */ 5 | 6 | #include "hbc/hbc_tik.h" 7 | 8 | __attribute__((aligned(32))) const unsigned char hbc_tik[] = { 9 | 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 10 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 11 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 12 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 13 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 14 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 15 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 16 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 17 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 19 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 26 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 27 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 28 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 29 | 0x52, 0x6f, 0x6f, 0x74, 0x2d, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x2d, 30 | 0x58, 0x53, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 31 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 32 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 33 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 37 | 0x68, 0x65, 0x4d, 0x6f, 0x73, 0x74, 0x41, 0x77, 0x65, 0x73, 0x6f, 0x6d, 0x65, 0x73, 0x74, 0x00, 38 | 0x00, 0x23, 0xdb, 0x44, 0x24, 0xd0, 0x33, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 39 | 0x4f, 0x48, 0x42, 0x43, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 44 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 45 | 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00 52 | }; 53 | const int hbc_tik_size = sizeof(hbc_tik); 54 | -------------------------------------------------------------------------------- /src/hbc/hbc_tmd.c: -------------------------------------------------------------------------------- 1 | /* 2 | This file was autogenerated by raw2c. 3 | Visit http://www.devkitpro.org 4 | */ 5 | 6 | #include "hbc/hbc_tmd.h" 7 | 8 | __attribute__((aligned(32))) const unsigned char hbc_tmd[] = { 9 | 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 10 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 11 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 12 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 13 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 14 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 15 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 16 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 17 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 19 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 26 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 27 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 28 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 29 | 0x52, 0x6f, 0x6f, 0x74, 0x2d, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x2d, 30 | 0x43, 0x50, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 31 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 32 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 33 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x01, 0x00, 0x01, 34 | 0x4f, 0x48, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x48, 0x42, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 35 | 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x02, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x02, 0x00, 0x02, 39 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x09, 0xbd, 0xb0, 0x2a, 0x19, 0xc1, 0xb3, 0x9f, 0x66, 0xc5, 0xe9, 0x94, 0xa1, 0xa2, 0x06, 41 | 0xaf, 0xc4, 0x61, 0xfb, 0xbc, 0x53, 0x4a, 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x41, 0xa0, 0x0f, 0xfa, 0x83, 0x95, 0x58, 0xf6, 0xa6, 0x30, 43 | 0xc8, 0xdc, 0x6d, 0x7e, 0x42, 0x18, 0x24, 0x5b, 0xff, 0x18, 0xe1, 0xbe 44 | }; 45 | const int hbc_tmd_size = sizeof(hbc_tmd); 46 | -------------------------------------------------------------------------------- /src/jpeglib/jcapistd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jcapistd.c 3 | * 4 | * Copyright (C) 1994-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains application interface code for the compression half 9 | * of the JPEG library. These are the "standard" API routines that are 10 | * used in the normal full-compression case. They are not used by a 11 | * transcoding-only application. Note that if an application links in 12 | * jpeg_start_compress, it will end up linking in the entire compressor. 13 | * We thus must separate this file from jcapimin.c to avoid linking the 14 | * whole compression library into a transcoder. 15 | */ 16 | 17 | #define JPEG_INTERNALS 18 | #include "jinclude.h" 19 | #include "jpeglib.h" 20 | 21 | 22 | /* 23 | * Compression initialization. 24 | * Before calling this, all parameters and a data destination must be set up. 25 | * 26 | * We require a write_all_tables parameter as a failsafe check when writing 27 | * multiple datastreams from the same compression object. Since prior runs 28 | * will have left all the tables marked sent_table=TRUE, a subsequent run 29 | * would emit an abbreviated stream (no tables) by default. This may be what 30 | * is wanted, but for safety's sake it should not be the default behavior: 31 | * programmers should have to make a deliberate choice to emit abbreviated 32 | * images. Therefore the documentation and examples should encourage people 33 | * to pass write_all_tables=TRUE; then it will take active thought to do the 34 | * wrong thing. 35 | */ 36 | 37 | GLOBAL(void) 38 | jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables) 39 | { 40 | if (cinfo->global_state != CSTATE_START) 41 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 42 | 43 | if (write_all_tables) 44 | jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */ 45 | 46 | /* (Re)initialize error mgr and destination modules */ 47 | (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); 48 | (*cinfo->dest->init_destination) (cinfo); 49 | /* Perform master selection of active modules */ 50 | jinit_compress_master(cinfo); 51 | /* Set up for the first pass */ 52 | (*cinfo->master->prepare_for_pass) (cinfo); 53 | /* Ready for application to drive first pass through jpeg_write_scanlines 54 | * or jpeg_write_raw_data. 55 | */ 56 | cinfo->next_scanline = 0; 57 | cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING); 58 | } 59 | 60 | 61 | /* 62 | * Write some scanlines of data to the JPEG compressor. 63 | * 64 | * The return value will be the number of lines actually written. 65 | * This should be less than the supplied num_lines only in case that 66 | * the data destination module has requested suspension of the compressor, 67 | * or if more than image_height scanlines are passed in. 68 | * 69 | * Note: we warn about excess calls to jpeg_write_scanlines() since 70 | * this likely signals an application programmer error. However, 71 | * excess scanlines passed in the last valid call are *silently* ignored, 72 | * so that the application need not adjust num_lines for end-of-image 73 | * when using a multiple-scanline buffer. 74 | */ 75 | 76 | GLOBAL(JDIMENSION) 77 | jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines, 78 | JDIMENSION num_lines) 79 | { 80 | JDIMENSION row_ctr, rows_left; 81 | 82 | if (cinfo->global_state != CSTATE_SCANNING) 83 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 84 | if (cinfo->next_scanline >= cinfo->image_height) 85 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); 86 | 87 | /* Call progress monitor hook if present */ 88 | if (cinfo->progress != NULL) { 89 | cinfo->progress->pass_counter = (long) cinfo->next_scanline; 90 | cinfo->progress->pass_limit = (long) cinfo->image_height; 91 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); 92 | } 93 | 94 | /* Give master control module another chance if this is first call to 95 | * jpeg_write_scanlines. This lets output of the frame/scan headers be 96 | * delayed so that application can write COM, etc, markers between 97 | * jpeg_start_compress and jpeg_write_scanlines. 98 | */ 99 | if (cinfo->master->call_pass_startup) 100 | (*cinfo->master->pass_startup) (cinfo); 101 | 102 | /* Ignore any extra scanlines at bottom of image. */ 103 | rows_left = cinfo->image_height - cinfo->next_scanline; 104 | if (num_lines > rows_left) 105 | num_lines = rows_left; 106 | 107 | row_ctr = 0; 108 | (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines); 109 | cinfo->next_scanline += row_ctr; 110 | return row_ctr; 111 | } 112 | 113 | 114 | /* 115 | * Alternate entry point to write raw data. 116 | * Processes exactly one iMCU row per call, unless suspended. 117 | */ 118 | 119 | GLOBAL(JDIMENSION) 120 | jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data, 121 | JDIMENSION num_lines) 122 | { 123 | JDIMENSION lines_per_iMCU_row; 124 | 125 | if (cinfo->global_state != CSTATE_RAW_OK) 126 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 127 | if (cinfo->next_scanline >= cinfo->image_height) { 128 | WARNMS(cinfo, JWRN_TOO_MUCH_DATA); 129 | return 0; 130 | } 131 | 132 | /* Call progress monitor hook if present */ 133 | if (cinfo->progress != NULL) { 134 | cinfo->progress->pass_counter = (long) cinfo->next_scanline; 135 | cinfo->progress->pass_limit = (long) cinfo->image_height; 136 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); 137 | } 138 | 139 | /* Give master control module another chance if this is first call to 140 | * jpeg_write_raw_data. This lets output of the frame/scan headers be 141 | * delayed so that application can write COM, etc, markers between 142 | * jpeg_start_compress and jpeg_write_raw_data. 143 | */ 144 | if (cinfo->master->call_pass_startup) 145 | (*cinfo->master->pass_startup) (cinfo); 146 | 147 | /* Verify that at least one iMCU row has been passed. */ 148 | lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE; 149 | if (num_lines < lines_per_iMCU_row) 150 | ERREXIT(cinfo, JERR_BUFFER_SIZE); 151 | 152 | /* Directly compress the row. */ 153 | if (! (*cinfo->coef->compress_data) (cinfo, data)) { 154 | /* If compressor did not consume the whole row, suspend processing. */ 155 | return 0; 156 | } 157 | 158 | /* OK, we processed one iMCU row. */ 159 | cinfo->next_scanline += lines_per_iMCU_row; 160 | return lines_per_iMCU_row; 161 | } 162 | -------------------------------------------------------------------------------- /src/jpeglib/jcinit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jcinit.c 3 | * 4 | * Copyright (C) 1991-1997, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains initialization logic for the JPEG compressor. 9 | * This routine is in charge of selecting the modules to be executed and 10 | * making an initialization call to each one. 11 | * 12 | * Logically, this code belongs in jcmaster.c. It's split out because 13 | * linking this routine implies linking the entire compression library. 14 | * For a transcoding-only application, we want to be able to use jcmaster.c 15 | * without linking in the whole library. 16 | */ 17 | 18 | #define JPEG_INTERNALS 19 | #include "jinclude.h" 20 | #include "jpeglib.h" 21 | 22 | 23 | /* 24 | * Master selection of compression modules. 25 | * This is done once at the start of processing an image. We determine 26 | * which modules will be used and give them appropriate initialization calls. 27 | */ 28 | 29 | GLOBAL(void) 30 | jinit_compress_master (j_compress_ptr cinfo) 31 | { 32 | /* Initialize master control (includes parameter checking/processing) */ 33 | jinit_c_master_control(cinfo, FALSE /* full compression */); 34 | 35 | /* Preprocessing */ 36 | if (! cinfo->raw_data_in) { 37 | jinit_color_converter(cinfo); 38 | jinit_downsampler(cinfo); 39 | jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */); 40 | } 41 | /* Forward DCT */ 42 | jinit_forward_dct(cinfo); 43 | /* Entropy encoding: either Huffman or arithmetic coding. */ 44 | if (cinfo->arith_code) { 45 | ERREXIT(cinfo, JERR_ARITH_NOTIMPL); 46 | } else { 47 | if (cinfo->progressive_mode) { 48 | #ifdef C_PROGRESSIVE_SUPPORTED 49 | jinit_phuff_encoder(cinfo); 50 | #else 51 | ERREXIT(cinfo, JERR_NOT_COMPILED); 52 | #endif 53 | } else 54 | jinit_huff_encoder(cinfo); 55 | } 56 | 57 | /* Need a full-image coefficient buffer in any multi-pass mode. */ 58 | jinit_c_coef_controller(cinfo, 59 | (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding)); 60 | jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */); 61 | 62 | jinit_marker_writer(cinfo); 63 | 64 | /* We can now tell the memory manager to allocate virtual arrays. */ 65 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); 66 | 67 | /* Write the datastream header (SOI) immediately. 68 | * Frame and scan headers are postponed till later. 69 | * This lets application insert special markers after the SOI. 70 | */ 71 | (*cinfo->marker->write_file_header) (cinfo); 72 | } 73 | -------------------------------------------------------------------------------- /src/jpeglib/jcomapi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jcomapi.c 3 | * 4 | * Copyright (C) 1994-1997, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains application interface routines that are used for both 9 | * compression and decompression. 10 | */ 11 | 12 | #define JPEG_INTERNALS 13 | #include "jinclude.h" 14 | #include "jpeglib.h" 15 | 16 | 17 | /* 18 | * Abort processing of a JPEG compression or decompression operation, 19 | * but don't destroy the object itself. 20 | * 21 | * For this, we merely clean up all the nonpermanent memory pools. 22 | * Note that temp files (virtual arrays) are not allowed to belong to 23 | * the permanent pool, so we will be able to close all temp files here. 24 | * Closing a data source or destination, if necessary, is the application's 25 | * responsibility. 26 | */ 27 | 28 | GLOBAL(void) 29 | jpeg_abort (j_common_ptr cinfo) 30 | { 31 | int pool; 32 | 33 | /* Do nothing if called on a not-initialized or destroyed JPEG object. */ 34 | if (cinfo->mem == NULL) 35 | return; 36 | 37 | /* Releasing pools in reverse order might help avoid fragmentation 38 | * with some (brain-damaged) malloc libraries. 39 | */ 40 | for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) { 41 | (*cinfo->mem->free_pool) (cinfo, pool); 42 | } 43 | 44 | /* Reset overall state for possible reuse of object */ 45 | if (cinfo->is_decompressor) { 46 | cinfo->global_state = DSTATE_START; 47 | /* Try to keep application from accessing now-deleted marker list. 48 | * A bit kludgy to do it here, but this is the most central place. 49 | */ 50 | ((j_decompress_ptr) cinfo)->marker_list = NULL; 51 | } else { 52 | cinfo->global_state = CSTATE_START; 53 | } 54 | } 55 | 56 | 57 | /* 58 | * Destruction of a JPEG object. 59 | * 60 | * Everything gets deallocated except the master jpeg_compress_struct itself 61 | * and the error manager struct. Both of these are supplied by the application 62 | * and must be freed, if necessary, by the application. (Often they are on 63 | * the stack and so don't need to be freed anyway.) 64 | * Closing a data source or destination, if necessary, is the application's 65 | * responsibility. 66 | */ 67 | 68 | GLOBAL(void) 69 | jpeg_destroy (j_common_ptr cinfo) 70 | { 71 | /* We need only tell the memory manager to release everything. */ 72 | /* NB: mem pointer is NULL if memory mgr failed to initialize. */ 73 | if (cinfo->mem != NULL) 74 | (*cinfo->mem->self_destruct) (cinfo); 75 | cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */ 76 | cinfo->global_state = 0; /* mark it destroyed */ 77 | } 78 | 79 | 80 | /* 81 | * Convenience routines for allocating quantization and Huffman tables. 82 | * (Would jutils.c be a more reasonable place to put these?) 83 | */ 84 | 85 | GLOBAL(JQUANT_TBL *) 86 | jpeg_alloc_quant_table (j_common_ptr cinfo) 87 | { 88 | JQUANT_TBL *tbl; 89 | 90 | tbl = (JQUANT_TBL *) 91 | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL)); 92 | tbl->sent_table = FALSE; /* make sure this is false in any new table */ 93 | return tbl; 94 | } 95 | 96 | 97 | GLOBAL(JHUFF_TBL *) 98 | jpeg_alloc_huff_table (j_common_ptr cinfo) 99 | { 100 | JHUFF_TBL *tbl; 101 | 102 | tbl = (JHUFF_TBL *) 103 | (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL)); 104 | tbl->sent_table = FALSE; /* make sure this is false in any new table */ 105 | return tbl; 106 | } 107 | -------------------------------------------------------------------------------- /src/jpeglib/jdatadst.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jdatadst.c 3 | * 4 | * Copyright (C) 1994-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains compression data destination routines for the case of 9 | * emitting JPEG data to a file (or any stdio stream). While these routines 10 | * are sufficient for most applications, some will want to use a different 11 | * destination manager. 12 | * IMPORTANT: we assume that fwrite() will correctly transcribe an array of 13 | * JOCTETs into 8-bit-wide elements on external storage. If char is wider 14 | * than 8 bits on your machine, you may need to do some tweaking. 15 | */ 16 | 17 | /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 18 | #include "jinclude.h" 19 | #include "jpeglib.h" 20 | #include "jerror.h" 21 | 22 | 23 | /* Expanded data destination object for stdio output */ 24 | 25 | typedef struct { 26 | struct jpeg_destination_mgr pub; /* public fields */ 27 | 28 | FILE * outfile; /* target stream */ 29 | JOCTET * buffer; /* start of buffer */ 30 | } my_destination_mgr; 31 | 32 | typedef my_destination_mgr * my_dest_ptr; 33 | 34 | #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ 35 | 36 | 37 | /* 38 | * Initialize destination --- called by jpeg_start_compress 39 | * before any data is actually written. 40 | */ 41 | 42 | METHODDEF(void) 43 | init_destination (j_compress_ptr cinfo) 44 | { 45 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; 46 | 47 | /* Allocate the output buffer --- it will be released when done with image */ 48 | dest->buffer = (JOCTET *) 49 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 50 | OUTPUT_BUF_SIZE * SIZEOF(JOCTET)); 51 | 52 | dest->pub.next_output_byte = dest->buffer; 53 | dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; 54 | } 55 | 56 | 57 | /* 58 | * Empty the output buffer --- called whenever buffer fills up. 59 | * 60 | * In typical applications, this should write the entire output buffer 61 | * (ignoring the current state of next_output_byte & free_in_buffer), 62 | * reset the pointer & count to the start of the buffer, and return TRUE 63 | * indicating that the buffer has been dumped. 64 | * 65 | * In applications that need to be able to suspend compression due to output 66 | * overrun, a FALSE return indicates that the buffer cannot be emptied now. 67 | * In this situation, the compressor will return to its caller (possibly with 68 | * an indication that it has not accepted all the supplied scanlines). The 69 | * application should resume compression after it has made more room in the 70 | * output buffer. Note that there are substantial restrictions on the use of 71 | * suspension --- see the documentation. 72 | * 73 | * When suspending, the compressor will back up to a convenient restart point 74 | * (typically the start of the current MCU). next_output_byte & free_in_buffer 75 | * indicate where the restart point will be if the current call returns FALSE. 76 | * Data beyond this point will be regenerated after resumption, so do not 77 | * write it out when emptying the buffer externally. 78 | */ 79 | 80 | METHODDEF(boolean) 81 | empty_output_buffer (j_compress_ptr cinfo) 82 | { 83 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; 84 | 85 | if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != 86 | (size_t) OUTPUT_BUF_SIZE) 87 | ERREXIT(cinfo, JERR_FILE_WRITE); 88 | 89 | dest->pub.next_output_byte = dest->buffer; 90 | dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; 91 | 92 | return TRUE; 93 | } 94 | 95 | 96 | /* 97 | * Terminate destination --- called by jpeg_finish_compress 98 | * after all data has been written. Usually needs to flush buffer. 99 | * 100 | * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 101 | * application must deal with any cleanup that should happen even 102 | * for error exit. 103 | */ 104 | 105 | METHODDEF(void) 106 | term_destination (j_compress_ptr cinfo) 107 | { 108 | my_dest_ptr dest = (my_dest_ptr) cinfo->dest; 109 | size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; 110 | 111 | /* Write any data remaining in the buffer */ 112 | if (datacount > 0) { 113 | if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount) 114 | ERREXIT(cinfo, JERR_FILE_WRITE); 115 | } 116 | fflush(dest->outfile); 117 | /* Make sure we wrote the output file OK */ 118 | if (ferror(dest->outfile)) 119 | ERREXIT(cinfo, JERR_FILE_WRITE); 120 | } 121 | 122 | 123 | /* 124 | * Prepare for output to a stdio stream. 125 | * The caller must have already opened the stream, and is responsible 126 | * for closing it after finishing compression. 127 | */ 128 | 129 | GLOBAL(void) 130 | jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) 131 | { 132 | my_dest_ptr dest; 133 | 134 | /* The destination object is made permanent so that multiple JPEG images 135 | * can be written to the same file without re-executing jpeg_stdio_dest. 136 | * This makes it dangerous to use this manager and a different destination 137 | * manager serially with the same JPEG object, because their private object 138 | * sizes may be different. Caveat programmer. 139 | */ 140 | if (cinfo->dest == NULL) { /* first time for this JPEG object? */ 141 | cinfo->dest = (struct jpeg_destination_mgr *) 142 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 143 | SIZEOF(my_destination_mgr)); 144 | } 145 | 146 | dest = (my_dest_ptr) cinfo->dest; 147 | dest->pub.init_destination = init_destination; 148 | dest->pub.empty_output_buffer = empty_output_buffer; 149 | dest->pub.term_destination = term_destination; 150 | dest->outfile = outfile; 151 | } 152 | -------------------------------------------------------------------------------- /src/jpeglib/jdatasrc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jdatasrc.c 3 | * 4 | * Copyright (C) 1994-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains decompression data source routines for the case of 9 | * reading JPEG data from a file (or any stdio stream). While these routines 10 | * are sufficient for most applications, some will want to use a different 11 | * source manager. 12 | * IMPORTANT: we assume that fread() will correctly transcribe an array of 13 | * JOCTETs from 8-bit-wide elements on external storage. If char is wider 14 | * than 8 bits on your machine, you may need to do some tweaking. 15 | */ 16 | 17 | /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 18 | #include "jinclude.h" 19 | #include "jpeglib.h" 20 | #include "jerror.h" 21 | 22 | 23 | /* Expanded data source object for stdio input */ 24 | 25 | typedef struct { 26 | struct jpeg_source_mgr pub; /* public fields */ 27 | 28 | FILE * infile; /* source stream */ 29 | JOCTET * buffer; /* start of buffer */ 30 | boolean start_of_file; /* have we gotten any data yet? */ 31 | } my_source_mgr; 32 | 33 | typedef my_source_mgr * my_src_ptr; 34 | 35 | #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ 36 | 37 | 38 | /* 39 | * Initialize source --- called by jpeg_read_header 40 | * before any data is actually read. 41 | */ 42 | 43 | METHODDEF(void) 44 | init_source (j_decompress_ptr cinfo) 45 | { 46 | my_src_ptr src = (my_src_ptr) cinfo->src; 47 | 48 | /* We reset the empty-input-file flag for each image, 49 | * but we don't clear the input buffer. 50 | * This is correct behavior for reading a series of images from one source. 51 | */ 52 | src->start_of_file = TRUE; 53 | } 54 | 55 | 56 | /* 57 | * Fill the input buffer --- called whenever buffer is emptied. 58 | * 59 | * In typical applications, this should read fresh data into the buffer 60 | * (ignoring the current state of next_input_byte & bytes_in_buffer), 61 | * reset the pointer & count to the start of the buffer, and return TRUE 62 | * indicating that the buffer has been reloaded. It is not necessary to 63 | * fill the buffer entirely, only to obtain at least one more byte. 64 | * 65 | * There is no such thing as an EOF return. If the end of the file has been 66 | * reached, the routine has a choice of ERREXIT() or inserting fake data into 67 | * the buffer. In most cases, generating a warning message and inserting a 68 | * fake EOI marker is the best course of action --- this will allow the 69 | * decompressor to output however much of the image is there. However, 70 | * the resulting error message is misleading if the real problem is an empty 71 | * input file, so we handle that case specially. 72 | * 73 | * In applications that need to be able to suspend compression due to input 74 | * not being available yet, a FALSE return indicates that no more data can be 75 | * obtained right now, but more may be forthcoming later. In this situation, 76 | * the decompressor will return to its caller (with an indication of the 77 | * number of scanlines it has read, if any). The application should resume 78 | * decompression after it has loaded more data into the input buffer. Note 79 | * that there are substantial restrictions on the use of suspension --- see 80 | * the documentation. 81 | * 82 | * When suspending, the decompressor will back up to a convenient restart point 83 | * (typically the start of the current MCU). next_input_byte & bytes_in_buffer 84 | * indicate where the restart point will be if the current call returns FALSE. 85 | * Data beyond this point must be rescanned after resumption, so move it to 86 | * the front of the buffer rather than discarding it. 87 | */ 88 | 89 | METHODDEF(boolean) 90 | fill_input_buffer (j_decompress_ptr cinfo) 91 | { 92 | my_src_ptr src = (my_src_ptr) cinfo->src; 93 | size_t nbytes; 94 | 95 | nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE); 96 | 97 | if (nbytes <= 0) { 98 | if (src->start_of_file) /* Treat empty input file as fatal error */ 99 | ERREXIT(cinfo, JERR_INPUT_EMPTY); 100 | WARNMS(cinfo, JWRN_JPEG_EOF); 101 | /* Insert a fake EOI marker */ 102 | src->buffer[0] = (JOCTET) 0xFF; 103 | src->buffer[1] = (JOCTET) JPEG_EOI; 104 | nbytes = 2; 105 | } 106 | 107 | src->pub.next_input_byte = src->buffer; 108 | src->pub.bytes_in_buffer = nbytes; 109 | src->start_of_file = FALSE; 110 | 111 | return TRUE; 112 | } 113 | 114 | 115 | /* 116 | * Skip data --- used to skip over a potentially large amount of 117 | * uninteresting data (such as an APPn marker). 118 | * 119 | * Writers of suspendable-input applications must note that skip_input_data 120 | * is not granted the right to give a suspension return. If the skip extends 121 | * beyond the data currently in the buffer, the buffer can be marked empty so 122 | * that the next read will cause a fill_input_buffer call that can suspend. 123 | * Arranging for additional bytes to be discarded before reloading the input 124 | * buffer is the application writer's problem. 125 | */ 126 | 127 | METHODDEF(void) 128 | skip_input_data (j_decompress_ptr cinfo, long num_bytes) 129 | { 130 | my_src_ptr src = (my_src_ptr) cinfo->src; 131 | 132 | /* Just a dumb implementation for now. Could use fseek() except 133 | * it doesn't work on pipes. Not clear that being smart is worth 134 | * any trouble anyway --- large skips are infrequent. 135 | */ 136 | if (num_bytes > 0) { 137 | while (num_bytes > (long) src->pub.bytes_in_buffer) { 138 | num_bytes -= (long) src->pub.bytes_in_buffer; 139 | (void) fill_input_buffer(cinfo); 140 | /* note we assume that fill_input_buffer will never return FALSE, 141 | * so suspension need not be handled. 142 | */ 143 | } 144 | src->pub.next_input_byte += (size_t) num_bytes; 145 | src->pub.bytes_in_buffer -= (size_t) num_bytes; 146 | } 147 | } 148 | 149 | 150 | /* 151 | * An additional method that can be provided by data source modules is the 152 | * resync_to_restart method for error recovery in the presence of RST markers. 153 | * For the moment, this source module just uses the default resync method 154 | * provided by the JPEG library. That method assumes that no backtracking 155 | * is possible. 156 | */ 157 | 158 | 159 | /* 160 | * Terminate source --- called by jpeg_finish_decompress 161 | * after all data has been read. Often a no-op. 162 | * 163 | * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 164 | * application must deal with any cleanup that should happen even 165 | * for error exit. 166 | */ 167 | 168 | METHODDEF(void) 169 | term_source (j_decompress_ptr cinfo) 170 | { 171 | /* no work necessary here */ 172 | } 173 | 174 | 175 | /* 176 | * Prepare for input from a stdio stream. 177 | * The caller must have already opened the stream, and is responsible 178 | * for closing it after finishing decompression. 179 | */ 180 | 181 | GLOBAL(void) 182 | jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile) 183 | { 184 | my_src_ptr src; 185 | 186 | /* The source object and input buffer are made permanent so that a series 187 | * of JPEG images can be read from the same file by calling jpeg_stdio_src 188 | * only before the first one. (If we discarded the buffer at the end of 189 | * one image, we'd likely lose the start of the next one.) 190 | * This makes it unsafe to use this manager and a different source 191 | * manager serially with the same JPEG object. Caveat programmer. 192 | */ 193 | if (cinfo->src == NULL) { /* first time for this JPEG object? */ 194 | cinfo->src = (struct jpeg_source_mgr *) 195 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 196 | SIZEOF(my_source_mgr)); 197 | src = (my_src_ptr) cinfo->src; 198 | src->buffer = (JOCTET *) 199 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 200 | INPUT_BUF_SIZE * SIZEOF(JOCTET)); 201 | } 202 | 203 | src = (my_src_ptr) cinfo->src; 204 | src->pub.init_source = init_source; 205 | src->pub.fill_input_buffer = fill_input_buffer; 206 | src->pub.skip_input_data = skip_input_data; 207 | src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 208 | src->pub.term_source = term_source; 209 | src->infile = infile; 210 | src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ 211 | src->pub.next_input_byte = NULL; /* until buffer loaded */ 212 | } 213 | -------------------------------------------------------------------------------- /src/jpeglib/jdtrans.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jdtrans.c 3 | * 4 | * Copyright (C) 1995-1997, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains library routines for transcoding decompression, 9 | * that is, reading raw DCT coefficient arrays from an input JPEG file. 10 | * The routines in jdapimin.c will also be needed by a transcoder. 11 | */ 12 | 13 | #define JPEG_INTERNALS 14 | #include "jinclude.h" 15 | #include "jpeglib.h" 16 | 17 | 18 | /* Forward declarations */ 19 | LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo)); 20 | 21 | 22 | /* 23 | * Read the coefficient arrays from a JPEG file. 24 | * jpeg_read_header must be completed before calling this. 25 | * 26 | * The entire image is read into a set of virtual coefficient-block arrays, 27 | * one per component. The return value is a pointer to the array of 28 | * virtual-array descriptors. These can be manipulated directly via the 29 | * JPEG memory manager, or handed off to jpeg_write_coefficients(). 30 | * To release the memory occupied by the virtual arrays, call 31 | * jpeg_finish_decompress() when done with the data. 32 | * 33 | * An alternative usage is to simply obtain access to the coefficient arrays 34 | * during a buffered-image-mode decompression operation. This is allowed 35 | * after any jpeg_finish_output() call. The arrays can be accessed until 36 | * jpeg_finish_decompress() is called. (Note that any call to the library 37 | * may reposition the arrays, so don't rely on access_virt_barray() results 38 | * to stay valid across library calls.) 39 | * 40 | * Returns NULL if suspended. This case need be checked only if 41 | * a suspending data source is used. 42 | */ 43 | 44 | GLOBAL(jvirt_barray_ptr *) 45 | jpeg_read_coefficients (j_decompress_ptr cinfo) 46 | { 47 | if (cinfo->global_state == DSTATE_READY) { 48 | /* First call: initialize active modules */ 49 | transdecode_master_selection(cinfo); 50 | cinfo->global_state = DSTATE_RDCOEFS; 51 | } 52 | if (cinfo->global_state == DSTATE_RDCOEFS) { 53 | /* Absorb whole file into the coef buffer */ 54 | for (;;) { 55 | int retcode; 56 | /* Call progress monitor hook if present */ 57 | if (cinfo->progress != NULL) 58 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); 59 | /* Absorb some more input */ 60 | retcode = (*cinfo->inputctl->consume_input) (cinfo); 61 | if (retcode == JPEG_SUSPENDED) 62 | return NULL; 63 | if (retcode == JPEG_REACHED_EOI) 64 | break; 65 | /* Advance progress counter if appropriate */ 66 | if (cinfo->progress != NULL && 67 | (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { 68 | if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { 69 | /* startup underestimated number of scans; ratchet up one scan */ 70 | cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; 71 | } 72 | } 73 | } 74 | /* Set state so that jpeg_finish_decompress does the right thing */ 75 | cinfo->global_state = DSTATE_STOPPING; 76 | } 77 | /* At this point we should be in state DSTATE_STOPPING if being used 78 | * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access 79 | * to the coefficients during a full buffered-image-mode decompression. 80 | */ 81 | if ((cinfo->global_state == DSTATE_STOPPING || 82 | cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { 83 | return cinfo->coef->coef_arrays; 84 | } 85 | /* Oops, improper usage */ 86 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 87 | return NULL; /* keep compiler happy */ 88 | } 89 | 90 | 91 | /* 92 | * Master selection of decompression modules for transcoding. 93 | * This substitutes for jdmaster.c's initialization of the full decompressor. 94 | */ 95 | 96 | LOCAL(void) 97 | transdecode_master_selection (j_decompress_ptr cinfo) 98 | { 99 | /* This is effectively a buffered-image operation. */ 100 | cinfo->buffered_image = TRUE; 101 | 102 | /* Entropy decoding: either Huffman or arithmetic coding. */ 103 | if (cinfo->arith_code) { 104 | ERREXIT(cinfo, JERR_ARITH_NOTIMPL); 105 | } else { 106 | if (cinfo->progressive_mode) { 107 | #ifdef D_PROGRESSIVE_SUPPORTED 108 | jinit_phuff_decoder(cinfo); 109 | #else 110 | ERREXIT(cinfo, JERR_NOT_COMPILED); 111 | #endif 112 | } else 113 | jinit_huff_decoder(cinfo); 114 | } 115 | 116 | /* Always get a full-image coefficient buffer. */ 117 | jinit_d_coef_controller(cinfo, TRUE); 118 | 119 | /* We can now tell the memory manager to allocate virtual arrays. */ 120 | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); 121 | 122 | /* Initialize input side of decompressor to consume first scan. */ 123 | (*cinfo->inputctl->start_input_pass) (cinfo); 124 | 125 | /* Initialize progress monitoring. */ 126 | if (cinfo->progress != NULL) { 127 | int nscans; 128 | /* Estimate number of scans to set pass_limit. */ 129 | if (cinfo->progressive_mode) { 130 | /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ 131 | nscans = 2 + 3 * cinfo->num_components; 132 | } else if (cinfo->inputctl->has_multiple_scans) { 133 | /* For a nonprogressive multiscan file, estimate 1 scan per component. */ 134 | nscans = cinfo->num_components; 135 | } else { 136 | nscans = 1; 137 | } 138 | cinfo->progress->pass_counter = 0L; 139 | cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; 140 | cinfo->progress->completed_passes = 0; 141 | cinfo->progress->total_passes = 1; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/jpeglib/jerror.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jerror.c 3 | * 4 | * Copyright (C) 1991-1998, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains simple error-reporting and trace-message routines. 9 | * These are suitable for Unix-like systems and others where writing to 10 | * stderr is the right thing to do. Many applications will want to replace 11 | * some or all of these routines. 12 | * 13 | * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile, 14 | * you get a Windows-specific hack to display error messages in a dialog box. 15 | * It ain't much, but it beats dropping error messages into the bit bucket, 16 | * which is what happens to output to stderr under most Windows C compilers. 17 | * 18 | * These routines are used by both the compression and decompression code. 19 | */ 20 | 21 | /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ 22 | #include 23 | 24 | #include "jinclude.h" 25 | #include "jpeglib.h" 26 | #include "jversion.h" 27 | #include "jerror.h" 28 | 29 | #ifdef USE_WINDOWS_MESSAGEBOX 30 | #include 31 | #endif 32 | 33 | #ifndef EXIT_FAILURE /* define exit() codes if not provided */ 34 | #define EXIT_FAILURE 1 35 | #endif 36 | 37 | 38 | /* 39 | * Create the message string table. 40 | * We do this from the master message list in jerror.h by re-reading 41 | * jerror.h with a suitable definition for macro JMESSAGE. 42 | * The message table is made an external symbol just in case any applications 43 | * want to refer to it directly. 44 | */ 45 | 46 | #ifdef NEED_SHORT_EXTERNAL_NAMES 47 | #define jpeg_std_message_table jMsgTable 48 | #endif 49 | 50 | #define JMESSAGE(code,string) string , 51 | 52 | const char * const jpeg_std_message_table[] = { 53 | #include "jerror.h" 54 | NULL 55 | }; 56 | 57 | 58 | /* 59 | * Error exit handler: must not return to caller. 60 | * 61 | * Applications may override this if they want to get control back after 62 | * an error. Typically one would longjmp somewhere instead of exiting. 63 | * The setjmp buffer can be made a private field within an expanded error 64 | * handler object. Note that the info needed to generate an error message 65 | * is stored in the error object, so you can generate the message now or 66 | * later, at your convenience. 67 | * You should make sure that the JPEG object is cleaned up (with jpeg_abort 68 | * or jpeg_destroy) at some point. 69 | */ 70 | 71 | METHODDEF(void) 72 | error_exit (j_common_ptr cinfo) 73 | { 74 | /* Always display the message */ 75 | (*cinfo->err->output_message) (cinfo); 76 | 77 | /* Let the memory manager delete any temp files before we die */ 78 | jpeg_destroy(cinfo); 79 | 80 | exit(EXIT_FAILURE); 81 | } 82 | 83 | 84 | /* 85 | * Actual output of an error or trace message. 86 | * Applications may override this method to send JPEG messages somewhere 87 | * other than stderr. 88 | * 89 | * On Windows, printing to stderr is generally completely useless, 90 | * so we provide optional code to produce an error-dialog popup. 91 | * Most Windows applications will still prefer to override this routine, 92 | * but if they don't, it'll do something at least marginally useful. 93 | * 94 | * NOTE: to use the library in an environment that doesn't support the 95 | * C stdio library, you may have to delete the call to fprintf() entirely, 96 | * not just not use this routine. 97 | */ 98 | 99 | METHODDEF(void) 100 | output_message (j_common_ptr cinfo) 101 | { 102 | char buffer[JMSG_LENGTH_MAX]; 103 | 104 | /* Create the message */ 105 | (*cinfo->err->format_message) (cinfo, buffer); 106 | 107 | #ifdef USE_WINDOWS_MESSAGEBOX 108 | /* Display it in a message dialog box */ 109 | MessageBox(GetActiveWindow(), buffer, "JPEG Library Error", 110 | MB_OK | MB_ICONERROR); 111 | #else 112 | /* Send it to stderr, adding a newline */ 113 | fprintf(stderr, "%s\n", buffer); 114 | #endif 115 | } 116 | 117 | 118 | /* 119 | * Decide whether to emit a trace or warning message. 120 | * msg_level is one of: 121 | * -1: recoverable corrupt-data warning, may want to abort. 122 | * 0: important advisory messages (always display to user). 123 | * 1: first level of tracing detail. 124 | * 2,3,...: successively more detailed tracing messages. 125 | * An application might override this method if it wanted to abort on warnings 126 | * or change the policy about which messages to display. 127 | */ 128 | 129 | METHODDEF(void) 130 | emit_message (j_common_ptr cinfo, int msg_level) 131 | { 132 | struct jpeg_error_mgr * err = cinfo->err; 133 | 134 | if (msg_level < 0) { 135 | /* It's a warning message. Since corrupt files may generate many warnings, 136 | * the policy implemented here is to show only the first warning, 137 | * unless trace_level >= 3. 138 | */ 139 | if (err->num_warnings == 0 || err->trace_level >= 3) 140 | (*err->output_message) (cinfo); 141 | /* Always count warnings in num_warnings. */ 142 | err->num_warnings++; 143 | } else { 144 | /* It's a trace message. Show it if trace_level >= msg_level. */ 145 | if (err->trace_level >= msg_level) 146 | (*err->output_message) (cinfo); 147 | } 148 | } 149 | 150 | 151 | /* 152 | * Format a message string for the most recent JPEG error or message. 153 | * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX 154 | * characters. Note that no '\n' character is added to the string. 155 | * Few applications should need to override this method. 156 | */ 157 | 158 | METHODDEF(void) 159 | format_message (j_common_ptr cinfo, char * buffer) 160 | { 161 | struct jpeg_error_mgr * err = cinfo->err; 162 | int msg_code = err->msg_code; 163 | const char * msgtext = NULL; 164 | const char * msgptr; 165 | char ch; 166 | boolean isstring; 167 | 168 | /* Look up message string in proper table */ 169 | if (msg_code > 0 && msg_code <= err->last_jpeg_message) { 170 | msgtext = err->jpeg_message_table[msg_code]; 171 | } else if (err->addon_message_table != NULL && 172 | msg_code >= err->first_addon_message && 173 | msg_code <= err->last_addon_message) { 174 | msgtext = err->addon_message_table[msg_code - err->first_addon_message]; 175 | } 176 | 177 | /* Defend against bogus message number */ 178 | if (msgtext == NULL) { 179 | err->msg_parm.i[0] = msg_code; 180 | msgtext = err->jpeg_message_table[0]; 181 | } 182 | 183 | /* Check for string parameter, as indicated by %s in the message text */ 184 | isstring = FALSE; 185 | msgptr = msgtext; 186 | while ((ch = *msgptr++) != '\0') { 187 | if (ch == '%') { 188 | if (*msgptr == 's') isstring = TRUE; 189 | break; 190 | } 191 | } 192 | 193 | /* Format the message into the passed buffer */ 194 | if (isstring) 195 | sprintf(buffer, msgtext, err->msg_parm.s); 196 | else 197 | sprintf(buffer, msgtext, 198 | err->msg_parm.i[0], err->msg_parm.i[1], 199 | err->msg_parm.i[2], err->msg_parm.i[3], 200 | err->msg_parm.i[4], err->msg_parm.i[5], 201 | err->msg_parm.i[6], err->msg_parm.i[7]); 202 | } 203 | 204 | 205 | /* 206 | * Reset error state variables at start of a new image. 207 | * This is called during compression startup to reset trace/error 208 | * processing to default state, without losing any application-specific 209 | * method pointers. An application might possibly want to override 210 | * this method if it has additional error processing state. 211 | */ 212 | 213 | METHODDEF(void) 214 | reset_error_mgr (j_common_ptr cinfo) 215 | { 216 | cinfo->err->num_warnings = 0; 217 | /* trace_level is not reset since it is an application-supplied parameter */ 218 | cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */ 219 | } 220 | 221 | 222 | /* 223 | * Fill in the standard error-handling methods in a jpeg_error_mgr object. 224 | * Typical call is: 225 | * struct jpeg_compress_struct cinfo; 226 | * struct jpeg_error_mgr err; 227 | * 228 | * cinfo.err = jpeg_std_error(&err); 229 | * after which the application may override some of the methods. 230 | */ 231 | 232 | GLOBAL(struct jpeg_error_mgr *) 233 | jpeg_std_error (struct jpeg_error_mgr * err) 234 | { 235 | err->error_exit = error_exit; 236 | err->emit_message = emit_message; 237 | err->output_message = output_message; 238 | err->format_message = format_message; 239 | err->reset_error_mgr = reset_error_mgr; 240 | 241 | err->trace_level = 0; /* default = no tracing */ 242 | err->num_warnings = 0; /* no warnings emitted yet */ 243 | err->msg_code = 0; /* may be useful as a flag for "no error" */ 244 | 245 | /* Initialize message table pointers */ 246 | err->jpeg_message_table = jpeg_std_message_table; 247 | err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1; 248 | 249 | err->addon_message_table = NULL; 250 | err->first_addon_message = 0; /* for safety */ 251 | err->last_addon_message = 0; 252 | 253 | return err; 254 | } 255 | -------------------------------------------------------------------------------- /src/jpeglib/jfdctflt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jfdctflt.c 3 | * 4 | * Copyright (C) 1994-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains a floating-point implementation of the 9 | * forward DCT (Discrete Cosine Transform). 10 | * 11 | * This implementation should be more accurate than either of the integer 12 | * DCT implementations. However, it may not give the same results on all 13 | * machines because of differences in roundoff behavior. Speed will depend 14 | * on the hardware's floating point capacity. 15 | * 16 | * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT 17 | * on each column. Direct algorithms are also available, but they are 18 | * much more complex and seem not to be any faster when reduced to code. 19 | * 20 | * This implementation is based on Arai, Agui, and Nakajima's algorithm for 21 | * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in 22 | * Japanese, but the algorithm is described in the Pennebaker & Mitchell 23 | * JPEG textbook (see REFERENCES section in file README). The following code 24 | * is based directly on figure 4-8 in P&M. 25 | * While an 8-point DCT cannot be done in less than 11 multiplies, it is 26 | * possible to arrange the computation so that many of the multiplies are 27 | * simple scalings of the final outputs. These multiplies can then be 28 | * folded into the multiplications or divisions by the JPEG quantization 29 | * table entries. The AA&N method leaves only 5 multiplies and 29 adds 30 | * to be done in the DCT itself. 31 | * The primary disadvantage of this method is that with a fixed-point 32 | * implementation, accuracy is lost due to imprecise representation of the 33 | * scaled quantization values. However, that problem does not arise if 34 | * we use floating point arithmetic. 35 | */ 36 | 37 | #define JPEG_INTERNALS 38 | #include "jinclude.h" 39 | #include "jpeglib.h" 40 | #include "jdct.h" /* Private declarations for DCT subsystem */ 41 | 42 | #ifdef DCT_FLOAT_SUPPORTED 43 | 44 | 45 | /* 46 | * This module is specialized to the case DCTSIZE = 8. 47 | */ 48 | 49 | #if DCTSIZE != 8 50 | Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ 51 | #endif 52 | 53 | 54 | /* 55 | * Perform the forward DCT on one block of samples. 56 | */ 57 | 58 | GLOBAL(void) 59 | jpeg_fdct_float (FAST_FLOAT * data) 60 | { 61 | FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; 62 | FAST_FLOAT tmp10, tmp11, tmp12, tmp13; 63 | FAST_FLOAT z1, z2, z3, z4, z5, z11, z13; 64 | FAST_FLOAT *dataptr; 65 | int ctr; 66 | 67 | /* Pass 1: process rows. */ 68 | 69 | dataptr = data; 70 | for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { 71 | tmp0 = dataptr[0] + dataptr[7]; 72 | tmp7 = dataptr[0] - dataptr[7]; 73 | tmp1 = dataptr[1] + dataptr[6]; 74 | tmp6 = dataptr[1] - dataptr[6]; 75 | tmp2 = dataptr[2] + dataptr[5]; 76 | tmp5 = dataptr[2] - dataptr[5]; 77 | tmp3 = dataptr[3] + dataptr[4]; 78 | tmp4 = dataptr[3] - dataptr[4]; 79 | 80 | /* Even part */ 81 | 82 | tmp10 = tmp0 + tmp3; /* phase 2 */ 83 | tmp13 = tmp0 - tmp3; 84 | tmp11 = tmp1 + tmp2; 85 | tmp12 = tmp1 - tmp2; 86 | 87 | dataptr[0] = tmp10 + tmp11; /* phase 3 */ 88 | dataptr[4] = tmp10 - tmp11; 89 | 90 | z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */ 91 | dataptr[2] = tmp13 + z1; /* phase 5 */ 92 | dataptr[6] = tmp13 - z1; 93 | 94 | /* Odd part */ 95 | 96 | tmp10 = tmp4 + tmp5; /* phase 2 */ 97 | tmp11 = tmp5 + tmp6; 98 | tmp12 = tmp6 + tmp7; 99 | 100 | /* The rotator is modified from fig 4-8 to avoid extra negations. */ 101 | z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */ 102 | z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */ 103 | z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */ 104 | z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */ 105 | 106 | z11 = tmp7 + z3; /* phase 5 */ 107 | z13 = tmp7 - z3; 108 | 109 | dataptr[5] = z13 + z2; /* phase 6 */ 110 | dataptr[3] = z13 - z2; 111 | dataptr[1] = z11 + z4; 112 | dataptr[7] = z11 - z4; 113 | 114 | dataptr += DCTSIZE; /* advance pointer to next row */ 115 | } 116 | 117 | /* Pass 2: process columns. */ 118 | 119 | dataptr = data; 120 | for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { 121 | tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; 122 | tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; 123 | tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; 124 | tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; 125 | tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; 126 | tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; 127 | tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; 128 | tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; 129 | 130 | /* Even part */ 131 | 132 | tmp10 = tmp0 + tmp3; /* phase 2 */ 133 | tmp13 = tmp0 - tmp3; 134 | tmp11 = tmp1 + tmp2; 135 | tmp12 = tmp1 - tmp2; 136 | 137 | dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */ 138 | dataptr[DCTSIZE*4] = tmp10 - tmp11; 139 | 140 | z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */ 141 | dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */ 142 | dataptr[DCTSIZE*6] = tmp13 - z1; 143 | 144 | /* Odd part */ 145 | 146 | tmp10 = tmp4 + tmp5; /* phase 2 */ 147 | tmp11 = tmp5 + tmp6; 148 | tmp12 = tmp6 + tmp7; 149 | 150 | /* The rotator is modified from fig 4-8 to avoid extra negations. */ 151 | z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */ 152 | z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */ 153 | z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */ 154 | z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */ 155 | 156 | z11 = tmp7 + z3; /* phase 5 */ 157 | z13 = tmp7 - z3; 158 | 159 | dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */ 160 | dataptr[DCTSIZE*3] = z13 - z2; 161 | dataptr[DCTSIZE*1] = z11 + z4; 162 | dataptr[DCTSIZE*7] = z11 - z4; 163 | 164 | dataptr++; /* advance pointer to next column */ 165 | } 166 | } 167 | 168 | #endif /* DCT_FLOAT_SUPPORTED */ 169 | -------------------------------------------------------------------------------- /src/jpeglib/jfdctfst.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jfdctfst.c 3 | * 4 | * Copyright (C) 1994-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains a fast, not so accurate integer implementation of the 9 | * forward DCT (Discrete Cosine Transform). 10 | * 11 | * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT 12 | * on each column. Direct algorithms are also available, but they are 13 | * much more complex and seem not to be any faster when reduced to code. 14 | * 15 | * This implementation is based on Arai, Agui, and Nakajima's algorithm for 16 | * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in 17 | * Japanese, but the algorithm is described in the Pennebaker & Mitchell 18 | * JPEG textbook (see REFERENCES section in file README). The following code 19 | * is based directly on figure 4-8 in P&M. 20 | * While an 8-point DCT cannot be done in less than 11 multiplies, it is 21 | * possible to arrange the computation so that many of the multiplies are 22 | * simple scalings of the final outputs. These multiplies can then be 23 | * folded into the multiplications or divisions by the JPEG quantization 24 | * table entries. The AA&N method leaves only 5 multiplies and 29 adds 25 | * to be done in the DCT itself. 26 | * The primary disadvantage of this method is that with fixed-point math, 27 | * accuracy is lost due to imprecise representation of the scaled 28 | * quantization values. The smaller the quantization table entry, the less 29 | * precise the scaled value, so this implementation does worse with high- 30 | * quality-setting files than with low-quality ones. 31 | */ 32 | 33 | #define JPEG_INTERNALS 34 | #include "jinclude.h" 35 | #include "jpeglib.h" 36 | #include "jdct.h" /* Private declarations for DCT subsystem */ 37 | 38 | #ifdef DCT_IFAST_SUPPORTED 39 | 40 | 41 | /* 42 | * This module is specialized to the case DCTSIZE = 8. 43 | */ 44 | 45 | #if DCTSIZE != 8 46 | Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ 47 | #endif 48 | 49 | 50 | /* Scaling decisions are generally the same as in the LL&M algorithm; 51 | * see jfdctint.c for more details. However, we choose to descale 52 | * (right shift) multiplication products as soon as they are formed, 53 | * rather than carrying additional fractional bits into subsequent additions. 54 | * This compromises accuracy slightly, but it lets us save a few shifts. 55 | * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) 56 | * everywhere except in the multiplications proper; this saves a good deal 57 | * of work on 16-bit-int machines. 58 | * 59 | * Again to save a few shifts, the intermediate results between pass 1 and 60 | * pass 2 are not upscaled, but are represented only to integral precision. 61 | * 62 | * A final compromise is to represent the multiplicative constants to only 63 | * 8 fractional bits, rather than 13. This saves some shifting work on some 64 | * machines, and may also reduce the cost of multiplication (since there 65 | * are fewer one-bits in the constants). 66 | */ 67 | 68 | #define CONST_BITS 8 69 | 70 | 71 | /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus 72 | * causing a lot of useless floating-point operations at run time. 73 | * To get around this we use the following pre-calculated constants. 74 | * If you change CONST_BITS you may want to add appropriate values. 75 | * (With a reasonable C compiler, you can just rely on the FIX() macro...) 76 | */ 77 | 78 | #if CONST_BITS == 8 79 | #define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */ 80 | #define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */ 81 | #define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */ 82 | #define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */ 83 | #else 84 | #define FIX_0_382683433 FIX(0.382683433) 85 | #define FIX_0_541196100 FIX(0.541196100) 86 | #define FIX_0_707106781 FIX(0.707106781) 87 | #define FIX_1_306562965 FIX(1.306562965) 88 | #endif 89 | 90 | 91 | /* We can gain a little more speed, with a further compromise in accuracy, 92 | * by omitting the addition in a descaling shift. This yields an incorrectly 93 | * rounded result half the time... 94 | */ 95 | 96 | #ifndef USE_ACCURATE_ROUNDING 97 | #undef DESCALE 98 | #define DESCALE(x,n) RIGHT_SHIFT(x, n) 99 | #endif 100 | 101 | 102 | /* Multiply a DCTELEM variable by an INT32 constant, and immediately 103 | * descale to yield a DCTELEM result. 104 | */ 105 | 106 | #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) 107 | 108 | 109 | /* 110 | * Perform the forward DCT on one block of samples. 111 | */ 112 | 113 | GLOBAL(void) 114 | jpeg_fdct_ifast (DCTELEM * data) 115 | { 116 | DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; 117 | DCTELEM tmp10, tmp11, tmp12, tmp13; 118 | DCTELEM z1, z2, z3, z4, z5, z11, z13; 119 | DCTELEM *dataptr; 120 | int ctr; 121 | SHIFT_TEMPS 122 | 123 | /* Pass 1: process rows. */ 124 | 125 | dataptr = data; 126 | for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { 127 | tmp0 = dataptr[0] + dataptr[7]; 128 | tmp7 = dataptr[0] - dataptr[7]; 129 | tmp1 = dataptr[1] + dataptr[6]; 130 | tmp6 = dataptr[1] - dataptr[6]; 131 | tmp2 = dataptr[2] + dataptr[5]; 132 | tmp5 = dataptr[2] - dataptr[5]; 133 | tmp3 = dataptr[3] + dataptr[4]; 134 | tmp4 = dataptr[3] - dataptr[4]; 135 | 136 | /* Even part */ 137 | 138 | tmp10 = tmp0 + tmp3; /* phase 2 */ 139 | tmp13 = tmp0 - tmp3; 140 | tmp11 = tmp1 + tmp2; 141 | tmp12 = tmp1 - tmp2; 142 | 143 | dataptr[0] = tmp10 + tmp11; /* phase 3 */ 144 | dataptr[4] = tmp10 - tmp11; 145 | 146 | z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */ 147 | dataptr[2] = tmp13 + z1; /* phase 5 */ 148 | dataptr[6] = tmp13 - z1; 149 | 150 | /* Odd part */ 151 | 152 | tmp10 = tmp4 + tmp5; /* phase 2 */ 153 | tmp11 = tmp5 + tmp6; 154 | tmp12 = tmp6 + tmp7; 155 | 156 | /* The rotator is modified from fig 4-8 to avoid extra negations. */ 157 | z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */ 158 | z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */ 159 | z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */ 160 | z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */ 161 | 162 | z11 = tmp7 + z3; /* phase 5 */ 163 | z13 = tmp7 - z3; 164 | 165 | dataptr[5] = z13 + z2; /* phase 6 */ 166 | dataptr[3] = z13 - z2; 167 | dataptr[1] = z11 + z4; 168 | dataptr[7] = z11 - z4; 169 | 170 | dataptr += DCTSIZE; /* advance pointer to next row */ 171 | } 172 | 173 | /* Pass 2: process columns. */ 174 | 175 | dataptr = data; 176 | for (ctr = DCTSIZE-1; ctr >= 0; ctr--) { 177 | tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7]; 178 | tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7]; 179 | tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6]; 180 | tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6]; 181 | tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5]; 182 | tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5]; 183 | tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4]; 184 | tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4]; 185 | 186 | /* Even part */ 187 | 188 | tmp10 = tmp0 + tmp3; /* phase 2 */ 189 | tmp13 = tmp0 - tmp3; 190 | tmp11 = tmp1 + tmp2; 191 | tmp12 = tmp1 - tmp2; 192 | 193 | dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */ 194 | dataptr[DCTSIZE*4] = tmp10 - tmp11; 195 | 196 | z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */ 197 | dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */ 198 | dataptr[DCTSIZE*6] = tmp13 - z1; 199 | 200 | /* Odd part */ 201 | 202 | tmp10 = tmp4 + tmp5; /* phase 2 */ 203 | tmp11 = tmp5 + tmp6; 204 | tmp12 = tmp6 + tmp7; 205 | 206 | /* The rotator is modified from fig 4-8 to avoid extra negations. */ 207 | z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */ 208 | z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */ 209 | z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */ 210 | z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */ 211 | 212 | z11 = tmp7 + z3; /* phase 5 */ 213 | z13 = tmp7 - z3; 214 | 215 | dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */ 216 | dataptr[DCTSIZE*3] = z13 - z2; 217 | dataptr[DCTSIZE*1] = z11 + z4; 218 | dataptr[DCTSIZE*7] = z11 - z4; 219 | 220 | dataptr++; /* advance pointer to next column */ 221 | } 222 | } 223 | 224 | #endif /* DCT_IFAST_SUPPORTED */ 225 | -------------------------------------------------------------------------------- /src/jpeglib/jidctflt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jidctflt.c 3 | * 4 | * Copyright (C) 1994-1998, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains a floating-point implementation of the 9 | * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine 10 | * must also perform dequantization of the input coefficients. 11 | * 12 | * This implementation should be more accurate than either of the integer 13 | * IDCT implementations. However, it may not give the same results on all 14 | * machines because of differences in roundoff behavior. Speed will depend 15 | * on the hardware's floating point capacity. 16 | * 17 | * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT 18 | * on each row (or vice versa, but it's more convenient to emit a row at 19 | * a time). Direct algorithms are also available, but they are much more 20 | * complex and seem not to be any faster when reduced to code. 21 | * 22 | * This implementation is based on Arai, Agui, and Nakajima's algorithm for 23 | * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in 24 | * Japanese, but the algorithm is described in the Pennebaker & Mitchell 25 | * JPEG textbook (see REFERENCES section in file README). The following code 26 | * is based directly on figure 4-8 in P&M. 27 | * While an 8-point DCT cannot be done in less than 11 multiplies, it is 28 | * possible to arrange the computation so that many of the multiplies are 29 | * simple scalings of the final outputs. These multiplies can then be 30 | * folded into the multiplications or divisions by the JPEG quantization 31 | * table entries. The AA&N method leaves only 5 multiplies and 29 adds 32 | * to be done in the DCT itself. 33 | * The primary disadvantage of this method is that with a fixed-point 34 | * implementation, accuracy is lost due to imprecise representation of the 35 | * scaled quantization values. However, that problem does not arise if 36 | * we use floating point arithmetic. 37 | */ 38 | 39 | #define JPEG_INTERNALS 40 | #include "jinclude.h" 41 | #include "jpeglib.h" 42 | #include "jdct.h" /* Private declarations for DCT subsystem */ 43 | 44 | #ifdef DCT_FLOAT_SUPPORTED 45 | 46 | 47 | /* 48 | * This module is specialized to the case DCTSIZE = 8. 49 | */ 50 | 51 | #if DCTSIZE != 8 52 | Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ 53 | #endif 54 | 55 | 56 | /* Dequantize a coefficient by multiplying it by the multiplier-table 57 | * entry; produce a float result. 58 | */ 59 | 60 | #define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval)) 61 | 62 | 63 | /* 64 | * Perform dequantization and inverse DCT on one block of coefficients. 65 | */ 66 | 67 | GLOBAL(void) 68 | jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr, 69 | JCOEFPTR coef_block, 70 | JSAMPARRAY output_buf, JDIMENSION output_col) 71 | { 72 | FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; 73 | FAST_FLOAT tmp10, tmp11, tmp12, tmp13; 74 | FAST_FLOAT z5, z10, z11, z12, z13; 75 | JCOEFPTR inptr; 76 | FLOAT_MULT_TYPE * quantptr; 77 | FAST_FLOAT * wsptr; 78 | JSAMPROW outptr; 79 | JSAMPLE *range_limit = IDCT_range_limit(cinfo); 80 | int ctr; 81 | FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */ 82 | SHIFT_TEMPS 83 | 84 | /* Pass 1: process columns from input, store into work array. */ 85 | 86 | inptr = coef_block; 87 | quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table; 88 | wsptr = workspace; 89 | for (ctr = DCTSIZE; ctr > 0; ctr--) { 90 | /* Due to quantization, we will usually find that many of the input 91 | * coefficients are zero, especially the AC terms. We can exploit this 92 | * by short-circuiting the IDCT calculation for any column in which all 93 | * the AC terms are zero. In that case each output is equal to the 94 | * DC coefficient (with scale factor as needed). 95 | * With typical images and quantization tables, half or more of the 96 | * column DCT calculations can be simplified this way. 97 | */ 98 | 99 | if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && 100 | inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && 101 | inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && 102 | inptr[DCTSIZE*7] == 0) { 103 | /* AC terms all zero */ 104 | FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); 105 | 106 | wsptr[DCTSIZE*0] = dcval; 107 | wsptr[DCTSIZE*1] = dcval; 108 | wsptr[DCTSIZE*2] = dcval; 109 | wsptr[DCTSIZE*3] = dcval; 110 | wsptr[DCTSIZE*4] = dcval; 111 | wsptr[DCTSIZE*5] = dcval; 112 | wsptr[DCTSIZE*6] = dcval; 113 | wsptr[DCTSIZE*7] = dcval; 114 | 115 | inptr++; /* advance pointers to next column */ 116 | quantptr++; 117 | wsptr++; 118 | continue; 119 | } 120 | 121 | /* Even part */ 122 | 123 | tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); 124 | tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); 125 | tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); 126 | tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); 127 | 128 | tmp10 = tmp0 + tmp2; /* phase 3 */ 129 | tmp11 = tmp0 - tmp2; 130 | 131 | tmp13 = tmp1 + tmp3; /* phases 5-3 */ 132 | tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */ 133 | 134 | tmp0 = tmp10 + tmp13; /* phase 2 */ 135 | tmp3 = tmp10 - tmp13; 136 | tmp1 = tmp11 + tmp12; 137 | tmp2 = tmp11 - tmp12; 138 | 139 | /* Odd part */ 140 | 141 | tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); 142 | tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); 143 | tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); 144 | tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); 145 | 146 | z13 = tmp6 + tmp5; /* phase 6 */ 147 | z10 = tmp6 - tmp5; 148 | z11 = tmp4 + tmp7; 149 | z12 = tmp4 - tmp7; 150 | 151 | tmp7 = z11 + z13; /* phase 5 */ 152 | tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */ 153 | 154 | z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ 155 | tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ 156 | tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ 157 | 158 | tmp6 = tmp12 - tmp7; /* phase 2 */ 159 | tmp5 = tmp11 - tmp6; 160 | tmp4 = tmp10 + tmp5; 161 | 162 | wsptr[DCTSIZE*0] = tmp0 + tmp7; 163 | wsptr[DCTSIZE*7] = tmp0 - tmp7; 164 | wsptr[DCTSIZE*1] = tmp1 + tmp6; 165 | wsptr[DCTSIZE*6] = tmp1 - tmp6; 166 | wsptr[DCTSIZE*2] = tmp2 + tmp5; 167 | wsptr[DCTSIZE*5] = tmp2 - tmp5; 168 | wsptr[DCTSIZE*4] = tmp3 + tmp4; 169 | wsptr[DCTSIZE*3] = tmp3 - tmp4; 170 | 171 | inptr++; /* advance pointers to next column */ 172 | quantptr++; 173 | wsptr++; 174 | } 175 | 176 | /* Pass 2: process rows from work array, store into output array. */ 177 | /* Note that we must descale the results by a factor of 8 == 2**3. */ 178 | 179 | wsptr = workspace; 180 | for (ctr = 0; ctr < DCTSIZE; ctr++) { 181 | outptr = output_buf[ctr] + output_col; 182 | /* Rows of zeroes can be exploited in the same way as we did with columns. 183 | * However, the column calculation has created many nonzero AC terms, so 184 | * the simplification applies less often (typically 5% to 10% of the time). 185 | * And testing floats for zero is relatively expensive, so we don't bother. 186 | */ 187 | 188 | /* Even part */ 189 | 190 | tmp10 = wsptr[0] + wsptr[4]; 191 | tmp11 = wsptr[0] - wsptr[4]; 192 | 193 | tmp13 = wsptr[2] + wsptr[6]; 194 | tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13; 195 | 196 | tmp0 = tmp10 + tmp13; 197 | tmp3 = tmp10 - tmp13; 198 | tmp1 = tmp11 + tmp12; 199 | tmp2 = tmp11 - tmp12; 200 | 201 | /* Odd part */ 202 | 203 | z13 = wsptr[5] + wsptr[3]; 204 | z10 = wsptr[5] - wsptr[3]; 205 | z11 = wsptr[1] + wsptr[7]; 206 | z12 = wsptr[1] - wsptr[7]; 207 | 208 | tmp7 = z11 + z13; 209 | tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); 210 | 211 | z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ 212 | tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ 213 | tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ 214 | 215 | tmp6 = tmp12 - tmp7; 216 | tmp5 = tmp11 - tmp6; 217 | tmp4 = tmp10 + tmp5; 218 | 219 | /* Final output stage: scale down by a factor of 8 and range-limit */ 220 | 221 | outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3) 222 | & RANGE_MASK]; 223 | outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3) 224 | & RANGE_MASK]; 225 | outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3) 226 | & RANGE_MASK]; 227 | outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3) 228 | & RANGE_MASK]; 229 | outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3) 230 | & RANGE_MASK]; 231 | outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3) 232 | & RANGE_MASK]; 233 | outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3) 234 | & RANGE_MASK]; 235 | outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3) 236 | & RANGE_MASK]; 237 | 238 | wsptr += DCTSIZE; /* advance pointer to next row */ 239 | } 240 | } 241 | 242 | #endif /* DCT_FLOAT_SUPPORTED */ 243 | -------------------------------------------------------------------------------- /src/jpeglib/jmemnobs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jmemnobs.c 3 | * 4 | * Copyright (C) 1992-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file provides a really simple implementation of the system- 9 | * dependent portion of the JPEG memory manager. This implementation 10 | * assumes that no backing-store files are needed: all required space 11 | * can be obtained from malloc(). 12 | * This is very portable in the sense that it'll compile on almost anything, 13 | * but you'd better have lots of main memory (or virtual memory) if you want 14 | * to process big images. 15 | * Note that the max_memory_to_use option is ignored by this implementation. 16 | */ 17 | 18 | #define JPEG_INTERNALS 19 | #include 20 | #include "jinclude.h" 21 | #include "jpeglib.h" 22 | #include "jmemsys.h" /* import the system-dependent declarations */ 23 | 24 | #ifndef HAVE_STDLIB_H /* should declare malloc(),free() */ 25 | extern void * malloc JPP((size_t size)); 26 | extern void free JPP((void *ptr)); 27 | #endif 28 | 29 | 30 | /* 31 | * Memory allocation and freeing are controlled by the regular library 32 | * routines malloc() and free(). 33 | */ 34 | 35 | GLOBAL(void *) 36 | jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) 37 | { 38 | return (void *) malloc(sizeofobject); 39 | } 40 | 41 | GLOBAL(void) 42 | jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) 43 | { 44 | free(object); 45 | } 46 | 47 | 48 | /* 49 | * "Large" objects are treated the same as "small" ones. 50 | * NB: although we include FAR keywords in the routine declarations, 51 | * this file won't actually work in 80x86 small/medium model; at least, 52 | * you probably won't be able to process useful-size images in only 64KB. 53 | */ 54 | 55 | GLOBAL(void FAR *) 56 | jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) 57 | { 58 | return (void FAR *) malloc(sizeofobject); 59 | } 60 | 61 | GLOBAL(void) 62 | jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) 63 | { 64 | free(object); 65 | } 66 | 67 | 68 | /* 69 | * This routine computes the total memory space available for allocation. 70 | * Here we always say, "we got all you want bud!" 71 | */ 72 | 73 | GLOBAL(long) 74 | jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, 75 | long max_bytes_needed, long already_allocated) 76 | { 77 | return max_bytes_needed; 78 | } 79 | 80 | 81 | /* 82 | * Backing store (temporary file) management. 83 | * Since jpeg_mem_available always promised the moon, 84 | * this should never be called and we can just error out. 85 | */ 86 | 87 | GLOBAL(void) 88 | jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, 89 | long total_bytes_needed) 90 | { 91 | ERREXIT(cinfo, JERR_NO_BACKING_STORE); 92 | } 93 | 94 | 95 | /* 96 | * These routines take care of any system-dependent initialization and 97 | * cleanup required. Here, there isn't any. 98 | */ 99 | 100 | GLOBAL(long) 101 | jpeg_mem_init (j_common_ptr cinfo) 102 | { 103 | return 0; /* just set max_memory_to_use to 0 */ 104 | } 105 | 106 | GLOBAL(void) 107 | jpeg_mem_term (j_common_ptr cinfo) 108 | { 109 | /* no work */ 110 | } 111 | -------------------------------------------------------------------------------- /src/jpeglib/jmemsrc.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * JPEG 6b Memory Source 3 | * 4 | * This is an additional memory source manager for libJPEG6b 5 | * Based entirely on jdatasrc.c 6 | ****************************************************************************/ 7 | #include 8 | #include 9 | #include 10 | 11 | /*** JPEG includes ***/ 12 | #include "jinclude.h" 13 | #include "jpeglib.h" 14 | #include "jerror.h" 15 | 16 | /* Expanded data source object for memory input */ 17 | 18 | typedef struct { 19 | struct jpeg_source_mgr pub; /* public fields */ 20 | 21 | JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */ 22 | } my_source_mgr; 23 | 24 | typedef my_source_mgr * my_src_ptr; 25 | 26 | 27 | /* 28 | * Initialize source --- called by jpeg_read_header 29 | * before any data is actually read. 30 | */ 31 | 32 | METHODDEF(void) 33 | init_source (j_decompress_ptr cinfo) 34 | { 35 | /* No work, since jpeg_memory_src set up the buffer pointer and count. 36 | * Indeed, if we want to read multiple JPEG images from one buffer, 37 | * this *must* not do anything to the pointer. 38 | */ 39 | } 40 | 41 | 42 | /* 43 | * Fill the input buffer --- called whenever buffer is emptied. 44 | * 45 | * In this application, this routine should never be called; if it is called, 46 | * the decompressor has overrun the end of the input buffer, implying we 47 | * supplied an incomplete or corrupt JPEG datastream. A simple error exit 48 | * might be the most appropriate response. 49 | * 50 | * But what we choose to do in this code is to supply dummy EOI markers 51 | * in order to force the decompressor to finish processing and supply 52 | * some sort of output image, no matter how corrupted. 53 | */ 54 | 55 | METHODDEF(boolean) 56 | fill_input_buffer (j_decompress_ptr cinfo) 57 | { 58 | 59 | my_src_ptr src = (my_src_ptr) cinfo->src; 60 | 61 | WARNMS(cinfo, JWRN_JPEG_EOF); 62 | 63 | /* Create a fake EOI marker */ 64 | src->eoi_buffer[0] = (JOCTET) 0xFF; 65 | src->eoi_buffer[1] = (JOCTET) JPEG_EOI; 66 | src->pub.next_input_byte = src->eoi_buffer; 67 | src->pub.bytes_in_buffer = 2; 68 | 69 | return TRUE; 70 | } 71 | 72 | 73 | /* 74 | * Skip data --- used to skip over a potentially large amount of 75 | * uninteresting data (such as an APPn marker). 76 | * 77 | * If we overrun the end of the buffer, we let fill_input_buffer deal with 78 | * it. An extremely large skip could cause some time-wasting here, but 79 | * it really isn't supposed to happen ... and the decompressor will never 80 | * skip more than 64K anyway. 81 | */ 82 | 83 | METHODDEF(void) 84 | skip_input_data (j_decompress_ptr cinfo, long num_bytes) 85 | { 86 | my_src_ptr src = (my_src_ptr) cinfo->src; 87 | 88 | if (num_bytes > 0) { 89 | while (num_bytes > (long) src->pub.bytes_in_buffer) { 90 | num_bytes -= (long) src->pub.bytes_in_buffer; 91 | (void) fill_input_buffer(cinfo); 92 | /* note we assume that fill_input_buffer will never return FALSE, 93 | * so suspension need not be handled. 94 | */ 95 | } 96 | src->pub.next_input_byte += (size_t) num_bytes; 97 | src->pub.bytes_in_buffer -= (size_t) num_bytes; 98 | } 99 | } 100 | 101 | 102 | /* 103 | * An additional method that can be provided by data source modules is the 104 | * resync_to_restart method for error recovery in the presence of RST markers. 105 | * For the moment, this source module just uses the default resync method 106 | * provided by the JPEG library. That method assumes that no backtracking 107 | * is possible. 108 | */ 109 | 110 | 111 | /* 112 | * Terminate source --- called by jpeg_finish_decompress 113 | * after all data has been read. Often a no-op. 114 | * 115 | * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 116 | * application must deal with any cleanup that should happen even 117 | * for error exit. 118 | */ 119 | 120 | METHODDEF(void) 121 | term_source (j_decompress_ptr cinfo) 122 | { 123 | /* no work necessary here */ 124 | } 125 | 126 | 127 | /* 128 | * Prepare for input from a memory buffer. 129 | */ 130 | 131 | GLOBAL(void) 132 | jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize) 133 | { 134 | my_src_ptr src; 135 | 136 | /* The source object is made permanent so that a series of JPEG images 137 | * can be read from a single buffer by calling jpeg_memory_src 138 | * only before the first one. 139 | * This makes it unsafe to use this manager and a different source 140 | * manager serially with the same JPEG object. Caveat programmer. 141 | */ 142 | if (cinfo->src == NULL) { /* first time for this JPEG object? */ 143 | cinfo->src = (struct jpeg_source_mgr *) 144 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 145 | SIZEOF(my_source_mgr)); 146 | } 147 | 148 | src = (my_src_ptr) cinfo->src; 149 | src->pub.init_source = init_source; 150 | src->pub.fill_input_buffer = fill_input_buffer; 151 | src->pub.skip_input_data = skip_input_data; 152 | src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 153 | src->pub.term_source = term_source; 154 | 155 | src->pub.next_input_byte = buffer; 156 | src->pub.bytes_in_buffer = bufsize; 157 | } 158 | 159 | -------------------------------------------------------------------------------- /src/jpeglib/jutils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * jutils.c 3 | * 4 | * Copyright (C) 1991-1996, Thomas G. Lane. 5 | * This file is part of the Independent JPEG Group's software. 6 | * For conditions of distribution and use, see the accompanying README file. 7 | * 8 | * This file contains tables and miscellaneous utility routines needed 9 | * for both compression and decompression. 10 | * Note we prefix all global names with "j" to minimize conflicts with 11 | * a surrounding application. 12 | */ 13 | 14 | #define JPEG_INTERNALS 15 | #include "jinclude.h" 16 | #include "jpeglib.h" 17 | 18 | 19 | /* 20 | * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element 21 | * of a DCT block read in natural order (left to right, top to bottom). 22 | */ 23 | 24 | #if 0 /* This table is not actually needed in v6a */ 25 | 26 | const int jpeg_zigzag_order[DCTSIZE2] = { 27 | 0, 1, 5, 6, 14, 15, 27, 28, 28 | 2, 4, 7, 13, 16, 26, 29, 42, 29 | 3, 8, 12, 17, 25, 30, 41, 43, 30 | 9, 11, 18, 24, 31, 40, 44, 53, 31 | 10, 19, 23, 32, 39, 45, 52, 54, 32 | 20, 22, 33, 38, 46, 51, 55, 60, 33 | 21, 34, 37, 47, 50, 56, 59, 61, 34 | 35, 36, 48, 49, 57, 58, 62, 63 35 | }; 36 | 37 | #endif 38 | 39 | /* 40 | * jpeg_natural_order[i] is the natural-order position of the i'th element 41 | * of zigzag order. 42 | * 43 | * When reading corrupted data, the Huffman decoders could attempt 44 | * to reference an entry beyond the end of this array (if the decoded 45 | * zero run length reaches past the end of the block). To prevent 46 | * wild stores without adding an inner-loop test, we put some extra 47 | * "63"s after the real entries. This will cause the extra coefficient 48 | * to be stored in location 63 of the block, not somewhere random. 49 | * The worst case would be a run-length of 15, which means we need 16 50 | * fake entries. 51 | */ 52 | 53 | const int jpeg_natural_order[DCTSIZE2+16] = { 54 | 0, 1, 8, 16, 9, 2, 3, 10, 55 | 17, 24, 32, 25, 18, 11, 4, 5, 56 | 12, 19, 26, 33, 40, 48, 41, 34, 57 | 27, 20, 13, 6, 7, 14, 21, 28, 58 | 35, 42, 49, 56, 57, 50, 43, 36, 59 | 29, 22, 15, 23, 30, 37, 44, 51, 60 | 58, 59, 52, 45, 38, 31, 39, 46, 61 | 53, 60, 61, 54, 47, 55, 62, 63, 62 | 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */ 63 | 63, 63, 63, 63, 63, 63, 63, 63 64 | }; 65 | 66 | 67 | /* 68 | * Arithmetic utilities 69 | */ 70 | 71 | GLOBAL(long) 72 | jdiv_round_up (long a, long b) 73 | /* Compute a/b rounded up to next integer, ie, ceil(a/b) */ 74 | /* Assumes a >= 0, b > 0 */ 75 | { 76 | return (a + b - 1L) / b; 77 | } 78 | 79 | 80 | GLOBAL(long) 81 | jround_up (long a, long b) 82 | /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */ 83 | /* Assumes a >= 0, b > 0 */ 84 | { 85 | a += b - 1L; 86 | return a - (a % b); 87 | } 88 | 89 | 90 | /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays 91 | * and coefficient-block arrays. This won't work on 80x86 because the arrays 92 | * are FAR and we're assuming a small-pointer memory model. However, some 93 | * DOS compilers provide far-pointer versions of memcpy() and memset() even 94 | * in the small-model libraries. These will be used if USE_FMEM is defined. 95 | * Otherwise, the routines below do it the hard way. (The performance cost 96 | * is not all that great, because these routines aren't very heavily used.) 97 | */ 98 | 99 | #ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */ 100 | #define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size) 101 | #define FMEMZERO(target,size) MEMZERO(target,size) 102 | #else /* 80x86 case, define if we can */ 103 | #ifdef USE_FMEM 104 | #define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size)) 105 | #define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size)) 106 | #endif 107 | #endif 108 | 109 | 110 | GLOBAL(void) 111 | jcopy_sample_rows (JSAMPARRAY input_array, int source_row, 112 | JSAMPARRAY output_array, int dest_row, 113 | int num_rows, JDIMENSION num_cols) 114 | /* Copy some rows of samples from one place to another. 115 | * num_rows rows are copied from input_array[source_row++] 116 | * to output_array[dest_row++]; these areas may overlap for duplication. 117 | * The source and destination arrays must be at least as wide as num_cols. 118 | */ 119 | { 120 | register JSAMPROW inptr, outptr; 121 | #ifdef FMEMCOPY 122 | register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE)); 123 | #else 124 | register JDIMENSION count; 125 | #endif 126 | register int row; 127 | 128 | input_array += source_row; 129 | output_array += dest_row; 130 | 131 | for (row = num_rows; row > 0; row--) { 132 | inptr = *input_array++; 133 | outptr = *output_array++; 134 | #ifdef FMEMCOPY 135 | FMEMCOPY(outptr, inptr, count); 136 | #else 137 | for (count = num_cols; count > 0; count--) 138 | *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */ 139 | #endif 140 | } 141 | } 142 | 143 | 144 | GLOBAL(void) 145 | jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row, 146 | JDIMENSION num_blocks) 147 | /* Copy a row of coefficient blocks from one place to another. */ 148 | { 149 | #ifdef FMEMCOPY 150 | FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF))); 151 | #else 152 | register JCOEFPTR inptr, outptr; 153 | register long count; 154 | 155 | inptr = (JCOEFPTR) input_row; 156 | outptr = (JCOEFPTR) output_row; 157 | for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) { 158 | *outptr++ = *inptr++; 159 | } 160 | #endif 161 | } 162 | 163 | 164 | GLOBAL(void) 165 | jzero_far (void FAR * target, size_t bytestozero) 166 | /* Zero out a chunk of FAR memory. */ 167 | /* This might be sample-array data, block-array data, or alloc_large data. */ 168 | { 169 | #ifdef FMEMZERO 170 | FMEMZERO(target, bytestozero); 171 | #else 172 | register char FAR * ptr = (char FAR *) target; 173 | register size_t count; 174 | 175 | for (count = bytestozero; count > 0; count--) { 176 | *ptr++ = 0; 177 | } 178 | #endif 179 | } 180 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "errorhandler.h" 11 | #include "errorcodes.h" 12 | 13 | /* [nitr8]: Added*/ 14 | #include "errorstrings.h" 15 | 16 | #include "seeprom.h" 17 | #include "boot2.h" 18 | #include "tools.h" 19 | #include "prodinfo.h" 20 | #include "runtimeiospatch.h" 21 | #include "menu.h" 22 | #include "flash.h" 23 | #include "version.h" 24 | #include "haxx.h" 25 | 26 | #include "jpgogc.h" 27 | 28 | /* [nitr8]: For use with JPEG, change it from a void pointer to an unsigned int pointer */ 29 | /* static void *xfb = NULL; */ 30 | static u32 *xfb = NULL; 31 | 32 | static GXRModeObj *rmode = NULL; 33 | 34 | /* [nitr8]: Add a "/dev/flash" lock counter so we don't have to restart the app every time */ 35 | static int dev_flash_access_count = 0; 36 | 37 | /* [nitr8]: Add a counter for this one as well as fatInitDefault() may take some time */ 38 | static int fat_init_count = 0; 39 | 40 | /* [nitr8]: Added - used by SEEPROM code */ 41 | int sd_initialized = 0; 42 | 43 | /* [nitr8]: Add nice and shiny RGD logo */ 44 | extern char bg_jpg[]; 45 | extern int bg_jpg_size; 46 | 47 | static void display_jpeg(JPEGIMG jpeg, int x, int y) 48 | { 49 | unsigned int *jpegout = (unsigned int *)jpeg.outbuffer; 50 | 51 | int i, j; 52 | int height = jpeg.height; 53 | int width = jpeg.width / 2; 54 | 55 | for (i = 0; i <= width; i++) 56 | for (j = 0; j <= height - 2; j++) 57 | xfb[(i + x) + 320 * (j + 16 + y)] = jpegout[i + width * j]; 58 | 59 | free(jpeg.outbuffer); 60 | } 61 | 62 | void printHeader(){ 63 | printf("\x1b[2;3H"); 64 | printf("RGD SDBoot Installer build %s - by \x1b[32mroot1024\x1b[37m, \x1b[36mnitr8\x1b[37m, \x1b[31mRedBees\x1b[37m, \x1b[33mDeadlyFoez\x1b[37m\n raregamingdump.ca", buildNumber); 65 | printf("\n Current boot2 version: %i", GetBoot2Version()); 66 | printf("\tCurrent IOS version: IOS%i v%i\n\n", IOS_GetVersion(), IOS_GetRevision()); 67 | printf("\x1b[1;0H"); 68 | printf("+------------------------------------------------------------------------------+"); 69 | int i; 70 | for(i=2; i<=4; i++){ 71 | printf("|\x1b[%d;79H|", i); 72 | } 73 | printf("+------------------------------------------------------------------------------+"); 74 | 75 | printf("\x1b[7;1H"); 76 | } 77 | 78 | int main(int argc, char **argv) 79 | { 80 | /* [nitr8]: Add check on /dev/flash lock release and filesystem initialization timeout */ 81 | int ret = 0; 82 | bool enableDebug = false; 83 | 84 | /* [nitr8]: Add nice and shiny RGD logo */ 85 | JPEGIMG about; 86 | 87 | /* [nitr8]: Add support for realtime debugging using a USB-Gecko */ 88 | #ifdef _DEBUG 89 | DEBUG_Init(GDBSTUB_DEVICE_USB, GDBSTUB_DEF_CHANNEL); 90 | 91 | /* [nitr8]: Set breakpoint here */ 92 | //_break(); 93 | #endif 94 | 95 | //gecko_printf("%08x\n", ipc_initialize()); 96 | 97 | /* [nitr8]: Add nice and shiny RGD logo */ 98 | memset(&about, 0, sizeof(JPEGIMG)); 99 | about.inbuffer = bg_jpg; 100 | about.inbufferlength = bg_jpg_size; 101 | JPEG_Decompress(&about); 102 | 103 | VIDEO_Init(); 104 | WPAD_Init(); 105 | PAD_Init(); 106 | 107 | rmode = VIDEO_GetPreferredMode(NULL); 108 | xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); 109 | console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ); 110 | 111 | /* [nitr8]: Add nice and shiny RGD logo */ 112 | //display_jpeg(about, 0, 344); 113 | 114 | VIDEO_Configure(rmode); 115 | VIDEO_SetNextFramebuffer(xfb); 116 | VIDEO_SetBlack(FALSE); 117 | VIDEO_Flush(); 118 | VIDEO_WaitVSync(); 119 | 120 | if (rmode->viTVMode&VI_NON_INTERLACE) 121 | VIDEO_WaitVSync(); 122 | 123 | /* [nitr8]: DEBUG */ 124 | // IOS_ReloadIOS(236); 125 | 126 | /* [nitr8]: This should help alot when it comes to restarting things... */ 127 | /* Better than having to "hard-reset" the console by holding down POWER every time */ 128 | SYS_SetResetCallback(console_reset); 129 | 130 | /* Get Bus Access (disable AHBPROT) */ 131 | Haxx_GetBusAccess(); 132 | 133 | gecko_init(1); 134 | 135 | if (!AHBPROT_DISABLED) 136 | { 137 | ThrowError(errorStrings[ErrStr_NeedPerms]); 138 | } 139 | 140 | Fix_ES_ImportBoot(); 141 | Enable_DevFlash(); 142 | Restore_Trucha(); 143 | 144 | if (IsDolphin()) 145 | { 146 | ThrowError(errorStrings[ErrStr_InDolphin]); 147 | } 148 | else if (IsWiiU()) 149 | { 150 | ThrowError(errorStrings[ErrStr_InCafe]); 151 | } 152 | 153 | /* [nitr8]: Disabled... */ 154 | 155 | if(!fatInitDefault()){ 156 | ThrowError(errorStrings[ErrStr_SDCard]); 157 | } 158 | 159 | /* [nitr8]: Instead, loop until the filesystem was initialized */ 160 | /*while (!sd_initialized) 161 | { 162 | sd_initialized = fatInitDefault(); 163 | fat_init_count++; 164 | 165 | ret = exit_on_error(fat_init_count, 2000, "fatInitDefault()", -1); 166 | 167 | if (ret != 0) 168 | return ret; 169 | }*/ 170 | 171 | /* [nitr8]: Added - Why is this missing??? */ 172 | /* Basically - trying to write a file into a directory which is non-existent will always turn into a DSI exception interrupt 173 | So for a BARE setup once this app was started, ALWAYS create the required directories */ 174 | create_directory("boot2"); 175 | 176 | #ifndef NO_DOLPHIN_CHECK 177 | /* [nitr8]: Disabled... */ 178 | /* 179 | if(NANDFlashInit() < 0){ 180 | ThrowError(errorStrings[ErrStr_DevFlashErr]); 181 | } 182 | */ 183 | /* [nitr8]: Instead, loop until we get a release from a locked access to /dev/flash */ 184 | while (NANDFlashInit() < 0) 185 | { 186 | dev_flash_access_count++; 187 | 188 | ret = exit_on_error(dev_flash_access_count, 2000, "NANDFlashInit()", -2); 189 | 190 | if (ret != 0) 191 | return ret; 192 | } 193 | #endif 194 | 195 | gecko_printf("Took %d iterations until filesystem was available\n", fat_init_count); 196 | gecko_printf("Took %d iterations until /dev/flash was available\n", dev_flash_access_count); 197 | 198 | /* [root1024]: After getting a working FD for /dev/flash, revert the patch so we get access 199 | * to /dev/boot2 once again. */ 200 | 201 | Enable_DevBoot2(); 202 | 203 | if (argc == 2) 204 | enableDebug = (strcmp(argv[1], "debug") == 0) ? true : false; 205 | 206 | enableDebug = true; 207 | 208 | /* [nitr8]: At the same time - mark that as a high risk warning! */ 209 | /* printf("\nWARNING: PLEASE READ THIS CAREFULLY!\n\n"); */ 210 | printf("\x1b[31mWARNING: PLEASE READ THIS CAREFULLY!\x1b[37m\n\n"); 211 | printf("THIS IS BETA SOFTWARE. AS SUCH, IT CARRIES A HIGH RISK OF BRICKING THE\nCONSOLE.\n\n"); 212 | 213 | /* [nitr8]: Regarding warning: don't forget about the SEEPROM here... */ 214 | /* [nitr8]: At the same time - mark that as a high risk warning! */ 215 | /* [nitr8]: Make room for an additional intro logo by removing unnecessary line feeds */ 216 | printf("\x1b[31mTHIS TOOL DIRECTLY WRITES TO BOOT1 AND BOOT2 AS WELL AS THE SEEPROM.\x1b[37m\n\n"); 217 | 218 | printf("IT'S ADVISED YOU USE THIS TOOL ONLY IF YOU HAVE AN EXTERNAL NAND FLASHER.\n"); 219 | printf("BY CONTINUING, YOU ACCEPT THAT YOU USE THIS PROGRAM AT YOUR OWN RISK.\n"); 220 | printf("THE AUTHORS CANNOT BE HELD RESPONSIBLE TO ANY DAMAGE THIS TOOL MAY CAUSE.\n"); 221 | printf("IF YOU DON'T AGREE TO THESE TERMS, PLEASE QUIT THIS PROGRAM IMMEDIATELY.\n\n"); 222 | 223 | printf("Press (A) to continue, or (HOME) / (Z) to quit and return to the HBC.\n"); 224 | 225 | 226 | /* [nitr8]: This should help alot when it comes to restarting things... */ 227 | /* Better than having to "hard-reset" the console by holding down POWER every time */ 228 | /* while(1) */ 229 | while (!console_reload) 230 | { 231 | /* [nitr8]: Added */ 232 | memstats(0); 233 | 234 | /* [nitr8]: Reworked - Fix menu bugs with button matches between a Wiimote and a GC Controller */ 235 | /* switch(WaitForPad()) { */ 236 | pad_t pad_button = WaitForPad(); 237 | 238 | /* [nitr8]: Much more simplified... */ 239 | switch (pad_button.button) 240 | { 241 | /* [nitr8]: Add Classic Controller button mapping */ 242 | case WPAD_CLASSIC_BUTTON_A: 243 | 244 | /* [nitr8]: Add forgotten Wiimote button */ 245 | case WPAD_BUTTON_A: 246 | 247 | case PAD_BUTTON_A: 248 | ClearScreen(); 249 | EnterMenu(enableDebug); 250 | 251 | /* [nitr8]: NOT REACHED HERE */ 252 | break; 253 | 254 | /* [nitr8]: Add special exit */ 255 | case WPAD_CLASSIC_BUTTON_HOME: 256 | case WPAD_BUTTON_HOME: 257 | case PAD_TRIGGER_Z: 258 | console_reload = 1; 259 | break; 260 | 261 | /* [nitr8]: Add default case */ 262 | default: 263 | if (!console_reload) 264 | continue; 265 | else 266 | break; 267 | } 268 | } 269 | 270 | /* [nitr8]: You really think so...? */ 271 | return 0; /* NOT REACHED HERE */ 272 | } 273 | 274 | -------------------------------------------------------------------------------- /src/prodinfo.c: -------------------------------------------------------------------------------- 1 | /* RGD SDBoot Installer */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "prodinfo.h" 10 | #include "errorhandler.h" 11 | #include "errorcodes.h" 12 | 13 | /* [nitr8]: Added*/ 14 | #include "errorstrings.h" 15 | 16 | /* [nitr8]: Added */ 17 | #include "gecko.h" 18 | 19 | /* [nitr8]: Added */ 20 | #include "tools.h" 21 | 22 | #define PROD_INFO_PATH "/title/00000001/00000002/data/setting.txt" 23 | #define PROD_INFO_KEY_SEED 0x73B5DBFA 24 | 25 | static char prodInfoBuffer[0x101] ALIGNED(32); 26 | 27 | /* [nitr8]: Disable this... (moved into function "IsMini()" below) */ 28 | #if 0 29 | /* [nitr8]: Make static */ 30 | /* int GetProdInfoEntry(const char *name, char *buf, int length) */ 31 | static int GetProdInfoEntry(const char *name, char *buf, int length) 32 | { 33 | char *delim, *end; 34 | int slen; 35 | int nlen = strlen(name); 36 | char *line = prodInfoBuffer; 37 | 38 | while(line < (prodInfoBuffer + 0x100)) 39 | { 40 | delim = strchr(line, '='); 41 | 42 | if(delim && ((delim - line) == nlen) && !memcmp(name, line, nlen)) 43 | { 44 | delim++; 45 | end = strchr(line, '\r'); 46 | 47 | if (!end) 48 | end = strchr(line, '\n'); 49 | 50 | if(end) 51 | { 52 | slen = end - delim; 53 | 54 | if(slen < length) 55 | { 56 | memcpy(buf, delim, slen); 57 | buf[slen] = 0; 58 | return slen; 59 | } 60 | else 61 | { 62 | ThrowError(errorStrings[ErrStr_SettingTooBig]); 63 | } 64 | } 65 | } 66 | 67 | while(line < (prodInfoBuffer + 0x100) && *line++ != '\n'); 68 | } 69 | 70 | ThrowError(errorStrings[ErrStr_SettingInvalid]); 71 | 72 | /* NOT REACHED HERE */ 73 | return 0; 74 | } 75 | #endif 76 | 77 | /* [nitr8]: Make static */ 78 | /* int OpenProdInfo(char* prodInfoPath) */ 79 | static int OpenProdInfo(char* prodInfoPath) 80 | { 81 | int fd = IOS_Open(prodInfoPath, 1); 82 | int rv = 0; 83 | int i = 0; 84 | u32 prodInfoKey = PROD_INFO_KEY_SEED; 85 | 86 | if (fd < 0) 87 | return fd; 88 | 89 | memset(prodInfoBuffer, 0, 0x101); 90 | rv = IOS_Read(fd, prodInfoBuffer, 0x100); 91 | IOS_Close(fd); 92 | 93 | if (rv != 0x100) 94 | ThrowError(errorStrings[ErrStr_SettingInvalid]); 95 | 96 | for (i = 0; i < 0x100; i++) 97 | { 98 | prodInfoBuffer[i] ^= prodInfoKey & 0xff; 99 | prodInfoKey = (prodInfoKey << 1) | (prodInfoKey >> 31); 100 | } 101 | 102 | return ALL_OK; 103 | } 104 | 105 | /* [nitr8]: Disable this... */ 106 | #if 0 107 | u8 IsMini(void) 108 | { 109 | /* [nitr8]: What? - Either array or pointer (depends on you) - but not both... */ 110 | static char* buf[17]; /* Official internal SC docs say that 16 characters should be allocated for the length. */ 111 | 112 | int rv = OpenProdInfo(PROD_INFO_PATH); 113 | 114 | if(rv < 0) 115 | return 0; 116 | 117 | /* [nitr8]: return value of GetProdInfoEntry is never checked... */ 118 | rv = GetProdInfoEntry("MODEL", buf, 17); 119 | 120 | if(!strncmp("RVL-201", buf, 7)) 121 | return 1; 122 | 123 | return 0; 124 | } 125 | #endif 126 | 127 | /* [nitr8]: Modified it by adding the code of function "GetProdInfoEntry" right here... */ 128 | u8 IsMini(void) 129 | { 130 | char *delim, *end; 131 | int slen; 132 | char* buf; 133 | char *line = prodInfoBuffer; 134 | int rv = OpenProdInfo(PROD_INFO_PATH); 135 | int ret = 0; 136 | const char *name = "MODEL"; 137 | int nlen = strlen(name); 138 | int length = 17; 139 | 140 | if (rv < 0) 141 | return ret; 142 | 143 | /* [nitr8]: like... "WHAT?!" - You can use a buffer pointer or some buffer array but what is this right here ??? */ 144 | /* static char* buf[17]; */ /* Official internal SC docs say that 16 characters should be allocated for the length. */ 145 | 146 | /* [nitr8]: allocate the buffer */ 147 | buf = malloc(length); 148 | 149 | while (line < (prodInfoBuffer + 0x100)) 150 | { 151 | delim = strchr(line, '='); 152 | 153 | if (delim && ((delim - line) == nlen) && !memcmp(name, line, nlen)) 154 | { 155 | delim++; 156 | end = strchr(line, '\r'); 157 | 158 | if (!end) 159 | end = strchr(line, '\n'); 160 | 161 | if (end) 162 | { 163 | slen = end - delim; 164 | 165 | if (slen < length) 166 | { 167 | memcpy(buf, delim, slen); 168 | buf[slen] = 0; 169 | rv = slen; 170 | 171 | /* [nitr8]: break out */ 172 | goto got_data; 173 | } 174 | else 175 | { 176 | ThrowError(errorStrings[ErrStr_SettingTooBig]); 177 | } 178 | } 179 | } 180 | 181 | while (line < (prodInfoBuffer + 0x100) && *line++ != '\n'); 182 | } 183 | 184 | ThrowError(errorStrings[ErrStr_SettingInvalid]); 185 | 186 | /* NOT REACHED HERE */ 187 | /* [nitr8]: You really think so...? */ 188 | rv = 0; 189 | 190 | /* [nitr8]: jump here */ 191 | got_data: 192 | 193 | /* [nitr8]: return value of GetProdInfoEntry is never checked... - why? */ 194 | gecko_printf("GetProdInfoEntry returned %d bytes ('%s')\n", rv, buf); 195 | 196 | if (!strncmp("RVL-201", buf, 7)) 197 | ret = 1; 198 | 199 | /* [nitr8]: free the buffer */ 200 | free(buf); 201 | 202 | return ret; 203 | } 204 | 205 | -------------------------------------------------------------------------------- /src/sha256.c: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * Filename: sha256.c 3 | * Author: Brad Conte (brad AT bradconte.com) 4 | * Copyright: 5 | * Disclaimer: This code is presented "as is" without any guarantees. 6 | * Details: Implementation of the SHA-256 hashing algorithm. 7 | SHA-256 is one of the three algorithms in the SHA2 8 | specification. The others, SHA-384 and SHA-512, are not 9 | offered in this implementation. 10 | Algorithm specification can be found here: 11 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf 12 | This implementation uses little endian byte order. 13 | *********************************************************************/ 14 | 15 | /*************************** HEADER FILES ***************************/ 16 | #include 17 | 18 | /* [nitr8]: error: header file missing... 19 | #include */ 20 | 21 | /* [nitr8]: ...but this fixes it */ 22 | #include 23 | 24 | #include "sha256.h" 25 | 26 | /****************************** MACROS ******************************/ 27 | #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) 28 | #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) 29 | 30 | #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) 31 | #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 32 | #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) 33 | #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) 34 | #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) 35 | #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) 36 | 37 | /**************************** VARIABLES *****************************/ 38 | static const WORD k[64] = { 39 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 40 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 41 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, 42 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, 43 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, 44 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, 45 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, 46 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 47 | }; 48 | 49 | /*********************** FUNCTION DEFINITIONS ***********************/ 50 | /* [nitr8]: Make static */ 51 | /* void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) */ 52 | static void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) 53 | { 54 | WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; 55 | 56 | for (i = 0, j = 0; i < 16; ++i, j += 4) 57 | m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); 58 | for ( ; i < 64; ++i) 59 | m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; 60 | 61 | a = ctx->state[0]; 62 | b = ctx->state[1]; 63 | c = ctx->state[2]; 64 | d = ctx->state[3]; 65 | e = ctx->state[4]; 66 | f = ctx->state[5]; 67 | g = ctx->state[6]; 68 | h = ctx->state[7]; 69 | 70 | for (i = 0; i < 64; ++i) { 71 | t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; 72 | t2 = EP0(a) + MAJ(a,b,c); 73 | h = g; 74 | g = f; 75 | f = e; 76 | e = d + t1; 77 | d = c; 78 | c = b; 79 | b = a; 80 | a = t1 + t2; 81 | } 82 | 83 | ctx->state[0] += a; 84 | ctx->state[1] += b; 85 | ctx->state[2] += c; 86 | ctx->state[3] += d; 87 | ctx->state[4] += e; 88 | ctx->state[5] += f; 89 | ctx->state[6] += g; 90 | ctx->state[7] += h; 91 | } 92 | 93 | void sha256_init(SHA256_CTX *ctx) 94 | { 95 | ctx->datalen = 0; 96 | ctx->bitlen = 0; 97 | ctx->state[0] = 0x6a09e667; 98 | ctx->state[1] = 0xbb67ae85; 99 | ctx->state[2] = 0x3c6ef372; 100 | ctx->state[3] = 0xa54ff53a; 101 | ctx->state[4] = 0x510e527f; 102 | ctx->state[5] = 0x9b05688c; 103 | ctx->state[6] = 0x1f83d9ab; 104 | ctx->state[7] = 0x5be0cd19; 105 | } 106 | 107 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len) 108 | { 109 | WORD i; 110 | 111 | for (i = 0; i < len; ++i) { 112 | ctx->data[ctx->datalen] = data[i]; 113 | ctx->datalen++; 114 | if (ctx->datalen == 64) { 115 | sha256_transform(ctx, ctx->data); 116 | ctx->bitlen += 512; 117 | ctx->datalen = 0; 118 | } 119 | } 120 | } 121 | 122 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]) 123 | { 124 | WORD i; 125 | 126 | i = ctx->datalen; 127 | 128 | /* Pad whatever data is left in the buffer. */ 129 | if (ctx->datalen < 56) { 130 | ctx->data[i++] = 0x80; 131 | while (i < 56) 132 | ctx->data[i++] = 0x00; 133 | } 134 | else { 135 | ctx->data[i++] = 0x80; 136 | while (i < 64) 137 | ctx->data[i++] = 0x00; 138 | sha256_transform(ctx, ctx->data); 139 | memset(ctx->data, 0, 56); 140 | } 141 | 142 | /* Append to the padding the total message's length in bits and transform. */ 143 | ctx->bitlen += ctx->datalen * 8; 144 | ctx->data[63] = ctx->bitlen; 145 | ctx->data[62] = ctx->bitlen >> 8; 146 | ctx->data[61] = ctx->bitlen >> 16; 147 | ctx->data[60] = ctx->bitlen >> 24; 148 | ctx->data[59] = ctx->bitlen >> 32; 149 | ctx->data[58] = ctx->bitlen >> 40; 150 | ctx->data[57] = ctx->bitlen >> 48; 151 | ctx->data[56] = ctx->bitlen >> 56; 152 | sha256_transform(ctx, ctx->data); 153 | 154 | /* Since this implementation uses little endian byte ordering and SHA uses big endian, 155 | reverse all the bytes when copying the final state to the output hash. */ 156 | for (i = 0; i < 4; ++i) { 157 | hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; 158 | hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; 159 | hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; 160 | hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; 161 | hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; 162 | hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; 163 | hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; 164 | hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; 165 | } 166 | } 167 | 168 | --------------------------------------------------------------------------------