aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-12-27 11:18:57 -0600
committerRob Landley <rob@landley.net>2018-12-27 11:18:57 -0600
commite5066ba5acc4d7044c7637fc04a51b349108ea0d (patch)
treef9a5e1ecab60876e1ed6ad6551e506dab91e3ea2 /lib
parent831266c06862c1be6195382a02b86cb52ef304dc (diff)
downloadtoybox-e5066ba5acc4d7044c7637fc04a51b349108ea0d.tar.gz
Make poke() alignment-agnostic.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c36
-rw-r--r--lib/lib.h4
2 files changed, 25 insertions, 15 deletions
diff --git a/lib/lib.c b/lib/lib.c
index ad85dc63..2ba009f9 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -586,26 +586,34 @@ int64_t peek_be(void *ptr, unsigned size)
int64_t peek(void *ptr, unsigned size)
{
- return IS_BIG_ENDIAN ? peek_be(ptr, size) : peek_le(ptr, size);
+ return (IS_BIG_ENDIAN ? peek_be : peek_le)(ptr, size);
}
-void poke(void *ptr, uint64_t val, int size)
+void poke_le(void *ptr, long long val, unsigned size)
{
- if (size & 8) {
- volatile uint64_t *p = (uint64_t *)ptr;
- *p = val;
- } else if (size & 4) {
- volatile int *p = (int *)ptr;
- *p = val;
- } else if (size & 2) {
- volatile short *p = (short *)ptr;
- *p = val;
- } else {
- volatile char *p = (char *)ptr;
- *p = val;
+ char *c = ptr;
+
+ while (size--) {
+ *c++ = val&255;
+ val >>= 8;
}
}
+void poke_be(void *ptr, long long val, unsigned size)
+{
+ char *c = ptr + size;
+
+ while (size--) {
+ *--c = val&255;
+ val >>=8;
+ }
+}
+
+void poke(void *ptr, long long val, unsigned size)
+{
+ (IS_BIG_ENDIAN ? poke_be : poke_le)(ptr, val, size);
+}
+
// Iterate through an array of files, opening each one and calling a function
// on that filehandle and name. The special filename "-" means stdin if
// flags is O_RDONLY, stdout otherwise. An empty argument list calls
diff --git a/lib/lib.h b/lib/lib.h
index 10308ff6..bc54a384 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -209,7 +209,9 @@ int highest_bit(unsigned long l);
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);
+void poke_le(void *ptr, long long val, unsigned size);
+void poke_be(void *ptr, long long val, unsigned size);
+void poke(void *ptr, long long val, unsigned size);
struct string_list *find_in_path(char *path, char *filename);
long long estrtol(char *str, char **end, int base);
long long xstrtol(char *str, char **end, int base);