Skip to content

Commit

Permalink
Refactor get_elf_section_offset_and_length
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAssassin committed May 9, 2018
1 parent 9fa3693 commit 241c4c2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
5 changes: 5 additions & 0 deletions include/appimage/appimage.h
Expand Up @@ -144,6 +144,11 @@ bool appimage_type2_digest_md5(const char* fname, char* digest);
*/
char* appimage_hexlify(const char* bytes, size_t numBytes);

/*
* Return the offset, and the length of an ELF section with a given name in a given ELF file
*/
bool appimage_get_elf_section_offset_and_length(const char* fname, const char* section_name, unsigned long* offset, unsigned long* length);

#ifdef __cplusplus
}
#endif
25 changes: 18 additions & 7 deletions src/appimagetool.c
Expand Up @@ -835,7 +835,13 @@ main (int argc, char *argv[])

unsigned long ui_offset = 0;
unsigned long ui_length = 0;
get_elf_section_offset_and_length(destination, ".upd_info", &ui_offset, &ui_length);

bool rv = appimage_get_elf_section_offset_and_length(destination, ".upd_info", &ui_offset, &ui_length);

if (!rv || ui_offset == 0 || ui_length == 0) {
die("Could not find section .upd_info in runtime");
}

if(verbose) {
printf("ui_offset: %lu\n", ui_offset);
printf("ui_length: %lu\n", ui_length);
Expand Down Expand Up @@ -864,9 +870,9 @@ main (int argc, char *argv[])
unsigned long digest_md5_offset = 0;
unsigned long digest_md5_length = 0;

int rv = get_elf_section_offset_and_length(destination, ".digest_md5", &digest_md5_offset, &digest_md5_length);
bool rv = appimage_get_elf_section_offset_and_length(destination, ".digest_md5", &digest_md5_offset, &digest_md5_length);

if (rv != 0 || digest_md5_offset == 0 || digest_md5_length == 0) {
if (!rv || digest_md5_offset == 0 || digest_md5_length == 0) {
die("Could not find section .digest_md5 in runtime");
}

Expand Down Expand Up @@ -1020,9 +1026,9 @@ main (int argc, char *argv[])
unsigned long sig_offset = 0;
unsigned long sig_length = 0;

int rv = get_elf_section_offset_and_length(destination, ".sha256_sig", &sig_offset, &sig_length);
bool rv = appimage_get_elf_section_offset_and_length(destination, ".sha256_sig", &sig_offset, &sig_length);

if (rv != 0 || sig_offset == 0 || sig_length == 0) {
if (!rv || sig_offset == 0 || sig_length == 0) {
die("Could not find section .sha256_sig in runtime");
}

Expand Down Expand Up @@ -1120,8 +1126,13 @@ main (int argc, char *argv[])
{
sprintf(command, "%s --batch --export --armor %s", gpg2_path, sign_key);

unsigned long key_offset, key_length;
int rv = get_elf_section_offset_and_length(destination, ".sig_key", &key_offset, &key_length);
unsigned long key_offset = 0, key_length = 0;

bool rv = appimage_get_elf_section_offset_and_length(destination, ".sig_key", &key_offset, &key_length);

if (!rv || key_offset == 0 || key_length == 0) {
die("Could not find section .sig_key in runtime");
}

if (verbose) {
printf("key_offset: %lu\n", key_offset);
Expand Down
6 changes: 3 additions & 3 deletions src/appimagetool_shared.c
Expand Up @@ -61,15 +61,15 @@ int appimage_get_type(const char* path, bool verbose)
bool appimage_type2_digest_md5(const char* path, char* digest) {
// skip digest, signature and key sections in digest calculation
unsigned long digest_md5_offset = 0, digest_md5_length = 0;
if (get_elf_section_offset_and_length(path, ".digest_md5", &digest_md5_offset, &digest_md5_length) != 0)
if (!appimage_get_elf_section_offset_and_length(path, ".digest_md5", &digest_md5_offset, &digest_md5_length))
return false;

unsigned long signature_offset = 0, signature_length = 0;
if (get_elf_section_offset_and_length(path, ".sha256_sig", &signature_offset, &signature_length) != 0)
if (!appimage_get_elf_section_offset_and_length(path, ".sha256_sig", &signature_offset, &signature_length))
return false;

unsigned long sig_key_offset = 0, sig_key_length = 0;
if (get_elf_section_offset_and_length(path, ".sig_key", &sig_key_offset, &sig_key_length) != 0)
if (!appimage_get_elf_section_offset_and_length(path, ".sig_key", &sig_key_offset, &sig_key_length))
return false;

GChecksum *checksum = g_checksum_new(G_CHECKSUM_MD5);
Expand Down
2 changes: 1 addition & 1 deletion src/digest_md5.c
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) {

static const char section_name[] = ".digest_md5";

if (get_elf_section_offset_and_length(fname, section_name, &offset, &length) != 0 || offset == 0 || length == 0) {
if (!appimage_get_elf_section_offset_and_length(fname, section_name, &offset, &length) || offset == 0 || length == 0) {
fprintf(stderr, "Could not find %s section in file\n", section_name);
return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions src/getsection.c
Expand Up @@ -2,13 +2,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include "light_elf.h"

/* Return the offset, and the length of an ELF section with a given name in a given ELF file */
int get_elf_section_offset_and_length(const char* fname, const char* section_name, unsigned long* offset,
unsigned long* length) {
bool appimage_get_elf_section_offset_and_length(const char* fname, const char* section_name, unsigned long* offset, unsigned long* length) {
uint8_t* data;
int i;
int fd = open(fname, O_RDONLY);
Expand Down Expand Up @@ -51,11 +51,11 @@ int get_elf_section_offset_and_length(const char* fname, const char* section_nam
} else {
sprintf(stderr, "Platforms other than 32-bit/64-bit are currently not supported!");
munmap(data, map_size);
return 2;
return false;
}

munmap(data, map_size);
return 0;
return true;
}

char* read_file_offset_length(const char* fname, unsigned long offset, unsigned long length) {
Expand Down
3 changes: 0 additions & 3 deletions src/getsection.h
@@ -1,9 +1,6 @@
#ifndef GETSECTION_H
#define GETSECTION_H

/* Return the offset, and the length of an ELF section with a given name in a given ELF file */
int get_elf_section_offset_and_length(const char* fname, const char* section_name, unsigned long *offset, unsigned long *length);

int print_hex(const char* fname, unsigned long offset, unsigned long length);

int print_binary(const char* fname, unsigned long offset, unsigned long length);
Expand Down

0 comments on commit 241c4c2

Please sign in to comment.