Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:35:22 2017 -0800
Revision:
19:2e0811512ceb
Parent:
11:77df6d7e65ae
Child:
20:95abdea56064
1.1.6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 //
Azure.IoT Build 0:fa2de1b79154 5 // PUT NO INCLUDES BEFORE HERE !!!!
Azure.IoT Build 0:fa2de1b79154 6 //
Azure.IoT Build 0:fa2de1b79154 7 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 8 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 9
Azure.IoT Build 0:fa2de1b79154 10 #include <stddef.h>
Azure.IoT Build 0:fa2de1b79154 11 #include <string.h>
Azure.IoT Build 0:fa2de1b79154 12 //
Azure.IoT Build 0:fa2de1b79154 13 // PUT NO CLIENT LIBRARY INCLUDES BEFORE HERE !!!!
Azure.IoT Build 0:fa2de1b79154 14 //
Azure.IoT Build 0:fa2de1b79154 15 #include "azure_c_shared_utility/urlencode.h"
Azure.IoT Build 6:c55b013dfc2a 16 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 17 #include "azure_c_shared_utility/strings.h"
Azure.IoT Build 0:fa2de1b79154 18
AzureIoTClient 11:77df6d7e65ae 19 #define NIBBLE_STR(c) (char)(c < 10 ? c + '0' : c - 10 + 'a')
AzureIoTClient 11:77df6d7e65ae 20 #define IS_PRINTABLE(c) ( \
AzureIoTClient 11:77df6d7e65ae 21 (c == 0) || \
AzureIoTClient 11:77df6d7e65ae 22 (c == '!') || \
AzureIoTClient 11:77df6d7e65ae 23 (c == '(') || (c == ')') || (c == '*') || \
AzureIoTClient 11:77df6d7e65ae 24 (c == '-') || (c == '.') || \
AzureIoTClient 11:77df6d7e65ae 25 ((c >= '0') && (c <= '9')) || \
AzureIoTClient 11:77df6d7e65ae 26 ((c >= 'A') && (c <= 'Z')) || \
AzureIoTClient 11:77df6d7e65ae 27 (c == '_') || \
AzureIoTClient 11:77df6d7e65ae 28 ((c >= 'a') && (c <= 'z')) \
AzureIoTClient 11:77df6d7e65ae 29 )
AzureIoTClient 11:77df6d7e65ae 30
AzureIoTClient 11:77df6d7e65ae 31 static size_t URL_PrintableChar(unsigned char charVal, char* buffer)
AzureIoTClient 11:77df6d7e65ae 32 {
AzureIoTClient 11:77df6d7e65ae 33 size_t size;
AzureIoTClient 11:77df6d7e65ae 34 if (IS_PRINTABLE(charVal))
AzureIoTClient 11:77df6d7e65ae 35 {
AzureIoTClient 11:77df6d7e65ae 36 buffer[0] = (char)charVal;
AzureIoTClient 11:77df6d7e65ae 37 size = 1;
AzureIoTClient 11:77df6d7e65ae 38 }
AzureIoTClient 11:77df6d7e65ae 39 else
AzureIoTClient 11:77df6d7e65ae 40 {
AzureIoTClient 11:77df6d7e65ae 41 char bigNibbleStr;
AzureIoTClient 11:77df6d7e65ae 42 char littleNibbleStr;
AzureIoTClient 11:77df6d7e65ae 43 unsigned char bigNibbleVal = charVal >> 4;
AzureIoTClient 11:77df6d7e65ae 44 unsigned char littleNibbleVal = charVal & 0x0F;
AzureIoTClient 11:77df6d7e65ae 45
AzureIoTClient 11:77df6d7e65ae 46 if (bigNibbleVal >= 0x0C)
AzureIoTClient 11:77df6d7e65ae 47 {
AzureIoTClient 11:77df6d7e65ae 48 bigNibbleVal -= 0x04;
AzureIoTClient 11:77df6d7e65ae 49 }
AzureIoTClient 11:77df6d7e65ae 50
AzureIoTClient 11:77df6d7e65ae 51 bigNibbleStr = NIBBLE_STR(bigNibbleVal);
AzureIoTClient 11:77df6d7e65ae 52 littleNibbleStr = NIBBLE_STR(littleNibbleVal);
AzureIoTClient 11:77df6d7e65ae 53
AzureIoTClient 11:77df6d7e65ae 54 buffer[0] = '%';
AzureIoTClient 11:77df6d7e65ae 55
AzureIoTClient 11:77df6d7e65ae 56 if (charVal < 0x80)
AzureIoTClient 11:77df6d7e65ae 57 {
AzureIoTClient 11:77df6d7e65ae 58 buffer[1] = bigNibbleStr;
AzureIoTClient 11:77df6d7e65ae 59 buffer[2] = littleNibbleStr;
AzureIoTClient 11:77df6d7e65ae 60 size = 3;
AzureIoTClient 11:77df6d7e65ae 61 }
AzureIoTClient 11:77df6d7e65ae 62 else
AzureIoTClient 11:77df6d7e65ae 63 {
AzureIoTClient 11:77df6d7e65ae 64 buffer[1] = 'c';
AzureIoTClient 11:77df6d7e65ae 65 buffer[3] = '%';
AzureIoTClient 11:77df6d7e65ae 66 buffer[4] = bigNibbleStr;
AzureIoTClient 11:77df6d7e65ae 67 buffer[5] = littleNibbleStr;
AzureIoTClient 11:77df6d7e65ae 68 if (charVal < 0xC0)
AzureIoTClient 11:77df6d7e65ae 69 {
AzureIoTClient 11:77df6d7e65ae 70 buffer[2] = '2';
AzureIoTClient 11:77df6d7e65ae 71 }
AzureIoTClient 11:77df6d7e65ae 72 else
AzureIoTClient 11:77df6d7e65ae 73 {
AzureIoTClient 11:77df6d7e65ae 74 buffer[2] = '3';
AzureIoTClient 11:77df6d7e65ae 75 }
AzureIoTClient 11:77df6d7e65ae 76 size = 6;
AzureIoTClient 11:77df6d7e65ae 77 }
AzureIoTClient 11:77df6d7e65ae 78 }
AzureIoTClient 11:77df6d7e65ae 79
AzureIoTClient 11:77df6d7e65ae 80 return size;
AzureIoTClient 11:77df6d7e65ae 81 }
AzureIoTClient 11:77df6d7e65ae 82
AzureIoTClient 11:77df6d7e65ae 83 static size_t URL_PrintableCharSize(unsigned char charVal)
AzureIoTClient 11:77df6d7e65ae 84 {
AzureIoTClient 11:77df6d7e65ae 85 size_t size;
AzureIoTClient 11:77df6d7e65ae 86 if (IS_PRINTABLE(charVal))
AzureIoTClient 11:77df6d7e65ae 87 {
AzureIoTClient 11:77df6d7e65ae 88 size = 1;
AzureIoTClient 11:77df6d7e65ae 89 }
AzureIoTClient 11:77df6d7e65ae 90 else
AzureIoTClient 11:77df6d7e65ae 91 {
AzureIoTClient 11:77df6d7e65ae 92 if (charVal < 0x80)
AzureIoTClient 11:77df6d7e65ae 93 {
AzureIoTClient 11:77df6d7e65ae 94 size = 3;
AzureIoTClient 11:77df6d7e65ae 95 }
AzureIoTClient 11:77df6d7e65ae 96 else
AzureIoTClient 11:77df6d7e65ae 97 {
AzureIoTClient 11:77df6d7e65ae 98 size = 6;
AzureIoTClient 11:77df6d7e65ae 99 }
AzureIoTClient 11:77df6d7e65ae 100 }
AzureIoTClient 11:77df6d7e65ae 101 return size;
AzureIoTClient 11:77df6d7e65ae 102 }
Azure.IoT Build 0:fa2de1b79154 103
Azure.IoT Build 0:fa2de1b79154 104 STRING_HANDLE URL_EncodeString(const char* textEncode)
Azure.IoT Build 0:fa2de1b79154 105 {
Azure.IoT Build 0:fa2de1b79154 106 STRING_HANDLE result;
Azure.IoT Build 0:fa2de1b79154 107 if (textEncode == NULL)
Azure.IoT Build 0:fa2de1b79154 108 {
Azure.IoT Build 0:fa2de1b79154 109 result = NULL;
Azure.IoT Build 0:fa2de1b79154 110 }
Azure.IoT Build 0:fa2de1b79154 111 else
Azure.IoT Build 0:fa2de1b79154 112 {
Azure.IoT Build 0:fa2de1b79154 113 STRING_HANDLE tempString = STRING_construct(textEncode);
Azure.IoT Build 0:fa2de1b79154 114 if (tempString == NULL)
Azure.IoT Build 0:fa2de1b79154 115 {
Azure.IoT Build 0:fa2de1b79154 116 result = NULL;
Azure.IoT Build 0:fa2de1b79154 117 }
Azure.IoT Build 0:fa2de1b79154 118 else
Azure.IoT Build 0:fa2de1b79154 119 {
Azure.IoT Build 0:fa2de1b79154 120 result = URL_Encode(tempString);
Azure.IoT Build 0:fa2de1b79154 121 STRING_delete(tempString);
Azure.IoT Build 0:fa2de1b79154 122 }
Azure.IoT Build 0:fa2de1b79154 123 }
Azure.IoT Build 0:fa2de1b79154 124 return result;
Azure.IoT Build 0:fa2de1b79154 125 }
Azure.IoT Build 0:fa2de1b79154 126
Azure.IoT Build 0:fa2de1b79154 127 STRING_HANDLE URL_Encode(STRING_HANDLE input)
Azure.IoT Build 0:fa2de1b79154 128 {
Azure.IoT Build 0:fa2de1b79154 129 STRING_HANDLE result;
Azure.IoT Build 0:fa2de1b79154 130 if (input == NULL)
Azure.IoT Build 0:fa2de1b79154 131 {
Azure.IoT Build 0:fa2de1b79154 132 /*Codes_SRS_URL_ENCODE_06_001: [If input is NULL then URL_Encode will return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 133 result = NULL;
AzureIoTClient 1:9190c0f4d23a 134 LogError("URL_Encode:: NULL input");
Azure.IoT Build 0:fa2de1b79154 135 }
Azure.IoT Build 0:fa2de1b79154 136 else
Azure.IoT Build 0:fa2de1b79154 137 {
Azure.IoT Build 0:fa2de1b79154 138 size_t lengthOfResult = 0;
Azure.IoT Build 0:fa2de1b79154 139 char* encodedURL;
Azure.IoT Build 0:fa2de1b79154 140 const char* currentInput;
Azure.IoT Build 0:fa2de1b79154 141 unsigned char currentUnsignedChar;
Azure.IoT Build 0:fa2de1b79154 142 currentInput = STRING_c_str(input);
Azure.IoT Build 0:fa2de1b79154 143 /*Codes_SRS_URL_ENCODE_06_003: [If input is a zero length string then URL_Encode will return a zero length string.]*/
Azure.IoT Build 0:fa2de1b79154 144 do
Azure.IoT Build 0:fa2de1b79154 145 {
Azure.IoT Build 0:fa2de1b79154 146 currentUnsignedChar = (unsigned char)(*currentInput++);
AzureIoTClient 11:77df6d7e65ae 147 lengthOfResult += URL_PrintableCharSize(currentUnsignedChar);
Azure.IoT Build 0:fa2de1b79154 148 } while (currentUnsignedChar != 0);
Azure.IoT Build 0:fa2de1b79154 149 if ((encodedURL = malloc(lengthOfResult)) == NULL)
Azure.IoT Build 0:fa2de1b79154 150 {
Azure.IoT Build 0:fa2de1b79154 151 /*Codes_SRS_URL_ENCODE_06_002: [If an error occurs during the encoding of input then URL_Encode will return NULL.]*/
Azure.IoT Build 0:fa2de1b79154 152 result = NULL;
AzureIoTClient 1:9190c0f4d23a 153 LogError("URL_Encode:: MALLOC failure on encode.");
Azure.IoT Build 0:fa2de1b79154 154 }
Azure.IoT Build 0:fa2de1b79154 155 else
Azure.IoT Build 0:fa2de1b79154 156 {
Azure.IoT Build 0:fa2de1b79154 157 size_t currentEncodePosition = 0;
Azure.IoT Build 0:fa2de1b79154 158 currentInput = STRING_c_str(input);
Azure.IoT Build 0:fa2de1b79154 159 do
Azure.IoT Build 0:fa2de1b79154 160 {
Azure.IoT Build 0:fa2de1b79154 161 currentUnsignedChar = (unsigned char)(*currentInput++);
AzureIoTClient 11:77df6d7e65ae 162 currentEncodePosition += URL_PrintableChar(currentUnsignedChar, &encodedURL[currentEncodePosition]);
Azure.IoT Build 0:fa2de1b79154 163 } while (currentUnsignedChar != 0);
AzureIoTClient 11:77df6d7e65ae 164
Azure.IoT Build 0:fa2de1b79154 165 result = STRING_new_with_memory(encodedURL);
AzureIoTClient 11:77df6d7e65ae 166 if (result == NULL)
AzureIoTClient 11:77df6d7e65ae 167 {
AzureIoTClient 11:77df6d7e65ae 168 LogError("URL_Encode:: MALLOC failure on encode.");
AzureIoTClient 11:77df6d7e65ae 169 free(encodedURL);
AzureIoTClient 11:77df6d7e65ae 170 }
Azure.IoT Build 0:fa2de1b79154 171 }
Azure.IoT Build 0:fa2de1b79154 172 }
Azure.IoT Build 0:fa2de1b79154 173 return result;
Azure.IoT Build 0:fa2de1b79154 174 }