Browse Source

Initial Commit.

main v1.0.0
Nichole Mattera 2 years ago
commit
d380f6f796
40 changed files with 9239 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. BIN
      Icon.jpg
  3. +339
    -0
      LICENSE
  4. +176
    -0
      Makefile
  5. +9
    -0
      README.md
  6. +244
    -0
      src/Application.cpp
  7. +46
    -0
      src/Application.hpp
  8. +79
    -0
      src/Menu.cpp
  9. +41
    -0
      src/Menu.hpp
  10. +216
    -0
      src/WebRequest.cpp
  11. +71
    -0
      src/WebRequest.hpp
  12. +29
    -0
      src/main.cpp
  13. +17
    -0
      src/minizip/LICENSE
  14. +81
    -0
      src/minizip/README.md
  15. +130
    -0
      src/minizip/crypt.h
  16. +369
    -0
      src/minizip/ioapi.c
  17. +175
    -0
      src/minizip/ioapi.h
  18. +192
    -0
      src/minizip/ioapi_mem.c
  19. +55
    -0
      src/minizip/ioapi_mem.h
  20. +1890
    -0
      src/minizip/unzip.c
  21. +316
    -0
      src/minizip/unzip.h
  22. +2021
    -0
      src/minizip/zip.c
  23. +200
    -0
      src/minizip/zip.h
  24. +104
    -0
      src/views/AppVersionView.cpp
  25. +38
    -0
      src/views/AppVersionView.hpp
  26. +123
    -0
      src/views/AppView.cpp
  27. +39
    -0
      src/views/AppView.hpp
  28. +115
    -0
      src/views/PackageVersionView.cpp
  29. +42
    -0
      src/views/PackageVersionView.hpp
  30. +149
    -0
      src/views/PackageView.cpp
  31. +44
    -0
      src/views/PackageView.hpp
  32. +622
    -0
      src/zipper/CDirEntry.cpp
  33. +215
    -0
      src/zipper/CDirEntry.h
  34. +21
    -0
      src/zipper/LICENSE.md
  35. +147
    -0
      src/zipper/README.md
  36. +66
    -0
      src/zipper/defs.h
  37. +145
    -0
      src/zipper/tools.cpp
  38. +20
    -0
      src/zipper/tools.h
  39. +562
    -0
      src/zipper/unzipper.cpp
  40. +88
    -0
      src/zipper/unzipper.h

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
build
dist
.vscode

BIN
Icon.jpg View File

Before After
Width: 256  |  Height: 256  |  Size: 33 KiB

+ 339
- 0
LICENSE View File

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

+ 176
- 0
Makefile View File

