Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2014-2018 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16 #include <string.h>
vithyat 0:977e87915078 17 #include <stdlib.h>
vithyat 0:977e87915078 18 #include <stdint.h>
vithyat 0:977e87915078 19 #include "common_functions.h"
vithyat 0:977e87915078 20 #include "ip4string.h"
vithyat 0:977e87915078 21
vithyat 0:977e87915078 22 /**
vithyat 0:977e87915078 23 * Convert numeric IPv4 address string to a binary.
vithyat 0:977e87915078 24 * \param ip4addr IPv4 address in string format.
vithyat 0:977e87915078 25 * \param len Length of IPv4 string, maximum of 16..
vithyat 0:977e87915078 26 * \param dest buffer for address. MUST be 4 bytes.
vithyat 0:977e87915078 27 * \return boolean set to true if conversion succeded, false if it didn't
vithyat 0:977e87915078 28 */
vithyat 0:977e87915078 29 bool stoip4(const char *ip4addr, size_t len, void *dest)
vithyat 0:977e87915078 30 {
vithyat 0:977e87915078 31 uint8_t *addr = dest;
vithyat 0:977e87915078 32
vithyat 0:977e87915078 33 if (len > 16) { // Too long, not possible
vithyat 0:977e87915078 34 return false;
vithyat 0:977e87915078 35 }
vithyat 0:977e87915078 36
vithyat 0:977e87915078 37 uint_fast8_t stringLength = 0, byteCount = 0;
vithyat 0:977e87915078 38
vithyat 0:977e87915078 39 //Iterate over each component of the IP. The exit condition is in the middle of the loop
vithyat 0:977e87915078 40 while (true) {
vithyat 0:977e87915078 41
vithyat 0:977e87915078 42 //No valid character (IPv4 addresses don't have implicit 0, that is x.y..z being read as x.y.0.z)
vithyat 0:977e87915078 43 if (stringLength == len || ip4addr[stringLength] < '0' || ip4addr[stringLength] > '9') {
vithyat 0:977e87915078 44 return false;
vithyat 0:977e87915078 45 }
vithyat 0:977e87915078 46
vithyat 0:977e87915078 47 //For each component, we convert it to the raw value
vithyat 0:977e87915078 48 uint_fast16_t byte = 0;
vithyat 0:977e87915078 49 while (stringLength < len && ip4addr[stringLength] >= '0' && ip4addr[stringLength] <= '9') {
vithyat 0:977e87915078 50 byte *= 10;
vithyat 0:977e87915078 51 byte += ip4addr[stringLength++] - '0';
vithyat 0:977e87915078 52
vithyat 0:977e87915078 53 //We go over the maximum value for an IPv4 component
vithyat 0:977e87915078 54 if (byte > 0xff) {
vithyat 0:977e87915078 55 return false;
vithyat 0:977e87915078 56 }
vithyat 0:977e87915078 57 }
vithyat 0:977e87915078 58
vithyat 0:977e87915078 59 //Append the component
vithyat 0:977e87915078 60 addr[byteCount++] = (uint8_t) byte;
vithyat 0:977e87915078 61
vithyat 0:977e87915078 62 //If we're at the end, we leave the loop. It's the only way to reach the `true` output
vithyat 0:977e87915078 63 if (byteCount == 4) {
vithyat 0:977e87915078 64 break;
vithyat 0:977e87915078 65 }
vithyat 0:977e87915078 66
vithyat 0:977e87915078 67 //If the next character is invalid, we return false
vithyat 0:977e87915078 68 if (stringLength == len || ip4addr[stringLength++] != '.') {
vithyat 0:977e87915078 69 return false;
vithyat 0:977e87915078 70 }
vithyat 0:977e87915078 71 }
vithyat 0:977e87915078 72
vithyat 0:977e87915078 73 return stringLength == len || ip4addr[stringLength] == '\0';
vithyat 0:977e87915078 74 }