Debian Codegraph @

src/analyzer/main.c

raw symbols SCIP
1 /*
2 mbed CMSIS-DAP debugger
3 Copyright (c) 2006-2015 ARM Limited
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */
17
18 #include<stdint.h>
19
20 typedef struct {
21 uint16_t size;
22 uint16_t addr;
23 } sector_info_t;
24
25 static uint32_t crc32_tab[256];
26
27 static uint32_t crc32(uint32_t crc, const void *buf, uint32_t size) {
28 const uint8_t *p;
29
30 p = buf;
31 crc = crc ^ ~0U;
32
33 while (size--) {
34 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
35 }
36
37 return crc ^ ~0U;
38 }
39
40 static void fill_table(void) {
41 int i;
42 uint32_t byte, crc, mask;
43 for (byte = 0; byte <= 255; byte++) {
44 crc = byte;
45 for (i = 7; i >= 0; i--) { // Do eight times.
46 mask = -(crc & 1);
47 crc = (crc >> 1) ^ (0xEDB88320 & mask);
48 }
49 crc32_tab[byte] = crc;
50 }
51 }
52
53 int compute_crc(void * data, uint32_t num) {
54 sector_info_t * sectors = data;
55 uint32_t * crcs = data;
56 uint32_t i;
57 uint32_t crc;
58 uint32_t addr;
59 uint32_t size;
60 fill_table();
61 for (i = 0; i < num; i++) {
62 size = 1 << sectors[i].size;
63 addr = size * sectors[i].addr;
64 crc = crc32(0, (void*)addr, size);
65 crcs[i] = crc;
66 }
67 size = 1 << num;
68 return 0;
69 }