From 39ac0d29e1062a28528e94756e16a2e856eb3b49 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Thu, 13 Feb 2014 20:02:00 +0100 Subject: utils: Files rename, new functions, proper types and few other cleanups Signed-off-by: Paul Kocialkowski --- samsung-ipc/utils.c | 407 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 407 insertions(+) create mode 100644 samsung-ipc/utils.c (limited to 'samsung-ipc/utils.c') diff --git a/samsung-ipc/utils.c b/samsung-ipc/utils.c new file mode 100644 index 0000000..364cbbd --- /dev/null +++ b/samsung-ipc/utils.c @@ -0,0 +1,407 @@ +/* + * This file is part of libsamsung-ipc. + * + * Copyright (C) 2013-2014 Paul Kocialkowski + * + * libsamsung-ipc is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libsamsung-ipc is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with libsamsung-ipc. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" + +void *file_data_read(const char *path, size_t size, size_t chunk_size, + size_t offset) +{ + void *data = NULL; + int fd = -1; + size_t count; + off_t seek; + unsigned char *p; + int rc; + + if (path == NULL || size == 0 || chunk_size == 0 || chunk_size > size) + return NULL; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto error; + + seek = lseek(fd, (off_t) offset, SEEK_SET); + if (seek < (off_t) offset) + goto error; + + data = calloc(1, size); + + p = (unsigned char *) data; + + count = 0; + while (count < size) { + rc = read(fd, p, size - count > chunk_size ? chunk_size : size - count); + if (rc <= 0) + goto error; + + p += rc; + count += rc; + } + + goto complete; + +error: + if (data != NULL) { + free(data); + data = NULL; + } + +complete: + if (fd >= 0) + close(fd); + + return data; +} + +int file_data_write(const char *path, const void *data, size_t size, + size_t chunk_size, size_t offset) +{ + int fd = -1; + size_t count; + off_t seek; + unsigned char *p; + int rc; + + if (path == NULL || data == NULL || size == 0 || chunk_size == 0 || chunk_size > size) + return -1; + + fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (fd < 0) + goto error; + + seek = lseek(fd, (off_t) offset, SEEK_SET); + if (seek < (off_t) offset) + goto error; + + p = (unsigned char *) data; + + count = 0; + while (count < size) { + rc = write(fd, p, size - count > chunk_size ? chunk_size : size - count); + if (rc <= 0) + goto error; + + p += rc; + count += rc; + } + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +int network_iface_up(const char *iface, int domain, int type) +{ + struct ifreq ifr; + int fd = -1; + int rc; + + if (iface == NULL) + return -1; + + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, iface, IFNAMSIZ); + + fd = socket(domain, type, 0); + if (fd < 0) + goto error; + + rc = ioctl(fd, SIOCGIFFLAGS, &ifr); + if (rc < 0) + goto error; + + ifr.ifr_flags |= IFF_UP; + + rc = ioctl(fd, SIOCSIFFLAGS, &ifr); + if (rc < 0) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +int network_iface_down(const char *iface, int domain, int type) +{ + struct ifreq ifr; + int fd = -1; + int rc; + + if (iface == NULL) + return -1; + + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, iface, IFNAMSIZ); + + fd = socket(domain, type, 0); + if (fd < 0) + goto error; + + rc = ioctl(fd, SIOCGIFFLAGS, &ifr); + if (rc < 0) + goto error; + + ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP)); + + rc = ioctl(fd, SIOCSIFFLAGS, &ifr); + if (rc < 0) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +int sysfs_value_read(const char *path) +{ + char buffer[100]; + int value; + int fd = -1; + int rc; + + if (path == NULL) + return -1; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto error; + + rc = read(fd, &buffer, sizeof(buffer)); + if (rc <= 0) + goto error; + + value = atoi(buffer); + goto complete; + +error: + value = -1; + +complete: + if (fd >= 0) + close(fd); + + return value; +} + +int sysfs_value_write(const char *path, int value) +{ + char buffer[100]; + int fd = -1; + int rc; + + if (path == NULL) + return -1; + + fd = open(path, O_WRONLY); + if (fd < 0) + goto error; + + snprintf((char *) &buffer, sizeof(buffer), "%d\n", value); + + rc = write(fd, buffer, strlen(buffer)); + if (rc < (int) strlen(buffer)) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +char *sysfs_string_read(const char *path, size_t length) +{ + char *string = NULL; + int fd = -1; + int rc; + + if (path == NULL || length == 0) + return NULL; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto error; + + string = (char *) calloc(1, length); + + rc = read(fd, string, length); + if (rc <= 0) + goto error; + + goto complete; + +error: + if (string != NULL) { + free(string); + string = NULL; + } + +complete: + if (fd >= 0) + close(fd); + + return string; +} + +int sysfs_string_write(const char *path, const char *buffer, size_t length) +{ + int fd = -1; + int rc; + + if (path == NULL || buffer == NULL || length == 0) + return -1; + + fd = open(path, O_WRONLY); + if (fd < 0) + goto error; + + rc = write(fd, buffer, length); + if (rc < (int) length) + goto error; + + rc = 0; + goto complete; + +error: + rc = -1; + +complete: + if (fd >= 0) + close(fd); + + return rc; +} + +char *data2string(const void *data, size_t size) +{ + char *string; + size_t length; + char *p; + size_t i; + + if (data == NULL || size == 0) + return NULL; + + length = size * 2 + 1; + string = (char *) calloc(1, length); + + p = string; + + for (i = 0; i < size; i++) { + sprintf(p, "%02x", *((unsigned char *) data + i)); + p += 2 * sizeof(char); + } + + return string; +} + +void *string2data(const char *string, size_t *size_p) +{ + void *data; + size_t size; + size_t length; + int shift; + unsigned char *p; + unsigned int b; + size_t i; + int rc; + + if (string == NULL) + return NULL; + + length = strlen(string); + if (length == 0) + return NULL; + + if (length % 2 == 0) { + size = length / 2; + shift = 0; + } else { + size = (length - (length % 2)) / 2 + 1; + shift = 1; + } + + data = calloc(1, size); + + p = (unsigned char *) data; + + for (i = 0; i < length; i++) { + rc = sscanf(&string[i], "%01x", &b); + if (rc < 1) + b = 0; + + if ((shift % 2) == 0) + *p |= ((b & 0x0f) << 4); + else + *p++ |= b & 0x0f; + + shift++; + } + + if (size_p != NULL) + *size_p = size; + + return data; +} + +// vim:ts=4:sw=4:expandtab -- cgit v1.1