|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2023 IMProject Development Team. All rights reserved. |
| 4 | + * Authors: Juraj Ciberlin <[email protected]> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * 3. Neither the name IMProject nor the names of its contributors may be |
| 17 | + * used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +#include "map.h" |
| 36 | + |
| 37 | +#include <string.h> |
| 38 | + |
| 39 | +#define INDEX_NOT_FOUND (-1) |
| 40 | + |
| 41 | +static int32_t |
| 42 | +Map_findIndex(const Map_t* map, const byte_t* key, int32_t size) { |
| 43 | + int32_t index = INDEX_NOT_FOUND; |
| 44 | + for (int32_t i = 0; i < size; ++i) { |
| 45 | + if (memcmp(&map->keys[i * map->key_size], key, (size_t)map->key_size) == 0) { |
| 46 | + index = i; |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + return index; |
| 51 | +} |
| 52 | + |
| 53 | +bool |
| 54 | +Map_initMap(Map_t* map, byte_t* keys, byte_t* values, int32_t key_size, int32_t value_size, |
| 55 | + int32_t max_map_size) { |
| 56 | + bool status = false; |
| 57 | + if ((key_size != 0) && (value_size != 0) && (max_map_size != 0) && (keys != NULL_PTR) && (values != NULL_PTR)) { |
| 58 | + map->keys = keys; |
| 59 | + map->values = values; |
| 60 | + map->key_size = key_size; |
| 61 | + map->value_size = value_size; |
| 62 | + map->max_map_size = max_map_size; |
| 63 | + map->current_size = 0; |
| 64 | + map->initialized = true; |
| 65 | + status = true; |
| 66 | + } |
| 67 | + return status; |
| 68 | +} |
| 69 | + |
| 70 | +bool |
| 71 | +Map_insert(Map_t* map, const byte_t* key, const byte_t* value) { |
| 72 | + bool status = false; |
| 73 | + if ((map != NULL_PTR) && (map->initialized)) { |
| 74 | + int32_t index = Map_findIndex(map, key, map->current_size); |
| 75 | + if (index == INDEX_NOT_FOUND) { |
| 76 | + if (map->current_size != map->max_map_size) { |
| 77 | + /* -E> compliant MC3R1.R19.1 3 Overlap will not happen because there is a check if current map size reached |
| 78 | + * max map size. */ |
| 79 | + // cppcheck-suppress misra-c2012-17.7; return value is not needed in this case |
| 80 | + memcpy(&map->keys[map->current_size * map->key_size], key, (uint32_t)map->key_size); |
| 81 | + /* -E> compliant MC3R1.R19.1 3 Overlap will not happen because there is a check if current map size reached |
| 82 | + * max map size. */ |
| 83 | + // cppcheck-suppress misra-c2012-17.7; return value is not needed in this case |
| 84 | + memcpy(&map->values[map->current_size * map->value_size], value, (uint32_t)map->value_size); |
| 85 | + ++map->current_size; |
| 86 | + status = true; |
| 87 | + } |
| 88 | + } else { |
| 89 | + /* -E> compliant MC3R1.R19.1 3 Overlap will not happen since the current key is the same as the key that is |
| 90 | + * previously inserted in the map. Therefore, new value will be mapped to that key. */ |
| 91 | + // cppcheck-suppress misra-c2012-17.7; return value is not needed in this case |
| 92 | + memcpy(&map->values[index * map->value_size], value, (uint32_t)map->value_size); |
| 93 | + status = true; |
| 94 | + } |
| 95 | + } |
| 96 | + return status; |
| 97 | +} |
| 98 | + |
| 99 | +bool |
| 100 | +Map_getValue(const Map_t* map, const byte_t* key, byte_t* value) { |
| 101 | + bool status = false; |
| 102 | + if ((map != NULL_PTR) && (map->initialized)) { |
| 103 | + int32_t index = Map_findIndex(map, key, map->current_size); |
| 104 | + if (index != INDEX_NOT_FOUND) { |
| 105 | + /* -E> compliant MC3R1.R19.1 3 Overlap will not happen because the map->value_size is the size of the value |
| 106 | + * and every element in map->values has that size */ |
| 107 | + // cppcheck-suppress misra-c2012-17.7; return value is not needed in this case |
| 108 | + memcpy(value, &map->values[index * map->value_size], (uint32_t)map->value_size); |
| 109 | + status = true; |
| 110 | + } |
| 111 | + } |
| 112 | + return status; |
| 113 | +} |
0 commit comments