Path: blob/main/contrib/elftoolchain/libpe/pe_dos.c
39478 views
/*-1* Copyright (c) 2015 Kai Wang2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <assert.h>27#include <errno.h>28#include <stdlib.h>29#include <string.h>3031#include "_libpe.h"3233ELFTC_VCSID("$Id: pe_dos.c 3312 2016-01-10 09:23:51Z kaiwang27 $");3435PE_DosHdr *36pe_msdos_header(PE *pe)37{3839if (pe == NULL) {40errno = EINVAL;41return (NULL);42}4344if (pe->pe_dh == NULL) {45errno = ENOENT;46return (NULL);47}4849return (pe->pe_dh);50}5152char *53pe_msdos_stub(PE *pe, size_t *len)54{5556if (pe == NULL || len == NULL) {57errno = EINVAL;58return (NULL);59}6061if (pe->pe_stub_ex > 0 &&62(pe->pe_flags & LIBPE_F_LOAD_DOS_STUB) == 0) {63assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);64(void) libpe_read_msdos_stub(pe);65}6667*len = sizeof(PE_DosHdr) + pe->pe_stub_ex;6869return (pe->pe_stub);70}7172int73ps_update_msdos_header(PE *pe, PE_DosHdr *dh)74{7576if (pe == NULL || dh == NULL) {77errno = EINVAL;78return (-1);79}8081if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {82errno = EACCES;83return (-1);84}8586if (pe->pe_dh == NULL) {87if ((pe->pe_dh = malloc(sizeof(PE_DosHdr))) == NULL) {88errno = ENOMEM;89return (-1);90}91}9293*pe->pe_dh = *dh;9495pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;9697return (0);98}99100int101ps_update_msdos_stub(PE *pe, char *dos_stub, size_t sz)102{103104if (pe == NULL || dos_stub == NULL || sz == 0) {105errno = EINVAL;106return (-1);107}108109if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {110errno = EACCES;111return (-1);112}113114pe->pe_stub_app = dos_stub;115pe->pe_stub_app_sz = sz;116117return (0);118}119120121