A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HttpString.cpp Source File

HttpString.cpp

00001 /*****************************************************************************
00002 *
00003 *  HttpString.c
00004 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *    Redistributions of source code must retain the above copyright
00011 *    notice, this list of conditions and the following disclaimer.
00012 *
00013 *    Redistributions in binary form must reproduce the above copyright
00014 *    notice, this list of conditions and the following disclaimer in the
00015 *    documentation and/or other materials provided with the   
00016 *    distribution.
00017 *
00018 *    Neither the name of Texas Instruments Incorporated nor the names of
00019 *    its contributors may be used to endorse or promote products derived
00020 *    from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00026 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00027 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 *
00034 *****************************************************************************/
00035 #include "mbed.h"
00036 #include "string.h"
00037 #include <stdlib.h>
00038 #include "HttpString.h"
00039 
00040 /** 
00041  * @addtogroup HttpString
00042  * @{
00043  */
00044 
00045 char digits[] = "0123456789";
00046 char hexDigits[] = "0123456789abcdef";
00047 
00048 int HttpString_strcmp(struct HttpBlob first, struct HttpBlob second)
00049 {
00050     int min,res;
00051     if (first.uLength > second.uLength)
00052         min = second.uLength;
00053     else
00054         min = first.uLength;
00055     
00056     // Compare a common length which might be equal
00057     res = memcmp(first.pData, second.pData, min);
00058     if (res != 0)
00059         return res;
00060 
00061     // Common length is equal, so the longer blob is "larger"
00062     if (first.uLength > second.uLength)
00063         return 1;
00064     else if (first.uLength < second.uLength)
00065         return -1;
00066     else
00067         return 0;
00068 }
00069 
00070 static void ToLower(char * str, uint16 len)
00071 {
00072     int i;
00073     for (i=0 ; i<=len; i++)
00074     {
00075         if (str[i]>='A' && str[i]<='Z')
00076             str[i] = str[i] + 32; 
00077     }
00078 }
00079 
00080 uint8* HttpString_nextToken(char* pToken, uint16 uTokenLength, struct HttpBlob blob)
00081 {
00082     uint8* pch = blob.pData;
00083     struct HttpBlob partialBlob;
00084     struct HttpBlob token;
00085     token.pData = (uint8*)pToken;
00086     token.uLength = uTokenLength;
00087     ToLower((char *)blob.pData, blob.uLength);
00088     while (pch < blob.pData + blob.uLength)
00089     {
00090         // Calculate how many blob bytes we should search
00091         int nMaxCount = blob.uLength - (pch - blob.pData) - uTokenLength + 1;
00092         if (nMaxCount < 1)
00093             return NULL;
00094 
00095         // Search for the first character of the token
00096         pch = (uint8*)memchr(pch, pToken[0], nMaxCount);
00097         if (pch==NULL)
00098             return NULL;
00099 
00100         // Found first character, now compare the rest
00101         partialBlob.pData = pch;
00102         partialBlob.uLength = uTokenLength;
00103         if (HttpString_strcmp(token, partialBlob)==0)
00104             return pch;
00105 
00106         // Skip this byte, and look for the token in the rest of the blob
00107         ++pch;
00108     }
00109     return NULL;
00110 }
00111 
00112 uint32 HttpString_atou(struct HttpBlob string)
00113 {
00114     return atoi((const char*)string.pData);
00115 }
00116 
00117 void HttpString_utoa(uint32 uNum, struct HttpBlob* pString)
00118 {
00119     char* ptr;
00120     uint32 uTemp = uNum;
00121 
00122     // value 0 is a special format
00123     if (uNum == 0)
00124     {
00125         pString->uLength = 1;
00126         *pString->pData = '0';
00127         return;
00128     }
00129 
00130     // Find out the length of the number, in decimal base
00131     pString->uLength = 0;
00132     while (uTemp > 0)
00133     {
00134         uTemp /= 10;
00135         pString->uLength++;
00136     }
00137 
00138     // Do the actual formatting, right to left
00139     uTemp = uNum;
00140     ptr = (char*)pString->pData + pString->uLength;
00141     while (uTemp > 0)
00142     {
00143         --ptr;
00144         *ptr = digits[uTemp % 10];
00145         uTemp /= 10;
00146     }
00147 }
00148 
00149 void HttpString_htoa(uint32 uNum, struct HttpBlob* pString, uint8 bPadZero)
00150 {
00151     uint8* ptr;
00152     uint32 uTemp = uNum;
00153 
00154     if (!bPadZero)
00155     {
00156         // value 0 is a special format
00157         if (uNum == 0)
00158         {
00159             pString->uLength = 1;
00160             *pString->pData = '0';
00161             return;
00162         }
00163 
00164         // Find out the length of the number, in hexadecimal base
00165         pString->uLength = 0;
00166         while (uTemp > 0)
00167         {
00168             uTemp /= 16;
00169             pString->uLength++;
00170         }
00171     }
00172 
00173     // Do the actual formatting, right to left
00174     uTemp = uNum;
00175     ptr = pString->pData + pString->uLength;
00176     while (ptr > pString->pData)
00177     {
00178         --ptr;
00179         *ptr = (uint8)hexDigits[uTemp % 16];
00180         uTemp /= 16;
00181     }
00182 }
00183