@ -0,0 +1,176 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
TOPDIR ?= $(CURDIR)
include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing header files
# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm".
#
# NO_ICON: if set to anything, do not use icon.
# NO_NACP: if set to anything, no .nacp file is generated.
# APP_TITLE is the name of the app stored in the .nacp file (Optional)
# APP_AUTHOR is the author of the app stored in the .nacp file (Optional)
# APP_VERSION is the version of the app stored in the .nacp file (Optional)
# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional)
# ICON is the filename of the icon (.jpg), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.jpg
# - icon.jpg
# - <libnx folder>/default_icon.jpg
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
DIST := dist
SOURCES := src $(wildcard src/**)
EXEFS_SRC := exefs_src
ICON := Icon.jpg
APP_TITLE := Kosmos Updater Diagnostic
APP_AUTHOR := Steven Mattera
APP_VERSION := 1.0.0
API_VERSION := v3
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
DEFINES += -D__SWITCH__ -DUSE_FILE32API -DVERSION=\"$(APP_VERSION)\" -DAPI_VERSION=\"$(API_VERSION)\"
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES) $(INCLUDE)
CXXFLAGS := $(CFLAGS) -fno-rtti -fexceptions -std=gnu++17
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := -lcurl -lz -lnx
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(LIBNX)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(DIST)/$(TARGET)
export TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o)
export INCLUDE := $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.jpg)
ifneq (,$(findstring $(TARGET).jpg,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).jpg
else
ifneq (,$(findstring icon.jpg,$(icons)))
export APP_ICON := $(TOPDIR)/icon.jpg
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif
ifeq ($(strip $(NO_ICON)),)
export NROFLAGS += --icon=$(APP_ICON)
endif
ifeq ($(strip $(NO_NACP)),)
export NROFLAGS += --nacp=$(CURDIR)/$(DIST)/$(TARGET).nacp
endif
ifneq ($(APP_TITLEID),)
export NACPFLAGS += --titleid=$(APP_TITLEID)
endif
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@[ -d $(DIST) ] || mkdir $(DIST)
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(DIST)
#---------------------------------------------------------------------------------
else
.PHONY: all
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
all : $(OUTPUT).pfs0 $(OUTPUT).nro
$(OUTPUT).pfs0 : $(OUTPUT).nso
$(OUTPUT).nso : $(OUTPUT).elf
ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif
$(OUTPUT).elf : $(OFILES)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

+ 9
- 0
README.md View File

@ -0,0 +1,9 @@
# Kosmos Updater Diagnostic
A homebrew application for the Nintendo Switch to diagnose potential issues with Kosmos Updater.
## Credits
* Thanks to @yellow8, and @plutoo for the example of [reading an NACP from an NRO](https://github.com/switchbrew/nx-hbmenu/blob/8e8e62ac33aa27a268b34d9e16ef141767a5434a/common/menu-entry.c#L127) in NX-HBMenu.
* Thanks to [switchbrew.org](https://switchbrew.org/) for the awesome documentaion on [NRO](https://switchbrew.org/wiki/NRO) and [NACP](https://switchbrew.org/wiki/Control.nacp).
* Thanks to @sebastiandev for their fork of minizip and their awesome wrapper for it, [Zipper](https://switchbrew.org/wiki/NRO).

+ 244
- 0
src/Application.cpp View File

@ -0,0 +1,244 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "Application.hpp"
#include <curl/curl.h>
using namespace std;
using namespace std::placeholders;
namespace KUDiag {
Application::Application() {
socketInitializeDefault();
consoleInit(NULL);
curl_global_init(CURL_GLOBAL_ALL);
vector<string> mainMenuItems;
mainMenuItems.push_back("Get latest app version");
mainMenuItems.push_back("Get latest app");
mainMenuItems.push_back("Get latest package version");
mainMenuItems.push_back("Get latest package");
mainMenuItems.push_back("Exit");
_mainMenu = new Menu(
string("Kosmos Updater Diagnostic ") + VERSION + " - Main Menu",
mainMenuItems,
bind(&Application::_menuItemSelected, this, _1),
bind(&Application::_back, this)
);
vector<string> bundleItems;
bundleItems.push_back("Kosmos");
bundleItems.push_back("Atmosphere");
bundleItems.push_back("Back");
_bundleMenu = new Menu(
string("Kosmos Updater Diagnostic ") + VERSION + " - Select a bundle",
bundleItems,
bind(&Application::_menuItemSelected, this, _1),
bind(&Application::_back, this)
);
vector<string> channelItems;
channelItems.push_back("Stable");
channelItems.push_back("Bleeding-Edge");
channelItems.push_back("Back");
_channelMenu = new Menu(
string("Kosmos Updater Diagnostic ") + VERSION + " - Select a channel",
channelItems,
bind(&Application::_menuItemSelected, this, _1),
bind(&Application::_back, this)
);
_appVersionView = new AppVersionView(bind(&Application::_back, this));
_appView = new AppView(bind(&Application::_back, this));
_packageVersionView = new PackageVersionView(bind(&Application::_back, this));
_packageView = new PackageView(bind(&Application::_back, this));
_state = 0;
}
Application::~Application() {
if (_mainMenu != NULL) {
delete _mainMenu;
}
if (_bundleMenu != NULL) {
delete _bundleMenu;
}
if (_channelMenu != NULL) {
delete _channelMenu;
}
if (_appVersionView != NULL) {
delete _appVersionView;
}
if (_appView != NULL) {
delete _appView;
}
if (_packageVersionView != NULL) {
delete _packageVersionView;
}
if (_packageView != NULL) {
delete _packageView;
}
curl_global_cleanup();
consoleExit(NULL);
socketExit();
}
void Application::start() {
while(appletMainLoop())
{
hidScanInput();
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
switch (_state) {
case 0:
_mainMenu->draw(kDown);
break;
case 1:
_appVersionView->draw(kDown);
break;
case 2:
_appView->draw(kDown);
break;
case 3:
case 5:
_channelMenu->draw(kDown);
break;
case 4:
_packageVersionView->draw(kDown);
break;
case 6:
_bundleMenu->draw(kDown);
break;
case 7:
_packageView->draw(kDown);
break;
default:
return;
}
consoleUpdate(NULL);
}
}
void Application::_menuItemSelected(int itemSelected) {
switch (_state) {
case 0:
if (itemSelected == 0) {
_appVersionView->reset();
_state = 1;
} else if (itemSelected == 1) {
_appView->reset();
_state = 2;
} else if (itemSelected == 2) {
_channelMenu->reset();
_state = 3;
} else if (itemSelected == 3) {
_channelMenu->reset();
_state = 5;
} else {
_state = -1;
}
break;
case 3:
if (itemSelected == 0) {
_packageVersionView->channel = 1;
_packageVersionView->reset();
_state = 4;
} else if (itemSelected == 1) {
_packageVersionView->channel = 2;
_packageVersionView->reset();
_state = 4;
} else {
_mainMenu->reset();
_state = 0;
}
break;
case 5:
if (itemSelected == 0) {
_packageView->channel = 1;
_bundleMenu->reset();
_state = 6;
} else if (itemSelected == 1) {
_packageView->channel = 2;
_bundleMenu->reset();
_state = 6;
} else {
_mainMenu->reset();
_state = 0;
}
break;
case 6:
if (itemSelected == 0) {
_packageView->bundle = 1;
_packageView->reset();
_state = 7;
} else if (itemSelected == 1) {
_packageView->bundle = 2;
_packageView->reset();
_state = 7;
} else {
_channelMenu->reset();
_state = 5;
}
break;
}
}
void Application::_back() {
switch (_state) {
case 0:
_state = -1;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 7:
_mainMenu->reset();
_state = 0;
break;
case 6:
_channelMenu->reset();
_state = 5;
break;
}
}
}

+ 46
- 0
src/Application.hpp View File

@ -0,0 +1,46 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "Menu.hpp"
#include "views/AppVersionView.hpp"
#include "views/AppView.hpp"
#include "views/PackageVersionView.hpp"
#include "views/PackageView.hpp"
#pragma once
namespace KUDiag {
class Application {
public:
Application();
~Application();
void start();
private:
int _state;
Menu * _mainMenu;
Menu * _bundleMenu;
Menu * _channelMenu;
AppVersionView * _appVersionView;
AppView * _appView;
PackageVersionView * _packageVersionView;
PackageView * _packageView;
void _menuItemSelected(int itemSelected);
void _back();
};
}

+ 79
- 0
src/Menu.cpp View File

@ -0,0 +1,79 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "Menu.hpp"
#include <iostream>
#include <string>
using namespace std;
namespace KUDiag {
Menu::Menu(string title, vector<string> items, function<void(u64)> itemSelectedCallback, function<void()> backCallback) {
_title = title;
_items = items;
_itemSelectedCallback = itemSelectedCallback;
_backCallback = backCallback;
_hasDrawn = false;
_itemSelected = 0;
}
void Menu::draw(u64 kDown) {
u64 itemSelectedChanged = 0;
if (kDown & KEY_A) {
_itemSelectedCallback(_itemSelected);
} else if (kDown & KEY_B) {
_backCallback();
} else if (kDown & KEY_UP && _itemSelected > 0) {
itemSelectedChanged = -1;
} else if (kDown & KEY_DOWN && _itemSelected < _items.size() - 1) {
itemSelectedChanged = 1;
}
if (!_hasDrawn) {
_itemSelected += itemSelectedChanged;
consoleClear();
cout << "\x1b[0;0H" << _title << "\n\n";
u64 index = 0;
for (auto const& item : _items) {
if (index == _itemSelected) {
cout << " > " << item << "\n";
} else {
cout << " " << item << "\n";
}
index++;
}
_hasDrawn = true;
} else if (itemSelectedChanged != 0) {
u64 previousItemIndex = _itemSelected;
_itemSelected += itemSelectedChanged;
printf("\x1b[%lu;0H ", previousItemIndex + 3);
printf("\x1b[%lu;0H > ", _itemSelected + 3);
}
}
void Menu::reset() {
_hasDrawn = false;
_itemSelected = 0;
}
}

+ 41
- 0
src/Menu.hpp View File

@ -0,0 +1,41 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <switch.h>
#include <string>
#include <vector>
#include <functional>
#pragma once
namespace KUDiag {
class Menu {
public:
Menu(std::string title, std::vector<std::string> items, std::function<void(u64)> itemSelectedCallback, std::function<void()> backCallback);
void draw(u64 kDown);
void reset();
private:
std::string _title;
std::vector<std::string> _items;
std::function<void(u64)> _itemSelectedCallback;
std::function<void()> _backCallback;
bool _hasDrawn;
u64 _itemSelected;
};
}

+ 216
- 0
src/WebRequest.cpp View File

@ -0,0 +1,216 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "WebRequest.hpp"
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
namespace KUDiag {
WebRequest::WebRequest(string method, string url) {
_method = method;
_url = url;
_size = 0;
_data = (char *) malloc(1);
mutexInit(&_mutex);
_progress = 0;
_completed = false;
_errored = false;
_errorMessage = "";
}
WebRequest::~WebRequest() {
if (_data != NULL) {
free(_data);
}
threadWaitForExit(&_thread);
threadClose(&_thread);
}
string WebRequest::getMethod() {
return _method;
}
string WebRequest::getURL() {
return _url;
}
Mutex WebRequest::getMutex() {
return _mutex;
}
u8 WebRequest::getProgress() {
return _progress;
}
bool WebRequest::isComplete() {
return _completed;
}
size_t WebRequest::getSize() {
return _size;
}
string WebRequest::getData() {
return string(_data);
}
char * WebRequest::getRawData() {
return _data;
}
bool WebRequest::saveData(string filename) {
ifstream fileExists(filename);
if (fileExists.good()) {
remove(filename.c_str());
}
fileExists.close();
ofstream file;
file.open(filename, ios::out | ios::binary);
file.write(_data, _size);
file.flush();
file.close();
return file.bad();
}
bool WebRequest::hasError() {
return _errored;
}
string WebRequest::getErrorMessage() {
return _errorMessage;
}
bool WebRequest::start() {
Result res;
if (R_FAILED( res = threadCreate(&_thread, _createRequest, (void *) this, 0x2000, 0x2B, -2)))
return false;
if (R_FAILED( res = threadStart(&_thread)))
return false;
return true;
}
size_t WebRequest::appendData(void *contents, size_t size, size_t nmemb) {
size_t realsize = size * nmemb;
mutexLock(&_mutex);
_data = (char *) realloc(_data, _size + realsize + 1);
if (_data == NULL) {
setErrored("Not enough memory.");
return 0;
}
memcpy(&(_data[_size]), contents, realsize);
_size += realsize;
_data[_size] = 0;
mutexUnlock(&_mutex);
return realsize;
}
int WebRequest::setProgress(curl_off_t dltotal, curl_off_t dlnow) {
mutexLock(&_mutex);
double progress = ((double) dlnow / (double) dltotal) * 100;
_progress = progress;
mutexUnlock(&_mutex);
return 0;
}
void WebRequest::setCompleted() {
mutexLock(&_mutex);
_completed = true;
mutexUnlock(&_mutex);
}
void WebRequest::setErrored(std::string message) {
mutexLock(&_mutex);
_completed = true;
_errored = true;
_errorMessage = message;
mutexUnlock(&_mutex);
}
void WebRequest::_createRequest(void * ptr) {
WebRequest * request = (WebRequest *) ptr;
CURL * curl;
CURLcode res;
string userAgent = string("kosmos-updater/diag-") + VERSION;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, request->getURL().c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request->getMethod().c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _writeFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) request);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, _progressFunction);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, (void *) request);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
request->setErrored(string(curl_easy_strerror(res)));
curl_easy_cleanup(curl);
return;
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
if (http_code != 200) {
request->setErrored("There was an error on the server, please try again later.");
return;
}
} else {
request->setErrored("Unable to initialize CURL.");
return;
}
request->setCompleted();
}
size_t WebRequest::_writeFunction(void *contents, size_t size, size_t nmemb, void * ptr) {
WebRequest * request = (WebRequest *) ptr;
return request->appendData(contents, size, nmemb);
}
size_t WebRequest::_progressFunction(void *ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
WebRequest * request = (WebRequest *) ptr;
return request->setProgress(dltotal, dlnow);
}
}

+ 71
- 0
src/WebRequest.hpp View File

@ -0,0 +1,71 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <switch.h>
#include <curl/curl.h>
#include <string>
#pragma once
namespace KUDiag {
class WebRequest {
public:
WebRequest(std::string method, std::string url);
~WebRequest();
std::string getMethod();
std::string getURL();
Mutex getMutex();
u8 getProgress();
bool isComplete();
size_t getSize();
std::string getData();
char * getRawData();
bool saveData(std::string filename);
bool hasError();
std::string getErrorMessage();
bool start();
size_t appendData(void *contents, size_t size, size_t nmemb);
int setProgress(curl_off_t dltotal, curl_off_t dlnow);
void setCompleted();
void setErrored(std::string message);
private:
static void _createRequest(void * ptr);
static size_t _writeFunction(void *contents, size_t size, size_t nmemb, void * ptr);
static size_t _progressFunction(void *ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
std::string _method;
std::string _url;
size_t _size;
char * _data;
Thread _thread;
Mutex _mutex;
u8 _progress;
bool _completed;
bool _errored;
std::string _errorMessage;
};
}

+ 29
- 0
src/main.cpp View File

@ -0,0 +1,29 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <switch.h>
#include "Application.hpp"
using namespace KUDiag;
int main(int argc, char **argv) {
Application * app = new Application();
app->start();
delete app;
return 0;
}

+ 17
- 0
src/minizip/LICENSE View File

@ -0,0 +1,17 @@
Condition of use and distribution are the same as zlib:
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgement in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

+ 81
- 0
src/minizip/README.md View File

@ -0,0 +1,81 @@
Minizip zlib contribution that includes:
- AES encryption
- I/O buffering
- PKWARE disk spanning
- Visual Studio 2008 project files
It also has the latest bug fixes that having been found all over the internet including the minizip forum and zlib developer's mailing list.
*AES Encryption*
+ Requires #define HAVE_AES
+ Requires AES library files
When using the zip library with password protection it will use AES 256-bit encryption.
When using the unzip library it will automatically use AES when applicable.
*I/O Buffering*
Improves I/O performance by buffering read and write operations.
```
zlib_filefunc64_def filefunc64 = {0};
ourbuffer_t buffered = {0};
fill_win32_filefunc64(&buffered->filefunc64);
fill_buffer_filefunc64(&filefunc64, buffered);
unzOpen2_64(filename, &filefunc64)
```
*PKWARE disk spanning*
To create an archive with multiple disks use zipOpen3_64 supplying a disk_size value in bytes.
```
extern zipFile ZEXPORT zipOpen3_64 OF((const void *pathname, int append,
ZPOS64_T disk_size, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def));
```
The central directory is the only data stored in the .zip and doesn't follow disk_size restrictions.
When using the unzip library it will automatically determine when in needs to span disks.
*I/O Memory*
To unzip from a zip file in memory use fill_memory_filefunc and supply a proper ourmemory_t structure.
```
zlib_filefunc_def filefunc32 = {0};
ourmemory_t unzmem = {0};
unzmem.size = bufsize;
unzmem.base = (char *)malloc(unzmem.size);
memcpy(unzmem.base, buffer, unzmem.size);
fill_memory_filefunc(&filefunc32, &unzmem);
unzOpen2("__notused__", &filefunc32);
```
To create a zip file in memory use fill_memory_filefunc and supply a proper ourmemory_t structure. It is important
not to forget to free zipmem->base when finished. If grow is set, zipmem->base will expand to fit the size of the zip.
If grow is not set be sure to fill out zipmem.base and zipmem.size.
```
zlib_filefunc_def filefunc32 = {0};
ourmemory_t zipmem = {0};
zipmem.grow = 1;
fill_memory_filefunc(&filefunc32, &zipmem);
zipOpen3("__notused__", APPEND_STATUS_CREATE, 0, 0, &filefunc32);
```
*BZIP2*
+ Requires #define HAVE_BZIP2
+ Requires BZIP2 library
*Windows RT*
+ Requires #define IOWIN32_USING_WINRT_API

+ 130
- 0
src/minizip/crypt.h View File

@ -0,0 +1,130 @@
/* crypt.h -- base code for traditional PKWARE encryption
Version 1.01e, February 12th, 2005
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
See the Info-ZIP LICENSE file version 2000-Apr-09 or later for terms of use
which also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
The encryption/decryption parts of this source code (as opposed to the
non-echoing password parts) were originally written in Europe. The
whole source package can be freely distributed, including from the USA.
(Prior to January 2000, re-export from the US was a violation of US law.)
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
If you don't need crypting in your application, just define symbols
NOCRYPT and NOUNCRYPT.
*/
#define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
/***********************************************************************
* Return the next byte in the pseudo-random sequence
*/
static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
{
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
* unpredictable manner on 16-bit systems; not a problem
* with any known compiler so far, though */
temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
}
/***********************************************************************
* Update the encryption keys with the next byte of plain text
*/
static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
{
(*(pkeys+0)) = CRC32((*(pkeys+0)), c);
(*(pkeys+1)) += (*(pkeys+0)) & 0xff;
(*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
{
register int keyshift = (int)((*(pkeys+1)) >> 24);
(*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
}
return c;
}
/***********************************************************************
* Initialize the encryption keys and the random header according to
* the given password.
*/
static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
{
*(pkeys+0) = 305419896L;
*(pkeys+1) = 591751049L;
*(pkeys+2) = 878082192L;
while (*passwd != 0) {
update_keys(pkeys,pcrc_32_tab,(int)*passwd);
passwd++;
}
}
#define zdecode(pkeys,pcrc_32_tab,c) \
(update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
#define zencode(pkeys,pcrc_32_tab,c,t) \
(t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
#define RAND_HEAD_LEN 12
/* "last resort" source for second part of crypt seed pattern */
# ifndef ZCR_SEED2
# define ZCR_SEED2 3141592654UL /* use PI as default pattern */
# endif
static int crypthead(const char* passwd, /* password string */
unsigned char* buf, /* where to write header */
int bufSize,
unsigned long* pkeys,
const unsigned long* pcrc_32_tab,
unsigned long crcForCrypting)
{
int n; /* index in random header */
int t; /* temporary */
int c; /* random byte */
unsigned char header[RAND_HEAD_LEN-2]; /* random header */
static unsigned calls = 0; /* ensure different random header each time */
if (bufSize<RAND_HEAD_LEN)
return 0;
/* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
* output of rand() to get less predictability, since rand() is
* often poorly implemented.
*/
if (++calls == 1)
{
srand((unsigned)(time(NULL) ^ ZCR_SEED2));
}
init_keys(passwd, pkeys, pcrc_32_tab);
for (n = 0; n < RAND_HEAD_LEN-2; n++)
{
c = (rand() >> 7) & 0xff;
header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
}
/* Encrypt random header (last two bytes is high word of crc) */
init_keys(passwd, pkeys, pcrc_32_tab);
for (n = 0; n < RAND_HEAD_LEN-2; n++)
{
buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
}
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
return n;
}
#endif

+ 369
- 0
src/minizip/ioapi.c View File

@ -0,0 +1,369 @@
/* ioapi.h -- IO base function header for compress/uncompress .zip
part of the MiniZip project
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
Modifications for Zip64 support
Copyright (C) 2009-2010 Mathias Svensson
http://result42.com
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <string.h>
#include "ioapi.h"
#if defined(_WIN32)
# define snprintf _snprintf
#endif
#ifdef __APPLE__
/* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */
# define FOPEN_FUNC(filename, mode) fopen(filename, mode)
# define FTELLO_FUNC(stream) ftello(stream)
# define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
#else
# define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
# define FTELLO_FUNC(stream) ftello64(stream)
# define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
#endif
/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
#ifndef SEEK_CUR
# define SEEK_CUR 1
#endif
#ifndef SEEK_END
# define SEEK_END 2
#endif
#ifndef SEEK_SET
# define SEEK_SET 0
#endif
voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
{
if (pfilefunc->zfile_func64.zopen64_file != NULL)
return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
}
voidpf call_zopendisk64 OF((const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, int number_disk, int mode))
{
if (pfilefunc->zfile_func64.zopendisk64_file != NULL)
return (*(pfilefunc->zfile_func64.zopendisk64_file)) (pfilefunc->zfile_func64.opaque,filestream,number_disk,mode);
return (*(pfilefunc->zopendisk32_file))(pfilefunc->zfile_func64.opaque,filestream,number_disk,mode);
}
long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
{
uLong offsetTruncated;
if (pfilefunc->zfile_func64.zseek64_file != NULL)
return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
offsetTruncated = (uLong)offset;
if (offsetTruncated != offset)
return -1;
return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
}
ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
{
uLong tell_uLong;
if (pfilefunc->zfile_func64.zseek64_file != NULL)
return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
if ((tell_uLong) == 0xffffffff)
return (ZPOS64_T)-1;
return tell_uLong;
}
void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
{
p_filefunc64_32->zfile_func64.zopen64_file = NULL;
p_filefunc64_32->zfile_func64.zopendisk64_file = NULL;
p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
p_filefunc64_32->zopendisk32_file = p_filefunc32->zopendisk_file;
p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
p_filefunc64_32->zfile_func64.ztell64_file = NULL;
p_filefunc64_32->zfile_func64.zseek64_file = NULL;
p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
}
static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
typedef struct
{
FILE *file;
int filenameLength;
void *filename;
} FILE_IOPOSIX;
static voidpf file_build_ioposix(FILE *file, const char *filename)
{
FILE_IOPOSIX *ioposix = NULL;
if (file == NULL)
return NULL;
ioposix = (FILE_IOPOSIX*)malloc(sizeof(FILE_IOPOSIX));
ioposix->file = file;
ioposix->filenameLength = strlen(filename) + 1;
ioposix->filename = (char*)malloc(ioposix->filenameLength * sizeof(char));
strncpy(ioposix->filename, filename, ioposix->filenameLength);
return (voidpf)ioposix;
}
static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
{
FILE* file = NULL;
const char* mode_fopen = NULL;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
mode_fopen = "rb";
else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
mode_fopen = "r+b";
else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
mode_fopen = "wb";
if ((filename != NULL) && (mode_fopen != NULL))
{
file = fopen(filename, mode_fopen);
return file_build_ioposix(file, filename);
}
return file;
}
static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
{
FILE* file = NULL;
const char* mode_fopen = NULL;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
mode_fopen = "rb";
else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
mode_fopen = "r+b";
else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
mode_fopen = "wb";
if ((filename != NULL) && (mode_fopen != NULL))
{
file = FOPEN_FUNC((const char*)filename, mode_fopen);
return file_build_ioposix(file, (const char*)filename);
}
return file;
}
static voidpf ZCALLBACK fopendisk64_file_func (voidpf opaque, voidpf stream, int number_disk, int mode)
{
FILE_IOPOSIX *ioposix = NULL;
char *diskFilename = NULL;
voidpf ret = NULL;
int i = 0;
if (stream == NULL)
return NULL;
ioposix = (FILE_IOPOSIX*)stream;
diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
strncpy(diskFilename, ioposix->filename, ioposix->filenameLength);
for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
{
if (diskFilename[i] != '.')
continue;
snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1);
break;
}
if (i >= 0)
ret = fopen64_file_func(opaque, diskFilename, mode);
free(diskFilename);
return ret;
}
static voidpf ZCALLBACK fopendisk_file_func (voidpf opaque, voidpf stream, int number_disk, int mode)
{
FILE_IOPOSIX *ioposix = NULL;
char *diskFilename = NULL;
voidpf ret = NULL;
int i = 0;
if (stream == NULL)
return NULL;
ioposix = (FILE_IOPOSIX*)stream;
diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
strncpy(diskFilename, ioposix->filename, ioposix->filenameLength);
for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
{
if (diskFilename[i] != '.')
continue;
snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1);
break;
}
if (i >= 0)
ret = fopen_file_func(opaque, diskFilename, mode);
free(diskFilename);
return ret;
}
static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
{
FILE_IOPOSIX *ioposix = NULL;
uLong ret;
if (stream == NULL)
return -1;