Fork for fixes
Embed:
(wiki syntax)
Show/hide line numbers
os2_ip4tos.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 "mbed_version.h" 00017 00018 #if MBED_MAJOR_VERSION == 2 00019 00020 #include <stdio.h> 00021 #include <string.h> 00022 #include "common_functions.h" 00023 #include "ip4string.h" 00024 00025 static void ipv4_itoa(char *string, uint8_t byte); 00026 00027 /** 00028 * Print binary IPv4 address to a string. 00029 * String must contain enough room for full address, 16 bytes exact. 00030 * \param addr IPv4 address. 00031 * \p buffer to write string to. 00032 */ 00033 uint_fast8_t ip4tos(const void *ip4addr, char *p) 00034 { 00035 uint_fast8_t outputPos = 0; 00036 const uint8_t *byteArray = ip4addr; 00037 00038 for (uint_fast8_t component = 0; component < 4; ++component) { 00039 //Convert the byte to string 00040 ipv4_itoa(&p[outputPos], byteArray[component]); 00041 00042 //Move outputPos to the end of the string 00043 while (p[outputPos] != '\0') { 00044 outputPos += 1; 00045 } 00046 00047 //Append a dot if this is not the last digit 00048 if (component < 3) { 00049 p[outputPos++] = '.'; 00050 } 00051 } 00052 00053 // Return length of generated string, excluding the terminating null character 00054 return outputPos; 00055 } 00056 00057 static void ipv4_itoa(char *string, uint8_t byte) 00058 { 00059 char *baseString = string; 00060 00061 //Write the digits to the buffer from the least significant to the most 00062 // This is the incorrect order but we will swap later 00063 do { 00064 *string++ = '0' + byte % 10; 00065 byte /= 10; 00066 } while (byte); 00067 00068 //We put the final \0, then go back one step on the last digit for the swap 00069 *string-- = '\0'; 00070 00071 //We now swap the digits 00072 while (baseString < string) { 00073 uint8_t tmp = *string; 00074 *string-- = *baseString; 00075 *baseString++ = tmp; 00076 } 00077 } 00078 #endif
Generated on Fri Jul 15 2022 22:55:10 by 1.7.2