Fixed misaligned reading and writing.

error: load of misaligned address 0x62500de0ab11 for type 'uint16_t',
    which requires 2 byte alignment.

Found by UndefinedBehaviorSanitizer.
diff --git a/auto/clang b/auto/clang
index a7b4023..4f4d1fe 100644
--- a/auto/clang
+++ b/auto/clang
@@ -129,6 +129,19 @@
 . auto/feature
 
 
+njs_feature="GCC __attribute__ packed"
+njs_feature_name=NJS_HAVE_GCC_ATTRIBUTE_PACKED
+njs_feature_run=no
+njs_feature_path=
+njs_feature_libs=
+njs_feature_test="struct __attribute__((packed)) s { char v; };
+
+                  int main(void) {
+                    return 0;
+                  }"
+. auto/feature
+
+
 njs_feature="Address sanitizer"
 njs_feature_name=NJS_HAVE_ADDRESS_SANITIZER
 njs_feature_run=no
diff --git a/src/njs_buffer.c b/src/njs_buffer.c
index f4d1f31..82c701d 100644
--- a/src/njs_buffer.c
+++ b/src/njs_buffer.c
@@ -989,7 +989,7 @@
         break;
 
     case 2:
-        u32 = *((uint16_t *) u8);
+        u32 = njs_get_u16(u8);
 
         if (swap) {
             u32 = njs_bswap_u16(u32);
@@ -1028,7 +1028,7 @@
         break;
 
     case 4:
-        u32 = *((uint32_t *) u8);
+        u32 = njs_get_u32(u8);
 
         if (swap) {
             u32 = njs_bswap_u32(u32);
@@ -1273,7 +1273,7 @@
             u32 = njs_bswap_u16(u32);
         }
 
-        *((uint16_t *) u8) = u32;
+        njs_set_u16(u8, u32);
         break;
 
     case 3:
@@ -1299,7 +1299,7 @@
             u32 = njs_bswap_u32(u32);
         }
 
-        *((uint32_t *) u8) = u32;
+        njs_set_u32(u8, u32);
         break;
 
     case 5:
@@ -2153,14 +2153,14 @@
     switch (size) {
     case 2:
         for (; p < end; p += 2) {
-            *((uint16_t *) p) = njs_bswap_u16(*((uint16_t *) p));
+            njs_set_u16(p, njs_bswap_u16(njs_get_u16(p)));
         }
 
         break;
 
     case 4:
         for (; p < end; p += 4) {
-            *((uint32_t *) p) = njs_bswap_u32(*((uint32_t *) p));
+            njs_set_u32(p, njs_bswap_u32(njs_get_u32(p)));
         }
 
         break;
@@ -2168,7 +2168,7 @@
     case 8:
     default:
         for (; p < end; p += 8) {
-            *((uint64_t *) p) = njs_bswap_u64(*((uint64_t *) p));
+            njs_set_u64(p, njs_bswap_u64(njs_get_u64(p)));
         }
     }
 
diff --git a/src/njs_clang.h b/src/njs_clang.h
index a2d60e9..a13e13d 100644
--- a/src/njs_clang.h
+++ b/src/njs_clang.h
@@ -138,6 +138,14 @@
 #endif
 
 
+#if (NJS_HAVE_GCC_ATTRIBUTE_PACKED)
+#define NJS_PACKED    __attribute__((packed))
+
+#else
+#define NJS_PACKED
+#endif
+
+
 #if (NJS_HAVE_GCC_ATTRIBUTE_MALLOC)
 #define NJS_MALLOC_LIKE    __attribute__((__malloc__))
 
diff --git a/src/njs_typed_array.c b/src/njs_typed_array.c
index 4899454..7fb6cf1 100644
--- a/src/njs_typed_array.c
+++ b/src/njs_typed_array.c
@@ -2736,7 +2736,7 @@
         break;
 
     case NJS_OBJ_TYPE_UINT16_ARRAY:
