aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-07-21 19:56:53 -0500
committerRob Landley <rob@landley.net>2014-07-21 19:56:53 -0500
commit546b293cb9369c9c421981c71577af29d83b925a (patch)
treec52ed4a4231229f8f0e417b1a960d2b0585f7dec /lib
parent280bb5942526c06211185144a5cdb4c5d05aa2f6 (diff)
downloadtoybox-546b293cb9369c9c421981c71577af29d83b925a.tar.gz
Little endian and big endian versions of peek (for host.c).
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c38
-rw-r--r--lib/lib.h4
2 files changed, 27 insertions, 15 deletions
diff --git a/lib/lib.c b/lib/lib.c
index bda69931..e16873f0 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -372,21 +372,31 @@ void msleep(long miliseconds)
nanosleep(&ts, &ts);
}
-int64_t peek(void *ptr, int size)
+// Inefficient, but deals with unaligned access
+int64_t peek_le(void *ptr, unsigned size)
{
- if (size & 8) {
- volatile int64_t *p = (int64_t *)ptr;
- return *p;
- } else if (size & 4) {
- volatile int *p = (int *)ptr;
- return *p;
- } else if (size & 2) {
- volatile short *p = (short *)ptr;
- return *p;
- } else {
- volatile char *p = (char *)ptr;
- return *p;
- }
+ int64_t ret = 0;
+ char *c = ptr;
+ int i;
+
+ for (i=0; i<size; i++) ret |= ((int64_t)c[i])<<i;
+
+ return ret;
+}
+
+int64_t peek_be(void *ptr, unsigned size)
+{
+ int64_t ret = 0;
+ char *c = ptr;
+
+ while (size--) ret = (ret<<8)|c[size];
+
+ return ret;
+}
+
+int64_t peek(void *ptr, unsigned size)
+{
+ return IS_BIG_ENDIAN ? peek_be(ptr, size) : peek_le(ptr, size);
}
void poke(void *ptr, uint64_t val, int size)
diff --git a/lib/lib.h b/lib/lib.h
index 9deacf22..11a1018c 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -138,7 +138,9 @@ int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
struct string_list **splitpath(char *path, struct string_list **list);
char *readfile(char *name, char *buf, off_t len);
void msleep(long miliseconds);
-int64_t peek(void *ptr, int size);
+int64_t peek_le(void *ptr, unsigned size);
+int64_t peek_be(void *ptr, unsigned size);
+int64_t peek(void *ptr, unsigned size);
void poke(void *ptr, uint64_t val, int size);
struct string_list *find_in_path(char *path, char *filename);
long atolx(char *c);