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.
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed
Fork of iothub_client_sample_amqp by
urlencode.c
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 // 00005 // PUT NO INCLUDES BEFORE HERE !!!! 00006 // 00007 #include <stdlib.h> 00008 #ifdef _CRTDBG_MAP_ALLOC 00009 #include <crtdbg.h> 00010 #endif 00011 #include "azure_c_shared_utility/gballoc.h" 00012 00013 #include <stddef.h> 00014 #include <string.h> 00015 // 00016 // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE !!!! 00017 // 00018 #include "azure_c_shared_utility/urlencode.h" 00019 #include "azure_c_shared_utility/xlogging.h" 00020 #include "azure_c_shared_utility/strings.h" 00021 00022 #define NIBBLE_STR(c) (char)(c < 10 ? c + '0' : c - 10 + 'a') 00023 #define IS_PRINTABLE(c) ( \ 00024 (c == 0) || \ 00025 (c == '!') || \ 00026 (c == '(') || (c == ')') || (c == '*') || \ 00027 (c == '-') || (c == '.') || \ 00028 ((c >= '0') && (c <= '9')) || \ 00029 ((c >= 'A') && (c <= 'Z')) || \ 00030 (c == '_') || \ 00031 ((c >= 'a') && (c <= 'z')) \ 00032 ) 00033 00034 static size_t URL_PrintableChar(unsigned char charVal, char* buffer) 00035 { 00036 size_t size; 00037 if (IS_PRINTABLE(charVal)) 00038 { 00039 buffer[0] = (char)charVal; 00040 size = 1; 00041 } 00042 else 00043 { 00044 char bigNibbleStr; 00045 char littleNibbleStr; 00046 unsigned char bigNibbleVal = charVal >> 4; 00047 unsigned char littleNibbleVal = charVal & 0x0F; 00048 00049 if (bigNibbleVal >= 0x0C) 00050 { 00051 bigNibbleVal -= 0x04; 00052 } 00053 00054 bigNibbleStr = NIBBLE_STR(bigNibbleVal); 00055 littleNibbleStr = NIBBLE_STR(littleNibbleVal); 00056 00057 buffer[0] = '%'; 00058 00059 if (charVal < 0x80) 00060 { 00061 buffer[1] = bigNibbleStr; 00062 buffer[2] = littleNibbleStr; 00063 size = 3; 00064 } 00065 else 00066 { 00067 buffer[1] = 'c'; 00068 buffer[3] = '%'; 00069 buffer[4] = bigNibbleStr; 00070 buffer[5] = littleNibbleStr; 00071 if (charVal < 0xC0) 00072 { 00073 buffer[2] = '2'; 00074 } 00075 else 00076 { 00077 buffer[2] = '3'; 00078 } 00079 size = 6; 00080 } 00081 } 00082 00083 return size; 00084 } 00085 00086 static size_t URL_PrintableCharSize(unsigned char charVal) 00087 { 00088 size_t size; 00089 if (IS_PRINTABLE(charVal)) 00090 { 00091 size = 1; 00092 } 00093 else 00094 { 00095 if (charVal < 0x80) 00096 { 00097 size = 3; 00098 } 00099 else 00100 { 00101 size = 6; 00102 } 00103 } 00104 return size; 00105 } 00106 00107 STRING_HANDLE URL_EncodeString(const char* textEncode) 00108 { 00109 STRING_HANDLE result; 00110 if (textEncode == NULL) 00111 { 00112 result = NULL; 00113 } 00114 else 00115 { 00116 STRING_HANDLE tempString = STRING_construct(textEncode); 00117 if (tempString == NULL) 00118 { 00119 result = NULL; 00120 } 00121 else 00122 { 00123 result = URL_Encode(tempString); 00124 STRING_delete(tempString); 00125 } 00126 } 00127 return result; 00128 } 00129 00130 STRING_HANDLE URL_Encode(STRING_HANDLE input) 00131 { 00132 STRING_HANDLE result; 00133 if (input == NULL) 00134 { 00135 /*Codes_SRS_URL_ENCODE_06_001: [If input is NULL then URL_Encode will return NULL.]*/ 00136 result = NULL; 00137 LogError("URL_Encode:: NULL input"); 00138 } 00139 else 00140 { 00141 size_t lengthOfResult = 0; 00142 char* encodedURL; 00143 const char* currentInput; 00144 unsigned char currentUnsignedChar; 00145 currentInput = STRING_c_str(input); 00146 /*Codes_SRS_URL_ENCODE_06_003: [If input is a zero length string then URL_Encode will return a zero length string.]*/ 00147 do 00148 { 00149 currentUnsignedChar = (unsigned char)(*currentInput++); 00150 lengthOfResult += URL_PrintableCharSize(currentUnsignedChar); 00151 } while (currentUnsignedChar != 0); 00152 if ((encodedURL = malloc(lengthOfResult)) == NULL) 00153 { 00154 /*Codes_SRS_URL_ENCODE_06_002: [If an error occurs during the encoding of input then URL_Encode will return NULL.]*/ 00155 result = NULL; 00156 LogError("URL_Encode:: MALLOC failure on encode."); 00157 } 00158 else 00159 { 00160 size_t currentEncodePosition = 0; 00161 currentInput = STRING_c_str(input); 00162 do 00163 { 00164 currentUnsignedChar = (unsigned char)(*currentInput++); 00165 currentEncodePosition += URL_PrintableChar(currentUnsignedChar, &encodedURL[currentEncodePosition]); 00166 } while (currentUnsignedChar != 0); 00167 00168 result = STRING_new_with_memory(encodedURL); 00169 if (result == NULL) 00170 { 00171 LogError("URL_Encode:: MALLOC failure on encode."); 00172 free(encodedURL); 00173 } 00174 } 00175 } 00176 return result; 00177 }
Generated on Tue Jul 12 2022 12:43:25 by
