VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/libzip/zip_add_entry.c
blob: bf12dd54176ee24305da92580b5220a5d9f7eab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
  zip_add_entry.c -- create and init struct zip_entry
  Copyright (C) 1999-2021 Dieter Baron and Thomas Klausner

  This file is part of libzip, a library to manipulate ZIP archives.
  The authors can be contacted at <info@libzip.org>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
  3. The names of the authors may not be used to endorse or promote
     products derived from this software without specific prior
     written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include <stdlib.h>

#include "zipint.h"


/* NOTE: Signed due to -1 on error.  See zip_add.c for more details. */

zip_int64_t
_zip_add_entry(zip_t *za) {
    zip_uint64_t idx;

    if (za->nentry + 1 >= za->nentry_alloc) {
        zip_entry_t *rentries;
        zip_uint64_t nalloc = za->nentry_alloc;
        zip_uint64_t additional_entries = 2 * nalloc;
        zip_uint64_t realloc_size;

        if (additional_entries < 16) {
            additional_entries = 16;
        }
        else if (additional_entries > 1024) {
            additional_entries = 1024;
        }
        /* neither + nor * overflows can happen: nentry_alloc * sizeof(struct zip_entry) < UINT64_MAX */
        nalloc += additional_entries;
        realloc_size = sizeof(struct zip_entry) * (size_t)nalloc;

        if (sizeof(struct zip_entry) * (size_t)za->nentry_alloc > realloc_size) {
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
            return -1;
        }
        rentries = (zip_entry_t *)realloc(za->entry, sizeof(struct zip_entry) * (size_t)nalloc);
        if (!rentries) {
            zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
            return -1;
        }
        za->entry = rentries;
        za->nentry_alloc = nalloc;
    }

    idx = za->nentry++;

    _zip_entry_init(za->entry + idx);

    return (zip_int64_t)idx;
}
ass="n">UnhandledException; }; #ifdef TC_UNIX static void OnFatalProgramErrorSignal (int, siginfo_t *signalInfo, void *contextArg) { TC_UNUSED_VAR ucontext_t *context = (ucontext_t *) contextArg; uint64 faultingInstructionAddress = 0; #ifdef TC_LINUX # ifdef REG_EIP faultingInstructionAddress = context->uc_mcontext.gregs[REG_EIP]; # elif defined (REG_RIP) faultingInstructionAddress = context->uc_mcontext.gregs[REG_RIP]; # endif #elif defined (TC_MACOSX) # ifdef __x86_64__ faultingInstructionAddress = context->uc_mcontext->__ss.__rip; # else faultingInstructionAddress = context->uc_mcontext->__ss.__eip; # endif #endif wstringstream vars; vars << L"cpus=" << wxThread::GetCPUCount(); vars << L"&cksum=" << hex << FatalErrorHandler::GetAppChecksum() << dec; vars << L"&err=" << signalInfo->si_signo; vars << L"&addr=" << hex << faultingInstructionAddress << dec; vars << FatalErrorHandler::GetCallStack (16); wxString url = Gui->GetHomepageLinkURL (L"err-report", vars.str()); url.Replace (L"=0x", L"="); url.Replace (L"=0X0x", L"=0x"); url.Replace (L"=0X", L"=0x"); wxString msg = L"A critical error has occurred and VeraCrypt must be terminated. If this is caused by a bug in VeraCrypt, we would like to fix it. To help us, you can send us an automatically generated error report containing the following items:\n\n- Program version\n- Operating system version\n- Hardware architecture\n- Checksum of VeraCrypt executable\n- Error category\n- Error address\n"; #if wxUSE_STACKWALKER == 1 msg += L"- VeraCrypt call stack\n"; #endif msg += L"\nIf you select 'Yes', the following URL (which contains the entire error report) will be opened in your default Internet browser.\n\n"; #ifdef __WXGTK__ wxString fUrl = url; fUrl.Replace (L"&st", L" &st"); msg += fUrl; #else msg += url; #endif msg += L"\n\nDo you want to send us the error report?"; if (Gui->AskYesNo (msg, true)) wxLaunchDefaultBrowser (url, wxBROWSER_NEW_WINDOW); _exit (1); } #endif // TC_UNIX void FatalErrorHandler::Deregister() { #ifdef TC_UNIX signal (SIGILL, SIG_DFL); signal (SIGFPE, SIG_DFL); signal (SIGSEGV, SIG_DFL); signal (SIGBUS, SIG_DFL); signal (SIGSYS, SIG_DFL); #endif #ifndef TC_WINDOWS std::set_terminate (DefaultTerminateHandler); #endif } uint32 FatalErrorHandler::GetAppChecksum () { uint32 checkSum = 0; try { File executable; executable.Open (Application::GetExecutablePath()); Buffer executableData (executable.Length()); executable.ReadCompleteBuffer (executableData); checkSum = Crc32::ProcessBuffer (executableData); } catch (...) { } return checkSum; } wstring FatalErrorHandler::GetCallStack (int depth) { #if wxUSE_STACKWALKER == 1 class StackWalker : public wxStackWalker { public: StackWalker () : FrameCount (0) { } void OnStackFrame (const wxStackFrame &frame) { if (FrameCount >= 32) return; StackVars << L"&st" << FrameCount++ << L"="; wxString functionName = frame.GetName(); if (!functionName.empty() && !frame.GetModule().empty()) { int p = functionName.Find (L"("); if (p != wxNOT_FOUND) functionName = functionName.Mid (0, p); for (size_t i = 0; i < functionName.size(); ++i) { if (!isalnum (functionName[i])) functionName[i] = L'_'; } while (functionName.Replace (L"__", L"_")); StackVars << wstring (functionName); } else StackVars << "0X" << hex << frame.GetAddress() << dec; } int FrameCount; wstringstream StackVars; }; StackWalker stackWalker; stackWalker.Walk (2); return stackWalker.StackVars.str(); #else // wxUSE_STACKWALKER return wstring(); #endif // wxUSE_STACKWALKER } void FatalErrorHandler::OnTerminate () { try { throw; } catch (UserAbort&) { } catch (Exception &e) { wxString vars; wxString exName = StringConverter::ToWide (StringConverter::GetTypeName (typeid (e))); if (exName.find (L"VeraCrypt::") != string::npos) exName = exName.Mid (11); wxString exPos = StringConverter::ToWide (e.what()); if (exPos.find (L"VeraCrypt::") != string::npos) exPos = exPos.Mid (11); vars << L"cpus=" << wxThread::GetCPUCount(); vars << wxString::Format (L"&cksum=%x", GetAppChecksum()); vars << L"&exception=" << exName; vars << L"&exlocation=" << exPos; vars << FatalErrorHandler::GetCallStack (16); vars.Replace (L"::", L"."); vars.Replace (L":", L"."); wxString url = Gui->GetHomepageLinkURL (L"err-report", vars); url.Replace (L"=0x", L"="); url.Replace (L"=0X0x", L"=0x"); url.Replace (L"=0X", L"=0x"); wxString msg = L"An unhandled exception has occurred and VeraCrypt must be terminated. If this is caused by a bug in VeraCrypt, we would like to fix it. To help us, you can send us an automatically generated error report containing the following items:\n\n- Program version\n- Operating system version\n- Hardware architecture\n- Checksum of VeraCrypt executable\n- Error description\n- Error location\n"; #if wxUSE_STACKWALKER == 1 msg += L"- VeraCrypt call stack\n"; #endif msg += L"\nIf you select 'Yes', the following URL (which contains the entire error report) will be opened in your default Internet browser.\n\n"; #ifdef __WXGTK__ wxString fUrl = url; fUrl.Replace (L"&st", L" &st"); msg += fUrl; #else msg += url; #endif msg += L"\n\nDo you want to send us the error report?"; if (Gui->AskYesNo (msg, true)) wxLaunchDefaultBrowser (url, wxBROWSER_NEW_WINDOW); } catch (exception &e) { Gui->ShowError (e); } catch (...) { Gui->ShowError (_("Unknown exception occurred.")); } _exit (1); } void FatalErrorHandler::Register () { #ifndef TC_WINDOWS // OnUnhandledException() seems to be called only on Windows DefaultTerminateHandler = std::set_terminate (OnTerminate); #endif #ifdef TC_UNIX struct sigaction action; Memory::Zero (&action, sizeof (action)); action.sa_flags = SA_SIGINFO; action.sa_sigaction = OnFatalProgramErrorSignal; throw_sys_if (sigaction (SIGILL, &action, nullptr) == -1); throw_sys_if (sigaction (SIGFPE, &action, nullptr) == -1); throw_sys_if (sigaction (SIGSEGV, &action, nullptr) == -1); throw_sys_if (sigaction (SIGBUS, &action, nullptr) == -1); throw_sys_if (sigaction (SIGSYS, &action, nullptr) == -1); #endif } }