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 --- Android.mk | 2 +- samsung-ipc/Makefile.am | 4 +- samsung-ipc/devices/aries/aries_ipc.c | 4 +- samsung-ipc/devices/crespo/crespo_ipc.c | 4 +- samsung-ipc/devices/maguro/maguro_ipc.c | 4 +- samsung-ipc/devices/xmm6160/xmm6160.c | 4 +- samsung-ipc/devices/xmm6260/xmm6260_hsic.c | 4 +- samsung-ipc/devices/xmm6260/xmm6260_mipi.c | 4 +- samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c | 2 +- samsung-ipc/rfs.c | 14 +- samsung-ipc/util.c | 280 ---------------- samsung-ipc/util.h | 35 -- samsung-ipc/utils.c | 407 ++++++++++++++++++++++++ samsung-ipc/utils.h | 39 +++ 14 files changed, 469 insertions(+), 338 deletions(-) delete mode 100644 samsung-ipc/util.c delete mode 100644 samsung-ipc/util.h create mode 100644 samsung-ipc/utils.c create mode 100644 samsung-ipc/utils.h diff --git a/Android.mk b/Android.mk index 4373b6d..8b6f445 100644 --- a/Android.mk +++ b/Android.mk @@ -63,7 +63,7 @@ LOCAL_SRC_FILES := \ samsung-ipc/devices/piranha/piranha_ipc.c \ samsung-ipc/devices/i9300/i9300_ipc.c \ samsung-ipc/devices/n7100/n7100_ipc.c \ - samsung-ipc/util.c \ + samsung-ipc/utils.c \ samsung-ipc/call.c \ samsung-ipc/sms.c \ samsung-ipc/sec.c \ diff --git a/samsung-ipc/Makefile.am b/samsung-ipc/Makefile.am index 3a93959..4c0ba02 100644 --- a/samsung-ipc/Makefile.am +++ b/samsung-ipc/Makefile.am @@ -52,8 +52,8 @@ libsamsung_ipc_la_SOURCES = \ devices/i9300/i9300_ipc.h \ devices/n7100/n7100_ipc.c \ devices/n7100/n7100_ipc.h \ - util.c \ - util.h \ + utils.c \ + utils.h \ call.c \ sms.c \ sec.c \ diff --git a/samsung-ipc/devices/aries/aries_ipc.c b/samsung-ipc/devices/aries/aries_ipc.c index 40e03d3..aede718 100644 --- a/samsung-ipc/devices/aries/aries_ipc.c +++ b/samsung-ipc/devices/aries/aries_ipc.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include "onedram.h" #include "phonet.h" @@ -64,7 +64,7 @@ int aries_ipc_bootstrap(struct ipc_client *client) ipc_client_log(client, "Starting aries modem bootstrap"); - modem_image_data = file_data_read(ARIES_MODEM_IMAGE_DEVICE, ARIES_MODEM_IMAGE_SIZE, 0x1000); + modem_image_data = file_data_read(ARIES_MODEM_IMAGE_DEVICE, ARIES_MODEM_IMAGE_SIZE, 0x1000, 0); if (modem_image_data == NULL) { ipc_client_log(client, "Reading modem image data failed"); goto error; diff --git a/samsung-ipc/devices/crespo/crespo_ipc.c b/samsung-ipc/devices/crespo/crespo_ipc.c index b29c4fc..94dc695 100644 --- a/samsung-ipc/devices/crespo/crespo_ipc.c +++ b/samsung-ipc/devices/crespo/crespo_ipc.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include "crespo_modem_ctl.h" @@ -51,7 +51,7 @@ int crespo_ipc_bootstrap(struct ipc_client *client) ipc_client_log(client, "Starting crespo modem bootstrap"); - modem_image_data = file_data_read(CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000); + modem_image_data = file_data_read(CRESPO_MODEM_IMAGE_DEVICE, CRESPO_MODEM_IMAGE_SIZE, 0x1000, 0); if (modem_image_data == NULL) { ipc_client_log(client, "Reading modem image data failed"); goto error; diff --git a/samsung-ipc/devices/maguro/maguro_ipc.c b/samsung-ipc/devices/maguro/maguro_ipc.c index 15c72ac..632cd3b 100644 --- a/samsung-ipc/devices/maguro/maguro_ipc.c +++ b/samsung-ipc/devices/maguro/maguro_ipc.c @@ -26,7 +26,7 @@ #include #include -#include +#include #include "xmm6260.h" #include "xmm6260_mipi.h" @@ -142,7 +142,7 @@ int maguro_ipc_bootstrap(struct ipc_client *client) } ipc_client_log(client, "Sent XMM6260 MIPI nv_data"); - mps_data = file_data_read(MAGURO_MPS_DATA_DEVICE, MAGURO_MPS_DATA_SIZE, MAGURO_MPS_DATA_SIZE); + mps_data = file_data_read(MAGURO_MPS_DATA_DEVICE, MAGURO_MPS_DATA_SIZE, MAGURO_MPS_DATA_SIZE, 0); if (mps_data == NULL) { ipc_client_log(client, "Reading MPS data failed"); goto error; diff --git a/samsung-ipc/devices/xmm6160/xmm6160.c b/samsung-ipc/devices/xmm6160/xmm6160.c index 2af6837..f0aa05b 100644 --- a/samsung-ipc/devices/xmm6160/xmm6160.c +++ b/samsung-ipc/devices/xmm6160/xmm6160.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include "xmm6160.h" @@ -252,7 +252,7 @@ int xmm6160_nv_data_send(struct ipc_client *client, int device_fd, } ipc_client_log(client, "Checked nv_data md5"); - nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); if (nv_data == NULL) { ipc_client_log(client, "Reading nv_data failed"); goto error; diff --git a/samsung-ipc/devices/xmm6260/xmm6260_hsic.c b/samsung-ipc/devices/xmm6260/xmm6260_hsic.c index ed3b453..d53ff11 100644 --- a/samsung-ipc/devices/xmm6260/xmm6260_hsic.c +++ b/samsung-ipc/devices/xmm6260/xmm6260_hsic.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include "xmm6260.h" #include "xmm6260_hsic.h" @@ -561,7 +561,7 @@ int xmm6260_hsic_nv_data_send(struct ipc_client *client, int device_fd) } ipc_client_log(client, "Checked nv_data md5"); - nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); if (nv_data == NULL) { ipc_client_log(client, "Reading nv_data failed"); goto error; diff --git a/samsung-ipc/devices/xmm6260/xmm6260_mipi.c b/samsung-ipc/devices/xmm6260/xmm6260_mipi.c index 4f11361..06a2b90 100644 --- a/samsung-ipc/devices/xmm6260/xmm6260_mipi.c +++ b/samsung-ipc/devices/xmm6260/xmm6260_mipi.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include "xmm6260.h" #include "xmm6260_mipi.h" @@ -611,7 +611,7 @@ int xmm6260_mipi_nv_data_send(struct ipc_client *client, int device_fd) } ipc_client_log(client, "Checked nv_data md5"); - nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + nv_data = file_data_read(ipc_client_nv_data_path(client), ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); if (nv_data == NULL) { ipc_client_log(client, "Reading nv_data failed"); goto error; diff --git a/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c b/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c index d3ddfae..e7b89b3 100644 --- a/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c +++ b/samsung-ipc/devices/xmm6260/xmm6260_sec_modem.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include "modem.h" #include "modem_prj.h" diff --git a/samsung-ipc/rfs.c b/samsung-ipc/rfs.c index a386775..5b56e6c 100644 --- a/samsung-ipc/rfs.c +++ b/samsung-ipc/rfs.c @@ -31,7 +31,7 @@ #include #include "ipc.h" -#include "util.h" +#include "utils.h" #define MD5_STRING_SIZE MD5_DIGEST_LENGTH * 2 + 1 @@ -79,7 +79,7 @@ void nv_data_md5_generate(struct ipc_client *client) ipc_client_log(client, "nv_data_md5_generate: generating MD5 hash"); nv_data_p = file_data_read(ipc_client_nv_data_path(client), - ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); nv_data_md5_compute(nv_data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash); free(nv_data_p); @@ -165,7 +165,7 @@ void nv_data_backup_create(struct ipc_client *client) /* Read the content of the backup file. */ nv_data_p = file_data_read(ipc_client_nv_data_path(client), - ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); /* Compute the backup file MD5 hash. */ nv_data_md5_compute(nv_data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash); @@ -262,7 +262,7 @@ nv_data_backup_create_write: /* Read the newly-written .nv_data.bak. */ nv_data_bak_p = file_data_read(ipc_client_nv_data_backup_path(client), - ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); /* Compute the MD5 hash for nv_data.bin. */ nv_data_md5_compute(nv_data_bak_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash); @@ -362,7 +362,7 @@ void nv_data_backup_restore(struct ipc_client *client) /* Read the content of the backup file. */ nv_data_bak_p = file_data_read(ipc_client_nv_data_backup_path(client), - ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); /* Compute the backup file MD5 hash. */ nv_data_md5_compute(nv_data_bak_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash); @@ -454,7 +454,7 @@ nv_data_backup_restore_write: /* Read the newly-written nv_data.bin. */ nv_data_p = file_data_read(ipc_client_nv_data_path(client), - ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); /* Compute the MD5 hash for nv_data.bin. */ nv_data_md5_compute(nv_data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash); @@ -564,7 +564,7 @@ int nv_data_md5_check(struct ipc_client *client) memset(nv_data_md5_hash_string, 0, MD5_STRING_SIZE); nv_data_p = file_data_read(ipc_client_nv_data_path(client), - ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client)); + ipc_client_nv_data_size(client), ipc_client_nv_data_chunk_size(client), 0); data_p = nv_data_p; nv_data_md5_compute(data_p, ipc_client_nv_data_size(client), ipc_client_nv_data_secret(client), nv_data_md5_hash); diff --git a/samsung-ipc/util.c b/samsung-ipc/util.c deleted file mode 100644 index 3dca3b4..0000000 --- a/samsung-ipc/util.c +++ /dev/null @@ -1,280 +0,0 @@ -/* - * This file is part of libsamsung-ipc. - * - * Copyright (C) 2013 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 "util.h" - -void *file_data_read(char *path, int size, int chunk) -{ - void *data = NULL; - int fd = -1; - - unsigned char *p; - int count; - int rc; - - if (path == NULL || size <= 0 || chunk <= 0) - return NULL; - - fd = open(path, O_RDONLY); - if (fd < 0) - goto error; - - data = malloc(size); - memset(data, 0, size); - - p = (unsigned char *) data; - - count = 0; - while (count < size) { - rc = read(fd, p, size - count > chunk ? chunk : 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 network_iface_up(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(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(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(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; -} - -int sysfs_string_read(char *path, char *buffer, int length) -{ - int fd = -1; - int rc; - - if (path == NULL || buffer == NULL || length <= 0) - return -1; - - fd = open(path, O_RDONLY); - if (fd < 0) - goto error; - - rc = read(fd, buffer, length); - if (rc <= 0) - goto error; - - rc = 0; - goto complete; - -error: - rc = -1; - -complete: - if (fd >= 0) - close(fd); - - return rc; -} - -int sysfs_string_write(char *path, char *buffer, int 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 <= 0) - goto error; - - rc = 0; - goto complete; - -error: - rc = -1; - -complete: - if (fd >= 0) - close(fd); - - return rc; -} - -// vim:ts=4:sw=4:expandtab diff --git a/samsung-ipc/util.h b/samsung-ipc/util.h deleted file mode 100644 index c2a36a1..0000000 --- a/samsung-ipc/util.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of libsamsung-ipc. - * - * Copyright (C) 2013 Paul Kocialkowski - * Copyright (C) 2010-2011 Joerie de Gram - * - * 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 . - * - */ - -#ifndef __UTIL_H__ -#define __UTIL_H__ - -void *file_data_read(char *file_name, int size, int block_size); -int network_iface_up(char *iface, int domain, int type); -int network_iface_down(char *iface, int domain, int type); -int sysfs_value_read(char *path); -int sysfs_value_write(char *path, int value); -int sysfs_string_read(char *path, char *buffer, int length); -int sysfs_string_write(char *path, char *buffer, int length); - -#endif - -// vim:ts=4:sw=4:expandtab 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 diff --git a/samsung-ipc/utils.h b/samsung-ipc/utils.h new file mode 100644 index 0000000..4e52ca6 --- /dev/null +++ b/samsung-ipc/utils.h @@ -0,0 +1,39 @@ +/* + * 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 . + * + */ + +#ifndef __UTIL_H__ +#define __UTIL_H__ + +void *file_data_read(const char *path, size_t size, size_t chunk_size, + size_t offset); +int file_data_write(const char *path, const void *data, size_t size, + size_t chunk_size, size_t offset); +int network_iface_up(const char *iface, int domain, int type); +int network_iface_down(const char *iface, int domain, int type); +int sysfs_value_read(const char *path); +int sysfs_value_write(const char *path, int value); +char *sysfs_string_read(const char *path, size_t length); +int sysfs_string_write(const char *path, const char *buffer, size_t length); +char *data2string(const void *data, size_t size); +void *string2data(const char *string, size_t *size_p); + +#endif + +// vim:ts=4:sw=4:expandtab -- cgit v1.1