/*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <config.h>
#include "csum.h"
+#include "unaligned.h"
/* Returns the IP checksum of the 'n' bytes in 'data'.
*
{
const uint16_t *data = data_;
- for (; n > 1; n -= 2) {
- partial = csum_add16(partial, *data++);
+ for (; n > 1; n -= 2, data++) {
+ partial = csum_add16(partial, get_unaligned_u16(data));
}
if (n) {
partial += *(uint8_t *) data;
#include <stdlib.h>
#include <string.h>
#include "random.h"
+#include "unaligned.h"
#include "util.h"
#undef NDEBUG
/* Test csum_add16(). */
partial = 0;
for (i = 0; i < tc->size / 2; i++) {
- partial = csum_add16(partial, data16[i]);
+ partial = csum_add16(partial, get_unaligned_u16(&data16[i]));
}
assert(ntohs(csum_finish(partial)) == tc->csum);
mark('.');
/* Test csum_add32(). */
partial = 0;
for (i = 0; i < tc->size / 4; i++) {
- partial = csum_add32(partial, data32[i]);
+ partial = csum_add32(partial, get_unaligned_u32(&data32[i]));
}
assert(ntohs(csum_finish(partial)) == tc->csum);
mark('.');
partial = 0;
for (i = 0; i < tc->size / 4; i++) {
if (i % 2) {
- partial = csum_add32(partial, data32[i]);
+ partial = csum_add32(partial, get_unaligned_u32(&data32[i]));
} else {
- partial = csum_add16(partial, data16[i * 2]);
- partial = csum_add16(partial, data16[i * 2 + 1]);
+ uint16_t u0 = get_unaligned_u16(&data16[i * 2]);
+ uint16_t u1 = get_unaligned_u16(&data16[i * 2 + 1]);
+ partial = csum_add16(partial, u0);
+ partial = csum_add16(partial, u1);
}
}
assert(ntohs(csum_finish(partial)) == tc->csum);