mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-20 20:23:25 +01:00
More spm fixes
This commit is contained in:
164
Sources/libimobiledevice/dependencies/libimobiledevice-glue/.github/workflows/build.yml
vendored
Normal file
164
Sources/libimobiledevice/dependencies/libimobiledevice-glue/.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
name: build
|
||||
|
||||
on:
|
||||
push:
|
||||
schedule:
|
||||
- cron: '0 0 1 * *'
|
||||
|
||||
jobs:
|
||||
build-linux-ubuntu:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: install dependencies
|
||||
run: |
|
||||
#sudo apt-get install cython
|
||||
- name: prepare environment
|
||||
run: |
|
||||
echo "target_triplet=`gcc -dumpmachine`" >> $GITHUB_ENV
|
||||
- name: fetch libplist
|
||||
uses: dawidd6/action-download-artifact@v2
|
||||
with:
|
||||
github_token: ${{secrets.GITHUB_TOKEN}}
|
||||
workflow: build.yml
|
||||
name: libplist-latest_${{env.target_triplet}}
|
||||
repo: libimobiledevice/libplist
|
||||
- name: install external dependencies
|
||||
run: |
|
||||
mkdir extract
|
||||
for I in *.tar; do
|
||||
tar -C extract -xvf $I
|
||||
done
|
||||
sudo cp -r extract/* /
|
||||
sudo ldconfig
|
||||
- uses: actions/checkout@v2
|
||||
- name: autogen
|
||||
run: ./autogen.sh PKG_CONFIG_PATH=/usr/local/lib/pkgconfig LDFLAGS="-Wl,-rpath=/usr/local/lib"
|
||||
- name: make
|
||||
run: make
|
||||
- name: make install
|
||||
run: sudo make install
|
||||
- name: prepare artifact
|
||||
run: |
|
||||
mkdir -p dest
|
||||
DESTDIR=`pwd`/dest make install
|
||||
tar -C dest -cf libimobiledevice-glue.tar usr
|
||||
- name: publish artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: libimobiledevice-glue-latest_${{env.target_triplet}}
|
||||
path: libimobiledevice-glue.tar
|
||||
build-macOS:
|
||||
runs-on: macOS-latest
|
||||
steps:
|
||||
- name: install dependencies
|
||||
run: |
|
||||
if test -x "`which port`"; then
|
||||
sudo port install libtool autoconf automake pkgconfig
|
||||
else
|
||||
brew install libtool autoconf automake pkgconfig
|
||||
fi
|
||||
shell: bash
|
||||
- name: fetch libplist
|
||||
uses: dawidd6/action-download-artifact@v2
|
||||
with:
|
||||
github_token: ${{secrets.GITHUB_TOKEN}}
|
||||
workflow: build.yml
|
||||
name: libplist-latest_macOS
|
||||
repo: libimobiledevice/libplist
|
||||
- name: install external dependencies
|
||||
run: |
|
||||
mkdir extract
|
||||
for I in *.tar; do
|
||||
tar -C extract -xvf $I
|
||||
done
|
||||
sudo cp -r extract/* /
|
||||
- uses: actions/checkout@v2
|
||||
- name: autogen
|
||||
run: |
|
||||
SDKDIR=`xcrun --sdk macosx --show-sdk-path`
|
||||
TESTARCHS="arm64 x86_64"
|
||||
USEARCHS=
|
||||
for ARCH in $TESTARCHS; do
|
||||
if echo "int main(int argc, char **argv) { return 0; }" |clang -arch $ARCH -o /dev/null -isysroot $SDKDIR -x c - 2>/dev/null; then
|
||||
USEARCHS="$USEARCHS -arch $ARCH"
|
||||
fi
|
||||
done
|
||||
export CFLAGS="$USEARCHS -isysroot $SDKDIR"
|
||||
echo "Using CFLAGS: $CFLAGS"
|
||||
./autogen.sh PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
|
||||
- name: make
|
||||
run: make
|
||||
- name: make install
|
||||
run: sudo make install
|
||||
- name: prepare artifact
|
||||
run: |
|
||||
mkdir -p dest
|
||||
DESTDIR=`pwd`/dest make install
|
||||
tar -C dest -cf libimobiledevice-glue.tar usr
|
||||
- name: publish artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: libimobiledevice-glue-latest_macOS
|
||||
path: libimobiledevice-glue.tar
|
||||
build-windows:
|
||||
runs-on: windows-2019
|
||||
defaults:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include: [
|
||||
{ msystem: MINGW64, arch: x86_64 },
|
||||
{ msystem: MINGW32, arch: i686 }
|
||||
]
|
||||
steps:
|
||||
- uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
msystem: ${{ matrix.msystem }}
|
||||
release: false
|
||||
update: false
|
||||
install: >-
|
||||
base-devel
|
||||
git
|
||||
mingw-w64-${{ matrix.arch }}-gcc
|
||||
make
|
||||
libtool
|
||||
autoconf
|
||||
automake-wrapper
|
||||
- name: prepare environment
|
||||
run: |
|
||||
dest=`echo ${{ matrix.msystem }} |tr [:upper:] [:lower:]`
|
||||
echo "dest=$dest" >> $GITHUB_ENV
|
||||
echo "target_triplet=`gcc -dumpmachine`" >> $GITHUB_ENV
|
||||
- name: fetch libplist
|
||||
uses: dawidd6/action-download-artifact@v2
|
||||
with:
|
||||
github_token: ${{secrets.GITHUB_TOKEN}}
|
||||
workflow: build.yml
|
||||
name: libplist-latest_${{ matrix.arch }}-${{ env.dest }}
|
||||
repo: libimobiledevice/libplist
|
||||
- name: install external dependencies
|
||||
run: |
|
||||
mkdir extract
|
||||
for I in *.tar; do
|
||||
tar -C extract -xvf $I
|
||||
done
|
||||
cp -r extract/* /
|
||||
- uses: actions/checkout@v2
|
||||
- name: autogen
|
||||
run: ./autogen.sh CC=gcc CXX=g++
|
||||
- name: make
|
||||
run: make
|
||||
- name: make install
|
||||
run: make install
|
||||
- name: prepare artifact
|
||||
run: |
|
||||
mkdir -p dest
|
||||
DESTDIR=`pwd`/dest make install
|
||||
tar -C dest -cf libimobiledevice-glue.tar ${{ env.dest }}
|
||||
- name: publish artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: libimobiledevice-glue-latest_${{ matrix.arch }}-${{ env.dest }}
|
||||
path: libimobiledevice-glue.tar
|
||||
39
Sources/libimobiledevice/dependencies/libimobiledevice-glue/.gitignore
vendored
Normal file
39
Sources/libimobiledevice/dependencies/libimobiledevice-glue/.gitignore
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
# git-ls-files --others --exclude-from=.git/info/exclude
|
||||
# Lines that start with '#' are comments.
|
||||
# For a project mostly in C, the following would be a good set of
|
||||
# exclude patterns (uncomment them if you want to use them):
|
||||
*.[oa]
|
||||
*~
|
||||
*.po
|
||||
*.lo
|
||||
*.la
|
||||
autom4te.cache/*
|
||||
*.in
|
||||
*/.deps/*
|
||||
m4/*
|
||||
swig/*
|
||||
*.swp
|
||||
*.patch
|
||||
aclocal.m4
|
||||
config.h
|
||||
config.log
|
||||
config.sub
|
||||
config.guess
|
||||
config.status
|
||||
configure
|
||||
depcomp
|
||||
install-sh
|
||||
compile
|
||||
main
|
||||
ltmain.sh
|
||||
missing
|
||||
mkinstalldirs
|
||||
libtool
|
||||
*Makefile
|
||||
py-compile
|
||||
stamp-h1
|
||||
src/.libs
|
||||
src/libimobiledevice-glue-1.0.pc
|
||||
.idea/
|
||||
.DS_Store
|
||||
.vscode/
|
||||
@@ -0,0 +1,502 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 2.1, February 1999
|
||||
|
||||
Copyright (C) 1991, 1999 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.
|
||||
|
||||
[This is the first released version of the Lesser GPL. It also counts
|
||||
as the successor of the GNU Library Public License, version 2, hence
|
||||
the version number 2.1.]
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
Licenses are intended to guarantee your freedom to share and change
|
||||
free software--to make sure the software is free for all its users.
|
||||
|
||||
This license, the Lesser General Public License, applies to some
|
||||
specially designated software packages--typically libraries--of the
|
||||
Free Software Foundation and other authors who decide to use it. You
|
||||
can use it too, but we suggest you first think carefully about whether
|
||||
this license or the ordinary General Public License is the better
|
||||
strategy to use in any particular case, based on the explanations below.
|
||||
|
||||
When we speak of free software, we are referring to freedom of use,
|
||||
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 and use pieces of
|
||||
it in new free programs; and that you are informed that you can do
|
||||
these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
distributors to deny you these rights or to ask you to surrender these
|
||||
rights. These restrictions translate to certain responsibilities for
|
||||
you if you distribute copies of the library or if you modify it.
|
||||
|
||||
For example, if you distribute copies of the library, whether gratis
|
||||
or for a fee, you must give the recipients all the rights that we gave
|
||||
you. You must make sure that they, too, receive or can get the source
|
||||
code. If you link other code with the library, you must provide
|
||||
complete object files to the recipients, so that they can relink them
|
||||
with the library after making changes to the library and recompiling
|
||||
it. And you must show them these terms so they know their rights.
|
||||
|
||||
We protect your rights with a two-step method: (1) we copyright the
|
||||
library, and (2) we offer you this license, which gives you legal
|
||||
permission to copy, distribute and/or modify the library.
|
||||
|
||||
To protect each distributor, we want to make it very clear that
|
||||
there is no warranty for the free library. Also, if the library is
|
||||
modified by someone else and passed on, the recipients should know
|
||||
that what they have is not the original version, so that the original
|
||||
author's reputation will not be affected by problems that might be
|
||||
introduced by others.
|
||||
|
||||
Finally, software patents pose a constant threat to the existence of
|
||||
any free program. We wish to make sure that a company cannot
|
||||
effectively restrict the users of a free program by obtaining a
|
||||
restrictive license from a patent holder. Therefore, we insist that
|
||||
any patent license obtained for a version of the library must be
|
||||
consistent with the full freedom of use specified in this license.
|
||||
|
||||
Most GNU software, including some libraries, is covered by the
|
||||
ordinary GNU General Public License. This license, the GNU Lesser
|
||||
General Public License, applies to certain designated libraries, and
|
||||
is quite different from the ordinary General Public License. We use
|
||||
this license for certain libraries in order to permit linking those
|
||||
libraries into non-free programs.
|
||||
|
||||
When a program is linked with a library, whether statically or using
|
||||
a shared library, the combination of the two is legally speaking a
|
||||
combined work, a derivative of the original library. The ordinary
|
||||
General Public License therefore permits such linking only if the
|
||||
entire combination fits its criteria of freedom. The Lesser General
|
||||
Public License permits more lax criteria for linking other code with
|
||||
the library.
|
||||
|
||||
We call this license the "Lesser" General Public License because it
|
||||
does Less to protect the user's freedom than the ordinary General
|
||||
Public License. It also provides other free software developers Less
|
||||
of an advantage over competing non-free programs. These disadvantages
|
||||
are the reason we use the ordinary General Public License for many
|
||||
libraries. However, the Lesser license provides advantages in certain
|
||||
special circumstances.
|
||||
|
||||
For example, on rare occasions, there may be a special need to
|
||||
encourage the widest possible use of a certain library, so that it becomes
|
||||
a de-facto standard. To achieve this, non-free programs must be
|
||||
allowed to use the library. A more frequent case is that a free
|
||||
library does the same job as widely used non-free libraries. In this
|
||||
case, there is little to gain by limiting the free library to free
|
||||
software only, so we use the Lesser General Public License.
|
||||
|
||||
In other cases, permission to use a particular library in non-free
|
||||
programs enables a greater number of people to use a large body of
|
||||
free software. For example, permission to use the GNU C Library in
|
||||
non-free programs enables many more people to use the whole GNU
|
||||
operating system, as well as its variant, the GNU/Linux operating
|
||||
system.
|
||||
|
||||
Although the Lesser General Public License is Less protective of the
|
||||
users' freedom, it does ensure that the user of a program that is
|
||||
linked with the Library has the freedom and the wherewithal to run
|
||||
that program using a modified version of the Library.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow. Pay close attention to the difference between a
|
||||
"work based on the library" and a "work that uses the library". The
|
||||
former contains code derived from the library, whereas the latter must
|
||||
be combined with the library in order to run.
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License Agreement applies to any software library or other
|
||||
program which contains a notice placed by the copyright holder or
|
||||
other authorized party saying it may be distributed under the terms of
|
||||
this Lesser General Public License (also called "this License").
|
||||
Each licensee is addressed as "you".
|
||||
|
||||
A "library" means a collection of software functions and/or data
|
||||
prepared so as to be conveniently linked with application programs
|
||||
(which use some of those functions and data) to form executables.
|
||||
|
||||
The "Library", below, refers to any such software library or work
|
||||
which has been distributed under these terms. A "work based on the
|
||||
Library" means either the Library or any derivative work under
|
||||
copyright law: that is to say, a work containing the Library or a
|
||||
portion of it, either verbatim or with modifications and/or translated
|
||||
straightforwardly into another language. (Hereinafter, translation is
|
||||
included without limitation in the term "modification".)
|
||||
|
||||
"Source code" for a work means the preferred form of the work for
|
||||
making modifications to it. For a library, 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 library.
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running a program using the Library is not restricted, and output from
|
||||
such a program is covered only if its contents constitute a work based
|
||||
on the Library (independent of the use of the Library in a tool for
|
||||
writing it). Whether that is true depends on what the Library does
|
||||
and what the program that uses the Library does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Library's
|
||||
complete 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 distribute a copy of this License along with the
|
||||
Library.
|
||||
|
||||
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 Library or any portion
|
||||
of it, thus forming a work based on the Library, 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) The modified work must itself be a software library.
|
||||
|
||||
b) You must cause the files modified to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
c) You must cause the whole of the work to be licensed at no
|
||||
charge to all third parties under the terms of this License.
|
||||
|
||||
d) If a facility in the modified Library refers to a function or a
|
||||
table of data to be supplied by an application program that uses
|
||||
the facility, other than as an argument passed when the facility
|
||||
is invoked, then you must make a good faith effort to ensure that,
|
||||
in the event an application does not supply such function or
|
||||
table, the facility still operates, and performs whatever part of
|
||||
its purpose remains meaningful.
|
||||
|
||||
(For example, a function in a library to compute square roots has
|
||||
a purpose that is entirely well-defined independent of the
|
||||
application. Therefore, Subsection 2d requires that any
|
||||
application-supplied function or table used by this function must
|
||||
be optional: if the application does not supply it, the square
|
||||
root function must still compute square roots.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Library,
|
||||
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 Library, 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 Library.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Library
|
||||
with the Library (or with a work based on the Library) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||
License instead of this License to a given copy of the Library. To do
|
||||
this, you must alter all the notices that refer to this License, so
|
||||
that they refer to the ordinary GNU General Public License, version 2,
|
||||
instead of to this License. (If a newer version than version 2 of the
|
||||
ordinary GNU General Public License has appeared, then you can specify
|
||||
that version instead if you wish.) Do not make any other change in
|
||||
these notices.
|
||||
|
||||
Once this change is made in a given copy, it is irreversible for
|
||||
that copy, so the ordinary GNU General Public License applies to all
|
||||
subsequent copies and derivative works made from that copy.
|
||||
|
||||
This option is useful when you wish to copy part of the code of
|
||||
the Library into a program that is not a library.
|
||||
|
||||
4. You may copy and distribute the Library (or a portion or
|
||||
derivative of it, under Section 2) in object code or executable form
|
||||
under the terms of Sections 1 and 2 above provided that you 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.
|
||||
|
||||
If distribution of 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 satisfies the requirement to
|
||||
distribute the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
5. A program that contains no derivative of any portion of the
|
||||
Library, but is designed to work with the Library by being compiled or
|
||||
linked with it, is called a "work that uses the Library". Such a
|
||||
work, in isolation, is not a derivative work of the Library, and
|
||||
therefore falls outside the scope of this License.
|
||||
|
||||
However, linking a "work that uses the Library" with the Library
|
||||
creates an executable that is a derivative of the Library (because it
|
||||
contains portions of the Library), rather than a "work that uses the
|
||||
library". The executable is therefore covered by this License.
|
||||
Section 6 states terms for distribution of such executables.
|
||||
|
||||
When a "work that uses the Library" uses material from a header file
|
||||
that is part of the Library, the object code for the work may be a
|
||||
derivative work of the Library even though the source code is not.
|
||||
Whether this is true is especially significant if the work can be
|
||||
linked without the Library, or if the work is itself a library. The
|
||||
threshold for this to be true is not precisely defined by law.
|
||||
|
||||
If such an object file uses only numerical parameters, data
|
||||
structure layouts and accessors, and small macros and small inline
|
||||
functions (ten lines or less in length), then the use of the object
|
||||
file is unrestricted, regardless of whether it is legally a derivative
|
||||
work. (Executables containing this object code plus portions of the
|
||||
Library will still fall under Section 6.)
|
||||
|
||||
Otherwise, if the work is a derivative of the Library, you may
|
||||
distribute the object code for the work under the terms of Section 6.
|
||||
Any executables containing that work also fall under Section 6,
|
||||
whether or not they are linked directly with the Library itself.
|
||||
|
||||
6. As an exception to the Sections above, you may also combine or
|
||||
link a "work that uses the Library" with the Library to produce a
|
||||
work containing portions of the Library, and distribute that work
|
||||
under terms of your choice, provided that the terms permit
|
||||
modification of the work for the customer's own use and reverse
|
||||
engineering for debugging such modifications.
|
||||
|
||||
You must give prominent notice with each copy of the work that the
|
||||
Library is used in it and that the Library and its use are covered by
|
||||
this License. You must supply a copy of this License. If the work
|
||||
during execution displays copyright notices, you must include the
|
||||
copyright notice for the Library among them, as well as a reference
|
||||
directing the user to the copy of this License. Also, you must do one
|
||||
of these things:
|
||||
|
||||
a) Accompany the work with the complete corresponding
|
||||
machine-readable source code for the Library including whatever
|
||||
changes were used in the work (which must be distributed under
|
||||
Sections 1 and 2 above); and, if the work is an executable linked
|
||||
with the Library, with the complete machine-readable "work that
|
||||
uses the Library", as object code and/or source code, so that the
|
||||
user can modify the Library and then relink to produce a modified
|
||||
executable containing the modified Library. (It is understood
|
||||
that the user who changes the contents of definitions files in the
|
||||
Library will not necessarily be able to recompile the application
|
||||
to use the modified definitions.)
|
||||
|
||||
b) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (1) uses at run time a
|
||||
copy of the library already present on the user's computer system,
|
||||
rather than copying library functions into the executable, and (2)
|
||||
will operate properly with a modified version of the library, if
|
||||
the user installs one, as long as the modified version is
|
||||
interface-compatible with the version that the work was made with.
|
||||
|
||||
c) Accompany the work with a written offer, valid for at
|
||||
least three years, to give the same user the materials
|
||||
specified in Subsection 6a, above, for a charge no more
|
||||
than the cost of performing this distribution.
|
||||
|
||||
d) If distribution of the work is made by offering access to copy
|
||||
from a designated place, offer equivalent access to copy the above
|
||||
specified materials from the same place.
|
||||
|
||||
e) Verify that the user has already received a copy of these
|
||||
materials or that you have already sent this user a copy.
|
||||
|
||||
For an executable, the required form of the "work that uses the
|
||||
Library" must include any data and utility programs needed for
|
||||
reproducing the executable from it. However, as a special exception,
|
||||
the materials to be 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.
|
||||
|
||||
It may happen that this requirement contradicts the license
|
||||
restrictions of other proprietary libraries that do not normally
|
||||
accompany the operating system. Such a contradiction means you cannot
|
||||
use both them and the Library together in an executable that you
|
||||
distribute.
|
||||
|
||||
7. You may place library facilities that are a work based on the
|
||||
Library side-by-side in a single library together with other library
|
||||
facilities not covered by this License, and distribute such a combined
|
||||
library, provided that the separate distribution of the work based on
|
||||
the Library and of the other library facilities is otherwise
|
||||
permitted, and provided that you do these two things:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work
|
||||
based on the Library, uncombined with any other library
|
||||
facilities. This must be distributed under the terms of the
|
||||
Sections above.
|
||||
|
||||
b) Give prominent notice with the combined library of the fact
|
||||
that part of it is a work based on the Library, and explaining
|
||||
where to find the accompanying uncombined form of the same work.
|
||||
|
||||
8. You may not copy, modify, sublicense, link with, or distribute
|
||||
the Library except as expressly provided under this License. Any
|
||||
attempt otherwise to copy, modify, sublicense, link with, or
|
||||
distribute the Library 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.
|
||||
|
||||
9. 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 Library or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Library (or any work based on the
|
||||
Library), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Library or works based on it.
|
||||
|
||||
10. Each time you redistribute the Library (or any work based on the
|
||||
Library), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute, link with or modify the Library
|
||||
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 with
|
||||
this License.
|
||||
|
||||
11. 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 Library at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Library 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 Library.
|
||||
|
||||
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.
|
||||
|
||||
12. If the distribution and/or use of the Library is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Library 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.
|
||||
|
||||
13. The Free Software Foundation may publish revised and/or new
|
||||
versions of the Lesser 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 Library
|
||||
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 Library does not specify a
|
||||
license version number, you may choose any version ever published by
|
||||
the Free Software Foundation.
|
||||
|
||||
14. If you wish to incorporate parts of the Library into other free
|
||||
programs whose distribution conditions are incompatible with these,
|
||||
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
|
||||
|
||||
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
|
||||
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
|
||||
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||
OTHER PARTIES PROVIDE THE LIBRARY "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
|
||||
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. 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 LIBRARY 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
|
||||
LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), 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 Libraries
|
||||
|
||||
If you develop a new library, and you want it to be of the greatest
|
||||
possible use to the public, we recommend making it free software that
|
||||
everyone can redistribute and change. You can do so by permitting
|
||||
redistribution under these terms (or, alternatively, under the terms of the
|
||||
ordinary General Public License).
|
||||
|
||||
To apply these terms, attach the following notices to the library. 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 library's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; 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.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the library, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the
|
||||
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1990
|
||||
Ty Coon, President of Vice
|
||||
|
||||
That's all there is to it!
|
||||
@@ -0,0 +1,10 @@
|
||||
AUTOMAKE_OPTIONS = foreign
|
||||
ACLOCAL_AMFLAGS = -I m4
|
||||
SUBDIRS = src include
|
||||
|
||||
EXTRA_DIST = \
|
||||
README.md
|
||||
|
||||
indent:
|
||||
indent -kr -ut -ts4 -l120 src/*.c src/*.h
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
# libimobiledevice-glue
|
||||
|
||||
Library with common code used by the libraries and tools around the
|
||||
**libimobiledevice** project.
|
||||
|
||||

|
||||
|
||||
## Features
|
||||
|
||||
The main functionality provided by this library are **socket** helper
|
||||
functions and a platform independent **thread/mutex** implementation.
|
||||
Besides that it comes with a number of string, file, and plist helper
|
||||
functions, as well as some other commonly used code that was originally
|
||||
duplicated in the dedicated projects.
|
||||
|
||||
Test on Linux, macOS, Windows.
|
||||
|
||||
## Projects using this library
|
||||
|
||||
- [libusbmuxd](https://github.com/libimobiledevice/libusbmuxd)
|
||||
- [libimobiledevice](https://github.com/libimobiledevice/libimobiledevice)
|
||||
- [usbmuxd](https://github.com/libimobiledevice/usbmuxd)
|
||||
- [libirecovery](https://github.com/libimobiledevice/libirecovery)
|
||||
- [idevicerestore](https://github.com/libimobiledevice/idevicerestore)
|
||||
|
||||
## Installation / Getting started
|
||||
|
||||
### Debian / Ubuntu Linux
|
||||
|
||||
First install all required dependencies and build tools:
|
||||
```shell
|
||||
sudo apt-get install \
|
||||
build-essential \
|
||||
pkg-config \
|
||||
checkinstall \
|
||||
git \
|
||||
autoconf \
|
||||
automake \
|
||||
libtool-bin \
|
||||
libplist-dev \
|
||||
```
|
||||
|
||||
Then clone the actual project repository:
|
||||
```shell
|
||||
git clone https://github.com/libimobiledevice/libimobiledevice-glue.git
|
||||
cd libimobiledevice-glue
|
||||
```
|
||||
|
||||
Now you can build and install it:
|
||||
```shell
|
||||
./autogen.sh
|
||||
make
|
||||
sudo make install
|
||||
```
|
||||
|
||||
If you require a custom prefix or other option being passed to `./configure`
|
||||
you can pass them directly to `./autogen.sh` like this:
|
||||
```bash
|
||||
./autogen.sh --prefix=/opt/local
|
||||
make
|
||||
sudo make install
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
This library is directly used by libusbmuxd, libimobiledevice, etc., so there
|
||||
is no need to do anything in particular.
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome contributions from anyone and are grateful for every pull request!
|
||||
|
||||
If you'd like to contribute, please fork the `master` branch, change, commit and
|
||||
send a pull request for review. Once approved it can be merged into the main
|
||||
code base.
|
||||
|
||||
If you plan to contribute larger changes or a major refactoring, please create a
|
||||
ticket first to discuss the idea upfront to ensure less effort for everyone.
|
||||
|
||||
Please make sure your contribution adheres to:
|
||||
* Try to follow the code style of the project
|
||||
* Commit messages should describe the change well without being too short
|
||||
* Try to split larger changes into individual commits of a common domain
|
||||
|
||||
## Links
|
||||
|
||||
* Homepage: https://libimobiledevice.org/
|
||||
* Repository: https://git.libimobiledevice.org/libimobiledevice-glue.git
|
||||
* Repository (Mirror): https://github.com/libimobiledevice/libimobiledevice-glue.git
|
||||
* Issue Tracker: https://github.com/libimobiledevice/libimobiledevice-glue/issues
|
||||
* Mailing List: https://lists.libimobiledevice.org/mailman/listinfo/libimobiledevice-devel
|
||||
* Twitter: https://twitter.com/libimobiledev
|
||||
|
||||
## License
|
||||
|
||||
This library and utilities are licensed under the [GNU Lesser General Public License v2.1](https://www.gnu.org/licenses/lgpl-2.1.en.html),
|
||||
also included in the repository in the `COPYING` file.
|
||||
|
||||
## Credits
|
||||
|
||||
Apple, iPhone, iPad, iPod, iPod Touch, Apple TV, Apple Watch, Mac, iOS,
|
||||
iPadOS, tvOS, watchOS, and macOS are trademarks of Apple Inc.
|
||||
|
||||
This project is an independent software and has not been authorized, sponsored,
|
||||
or otherwise approved by Apple Inc.
|
||||
|
||||
README Updated on: 2022-04-04
|
||||
26
Sources/libimobiledevice/dependencies/libimobiledevice-glue/autogen.sh
Executable file
26
Sources/libimobiledevice/dependencies/libimobiledevice-glue/autogen.sh
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/bin/sh
|
||||
|
||||
olddir=`pwd`
|
||||
srcdir=`dirname $0`
|
||||
test -z "$srcdir" && srcdir=.
|
||||
|
||||
(
|
||||
cd "$srcdir"
|
||||
|
||||
gprefix=`which glibtoolize 2>&1 >/dev/null`
|
||||
if [ $? -eq 0 ]; then
|
||||
glibtoolize --force
|
||||
else
|
||||
libtoolize --force
|
||||
fi
|
||||
aclocal -I m4
|
||||
autoheader
|
||||
automake --add-missing
|
||||
autoconf
|
||||
|
||||
cd "$olddir"
|
||||
)
|
||||
|
||||
if [ -z "$NOCONFIGURE" ]; then
|
||||
$srcdir/configure "$@"
|
||||
fi
|
||||
@@ -0,0 +1,136 @@
|
||||
# -*- Autoconf -*-
|
||||
# Process this file with autoconf to produce a configure script.
|
||||
|
||||
AC_PREREQ(2.68)
|
||||
AC_INIT([libimobiledevice-glue], [1.0.0], [https://github.com/libimobiledevice/libimobiledevice-glue/issues],, [https://libimobiledevice.org])
|
||||
AM_INIT_AUTOMAKE([dist-bzip2 no-dist-gzip check-news])
|
||||
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES])
|
||||
AC_CONFIG_SRCDIR([src/])
|
||||
AC_CONFIG_HEADERS([config.h])
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
|
||||
dnl libtool versioning
|
||||
# +1 : 0 : +1 == adds new functions to the interface
|
||||
# +1 : 0 : 0 == changes or removes functions (changes include both
|
||||
# changes to the signature and the semantic)
|
||||
# ? :+1 : ? == just internal changes
|
||||
# CURRENT : REVISION : AGE
|
||||
LIBIMOBILEDEVICE_GLUE_SO_VERSION=0:0:0
|
||||
|
||||
dnl Minimum package versions
|
||||
LIBPLIST_VERSION=2.2.0
|
||||
|
||||
AC_SUBST(LIBIMOBILEDEVICE_GLUE_SO_VERSION)
|
||||
AC_SUBST(LIBPLIST_VERSION)
|
||||
|
||||
# Checks for programs.
|
||||
AC_PROG_CC
|
||||
#AC_PROG_CXX
|
||||
AM_PROG_CC_C_O
|
||||
LT_INIT
|
||||
|
||||
# Checks for libraries.
|
||||
PKG_CHECK_MODULES(libplist, libplist-2.0 >= $LIBPLIST_VERSION)
|
||||
|
||||
# Checks for header files.
|
||||
AC_CHECK_HEADERS([stdint.h stdlib.h string.h])
|
||||
|
||||
# Checks for typedefs, structures, and compiler characteristics.
|
||||
AC_C_CONST
|
||||
AC_TYPE_SIZE_T
|
||||
AC_TYPE_SSIZE_T
|
||||
AC_TYPE_UINT16_T
|
||||
AC_TYPE_UINT32_T
|
||||
AC_TYPE_UINT8_T
|
||||
|
||||
# Checks for library functions.
|
||||
AC_CHECK_FUNCS([asprintf strcasecmp strdup strerror strndup stpcpy vasprintf getifaddrs])
|
||||
|
||||
AC_CHECK_HEADER(endian.h, [ac_cv_have_endian_h="yes"], [ac_cv_have_endian_h="no"])
|
||||
if test "x$ac_cv_have_endian_h" = "xno"; then
|
||||
AC_DEFINE(__LITTLE_ENDIAN,1234,[little endian])
|
||||
AC_DEFINE(__BIG_ENDIAN,4321,[big endian])
|
||||
AC_C_BIGENDIAN([ac_cv_c_bigendian="yes"], [ac_cv_c_bigendian="no"], [], [])
|
||||
if test "x$ac_cv_c_bigendian" = "xyes"; then
|
||||
AC_DEFINE(__BYTE_ORDER,4321,[big endian byte order])
|
||||
else
|
||||
AC_DEFINE(__BYTE_ORDER,1234,[little endian byte order])
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check for operating system
|
||||
AC_MSG_CHECKING([for platform-specific build settings])
|
||||
case ${host_os} in
|
||||
*mingw32*|*cygwin*)
|
||||
AC_MSG_RESULT([${host_os}])
|
||||
win32=true
|
||||
AC_DEFINE(WINVER, 0x0501, [minimum Windows version])
|
||||
;;
|
||||
darwin*)
|
||||
AC_MSG_RESULT([${host_os}])
|
||||
AX_PTHREAD([], [AC_MSG_ERROR([pthread is required to build $PACKAGE])])
|
||||
AC_CHECK_FUNCS([pthread_once pthread_cancel])
|
||||
;;
|
||||
*)
|
||||
AC_MSG_RESULT([${host_os}])
|
||||
AX_PTHREAD([], [AC_MSG_ERROR([pthread is required to build $PACKAGE_NAME])])
|
||||
AC_CHECK_FUNC(pthread_cancel, [AC_DEFINE(HAVE_PTHREAD_CANCEL)], [
|
||||
AC_CHECK_LIB(pthread, [pthread_cancel],[AC_DEFINE(HAVE_PTHREAD_CANCEL)])
|
||||
])
|
||||
AC_CHECK_FUNC(pthread_once, [AC_DEFINE(HAVE_PTHREAD_ONCE)], [
|
||||
AC_CHECK_LIB(pthread, [pthread_once], [], [AC_MSG_ERROR([pthread with pthread_once required to build $PACKAGE_NAME])])
|
||||
])
|
||||
;;
|
||||
esac
|
||||
AM_CONDITIONAL(WIN32, test x$win32 = xtrue)
|
||||
|
||||
# Check if the C compiler supports __attribute__((constructor))
|
||||
AC_CACHE_CHECK([wether the C compiler supports constructor/destructor attributes],
|
||||
ac_cv_attribute_constructor, [
|
||||
ac_cv_attribute_constructor=no
|
||||
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
|
||||
[[
|
||||
static void __attribute__((constructor)) test_constructor(void) {
|
||||
}
|
||||
static void __attribute__((destructor)) test_destructor(void) {
|
||||
}
|
||||
]], [])],
|
||||
[ac_cv_attribute_constructor=yes]
|
||||
)]
|
||||
)
|
||||
if test "$ac_cv_attribute_constructor" = "yes"; then
|
||||
AC_DEFINE(HAVE_ATTRIBUTE_CONSTRUCTOR, 1, [Define if the C compiler supports constructor/destructor attributes])
|
||||
fi
|
||||
|
||||
AC_CHECK_MEMBER(struct dirent.d_type, AC_DEFINE(HAVE_DIRENT_D_TYPE, 1, [define if struct dirent has member d_type]),, [#include <dirent.h>])
|
||||
|
||||
AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter -fsigned-char -fvisibility=hidden")
|
||||
AC_SUBST(GLOBAL_CFLAGS)
|
||||
|
||||
case "$GLOBAL_CFLAGS" in
|
||||
*-fvisibility=hidden*)
|
||||
AC_DEFINE([HAVE_FVISIBILITY], [1], [Define if compiled with -fvisibility=hidden])
|
||||
esac
|
||||
|
||||
# check for large file support
|
||||
AC_SYS_LARGEFILE
|
||||
|
||||
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
|
||||
|
||||
AC_CONFIG_FILES([
|
||||
Makefile
|
||||
src/Makefile
|
||||
src/libimobiledevice-glue-1.0.pc
|
||||
include/Makefile
|
||||
])
|
||||
AC_OUTPUT
|
||||
|
||||
echo "
|
||||
Configuration for $PACKAGE $VERSION:
|
||||
-------------------------------------------
|
||||
|
||||
Install prefix: .........: $prefix
|
||||
|
||||
Now type 'make' to build $PACKAGE $VERSION,
|
||||
and then 'make install' for installation.
|
||||
"
|
||||
@@ -0,0 +1,12 @@
|
||||
EXTRA_DIST = \
|
||||
endianness.h
|
||||
|
||||
nobase_include_HEADERS = \
|
||||
libimobiledevice-glue/socket.h \
|
||||
libimobiledevice-glue/thread.h \
|
||||
libimobiledevice-glue/utils.h \
|
||||
libimobiledevice-glue/collection.h \
|
||||
libimobiledevice-glue/termcolors.h \
|
||||
libimobiledevice-glue/cbuf.h \
|
||||
libimobiledevice-glue/opack.h \
|
||||
libimobiledevice-glue/tlv.h
|
||||
@@ -0,0 +1,123 @@
|
||||
#ifndef ENDIANNESS_H
|
||||
#define ENDIANNESS_H
|
||||
|
||||
#ifndef __LITTLE_ENDIAN
|
||||
#define __LITTLE_ENDIAN 1234
|
||||
#endif
|
||||
|
||||
#ifndef __BIG_ENDIAN
|
||||
#define __BIG_ENDIAN 4321
|
||||
#endif
|
||||
|
||||
#ifndef __BYTE_ORDER
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
#define __BYTE_ORDER __LITTLE_ENDIAN
|
||||
#else
|
||||
#ifdef __BIG_ENDIAN__
|
||||
#define __BYTE_ORDER __BIG_ENDIAN
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef be16toh
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define be16toh(x) (x)
|
||||
#else
|
||||
#define be16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef htobe16
|
||||
#define htobe16 be16toh
|
||||
#endif
|
||||
|
||||
#ifndef le16toh
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define le16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
|
||||
#else
|
||||
#define le16toh(x) (x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef htole16
|
||||
#define htole16 le16toh
|
||||
#endif
|
||||
|
||||
#ifndef __bswap_32
|
||||
#define __bswap_32(x) ((((x) & 0xFF000000) >> 24) \
|
||||
| (((x) & 0x00FF0000) >> 8) \
|
||||
| (((x) & 0x0000FF00) << 8) \
|
||||
| (((x) & 0x000000FF) << 24))
|
||||
#endif
|
||||
|
||||
#ifndef be32toh
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define be32toh(x) (x)
|
||||
#else
|
||||
#define be32toh(x) __bswap_32(x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef htobe32
|
||||
#define htobe32 be32toh
|
||||
#endif
|
||||
|
||||
#ifndef le32toh
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define le32toh(x) __bswap_32(x)
|
||||
#else
|
||||
#define le32toh(x) (x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef htole32
|
||||
#define htole32 le32toh
|
||||
#endif
|
||||
|
||||
#ifndef __bswap_64
|
||||
#define __bswap_64(x) ((((x) & 0xFF00000000000000ull) >> 56) \
|
||||
| (((x) & 0x00FF000000000000ull) >> 40) \
|
||||
| (((x) & 0x0000FF0000000000ull) >> 24) \
|
||||
| (((x) & 0x000000FF00000000ull) >> 8) \
|
||||
| (((x) & 0x00000000FF000000ull) << 8) \
|
||||
| (((x) & 0x0000000000FF0000ull) << 24) \
|
||||
| (((x) & 0x000000000000FF00ull) << 40) \
|
||||
| (((x) & 0x00000000000000FFull) << 56))
|
||||
#endif
|
||||
|
||||
#ifndef htobe64
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define htobe64(x) (x)
|
||||
#else
|
||||
#define htobe64(x) __bswap_64(x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef be64toh
|
||||
#define be64toh htobe64
|
||||
#endif
|
||||
|
||||
#ifndef le64toh
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define le64toh(x) (x)
|
||||
#else
|
||||
#define le64toh(x) __bswap_64(x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef htole64
|
||||
#define htole64 le64toh
|
||||
#endif
|
||||
|
||||
#if (defined(__BIG_ENDIAN__) \
|
||||
&& !defined(__FLOAT_WORD_ORDER__)) \
|
||||
|| (defined(__FLOAT_WORD_ORDER__) \
|
||||
&& __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
#define float_bswap64(x) __bswap_64(x)
|
||||
#define float_bswap32(x) __bswap_32(x)
|
||||
#else
|
||||
#define float_bswap64(x) (x)
|
||||
#define float_bswap32(x) (x)
|
||||
#endif
|
||||
|
||||
#endif /* ENDIANNESS_H */
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* cbuf.h
|
||||
* Simple char buffer implementation.
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __CBUF_H
|
||||
#define __CBUF_H
|
||||
|
||||
struct char_buf {
|
||||
unsigned char* data;
|
||||
unsigned int length;
|
||||
unsigned int capacity;
|
||||
};
|
||||
|
||||
struct char_buf* char_buf_new();
|
||||
void char_buf_free(struct char_buf* cbuf);
|
||||
void char_buf_append(struct char_buf* cbuf, unsigned int length, unsigned char* data);
|
||||
|
||||
#endif /* __CBUF_H */
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* collection.h
|
||||
*
|
||||
* Copyright (C) 2009 Hector Martin <hector@marcansoft.com>
|
||||
* Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef COLLECTION_H
|
||||
#define COLLECTION_H
|
||||
|
||||
struct collection {
|
||||
void **list;
|
||||
int capacity;
|
||||
};
|
||||
|
||||
void collection_init(struct collection *col);
|
||||
void collection_add(struct collection *col, void *element);
|
||||
int collection_remove(struct collection *col, void *element);
|
||||
int collection_count(struct collection *col);
|
||||
void collection_free(struct collection *col);
|
||||
void collection_copy(struct collection *dest, struct collection *src);
|
||||
|
||||
#define MERGE_(a,b) a ## _ ## b
|
||||
#define LABEL_(a,b) MERGE_(a, b)
|
||||
#define UNIQUE_VAR(a) LABEL_(a, __LINE__)
|
||||
|
||||
#define FOREACH(var, col) \
|
||||
do { \
|
||||
int UNIQUE_VAR(_iter); \
|
||||
for(UNIQUE_VAR(_iter)=0; UNIQUE_VAR(_iter)<(col)->capacity; UNIQUE_VAR(_iter)++) { \
|
||||
if(!(col)->list[UNIQUE_VAR(_iter)]) continue; \
|
||||
var = (col)->list[UNIQUE_VAR(_iter)];
|
||||
|
||||
#define ENDFOREACH \
|
||||
} \
|
||||
} while(0);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* opack.h
|
||||
* "opack" format encoder/decoder implementation.
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef __OPACK_H
|
||||
#define __OPACK_H
|
||||
|
||||
#include <plist/plist.h>
|
||||
|
||||
void opack_encode_from_plist(plist_t plist, unsigned char** out, unsigned int* out_len);
|
||||
int opack_decode_to_plist(unsigned char* buf, unsigned int buf_len, plist_t* plist_out);
|
||||
|
||||
#endif /* __OPACK_H */
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* socket.h
|
||||
*
|
||||
* Copyright (C) 2012-2020 Nikias Bassen <nikias@gmx.li>
|
||||
* Copyright (C) 2012 Martin Szulecki <m.szulecki@libimobiledevice.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef SOCKET_SOCKET_H
|
||||
#define SOCKET_SOCKET_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum fd_mode {
|
||||
FDM_READ,
|
||||
FDM_WRITE,
|
||||
FDM_EXCEPT
|
||||
};
|
||||
typedef enum fd_mode fd_mode;
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#define SHUT_RD SD_READ
|
||||
#define SHUT_WR SD_WRITE
|
||||
#define SHUT_RDWR SD_BOTH
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#ifndef WIN32
|
||||
int socket_create_unix(const char *filename);
|
||||
int socket_connect_unix(const char *filename);
|
||||
#endif
|
||||
int socket_create(const char *addr, uint16_t port);
|
||||
int socket_connect_addr(struct sockaddr *addr, uint16_t port);
|
||||
int socket_connect(const char *addr, uint16_t port);
|
||||
int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout);
|
||||
int socket_accept(int fd, uint16_t port);
|
||||
|
||||
int socket_shutdown(int fd, int how);
|
||||
int socket_close(int fd);
|
||||
|
||||
int socket_receive(int fd, void *data, size_t length);
|
||||
int socket_peek(int fd, void *data, size_t length);
|
||||
int socket_receive_timeout(int fd, void *data, size_t length, int flags, unsigned int timeout);
|
||||
int socket_send(int fd, void *data, size_t length);
|
||||
|
||||
int socket_get_socket_port(int fd, uint16_t *port);
|
||||
|
||||
void socket_set_verbose(int level);
|
||||
|
||||
const char *socket_addr_to_string(struct sockaddr *addr, char *addr_out, size_t addr_out_size);
|
||||
|
||||
int get_primary_mac_address(unsigned char mac_addr_buf[6]);
|
||||
|
||||
#endif /* SOCKET_SOCKET_H */
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* termcolors.h
|
||||
*
|
||||
* Copyright (c) 2020-2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef TERMCOLORS_H
|
||||
#define TERMCOLORS_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define COLOR_RESET "\e[m"
|
||||
#define STYLE_NORMAL "\e[0m"
|
||||
#define STYLE_BRIGHT "\e[1m"
|
||||
#define STYLE_DARK "\e[2m"
|
||||
#define FG_BLACK "\e[0;30m"
|
||||
#define FG_DARK_GRAY "\e[1;30m"
|
||||
#define FG_RED "\e[0;31m"
|
||||
#define FG_BRIGHT_RED "\e[1;31m"
|
||||
#define FG_DARK_RED "\e[2;31m"
|
||||
#define FG_GREEN "\e[0;32m"
|
||||
#define FG_BRIGHT_GREEN "\e[1;32m"
|
||||
#define FG_DARK_GREEN "\e[2;32m"
|
||||
#define FG_YELLOW "\e[0;33m"
|
||||
#define FG_BRIGHT_YELLOW "\e[1;33m"
|
||||
#define FG_DARK_YELLOW "\e[2;33m"
|
||||
#define FG_BLUE "\e[0;34m"
|
||||
#define FG_BRIGHT_BLUE "\e[1;34m"
|
||||
#define FG_DARK_BLUE "\e[2;34m"
|
||||
#define FG_MAGENTA "\e[0;35m"
|
||||
#define FG_BRIGHT_MAGENTA "\e[1;35m"
|
||||
#define FG_DARK_MAGENTA "\e[2;35m"
|
||||
#define FG_CYAN "\e[0;36m"
|
||||
#define FG_BRIGHT_CYAN "\e[1;36m"
|
||||
#define FG_DARK_CYAN "\e[2;36m"
|
||||
#define FG_LIGHT_GRAY "\e[0;37m"
|
||||
#define FG_WHITE "\e[1;37m"
|
||||
#define FG_GRAY "\e[2;37m"
|
||||
#define FG_DEFAULT "\e[39m"
|
||||
#define BG_BLACK "\e[40m"
|
||||
#define BG_GRAY "\e[100m"
|
||||
#define BG_RED "\e[41m"
|
||||
#define BG_BRIGHT_RED "\e[101m"
|
||||
#define BG_GREEN "\e[42m"
|
||||
#define BG_BRIGHT_GREEN "\e[102m"
|
||||
#define BG_YELLOW "\e[43m"
|
||||
#define BG_BRIGHT_YELLOW "\e[103m"
|
||||
#define BG_BLUE "\e[44m"
|
||||
#define BG_BRIGHT_BLUE "\e[104m"
|
||||
#define BG_MAGENTA "\e[45m"
|
||||
#define BG_BRIGHT_MAGENTA "\e[105m"
|
||||
#define BG_CYAN "\e[46m"
|
||||
#define BG_BRIGHT_CYAN "\e[106m"
|
||||
#define BG_LIGHT_GRAY "\e[47m"
|
||||
#define BG_WHITE "\e[107m"
|
||||
#define BG_DEFAULT "\e[49m"
|
||||
|
||||
/* automatically called by library constructor */
|
||||
void term_colors_init();
|
||||
|
||||
/* enable / disable terminal colors */
|
||||
void term_colors_set_enabled(int en);
|
||||
|
||||
/* color-aware *printf variants */
|
||||
int cprintf(const char* fmt, ...);
|
||||
int cfprintf(FILE* stream, const char* fmt, ...);
|
||||
int cvfprintf(FILE* stream, const char* fmt, va_list vargs);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* thread.h
|
||||
*
|
||||
* Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved.
|
||||
* Copyright (c) 2012 Martin Szulecki, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __THREAD_H
|
||||
#define __THREAD_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
typedef HANDLE THREAD_T;
|
||||
typedef CRITICAL_SECTION mutex_t;
|
||||
typedef struct {
|
||||
HANDLE sem;
|
||||
} cond_t;
|
||||
typedef volatile struct {
|
||||
LONG lock;
|
||||
int state;
|
||||
} thread_once_t;
|
||||
#define THREAD_ONCE_INIT {0, 0}
|
||||
#define THREAD_ID GetCurrentThreadId()
|
||||
#define THREAD_T_NULL (THREAD_T)NULL
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/time.h>
|
||||
typedef pthread_t THREAD_T;
|
||||
typedef pthread_mutex_t mutex_t;
|
||||
typedef pthread_cond_t cond_t;
|
||||
typedef pthread_once_t thread_once_t;
|
||||
#define THREAD_ONCE_INIT PTHREAD_ONCE_INIT
|
||||
#define THREAD_ID pthread_self()
|
||||
#define THREAD_T_NULL (THREAD_T)NULL
|
||||
#endif
|
||||
|
||||
typedef void* (*thread_func_t)(void* data);
|
||||
|
||||
int thread_new(THREAD_T* thread, thread_func_t thread_func, void* data);
|
||||
void thread_detach(THREAD_T thread);
|
||||
void thread_free(THREAD_T thread);
|
||||
int thread_join(THREAD_T thread);
|
||||
int thread_alive(THREAD_T thread);
|
||||
|
||||
int thread_cancel(THREAD_T thread);
|
||||
|
||||
#ifdef WIN32
|
||||
#undef HAVE_THREAD_CLEANUP
|
||||
#else
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
#define HAVE_THREAD_CLEANUP 1
|
||||
#define thread_cleanup_push(routine, arg) pthread_cleanup_push(routine, arg)
|
||||
#define thread_cleanup_pop(execute) pthread_cleanup_pop(execute)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void mutex_init(mutex_t* mutex);
|
||||
void mutex_destroy(mutex_t* mutex);
|
||||
void mutex_lock(mutex_t* mutex);
|
||||
void mutex_unlock(mutex_t* mutex);
|
||||
|
||||
void thread_once(thread_once_t *once_control, void (*init_routine)(void));
|
||||
|
||||
void cond_init(cond_t* cond);
|
||||
void cond_destroy(cond_t* cond);
|
||||
int cond_signal(cond_t* cond);
|
||||
int cond_wait(cond_t* cond, mutex_t* mutex);
|
||||
int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* tlv.h
|
||||
* Simple TLV implementation.
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef __TLV_H
|
||||
#define __TLV_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct tlv_buf {
|
||||
unsigned char* data;
|
||||
unsigned int length;
|
||||
unsigned int capacity;
|
||||
};
|
||||
typedef struct tlv_buf* tlv_buf_t;
|
||||
|
||||
tlv_buf_t tlv_buf_new();
|
||||
void tlv_buf_free(tlv_buf_t tlv);
|
||||
|
||||
void tlv_buf_append(tlv_buf_t tlv, uint8_t tag, unsigned int length, void* data);
|
||||
unsigned char* tlv_get_data_ptr(const void* tlv_data, void* tlv_end, uint8_t tag, uint8_t* length);
|
||||
int tlv_data_get_uint(const void* tlv_data, unsigned int tlv_length, uint8_t tag, uint64_t* value);
|
||||
int tlv_data_get_uint8(const void* tlv_data, unsigned int tlv_length, uint8_t tag, uint8_t* value);
|
||||
int tlv_data_copy_data(const void* tlv_data, unsigned int tlv_length, uint8_t tag, void** out, unsigned int* out_len);
|
||||
|
||||
#endif /* __TLV_H */
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* utils.h
|
||||
* Miscellaneous utilities for string manipulation,
|
||||
* file I/O and plist helper.
|
||||
*
|
||||
* Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
|
||||
* Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
|
||||
* Copyright (c) 2013 Federico Mena Quintero
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __UTILS_H
|
||||
#define __UTILS_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <plist/plist.h>
|
||||
|
||||
#define MAC_EPOCH 978307200
|
||||
|
||||
char *string_concat(const char *str, ...);
|
||||
char *string_append(char *str, ...);
|
||||
char *string_build_path(const char *elem, ...);
|
||||
char *string_format_size(uint64_t size);
|
||||
char *string_toupper(char *str);
|
||||
char *generate_uuid(void);
|
||||
|
||||
int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length);
|
||||
int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length);
|
||||
|
||||
enum plist_format_t {
|
||||
PLIST_FORMAT_XML,
|
||||
PLIST_FORMAT_BINARY
|
||||
};
|
||||
|
||||
int plist_read_from_filename(plist_t *plist, const char *filename);
|
||||
int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format);
|
||||
|
||||
void plist_print_to_stream(plist_t plist, FILE* stream);
|
||||
void plist_print_to_stream_with_indentation(plist_t plist, FILE* stream, unsigned int indentation);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)
|
||||
|
||||
AM_CFLAGS = $(GLOBAL_CFLAGS) $(PTHREAD_CFLAGS) $(libplist_CFLAGS)
|
||||
|
||||
AM_LDFLAGS = $(PTHREAD_LIBS) $(libplist_LIBS)
|
||||
|
||||
lib_LTLIBRARIES = libimobiledevice-glue-1.0.la
|
||||
libimobiledevice_glue_1_0_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBIMOBILEDEVICE_GLUE_SO_VERSION) -no-undefined
|
||||
libimobiledevice_glue_1_0_la_LIBADD =
|
||||
libimobiledevice_glue_1_0_la_SOURCES = \
|
||||
glue.c \
|
||||
socket.c \
|
||||
thread.c \
|
||||
utils.c \
|
||||
collection.c \
|
||||
termcolors.c \
|
||||
cbuf.c \
|
||||
opack.c \
|
||||
tlv.c \
|
||||
common.h
|
||||
|
||||
if WIN32
|
||||
libimobiledevice_glue_1_0_la_LDFLAGS += -avoid-version -static-libgcc
|
||||
libimobiledevice_glue_1_0_la_LIBADD += -lws2_32 -lIphlpapi
|
||||
endif
|
||||
|
||||
pkgconfigdir = $(libdir)/pkgconfig
|
||||
pkgconfig_DATA = libimobiledevice-glue-1.0.pc
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* cbuf.c
|
||||
* Simple char buffer implementation.
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/cbuf.h"
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API struct char_buf* char_buf_new()
|
||||
{
|
||||
struct char_buf* cbuf = (struct char_buf*)malloc(sizeof(struct char_buf));
|
||||
cbuf->capacity = 256;
|
||||
cbuf->length = 0;
|
||||
cbuf->data = (unsigned char*)malloc(cbuf->capacity);
|
||||
return cbuf;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void char_buf_free(struct char_buf* cbuf)
|
||||
{
|
||||
if (cbuf) {
|
||||
free(cbuf->data);
|
||||
free(cbuf);
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void char_buf_append(struct char_buf* cbuf, unsigned int length, unsigned char* data)
|
||||
{
|
||||
if (!cbuf || !cbuf->data) {
|
||||
return;
|
||||
}
|
||||
if (cbuf->length + length > cbuf->capacity) {
|
||||
unsigned int newcapacity = cbuf->capacity + ((length / 256) + 1) * 256;
|
||||
unsigned char* newdata = realloc(cbuf->data, newcapacity);
|
||||
if (!newdata) {
|
||||
fprintf(stderr, "%s: ERROR: Failed to realloc\n", __func__);
|
||||
return;
|
||||
}
|
||||
cbuf->data = newdata;
|
||||
cbuf->capacity = newcapacity;
|
||||
}
|
||||
memcpy(cbuf->data + cbuf->length, data, length);
|
||||
cbuf->length += length;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* collection.c
|
||||
*
|
||||
* Copyright (C) 2009 Hector Martin <hector@marcansoft.com>
|
||||
* Copyright (C) 2009 Nikias Bassen <nikias@gmx.li>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/collection.h"
|
||||
|
||||
#undef NDEBUG // we need to make sure we still get assertions because we can't handle memory allocation errors
|
||||
#include <assert.h>
|
||||
|
||||
#define INIT_NULL(addr, count) { unsigned int i_ = 0; for (i_ = 0; i_ < (count); i_++) ((void**)(addr))[i_] = NULL; }
|
||||
|
||||
#define CAPACITY_STEP 8
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void collection_init(struct collection *col)
|
||||
{
|
||||
col->list = malloc(sizeof(void *) * CAPACITY_STEP);
|
||||
assert(col->list);
|
||||
INIT_NULL(col->list, CAPACITY_STEP);
|
||||
col->capacity = CAPACITY_STEP;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void collection_free(struct collection *col)
|
||||
{
|
||||
free(col->list);
|
||||
col->list = NULL;
|
||||
col->capacity = 0;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void collection_add(struct collection *col, void *element)
|
||||
{
|
||||
int i;
|
||||
for(i=0; i<col->capacity; i++) {
|
||||
if(!col->list[i]) {
|
||||
col->list[i] = element;
|
||||
return;
|
||||
}
|
||||
}
|
||||
void **newlist = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP));
|
||||
assert(newlist);
|
||||
col->list = newlist;
|
||||
INIT_NULL(&col->list[col->capacity], CAPACITY_STEP);
|
||||
col->list[col->capacity] = element;
|
||||
col->capacity += CAPACITY_STEP;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int collection_remove(struct collection *col, void *element)
|
||||
{
|
||||
int i;
|
||||
for(i=0; i<col->capacity; i++) {
|
||||
if(col->list[i] == element) {
|
||||
col->list[i] = NULL;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "%s: WARNING: element %p not present in collection %p (cap %d)", __func__, element, col, col->capacity);
|
||||
return -1;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int collection_count(struct collection *col)
|
||||
{
|
||||
int i, cnt = 0;
|
||||
for(i=0; i<col->capacity; i++) {
|
||||
if(col->list[i])
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void collection_copy(struct collection *dest, struct collection *src)
|
||||
{
|
||||
if (!dest || !src) return;
|
||||
dest->capacity = src->capacity;
|
||||
dest->list = malloc(sizeof(void*) * src->capacity);
|
||||
memcpy(dest->list, src->list, sizeof(void*) * src->capacity);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* common.h
|
||||
*
|
||||
* Copyright (c) 2020 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __COMMON_H
|
||||
#define __COMMON_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#define LIBIMOBILEDEVICE_GLUE_API __declspec( dllexport )
|
||||
#else
|
||||
#ifdef HAVE_FVISIBILITY
|
||||
#define LIBIMOBILEDEVICE_GLUE_API __attribute__((visibility("default")))
|
||||
#else
|
||||
#define LIBIMOBILEDEVICE_GLUE_API
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* glue.c
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/thread.h"
|
||||
|
||||
extern void term_colors_init();
|
||||
|
||||
static void internal_glue_init(void)
|
||||
{
|
||||
term_colors_init();
|
||||
}
|
||||
|
||||
static void internal_glue_deinit(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static thread_once_t init_once = THREAD_ONCE_INIT;
|
||||
static thread_once_t deinit_once = THREAD_ONCE_INIT;
|
||||
|
||||
#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR
|
||||
#if defined(__llvm__) || defined(__GNUC__)
|
||||
#define HAVE_ATTRIBUTE_CONSTRUCTOR
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR
|
||||
static void __attribute__((constructor)) limd_glue_initialize(void)
|
||||
{
|
||||
thread_once(&init_once, internal_glue_init);
|
||||
}
|
||||
|
||||
static void __attribute__((destructor)) limd_glue_deinitialize(void)
|
||||
{
|
||||
thread_once(&deinit_once, internal_glue_deinit);
|
||||
}
|
||||
#elif defined(WIN32)
|
||||
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
|
||||
{
|
||||
switch (dwReason) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
thread_once(&init_once, internal_glue_init);
|
||||
break;
|
||||
case DLL_PROCESS_DETACH:
|
||||
thread_once(&deinit_once, internal_glue_deinit);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
#warning No compiler support for constructor/destructor attributes, some features might not be available.
|
||||
#endif
|
||||
@@ -0,0 +1,476 @@
|
||||
/*
|
||||
* opack.c
|
||||
* "opack" format encoder/decoder implementation.
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/cbuf.h"
|
||||
#include "libimobiledevice-glue/opack.h"
|
||||
#include "endianness.h"
|
||||
|
||||
#define MAC_EPOCH 978307200
|
||||
|
||||
static void opack_encode_node(plist_t node, struct char_buf* cbuf)
|
||||
{
|
||||
plist_type type = plist_get_node_type(node);
|
||||
switch (type) {
|
||||
case PLIST_DICT: {
|
||||
uint32_t count = plist_dict_get_size(node);
|
||||
uint8_t blen = 0xEF;
|
||||
if (count < 15)
|
||||
blen = (uint8_t)count-32;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
plist_dict_iter iter = NULL;
|
||||
plist_dict_new_iter(node, &iter);
|
||||
if (iter) {
|
||||
plist_t sub = NULL;
|
||||
do {
|
||||
sub = NULL;
|
||||
plist_dict_next_item(node, iter, NULL, &sub);
|
||||
if (sub) {
|
||||
plist_t key = plist_dict_item_get_key(sub);
|
||||
opack_encode_node(key, cbuf);
|
||||
opack_encode_node(sub, cbuf);
|
||||
}
|
||||
} while (sub);
|
||||
free(iter);
|
||||
if (count > 14) {
|
||||
uint8_t term = 0x03;
|
||||
char_buf_append(cbuf, 1, &term);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case PLIST_ARRAY: {
|
||||
uint32_t count = plist_array_get_size(node);
|
||||
uint8_t blen = 0xDF;
|
||||
if (count < 15)
|
||||
blen = (uint8_t)(count-48);
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
plist_array_iter iter = NULL;
|
||||
plist_array_new_iter(node, &iter);
|
||||
if (iter) {
|
||||
plist_t sub = NULL;
|
||||
do {
|
||||
sub = NULL;
|
||||
plist_array_next_item(node, iter, &sub);
|
||||
if (sub) {
|
||||
opack_encode_node(sub, cbuf);
|
||||
}
|
||||
} while (sub);
|
||||
free(iter);
|
||||
if (count > 14) {
|
||||
uint8_t term = 0x03;
|
||||
char_buf_append(cbuf, 1, &term);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case PLIST_BOOLEAN: {
|
||||
uint8_t bval = 2 - plist_bool_val_is_true(node);
|
||||
char_buf_append(cbuf, 1, &bval);
|
||||
} break;
|
||||
case PLIST_UINT: {
|
||||
uint64_t u64val = 0;
|
||||
plist_get_uint_val(node, &u64val);
|
||||
if ((uint8_t)u64val == u64val) {
|
||||
uint8_t u8val = (uint8_t)u64val;
|
||||
if (u8val > 0x27) {
|
||||
uint8_t blen = 0x30;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
char_buf_append(cbuf, 1, &u8val);
|
||||
} else {
|
||||
u8val += 8;
|
||||
char_buf_append(cbuf, 1, &u8val);
|
||||
}
|
||||
} else if ((uint32_t)u64val == u64val) {
|
||||
uint8_t blen = 0x32;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint32_t u32val = (uint32_t)u64val;
|
||||
u32val = htole32(u32val);
|
||||
char_buf_append(cbuf, 4, (unsigned char*)&u32val);
|
||||
} else {
|
||||
uint8_t blen = 0x33;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
u64val = htole64(u64val);
|
||||
char_buf_append(cbuf, 8, (unsigned char*)&u64val);
|
||||
}
|
||||
} break;
|
||||
case PLIST_REAL: {
|
||||
double dval = 0;
|
||||
plist_get_real_val(node, &dval);
|
||||
if ((float)dval == dval) {
|
||||
float fval = (float)dval;
|
||||
uint32_t u32val = 0;
|
||||
memcpy(&u32val, &fval, 4);
|
||||
u32val = float_bswap32(u32val);
|
||||
uint8_t blen = 0x35;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
char_buf_append(cbuf, 4, (unsigned char*)&u32val);
|
||||
} else {
|
||||
uint64_t u64val = 0;
|
||||
memcpy(&u64val, &dval, 8);
|
||||
u64val = float_bswap64(u64val);
|
||||
uint8_t blen = 0x36;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
char_buf_append(cbuf, 8, (unsigned char*)&u64val);
|
||||
}
|
||||
} break;
|
||||
case PLIST_DATE: {
|
||||
int32_t sec = 0;
|
||||
int32_t usec = 0;
|
||||
plist_get_date_val(node, &sec, &usec);
|
||||
time_t tsec = sec;
|
||||
tsec -= MAC_EPOCH;
|
||||
double dval = (double)tsec + ((double)usec / 1000000);
|
||||
uint8_t blen = 0x06;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint64_t u64val = 0;
|
||||
memcpy(&u64val, &dval, 8);
|
||||
u64val = float_bswap64(u64val);
|
||||
char_buf_append(cbuf, 8, (unsigned char*)&u64val);
|
||||
} break;
|
||||
case PLIST_STRING:
|
||||
case PLIST_KEY: {
|
||||
uint64_t len = 0;
|
||||
char* str = NULL;
|
||||
if (type == PLIST_KEY) {
|
||||
plist_get_key_val(node, &str);
|
||||
len = strlen(str);
|
||||
} else {
|
||||
str = (char*)plist_get_string_ptr(node, &len);
|
||||
}
|
||||
if (len > 0x20) {
|
||||
if (len > 0xFF) {
|
||||
if (len > 0xFFFF) {
|
||||
if (len >> 32) {
|
||||
uint8_t blen = 0x64;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint64_t u64val = htole64(len);
|
||||
char_buf_append(cbuf, 8, (unsigned char*)&u64val);
|
||||
} else {
|
||||
uint8_t blen = 0x63;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint32_t u32val = htole32((uint32_t)len);
|
||||
char_buf_append(cbuf, 4, (unsigned char*)&u32val);
|
||||
}
|
||||
} else {
|
||||
uint8_t blen = 0x62;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint16_t u16val = htole16((uint16_t)len);
|
||||
char_buf_append(cbuf, 2, (unsigned char*)&u16val);
|
||||
}
|
||||
} else {
|
||||
uint8_t blen = 0x61;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
char_buf_append(cbuf, 1, (unsigned char*)&len);
|
||||
}
|
||||
} else {
|
||||
uint8_t blen = 0x40 + len;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
}
|
||||
char_buf_append(cbuf, len, (unsigned char*)str);
|
||||
if (type == PLIST_KEY) {
|
||||
free(str);
|
||||
}
|
||||
} break;
|
||||
case PLIST_DATA: {
|
||||
uint64_t len = 0;
|
||||
const char* data = plist_get_data_ptr(node, &len);
|
||||
if (len > 0x20) {
|
||||
if (len > 0xFF) {
|
||||
if (len > 0xFFFF) {
|
||||
if (len >> 32) {
|
||||
uint8_t blen = 0x94;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint32_t u64val = htole64(len);
|
||||
char_buf_append(cbuf, 8, (unsigned char*)&u64val);
|
||||
} else {
|
||||
uint8_t blen = 0x93;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint32_t u32val = htole32((uint32_t)len);
|
||||
char_buf_append(cbuf, 4, (unsigned char*)&u32val);
|
||||
}
|
||||
} else {
|
||||
uint8_t blen = 0x92;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
uint16_t u16val = htole16((uint16_t)len);
|
||||
char_buf_append(cbuf, 2, (unsigned char*)&u16val);
|
||||
}
|
||||
} else {
|
||||
uint8_t blen = 0x91;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
char_buf_append(cbuf, 1, (unsigned char*)&len);
|
||||
}
|
||||
} else {
|
||||
uint8_t blen = 0x70 + len;
|
||||
char_buf_append(cbuf, 1, &blen);
|
||||
}
|
||||
char_buf_append(cbuf, len, (unsigned char*)data);
|
||||
} break;
|
||||
default:
|
||||
fprintf(stderr, "%s: ERROR: Unsupported data type in plist\n", __func__);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void opack_encode_from_plist(plist_t plist, unsigned char** out, unsigned int* out_len)
|
||||
{
|
||||
if (!plist || !out || !out_len) {
|
||||
return;
|
||||
}
|
||||
struct char_buf* cbuf = char_buf_new();
|
||||
opack_encode_node(plist, cbuf);
|
||||
*out = cbuf->data;
|
||||
*out_len = cbuf->length;
|
||||
cbuf->data = NULL;
|
||||
char_buf_free(cbuf);
|
||||
}
|
||||
|
||||
static int opack_decode_obj(unsigned char** p, unsigned char* end, plist_t* plist_out, uint32_t level)
|
||||
{
|
||||
uint8_t type = **p;
|
||||
if (type == 0x02) {
|
||||
/* bool: false */
|
||||
*plist_out = plist_new_bool(0);
|
||||
(*p)++;
|
||||
return 0;
|
||||
} else if (type == 0x01) {
|
||||
/* bool: true */
|
||||
*plist_out = plist_new_bool(1);
|
||||
(*p)++;
|
||||
return 0;
|
||||
} else if (type == 0x03) {
|
||||
/* NULL / structured type child node terminator */
|
||||
(*p)++;
|
||||
return -2;
|
||||
} else if (type == 0x06) {
|
||||
/* date type */
|
||||
(*p)++;
|
||||
double value = *(double*)*p;
|
||||
time_t sec = (time_t)value;
|
||||
value -= sec;
|
||||
uint32_t usec = value * 1000000;
|
||||
(*p)+=8;
|
||||
*plist_out = plist_new_date(sec, usec);
|
||||
} else if (type >= 0x08 && type <= 0x36) {
|
||||
/* numerical type */
|
||||
(*p)++;
|
||||
uint64_t value = 0;
|
||||
if (type == 0x36) {
|
||||
/* double */
|
||||
uint64_t u64val = float_bswap64(*(uint64_t*)(*p));
|
||||
(*p)+=8;
|
||||
double dval = 0;
|
||||
memcpy(&dval, &u64val, 8);
|
||||
*plist_out = plist_new_real(dval);
|
||||
return 0;
|
||||
} else if (type == 0x35) {
|
||||
/* float */
|
||||
uint32_t u32val = float_bswap32(*(uint32_t*)(*p));
|
||||
(*p)+=4;
|
||||
float fval = 0;
|
||||
memcpy(&fval, &u32val, 4);
|
||||
*plist_out = plist_new_real((double)fval);
|
||||
return 0;
|
||||
} else if (type < 0x30) {
|
||||
value = type - 8;
|
||||
} else if (type == 0x30) {
|
||||
value = (int8_t)**p;
|
||||
(*p)++;
|
||||
} else if (type == 0x32) {
|
||||
uint32_t u32val = *(uint32_t*)*p;
|
||||
value = (int32_t)le32toh(u32val);
|
||||
(p)+=4;
|
||||
} else if (type == 0x33) {
|
||||
uint64_t u64val = *(uint64_t*)*p;
|
||||
value = le64toh(u64val);
|
||||
(p)+=8;
|
||||
} else {
|
||||
fprintf(stderr, "%s: ERROR: Invalid encoded byte '%02x'\n", __func__, type);
|
||||
*p = end;
|
||||
return -1;
|
||||
}
|
||||
*plist_out = plist_new_uint(value);
|
||||
} else if (type >= 0x40 && type <= 0x64) {
|
||||
/* string */
|
||||
(*p)++;
|
||||
size_t slen = 0;
|
||||
if (type < 0x61) {
|
||||
slen = type - 0x40;
|
||||
} else {
|
||||
if (type == 0x61) {
|
||||
slen = **p;
|
||||
(*p)++;
|
||||
} else if (type == 0x62) {
|
||||
uint16_t u16val = *(uint16_t*)*p;
|
||||
slen = le16toh(u16val);
|
||||
(*p)+=2;
|
||||
} else if (type == 0x63) {
|
||||
uint32_t u32val = *(uint32_t*)*p;
|
||||
slen = le32toh(u32val);
|
||||
(*p)+=4;
|
||||
} else if (type == 0x64) {
|
||||
uint64_t u64val = *(uint64_t*)*p;
|
||||
slen = le64toh(u64val);
|
||||
(*p)+=8;
|
||||
}
|
||||
}
|
||||
if (*p + slen > end) {
|
||||
fprintf(stderr, "%s: ERROR: Size points past end of data\n", __func__);
|
||||
*p = end;
|
||||
return -1;
|
||||
}
|
||||
char* str = malloc(slen+1);
|
||||
strncpy(str, (const char*)*p, slen);
|
||||
str[slen] = '\0';
|
||||
*plist_out = plist_new_string(str);
|
||||
(*p)+=slen;
|
||||
free(str);
|
||||
} else if (type >= 0x70 && type <= 0x94) {
|
||||
/* data */
|
||||
(*p)++;
|
||||
size_t dlen = 0;
|
||||
if (type < 0x91) {
|
||||
dlen = type - 0x70;
|
||||
} else {
|
||||
if (type == 0x91) {
|
||||
dlen = **p;
|
||||
(*p)++;
|
||||
} else if (type == 0x92) {
|
||||
uint16_t u16val = *(uint16_t*)*p;
|
||||
dlen = le16toh(u16val);
|
||||
(*p)+=2;
|
||||
} else if (type == 0x93) {
|
||||
uint32_t u32val = *(uint32_t*)*p;
|
||||
dlen = le32toh(u32val);
|
||||
(*p)+=4;
|
||||
} else if (type == 0x94) {
|
||||
uint64_t u64val = *(uint64_t*)*p;
|
||||
dlen = le64toh(u64val);
|
||||
(*p)+=8;
|
||||
}
|
||||
}
|
||||
if (*p + dlen > end) {
|
||||
fprintf(stderr, "%s: ERROR: Size points past end of data\n", __func__);
|
||||
*p = end;
|
||||
return -1;
|
||||
}
|
||||
*plist_out = plist_new_data((const char*)*p, dlen);
|
||||
(*p)+=dlen;
|
||||
} else if (type >= 0xE0 && type <= 0xEF) {
|
||||
/* dictionary */
|
||||
(*p)++;
|
||||
plist_t dict = plist_new_dict();
|
||||
uint32_t num_children = 0xFFFFFFFF;
|
||||
if (type < 0xEF) {
|
||||
/* explicit number of children */
|
||||
num_children = type - 0xE0;
|
||||
}
|
||||
if (!*plist_out) {
|
||||
*plist_out = dict;
|
||||
}
|
||||
uint32_t i = 0;
|
||||
while (i++ < num_children) {
|
||||
plist_t keynode = NULL;
|
||||
int res = opack_decode_obj(p, end, &keynode, level+1);
|
||||
if (res == -2) {
|
||||
break;
|
||||
} else if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (!PLIST_IS_STRING(keynode)) {
|
||||
plist_free(keynode);
|
||||
fprintf(stderr, "%s: ERROR: Invalid node type for dictionary key node\n", __func__);
|
||||
*p = end;
|
||||
return -1;
|
||||
}
|
||||
char* key = NULL;
|
||||
plist_get_string_val(keynode, &key);
|
||||
plist_free(keynode);
|
||||
plist_t valnode = NULL;
|
||||
if (opack_decode_obj(p, end, &valnode, level+1) < 0) {
|
||||
free(key);
|
||||
return -1;
|
||||
}
|
||||
plist_dict_set_item(dict, key, valnode);
|
||||
}
|
||||
if (level == 0) {
|
||||
*p = end;
|
||||
return 0;
|
||||
}
|
||||
} else if (type >= 0xD0 && type <= 0xDF) {
|
||||
/* array */
|
||||
(*p)++;
|
||||
plist_t array = plist_new_array();
|
||||
if (!*plist_out) {
|
||||
*plist_out = array;
|
||||
}
|
||||
uint32_t num_children = 0xFFFFFFFF;
|
||||
if (type < 0xDF) {
|
||||
/* explicit number of children */
|
||||
num_children = type - 0xD0;
|
||||
}
|
||||
uint32_t i = 0;
|
||||
while (i++ < num_children) {
|
||||
plist_t child = NULL;
|
||||
int res = opack_decode_obj(p, end, &child, level+1);
|
||||
if (res == -2) {
|
||||
if (type < 0xDF) {
|
||||
fprintf(stderr, "%s: ERROR: Expected child node, found terminator\n", __func__);
|
||||
*p = end;
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
} else if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
plist_array_append_item(array, child);
|
||||
}
|
||||
if (level == 0) {
|
||||
*p = end;
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: ERROR: Unexpected character '%02x encountered\n", __func__, type);
|
||||
*p = end;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int opack_decode_to_plist(unsigned char* buf, unsigned int buf_len, plist_t* plist_out)
|
||||
{
|
||||
if (!buf || buf_len == 0 || !plist_out) {
|
||||
return -1;
|
||||
}
|
||||
unsigned char* p = buf;
|
||||
unsigned char* end = buf + buf_len;
|
||||
while (p < end) {
|
||||
opack_decode_obj(&p, end, plist_out, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,319 @@
|
||||
/*
|
||||
* termcolors.c
|
||||
*
|
||||
* Copyright (c) 2020-2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/termcolors.h"
|
||||
|
||||
static int use_colors = 0;
|
||||
|
||||
#ifdef WIN32
|
||||
static int WIN32_LEGACY_MODE = 1;
|
||||
static WORD COLOR_RESET_ATTR = 0;
|
||||
static WORD DEFAULT_FG_ATTR = 0;
|
||||
static WORD DEFAULT_BG_ATTR = 0;
|
||||
static HANDLE h_stdout = INVALID_HANDLE_VALUE;
|
||||
static HANDLE h_stderr = INVALID_HANDLE_VALUE;
|
||||
|
||||
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
||||
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
|
||||
#endif
|
||||
|
||||
#define STYLE_DIM (1 << 16)
|
||||
|
||||
#define FG_COLOR_MASK (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
|
||||
#define BG_COLOR_MASK (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
|
||||
#define FG_COLOR_ATTR_MASK (FG_COLOR_MASK | FOREGROUND_INTENSITY)
|
||||
#define BG_COLOR_ATTR_MASK (BG_COLOR_MASK | BACKGROUND_INTENSITY)
|
||||
|
||||
static int style_map[9] = {
|
||||
/* RESET ALL */ 0,
|
||||
/* BRIGHT */ FOREGROUND_INTENSITY,
|
||||
/* DIM */ STYLE_DIM,
|
||||
/* (n/a) */ 0,
|
||||
/* UNDERLINED */ 0, // COMMON_LVB_UNDERSCORE ?
|
||||
/* BLINK */ 0, // NOT SUPPORTED
|
||||
/* (n/a) */ 0,
|
||||
/* REVERSE CLR */ 0, // COMMON_LVB_REVERSE_VIDEO ?
|
||||
/* HIDDEN */ 0 // NOT SUPPORTED
|
||||
};
|
||||
|
||||
static int fgcolor_map[8] = {
|
||||
/* BLACK */ 0,
|
||||
/* RED */ FOREGROUND_RED,
|
||||
/* GREEN */ FOREGROUND_GREEN,
|
||||
/* YELLOW */ FOREGROUND_GREEN | FOREGROUND_RED,
|
||||
/* BLUE */ FOREGROUND_BLUE,
|
||||
/* MAGENTA */ FOREGROUND_BLUE | FOREGROUND_RED,
|
||||
/* CYAN */ FOREGROUND_BLUE | FOREGROUND_GREEN,
|
||||
/* WHITE */ FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
|
||||
};
|
||||
static int bgcolor_map[8] = {
|
||||
/* BLACK */ 0,
|
||||
/* RED */ BACKGROUND_RED,
|
||||
/* GREEN */ BACKGROUND_GREEN,
|
||||
/* YELLOW */ BACKGROUND_GREEN | BACKGROUND_RED,
|
||||
/* BLUE */ BACKGROUND_BLUE,
|
||||
/* MAGENTA */ BACKGROUND_BLUE | BACKGROUND_RED,
|
||||
/* CYAN */ BACKGROUND_BLUE | BACKGROUND_GREEN,
|
||||
/* WHITE */ BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
|
||||
};
|
||||
#else
|
||||
static int WIN32_LEGACY_MODE = 0;
|
||||
#endif
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void term_colors_init()
|
||||
{
|
||||
#ifdef WIN32
|
||||
DWORD conmode = 0;
|
||||
h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
GetConsoleMode(h_stdout, &conmode);
|
||||
if (conmode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) {
|
||||
WIN32_LEGACY_MODE = 0;
|
||||
} else {
|
||||
conmode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
||||
if (SetConsoleMode(h_stdout, conmode)) {
|
||||
WIN32_LEGACY_MODE = 0;
|
||||
} else {
|
||||
WIN32_LEGACY_MODE = 1;
|
||||
}
|
||||
}
|
||||
if (WIN32_LEGACY_MODE) {
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
if (GetConsoleScreenBufferInfo(h_stdout, &csbi)) {
|
||||
COLOR_RESET_ATTR = csbi.wAttributes;
|
||||
DEFAULT_FG_ATTR = csbi.wAttributes & FG_COLOR_ATTR_MASK;
|
||||
DEFAULT_BG_ATTR = csbi.wAttributes & BG_COLOR_ATTR_MASK;
|
||||
}
|
||||
h_stderr = GetStdHandle(STD_ERROR_HANDLE);
|
||||
}
|
||||
#endif
|
||||
use_colors = isatty(1);
|
||||
const char* color_env = getenv("COLOR");
|
||||
if (color_env) {
|
||||
long val = strtol(color_env, NULL, 10);
|
||||
use_colors = (val != 0);
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void term_colors_set_enabled(int en)
|
||||
{
|
||||
use_colors = en;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int cvfprintf(FILE* stream, const char* fmt, va_list vargs)
|
||||
{
|
||||
int res = 0;
|
||||
int colorize = use_colors;
|
||||
#ifdef WIN32
|
||||
struct esc_item {
|
||||
int pos;
|
||||
WORD attr;
|
||||
};
|
||||
HANDLE h_stream = h_stdout;
|
||||
|
||||
if (WIN32_LEGACY_MODE) {
|
||||
if (stream == stdout) {
|
||||
h_stream = h_stdout;
|
||||
} else if (stream == stderr) {
|
||||
h_stream = h_stderr;
|
||||
} else {
|
||||
colorize = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (!colorize || WIN32_LEGACY_MODE) {
|
||||
// first, we need to print the string WITH escape sequences
|
||||
va_list vargs_copy;
|
||||
va_copy(vargs_copy, vargs);
|
||||
int len = vsnprintf(NULL, 0, fmt, vargs);
|
||||
char* newbuf = (char*)malloc(len+1);
|
||||
res = vsnprintf(newbuf, len+1, fmt, vargs_copy);
|
||||
va_end(vargs_copy);
|
||||
|
||||
// then, we need to remove the escape sequences, if any
|
||||
#ifdef WIN32
|
||||
// if colorize is on, we need to keep their positions for later
|
||||
struct esc_item* esc_items = NULL;
|
||||
int attr = 0;
|
||||
if (colorize) {
|
||||
esc_items = (struct esc_item*)malloc(sizeof(struct esc_item) * ((len/3)+1));
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
if (GetConsoleScreenBufferInfo(h_stream, &csbi)) {
|
||||
attr = csbi.wAttributes;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
int num_esc = 0;
|
||||
char* start = &newbuf[0];
|
||||
char* p = start;
|
||||
char* end = start + len + 1;
|
||||
while (p < end-1) {
|
||||
char* cur = p;
|
||||
if (*p == '\e' && end-p > 2 && *(p+1) == '[') {
|
||||
p+=2;
|
||||
if (*p == 'm') {
|
||||
#ifdef WIN32
|
||||
attr = COLOR_RESET_ATTR;
|
||||
#endif
|
||||
int move_by = (p+1)-cur;
|
||||
int move_amount = end-(p+1);
|
||||
memmove(cur, p+1, move_amount);
|
||||
end -= move_by;
|
||||
p = cur;
|
||||
} else {
|
||||
char* endp = NULL;
|
||||
do {
|
||||
long lval = strtol(p, &endp, 10);
|
||||
if (!endp || (*endp != ';' && *endp != 'm')) {
|
||||
fprintf(stderr, "\n*** %s: Invalid escape sequence in format string, expected ';' or 'm' ***\n", __func__);
|
||||
#ifdef WIN32
|
||||
free(esc_items);
|
||||
#endif
|
||||
free(newbuf);
|
||||
return -1;
|
||||
}
|
||||
#ifdef WIN32
|
||||
if (colorize) {
|
||||
if (lval >= 0 && lval <= 8) {
|
||||
/* style attributes */
|
||||
attr &= ~FOREGROUND_INTENSITY;
|
||||
attr |= style_map[lval];
|
||||
} else if (lval >= 30 && lval <= 37) {
|
||||
/* foreground color */
|
||||
attr &= ~FG_COLOR_MASK;
|
||||
attr |= fgcolor_map[lval-30];
|
||||
} else if (lval == 39) {
|
||||
/* default foreground color */
|
||||
attr &= ~FG_COLOR_ATTR_MASK;
|
||||
attr |= DEFAULT_FG_ATTR;
|
||||
} else if (lval >= 40 && lval <= 47) {
|
||||
/* background color */
|
||||
attr &= ~BG_COLOR_MASK;
|
||||
attr |= bgcolor_map[lval-40];
|
||||
} else if (lval == 49) {
|
||||
/* default background color */
|
||||
attr &= ~BG_COLOR_ATTR_MASK;
|
||||
attr |= DEFAULT_BG_ATTR;
|
||||
} else if (lval >= 90 && lval <= 97) {
|
||||
/* foreground color bright */
|
||||
attr &= ~FG_COLOR_ATTR_MASK;
|
||||
attr |= fgcolor_map[lval-90];
|
||||
attr |= FOREGROUND_INTENSITY;
|
||||
} else if (lval >= 100 && lval <= 107) {
|
||||
/* background color bright */
|
||||
attr &= ~BG_COLOR_MASK;
|
||||
attr |= bgcolor_map[lval-100];
|
||||
attr |= BACKGROUND_INTENSITY;
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)lval; // suppress compiler warning about unused variable
|
||||
#endif
|
||||
p = endp+1;
|
||||
} while (p < end && *endp == ';');
|
||||
|
||||
int move_by = (endp+1)-cur;
|
||||
int move_amount = end-(endp+1);
|
||||
memmove(cur, endp+1, move_amount);
|
||||
end -= move_by;
|
||||
p = cur;
|
||||
}
|
||||
#ifdef WIN32
|
||||
if (colorize) {
|
||||
esc_items[num_esc].pos = p-start;
|
||||
if (attr & STYLE_DIM) {
|
||||
attr &= ~STYLE_DIM;
|
||||
attr &= ~FOREGROUND_INTENSITY;
|
||||
}
|
||||
esc_items[num_esc].attr = (WORD)attr;
|
||||
num_esc++;
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
if (num_esc == 0) {
|
||||
res = fputs(newbuf, stream);
|
||||
free(newbuf);
|
||||
#ifdef WIN32
|
||||
free(esc_items);
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
#ifdef WIN32
|
||||
else {
|
||||
p = &newbuf[0];
|
||||
char* lastp = &newbuf[0];
|
||||
int i;
|
||||
for (i = 0; i < num_esc; i++) {
|
||||
p = &newbuf[esc_items[i].pos];
|
||||
if (lastp < p) {
|
||||
fprintf(stream, "%.*s", (int)(p-lastp), lastp);
|
||||
lastp = p;
|
||||
}
|
||||
SetConsoleTextAttribute(h_stream, esc_items[i].attr);
|
||||
}
|
||||
if (lastp < end) {
|
||||
fprintf(stream, "%.*s", (int)(end-lastp), lastp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
res = vfprintf(stream, fmt, vargs);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int cfprintf(FILE* stream, const char* fmt, ...)
|
||||
{
|
||||
int res = 0;
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
res = cvfprintf(stream, fmt, va);
|
||||
va_end(va);
|
||||
return res;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int cprintf(const char* fmt, ...)
|
||||
{
|
||||
int res = 0;
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
res = cvfprintf(stdout, fmt, va);
|
||||
va_end(va);
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* thread.c
|
||||
*
|
||||
* Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved.
|
||||
* Copyright (c) 2012 Martin Szulecki, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/thread.h"
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int thread_new(THREAD_T *thread, thread_func_t thread_func, void* data)
|
||||
{
|
||||
#ifdef WIN32
|
||||
HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL);
|
||||
if (th == NULL) {
|
||||
return -1;
|
||||
}
|
||||
*thread = th;
|
||||
return 0;
|
||||
#else
|
||||
int res = pthread_create(thread, NULL, thread_func, data);
|
||||
return res;
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void thread_detach(THREAD_T thread)
|
||||
{
|
||||
#ifdef WIN32
|
||||
CloseHandle(thread);
|
||||
#else
|
||||
pthread_detach(thread);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void thread_free(THREAD_T thread)
|
||||
{
|
||||
#ifdef WIN32
|
||||
CloseHandle(thread);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int thread_join(THREAD_T thread)
|
||||
{
|
||||
/* wait for thread to complete */
|
||||
#ifdef WIN32
|
||||
return (int)WaitForSingleObject(thread, INFINITE);
|
||||
#else
|
||||
return pthread_join(thread, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int thread_alive(THREAD_T thread)
|
||||
{
|
||||
if (!thread)
|
||||
return 0;
|
||||
#ifdef WIN32
|
||||
return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT;
|
||||
#else
|
||||
return pthread_kill(thread, 0) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int thread_cancel(THREAD_T thread)
|
||||
{
|
||||
#ifdef WIN32
|
||||
return -1;
|
||||
#else
|
||||
#ifdef HAVE_PTHREAD_CANCEL
|
||||
return pthread_cancel(thread);
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void mutex_init(mutex_t* mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
InitializeCriticalSection(mutex);
|
||||
#else
|
||||
pthread_mutex_init(mutex, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void mutex_destroy(mutex_t* mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
DeleteCriticalSection(mutex);
|
||||
#else
|
||||
pthread_mutex_destroy(mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void mutex_lock(mutex_t* mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
EnterCriticalSection(mutex);
|
||||
#else
|
||||
pthread_mutex_lock(mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void mutex_unlock(mutex_t* mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
LeaveCriticalSection(mutex);
|
||||
#else
|
||||
pthread_mutex_unlock(mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void thread_once(thread_once_t *once_control, void (*init_routine)(void))
|
||||
{
|
||||
#ifdef WIN32
|
||||
while (InterlockedExchange(&(once_control->lock), 1) != 0) {
|
||||
Sleep(1);
|
||||
}
|
||||
if (!once_control->state) {
|
||||
once_control->state = 1;
|
||||
init_routine();
|
||||
}
|
||||
InterlockedExchange(&(once_control->lock), 0);
|
||||
#else
|
||||
pthread_once(once_control, init_routine);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void cond_init(cond_t* cond)
|
||||
{
|
||||
#ifdef WIN32
|
||||
cond->sem = CreateSemaphore(NULL, 0, 32767, NULL);
|
||||
#else
|
||||
pthread_cond_init(cond, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void cond_destroy(cond_t* cond)
|
||||
{
|
||||
#ifdef WIN32
|
||||
CloseHandle(cond->sem);
|
||||
#else
|
||||
pthread_cond_destroy(cond);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int cond_signal(cond_t* cond)
|
||||
{
|
||||
#ifdef WIN32
|
||||
int result = 0;
|
||||
if (!ReleaseSemaphore(cond->sem, 1, NULL)) {
|
||||
result = -1;
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
return pthread_cond_signal(cond);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int cond_wait(cond_t* cond, mutex_t* mutex)
|
||||
{
|
||||
#ifdef WIN32
|
||||
mutex_unlock(mutex);
|
||||
DWORD res = WaitForSingleObject(cond->sem, INFINITE);
|
||||
switch (res) {
|
||||
case WAIT_OBJECT_0:
|
||||
return 0;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
return pthread_cond_wait(cond, mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms)
|
||||
{
|
||||
#ifdef WIN32
|
||||
mutex_unlock(mutex);
|
||||
DWORD res = WaitForSingleObject(cond->sem, timeout_ms);
|
||||
switch (res) {
|
||||
case WAIT_OBJECT_0:
|
||||
case WAIT_TIMEOUT:
|
||||
return 0;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
struct timespec ts;
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
ts.tv_sec = now.tv_sec + timeout_ms / 1000;
|
||||
ts.tv_nsec = now.tv_usec * 1000 + 1000 * 1000 * (timeout_ms % 1000);
|
||||
ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
|
||||
ts.tv_nsec %= (1000 * 1000 * 1000);
|
||||
|
||||
return pthread_cond_timedwait(cond, mutex, &ts);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* tlv.c
|
||||
* Simple TLV implementation.
|
||||
*
|
||||
* Copyright (c) 2021 Nikias Bassen, All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/tlv.h"
|
||||
#include "endianness.h"
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API tlv_buf_t tlv_buf_new()
|
||||
{
|
||||
tlv_buf_t tlv = malloc(sizeof(struct tlv_buf));
|
||||
tlv->capacity = 1024;
|
||||
tlv->length = 0;
|
||||
tlv->data = malloc(tlv->capacity);
|
||||
return tlv;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void tlv_buf_free(tlv_buf_t tlv)
|
||||
{
|
||||
if (tlv) {
|
||||
free(tlv->data);
|
||||
free(tlv);
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void tlv_buf_append(tlv_buf_t tlv, uint8_t tag, unsigned int length, void* data)
|
||||
{
|
||||
if (!tlv || !tlv->data) {
|
||||
return;
|
||||
}
|
||||
unsigned int req_len = (length > 255) ? (length / 255) * 257 + (2 + (length % 255)) : length;
|
||||
if (tlv->length + req_len > tlv->capacity) {
|
||||
unsigned int newcapacity = tlv->capacity + ((req_len / 1024) + 1) * 1024;
|
||||
unsigned char* newdata = realloc(tlv->data, newcapacity);
|
||||
if (!newdata) {
|
||||
fprintf(stderr, "%s: ERROR: Failed to realloc\n", __func__);
|
||||
return;
|
||||
}
|
||||
tlv->data = newdata;
|
||||
tlv->capacity = newcapacity;
|
||||
}
|
||||
unsigned char* p = tlv->data + tlv->length;
|
||||
unsigned int cur = 0;
|
||||
while (length - cur > 0) {
|
||||
if (length - cur > 255) {
|
||||
*(p++) = tag;
|
||||
*(p++) = 0xFF;
|
||||
memcpy(p, (unsigned char*)data + cur, 255);
|
||||
p += 255;
|
||||
cur += 255;
|
||||
} else {
|
||||
uint8_t rem = length - cur;
|
||||
*(p++) = tag;
|
||||
*(p++) = rem;
|
||||
memcpy(p, (unsigned char*)data + cur, rem);
|
||||
p += rem;
|
||||
cur += rem;
|
||||
}
|
||||
}
|
||||
tlv->length = p - tlv->data;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API unsigned char* tlv_get_data_ptr(const void* tlv_data, void* tlv_end, uint8_t tag, uint8_t* length)
|
||||
{
|
||||
unsigned char* p = (unsigned char*)tlv_data;
|
||||
unsigned char* end = (unsigned char*)tlv_end;
|
||||
while (p < end) {
|
||||
uint8_t cur_tag = *(p++);
|
||||
uint8_t len = *(p++);
|
||||
if (cur_tag == tag) {
|
||||
*length = len;
|
||||
return p;
|
||||
}
|
||||
p+=len;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int tlv_data_get_uint(const void* tlv_data, unsigned int tlv_length, uint8_t tag, uint64_t* value)
|
||||
{
|
||||
if (!tlv_data || tlv_length < 2 || !value) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t length = 0;
|
||||
unsigned char* ptr = tlv_get_data_ptr(tlv_data, (unsigned char*)tlv_data+tlv_length, tag, &length);
|
||||
if (!ptr) {
|
||||
return 0;
|
||||
}
|
||||
if (ptr + length > (unsigned char*)tlv_data + tlv_length) {
|
||||
return 0;
|
||||
}
|
||||
if (length == 1) {
|
||||
uint8_t val = *ptr;
|
||||
*value = val;
|
||||
} else if (length == 2) {
|
||||
uint16_t val = *(uint16_t*)ptr;
|
||||
val = le16toh(val);
|
||||
*value = val;
|
||||
} else if (length == 4) {
|
||||
uint32_t val = *(uint32_t*)ptr;
|
||||
val = le32toh(val);
|
||||
*value = val;
|
||||
} else if (length == 8) {
|
||||
uint64_t val = *(uint64_t*)ptr;
|
||||
val = le64toh(val);
|
||||
*value = val;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int tlv_data_get_uint8(const void* tlv_data, unsigned int tlv_length, uint8_t tag, uint8_t* value)
|
||||
{
|
||||
if (!tlv_data || tlv_length < 2 || !value) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t length = 0;
|
||||
unsigned char* ptr = tlv_get_data_ptr(tlv_data, (unsigned char*)tlv_data+tlv_length, tag, &length);
|
||||
if (!ptr) {
|
||||
return 0;
|
||||
}
|
||||
if (length != 1) {
|
||||
return 0;
|
||||
}
|
||||
if (ptr + length > (unsigned char*)tlv_data + tlv_length) {
|
||||
return 0;
|
||||
}
|
||||
*value = *ptr;
|
||||
return 1;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int tlv_data_copy_data(const void* tlv_data, unsigned int tlv_length, uint8_t tag, void** out, unsigned int* out_len)
|
||||
{
|
||||
if (!tlv_data || tlv_length < 2 || !out || !out_len) {
|
||||
return 0;
|
||||
}
|
||||
*out = NULL;
|
||||
*out_len = 0;
|
||||
|
||||
unsigned char* dest = NULL;
|
||||
unsigned int dest_len = 0;
|
||||
|
||||
unsigned char* ptr = (unsigned char*)tlv_data;
|
||||
unsigned char* end = (unsigned char*)tlv_data + tlv_length;
|
||||
while (ptr < end) {
|
||||
uint8_t length = 0;
|
||||
ptr = tlv_get_data_ptr(ptr, end, tag, &length);
|
||||
if (!ptr) {
|
||||
break;
|
||||
}
|
||||
unsigned char* newdest = realloc(dest, dest_len + length);
|
||||
if (!newdest) {
|
||||
free(dest);
|
||||
return 0;
|
||||
}
|
||||
dest = newdest;
|
||||
memcpy(dest + dest_len, ptr, length);
|
||||
dest_len += length;
|
||||
ptr += length;
|
||||
}
|
||||
if (!dest) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*out = (void*)dest;
|
||||
*out_len = dest_len;
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,558 @@
|
||||
/*
|
||||
* utils.c
|
||||
* Miscellaneous utilities for string manipulation,
|
||||
* file I/O and plist helper.
|
||||
*
|
||||
* Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
|
||||
* Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
|
||||
* Copyright (c) 2013 Federico Mena Quintero
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <inttypes.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "libimobiledevice-glue/utils.h"
|
||||
|
||||
#ifndef HAVE_STPCPY
|
||||
#undef stpcpy
|
||||
char *stpcpy(char *s1, const char *s2);
|
||||
|
||||
/**
|
||||
* Copy characters from one string into another
|
||||
*
|
||||
* @note: The strings should not overlap, as the behavior is undefined.
|
||||
*
|
||||
* @s1: The source string.
|
||||
* @s2: The destination string.
|
||||
*
|
||||
* @return a pointer to the terminating `\0' character of @s1,
|
||||
* or NULL if @s1 or @s2 is NULL.
|
||||
*/
|
||||
char *stpcpy(char *s1, const char *s2)
|
||||
{
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return NULL;
|
||||
|
||||
strcpy(s1, s2);
|
||||
|
||||
return s1 + strlen(s2);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Concatenate strings into a newly allocated string
|
||||
*
|
||||
* @note: Specify NULL for the last string in the varargs list
|
||||
*
|
||||
* @str: The first string in the list
|
||||
* @...: Subsequent strings. Use NULL for the last item.
|
||||
*
|
||||
* @return a newly allocated string, or NULL if @str is NULL. This will also
|
||||
* return NULL and set errno to ENOMEM if memory is exhausted.
|
||||
*/
|
||||
LIBIMOBILEDEVICE_GLUE_API char *string_concat(const char *str, ...)
|
||||
{
|
||||
size_t len;
|
||||
va_list args;
|
||||
char *s;
|
||||
char *result;
|
||||
char *dest;
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
/* Compute final length */
|
||||
|
||||
len = strlen(str) + 1; /* plus 1 for the null terminator */
|
||||
|
||||
va_start(args, str);
|
||||
s = va_arg(args, char *);
|
||||
while (s) {
|
||||
len += strlen(s);
|
||||
s = va_arg(args, char*);
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
/* Concat each string */
|
||||
|
||||
result = malloc(len);
|
||||
if (!result)
|
||||
return NULL; /* errno remains set */
|
||||
|
||||
dest = result;
|
||||
|
||||
dest = stpcpy(dest, str);
|
||||
|
||||
va_start(args, str);
|
||||
s = va_arg(args, char *);
|
||||
while (s) {
|
||||
dest = stpcpy(dest, s);
|
||||
s = va_arg(args, char *);
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API char *string_append(char* str, ...)
|
||||
{
|
||||
size_t len = 0;
|
||||
size_t slen;
|
||||
va_list args;
|
||||
char *s;
|
||||
char *result;
|
||||
char *dest;
|
||||
|
||||
/* Compute final length */
|
||||
|
||||
if (str) {
|
||||
len = strlen(str);
|
||||
}
|
||||
slen = len;
|
||||
len++; /* plus 1 for the null terminator */
|
||||
|
||||
va_start(args, str);
|
||||
s = va_arg(args, char *);
|
||||
while (s) {
|
||||
len += strlen(s);
|
||||
s = va_arg(args, char*);
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
result = realloc(str, len);
|
||||
if (!result)
|
||||
return NULL; /* errno remains set */
|
||||
|
||||
dest = result + slen;
|
||||
|
||||
/* Concat additional strings */
|
||||
|
||||
va_start(args, str);
|
||||
s = va_arg(args, char *);
|
||||
while (s) {
|
||||
dest = stpcpy(dest, s);
|
||||
s = va_arg(args, char *);
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API char *string_build_path(const char *elem, ...)
|
||||
{
|
||||
if (!elem)
|
||||
return NULL;
|
||||
va_list args;
|
||||
int len = strlen(elem)+1;
|
||||
va_start(args, elem);
|
||||
char *arg = va_arg(args, char*);
|
||||
while (arg) {
|
||||
len += strlen(arg)+1;
|
||||
arg = va_arg(args, char*);
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
char* out = (char*)malloc(len);
|
||||
strcpy(out, elem);
|
||||
|
||||
va_start(args, elem);
|
||||
arg = va_arg(args, char*);
|
||||
while (arg) {
|
||||
strcat(out, "/");
|
||||
strcat(out, arg);
|
||||
arg = va_arg(args, char*);
|
||||
}
|
||||
va_end(args);
|
||||
return out;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API char *string_format_size(uint64_t size)
|
||||
{
|
||||
char buf[80];
|
||||
double sz;
|
||||
if (size >= 1000000000000LL) {
|
||||
sz = ((double)size / 1000000000000.0F);
|
||||
sprintf(buf, "%0.1f TB", sz);
|
||||
} else if (size >= 1000000000LL) {
|
||||
sz = ((double)size / 1000000000.0F);
|
||||
sprintf(buf, "%0.1f GB", sz);
|
||||
} else if (size >= 1000000LL) {
|
||||
sz = ((double)size / 1000000.0F);
|
||||
sprintf(buf, "%0.1f MB", sz);
|
||||
} else if (size >= 1000LL) {
|
||||
sz = ((double)size / 1000.0F);
|
||||
sprintf(buf, "%0.1f KB", sz);
|
||||
} else {
|
||||
sprintf(buf, "%d Bytes", (int)size);
|
||||
}
|
||||
return strdup(buf);
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API char *string_toupper(char* str)
|
||||
{
|
||||
char *res = strdup(str);
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(res); i++) {
|
||||
res[i] = toupper(res[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int get_rand(int min, int max)
|
||||
{
|
||||
int retval = (rand() % (max - min)) + min;
|
||||
return retval;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API char *generate_uuid()
|
||||
{
|
||||
const char *chars = "ABCDEF0123456789";
|
||||
int i = 0;
|
||||
char *uuid = (char *) malloc(sizeof(char) * 37);
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (i = 0; i < 36; i++) {
|
||||
if (i == 8 || i == 13 || i == 18 || i == 23) {
|
||||
uuid[i] = '-';
|
||||
continue;
|
||||
}
|
||||
uuid[i] = chars[get_rand(0, 16)];
|
||||
}
|
||||
|
||||
/* make it a real string */
|
||||
uuid[36] = '\0';
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length)
|
||||
{
|
||||
FILE *f;
|
||||
uint64_t size;
|
||||
|
||||
if (!filename || !buffer || !length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*length = 0;
|
||||
|
||||
f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
size = ftell(f);
|
||||
rewind(f);
|
||||
|
||||
if (size == 0) {
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*buffer = (char*)malloc(sizeof(char)*(size+1));
|
||||
|
||||
if (*buffer == NULL) {
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 1;
|
||||
if (fread(*buffer, sizeof(char), size, f) != size) {
|
||||
free(*buffer);
|
||||
ret = 0;
|
||||
errno = EIO;
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
*length = size;
|
||||
return ret;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
f = fopen(filename, "wb");
|
||||
if (f) {
|
||||
size_t written = fwrite(buffer, sizeof(char), length, f);
|
||||
fclose(f);
|
||||
|
||||
if (written == length) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
// Not all data could be written.
|
||||
errno = EIO;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Failed to open the file, let the caller know.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int plist_read_from_filename(plist_t *plist, const char *filename)
|
||||
{
|
||||
char *buffer = NULL;
|
||||
uint64_t length;
|
||||
|
||||
if (!filename)
|
||||
return 0;
|
||||
|
||||
if (!buffer_read_from_filename(filename, &buffer, &length)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
plist_from_memory(buffer, length, plist);
|
||||
|
||||
free(buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format)
|
||||
{
|
||||
char *buffer = NULL;
|
||||
uint32_t length;
|
||||
|
||||
if (!plist || !filename)
|
||||
return 0;
|
||||
|
||||
if (format == PLIST_FORMAT_XML)
|
||||
plist_to_xml(plist, &buffer, &length);
|
||||
else if (format == PLIST_FORMAT_BINARY)
|
||||
plist_to_bin(plist, &buffer, &length);
|
||||
else
|
||||
return 0;
|
||||
|
||||
int res = buffer_write_to_filename(filename, buffer, length);
|
||||
|
||||
free(buffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static const char base64_pad = '=';
|
||||
|
||||
static char *base64encode(const unsigned char *buf, size_t size)
|
||||
{
|
||||
if (!buf || !(size > 0)) return NULL;
|
||||
int outlen = (size / 3) * 4;
|
||||
char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0'
|
||||
size_t n = 0;
|
||||
size_t m = 0;
|
||||
unsigned char input[3];
|
||||
unsigned int output[4];
|
||||
while (n < size) {
|
||||
input[0] = buf[n];
|
||||
input[1] = (n+1 < size) ? buf[n+1] : 0;
|
||||
input[2] = (n+2 < size) ? buf[n+2] : 0;
|
||||
output[0] = input[0] >> 2;
|
||||
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
|
||||
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
|
||||
output[3] = input[2] & 63;
|
||||
outbuf[m++] = base64_str[(int)output[0]];
|
||||
outbuf[m++] = base64_str[(int)output[1]];
|
||||
outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad;
|
||||
outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad;
|
||||
n+=3;
|
||||
}
|
||||
outbuf[m] = 0; // 0-termination!
|
||||
return outbuf;
|
||||
}
|
||||
|
||||
static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream);
|
||||
|
||||
static void plist_array_print_to_stream(plist_t node, int* indent_level, FILE* stream)
|
||||
{
|
||||
/* iterate over items */
|
||||
int i, count;
|
||||
plist_t subnode = NULL;
|
||||
|
||||
count = plist_array_get_size(node);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
subnode = plist_array_get_item(node, i);
|
||||
fprintf(stream, "%*s", *indent_level, "");
|
||||
fprintf(stream, "%d: ", i);
|
||||
plist_node_print_to_stream(subnode, indent_level, stream);
|
||||
}
|
||||
}
|
||||
|
||||
static void plist_dict_print_to_stream(plist_t node, int* indent_level, FILE* stream)
|
||||
{
|
||||
/* iterate over key/value pairs */
|
||||
plist_dict_iter it = NULL;
|
||||
|
||||
char* key = NULL;
|
||||
plist_t subnode = NULL;
|
||||
plist_dict_new_iter(node, &it);
|
||||
plist_dict_next_item(node, it, &key, &subnode);
|
||||
while (subnode)
|
||||
{
|
||||
fprintf(stream, "%*s", *indent_level, "");
|
||||
fprintf(stream, "%s", key);
|
||||
if (plist_get_node_type(subnode) == PLIST_ARRAY)
|
||||
fprintf(stream, "[%d]: ", plist_array_get_size(subnode));
|
||||
else
|
||||
fprintf(stream, ": ");
|
||||
free(key);
|
||||
key = NULL;
|
||||
plist_node_print_to_stream(subnode, indent_level, stream);
|
||||
plist_dict_next_item(node, it, &key, &subnode);
|
||||
}
|
||||
free(it);
|
||||
}
|
||||
|
||||
static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream)
|
||||
{
|
||||
char *s = NULL;
|
||||
char *data = NULL;
|
||||
double d;
|
||||
uint8_t b;
|
||||
uint64_t u = 0;
|
||||
struct timeval tv = { 0, 0 };
|
||||
|
||||
plist_type t;
|
||||
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
t = plist_get_node_type(node);
|
||||
|
||||
switch (t) {
|
||||
case PLIST_BOOLEAN:
|
||||
plist_get_bool_val(node, &b);
|
||||
fprintf(stream, "%s\n", (b ? "true" : "false"));
|
||||
break;
|
||||
|
||||
case PLIST_UINT:
|
||||
plist_get_uint_val(node, &u);
|
||||
fprintf(stream, "%"PRIu64"\n", u);
|
||||
break;
|
||||
|
||||
case PLIST_REAL:
|
||||
plist_get_real_val(node, &d);
|
||||
fprintf(stream, "%f\n", d);
|
||||
break;
|
||||
|
||||
case PLIST_STRING:
|
||||
plist_get_string_val(node, &s);
|
||||
fprintf(stream, "%s\n", s);
|
||||
free(s);
|
||||
break;
|
||||
|
||||
case PLIST_KEY:
|
||||
plist_get_key_val(node, &s);
|
||||
fprintf(stream, "%s: ", s);
|
||||
free(s);
|
||||
break;
|
||||
|
||||
case PLIST_DATA:
|
||||
plist_get_data_val(node, &data, &u);
|
||||
if (u > 0) {
|
||||
s = base64encode((unsigned char*)data, u);
|
||||
free(data);
|
||||
if (s) {
|
||||
fprintf(stream, "%s\n", s);
|
||||
free(s);
|
||||
} else {
|
||||
fprintf(stream, "\n");
|
||||
}
|
||||
} else {
|
||||
fprintf(stream, "\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case PLIST_DATE:
|
||||
plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec);
|
||||
{
|
||||
time_t ti = (time_t)tv.tv_sec + MAC_EPOCH;
|
||||
struct tm *btime = localtime(&ti);
|
||||
if (btime) {
|
||||
s = (char*)malloc(24);
|
||||
memset(s, 0, 24);
|
||||
if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) {
|
||||
free (s);
|
||||
s = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s) {
|
||||
fprintf(stream, "%s\n", s);
|
||||
free(s);
|
||||
} else {
|
||||
fprintf(stream, "\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case PLIST_ARRAY:
|
||||
fprintf(stream, "\n");
|
||||
(*indent_level)++;
|
||||
plist_array_print_to_stream(node, indent_level, stream);
|
||||
(*indent_level)--;
|
||||
break;
|
||||
|
||||
case PLIST_DICT:
|
||||
fprintf(stream, "\n");
|
||||
(*indent_level)++;
|
||||
plist_dict_print_to_stream(node, indent_level, stream);
|
||||
(*indent_level)--;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void plist_print_to_stream_with_indentation(plist_t plist, FILE* stream, unsigned int indentation)
|
||||
{
|
||||
if (!plist || !stream)
|
||||
return;
|
||||
|
||||
int indent = indentation;
|
||||
switch (plist_get_node_type(plist)) {
|
||||
case PLIST_DICT:
|
||||
plist_dict_print_to_stream(plist, &indent, stream);
|
||||
break;
|
||||
case PLIST_ARRAY:
|
||||
plist_array_print_to_stream(plist, &indent, stream);
|
||||
break;
|
||||
default:
|
||||
plist_node_print_to_stream(plist, &indent, stream);
|
||||
}
|
||||
}
|
||||
|
||||
LIBIMOBILEDEVICE_GLUE_API void plist_print_to_stream(plist_t plist, FILE* stream)
|
||||
{
|
||||
plist_print_to_stream_with_indentation(plist, stream, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user