Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stoip4.c
00001 /* 00002 * Copyright (c) 2014-2018 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include <string.h> 00017 #include <stdlib.h> 00018 #include <stdint.h> 00019 #include "common_functions.h" 00020 #include "ip4string.h" 00021 00022 /** 00023 * Convert numeric IPv4 address string to a binary. 00024 * \param ip4addr IPv4 address in string format. 00025 * \param len Length of IPv4 string, maximum of 16.. 00026 * \param dest buffer for address. MUST be 4 bytes. 00027 * \return boolean set to true if conversion succeded, false if it didn't 00028 */ 00029 bool stoip4(const char *ip4addr, size_t len, void *dest) 00030 { 00031 uint8_t *addr = dest; 00032 00033 if (len > 16) { // Too long, not possible 00034 return false; 00035 } 00036 00037 uint_fast8_t stringLength = 0, byteCount = 0; 00038 00039 //Iterate over each component of the IP. The exit condition is in the middle of the loop 00040 while (true) { 00041 00042 //No valid character (IPv4 addresses don't have implicit 0, that is x.y..z being read as x.y.0.z) 00043 if (stringLength == len || ip4addr[stringLength] < '0' || ip4addr[stringLength] > '9') { 00044 return false; 00045 } 00046 00047 //For each component, we convert it to the raw value 00048 uint_fast16_t byte = 0; 00049 while (stringLength < len && ip4addr[stringLength] >= '0' && ip4addr[stringLength] <= '9') { 00050 byte *= 10; 00051 byte += ip4addr[stringLength++] - '0'; 00052 00053 //We go over the maximum value for an IPv4 component 00054 if (byte > 0xff) { 00055 return false; 00056 } 00057 } 00058 00059 //Append the component 00060 addr[byteCount++] = (uint8_t) byte; 00061 00062 //If we're at the end, we leave the loop. It's the only way to reach the `true` output 00063 if (byteCount == 4) { 00064 break; 00065 } 00066 00067 //If the next character is invalid, we return false 00068 if (stringLength == len || ip4addr[stringLength++] != '.') { 00069 return false; 00070 } 00071 } 00072 00073 return stringLength == len || ip4addr[stringLength] == '\0'; 00074 }
Generated on Mon Aug 29 2022 19:53:42 by
