aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorAlfredo Cardigliano <cardigliano@ntop.org>2017-05-23 18:02:53 +0200
committerAlfredo Cardigliano <cardigliano@ntop.org>2017-05-23 18:02:53 +0200
commit26bd42a71c249575837021d5d8a6ae4c5bfa57b6 (patch)
tree68e7d4c121c7a198bb866cb3fe2f3caad1e0ea1e /example
parent601f7f59ff2ae016970a796c6314f55423ed7fe9 (diff)
crc32 fix
Diffstat (limited to 'example')
-rw-r--r--example/ndpiReader.c7
-rw-r--r--example/ndpi_util.c34
-rw-r--r--example/ndpi_util.h2
3 files changed, 21 insertions, 22 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index a1fa6f318..6aed8ec88 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -1788,7 +1788,6 @@ static pcap_t * openPcapFileOrDevice(u_int16_t thread_id, const u_char * pcap_fi
return pcap_handle;
}
-
/**
* @brief Check pcap packet
*/
@@ -1868,9 +1867,9 @@ static void pcap_process_packet(u_char *args,
trailer->master_protocol = htons(p.master_protocol), trailer->app_protocol = htons(p.app_protocol);
ndpi_protocol2name(ndpi_thread_info[thread_id].workflow->ndpi_struct, p, trailer->name, sizeof(trailer->name));
crc = (uint32_t*)&extcap_buf[h.caplen+sizeof(struct ndpi_packet_trailer)];
- *crc = 0;
- ethernet_crc32((const void*)extcap_buf, h.caplen+sizeof(struct ndpi_packet_trailer), crc);
- h.caplen += delta, h.len += delta;
+ *crc = ethernet_crc32((const void*)extcap_buf, h.caplen+sizeof(struct ndpi_packet_trailer));
+ h.caplen += delta;
+ h.len += delta;
#ifdef DEBUG_TRACE
if(trace) fprintf(trace, "Dumping %u bytes packet\n", h.caplen);
diff --git a/example/ndpi_util.c b/example/ndpi_util.c
index d6836176a..1e7e498d0 100644
--- a/example/ndpi_util.c
+++ b/example/ndpi_util.c
@@ -884,10 +884,8 @@ struct ndpi_proto ndpi_workflow_process_packet (struct ndpi_workflow * workflow,
/* ********************************************************** */
static uint32_t crc32_for_byte(uint32_t r) {
- int j;
-
- for(j = 0; j < 8; ++j)
- r = ((r & 1) ? 0 : ((uint32_t)0xEDB88320L) ^ r >> 1);
+ for(int j = 0; j < 8; ++j)
+ r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
return r ^ (uint32_t)0xFF000000L;
}
@@ -897,31 +895,33 @@ static uint32_t crc32_for_byte(uint32_t r) {
typedef unsigned long accum_t;
static void init_tables(uint32_t* table, uint32_t* wtable) {
- size_t i, k, w, j;
-
- for(i = 0; i < 0x100; ++i)
+ for(size_t i = 0; i < 0x100; ++i)
table[i] = crc32_for_byte(i);
- for(k = 0; k < sizeof(accum_t); ++k)
- for(i = 0; i < 0x100; ++i) {
- for(j = w = 0; j < sizeof(accum_t); ++j)
+ for(size_t k = 0; k < sizeof(accum_t); ++k)
+ for(size_t w, i = 0; i < 0x100; ++i) {
+ for(size_t j = w = 0; j < sizeof(accum_t); ++j)
w = table[(uint8_t)(j == k? w ^ i: w)] ^ w >> 8;
wtable[(k << 8) + i] = w ^ (k? wtable[0]: 0);
}
}
-void ethernet_crc32(const void* data, size_t n_bytes, uint32_t* crc) {
+static void __crc32(const void* data, size_t n_bytes, uint32_t* crc) {
static uint32_t table[0x100], wtable[0x100*sizeof(accum_t)];
size_t n_accum = n_bytes/sizeof(accum_t);
- size_t i, j;
-
if(!*table)
init_tables(table, wtable);
- for(i = 0; i < n_accum; ++i) {
+ for(size_t i = 0; i < n_accum; ++i) {
accum_t a = *crc ^ ((accum_t*)data)[i];
- for(j = *crc = 0; j < sizeof(accum_t); ++j)
+ for(size_t j = *crc = 0; j < sizeof(accum_t); ++j)
*crc ^= wtable[(j << 8) + (uint8_t)(a >> 8*j)];
}
-
- for(i = n_accum*sizeof(accum_t); i < n_bytes; ++i)
+ for(size_t i = n_accum*sizeof(accum_t); i < n_bytes; ++i)
*crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
}
+
+u_int32_t ethernet_crc32(const void* data, size_t n_bytes) {
+ u_int32_t crc = 0;
+ __crc32(data, n_bytes, &crc);
+ return crc;
+}
+
diff --git a/example/ndpi_util.h b/example/ndpi_util.h
index e8d301acb..d7fdd9928 100644
--- a/example/ndpi_util.h
+++ b/example/ndpi_util.h
@@ -165,6 +165,6 @@ static inline void ndpi_workflow_set_flow_giveup_callback(struct ndpi_workflow *
/* compare two nodes in workflow */
int ndpi_workflow_node_cmp(const void *a, const void *b);
void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_flow_info *flow);
-void ethernet_crc32(const void* data, size_t n_bytes, uint32_t* crc);
+u_int32_t ethernet_crc32(const void* data, size_t n_bytes);
void ndpi_flow_info_freer(void *node);
#endif