-        u32 = *((uint16_t *) u8);
+        u32 = njs_get_u16(u8);
 
         if (swap) {
             u32 = njs_bswap_u16(u32);
@@ -2746,7 +2746,7 @@
         break;
 
     case NJS_OBJ_TYPE_INT16_ARRAY:
-        u32 = *((uint16_t *) u8);
+        u32 = njs_get_u16(u8);
 
         if (swap) {
             u32 = njs_bswap_u16(u32);
@@ -2758,7 +2758,7 @@
     case NJS_OBJ_TYPE_UINT32_ARRAY:
     case NJS_OBJ_TYPE_INT32_ARRAY:
     case NJS_OBJ_TYPE_FLOAT32_ARRAY:
-        u32 = *((uint32_t *) u8);
+        u32 = njs_get_u32(u8);
 
         if (swap) {
             u32 = njs_bswap_u32(u32);
@@ -2783,7 +2783,7 @@
     default:
         /* NJS_OBJ_TYPE_FLOAT64_ARRAY. */
 
-        conv_f64.u = *((uint64_t *) u8);
+        conv_f64.u = njs_get_u64(u8);
 
         if (swap) {
             conv_f64.u = njs_bswap_u64(conv_f64.u);
@@ -2865,7 +2865,7 @@
             u32 = njs_bswap_u16(u32);
         }
 
-        *((uint16_t *) u8) = u32;
+        njs_set_u16(u8, u32);
         break;
 
     case NJS_OBJ_TYPE_UINT32_ARRAY:
@@ -2876,7 +2876,7 @@
             u32 = njs_bswap_u32(u32);
         }
 
-        *((uint32_t *) u8) = u32;
+        njs_set_u32(u8, u32);
         break;
 
     case NJS_OBJ_TYPE_FLOAT32_ARRAY:
@@ -2886,7 +2886,7 @@
             conv_f32.u = njs_bswap_u32(conv_f32.u);
         }
 
-        *((uint32_t *) u8) = conv_f32.u;
+        njs_set_u32(u8, conv_f32.u);
         break;
 
     default:
@@ -2898,7 +2898,7 @@
             conv_f64.u = njs_bswap_u64(conv_f64.u);
         }
 
-        *((uint64_t *) u8) = conv_f64.u;
+        njs_set_u64(u8, conv_f64.u);
     }
 
     njs_set_undefined(&vm->retval);
diff --git a/src/njs_utils.h b/src/njs_utils.h
index c729ca1..547ad85 100644
--- a/src/njs_utils.h
+++ b/src/njs_utils.h
@@ -20,6 +20,19 @@
 } njs_conv_f64_t;
 
 
+struct NJS_PACKED njs_packed_u16_t {
+    uint16_t v;
+};
+
+struct NJS_PACKED njs_packed_u32_t {
+    uint32_t v;
+};
+
+struct NJS_PACKED njs_packed_u64_t {
+    uint64_t v;
+};
+
+
 typedef int (*njs_sort_cmp_t)(const void *, const void *, void *ctx);
 
 void njs_qsort(void *base, size_t n, size_t size, njs_sort_cmp_t cmp,
@@ -115,5 +128,41 @@
            | ((u64 & 0x00000000000000ffULL) << 56);
 }
 
+njs_inline uint16_t
+njs_get_u16(const uint8_t *p)
+{
+    return ((const struct njs_packed_u16_t *) p)->v;
+}
+
+njs_inline uint32_t
+njs_get_u32(const uint8_t *p)
+{
+    return ((const struct njs_packed_u32_t *) p)->v;
+}
+
+njs_inline uint64_t
+njs_get_u64(const uint8_t *p)
+{
+    return ((const struct njs_packed_u64_t *) p)->v;
+}
+
+njs_inline void
+njs_set_u16(uint8_t *p, uint16_t val)
+{
+    ((struct njs_packed_u16_t *) p)->v = val;
+}
+
+njs_inline void
+njs_set_u32(uint8_t *p, uint32_t val)
+{
+    ((struct njs_packed_u32_t *) p)->v = val;
+}
+
+njs_inline void
+njs_set_u64(uint8_t *p, uint64_t val)
+{
+    ((struct njs_packed_u64_t *) p)->v = val;
+}
+
 
 #endif /* _NJS_UTILS_H_INCLUDED_ */