新浪围脖开放平台三"urlEncode"

//.h

/**
*  @brief URL Encode
*  @file Urlcode.h
*  @author loach <loachmr@sina.com>
*
* Copyright (C) 1996-2010 SINA Corporation, All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/


#ifndef __urlcode_H__
#define __urlcode_H__

 

#ifdef __cplusplus
extern "C" {
#endif

 int  lo_UrlDecode(wchar_t** out,const char* pin,  int len);
 int  lo_UrlDecodeA(char** out,const char* pin, int len);

 int  lo_UrlEncode(wchar_t** out , const char* pin ,  int len); 
 int  lo_UrlEncodeA(char** out, const char* pin ,  int len);

#ifdef __cplusplus
}
#endif

#endif //__urlcode_H__

 

//.cpp

/**
*  @brief URL Encode
*  @file  Urlcode.cpp
*  @author loach
*  @Email < loachmr@sina.com >
*
* Copyright (C) 1996-2010 SINA Corporation, All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/


#include "urlCode.h"
#include "strconv.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef __cplusplus
extern "C" {
#endif

 int lo_UrlDecodeA(char** pout,const char* pin , int len)
 {
  char sca[4];
  char* buff = 0 , *pstr = 0;
  const char* pin_end = 0;
  int  c = 0;

  if( !pin )
   return 0;

  if( len <= 0 ){
   len = strlen( pin );
   if( len <= 0 )
    return 0;
  }
  pin_end = pin + len;

  buff = (char*)malloc( len+1 );
  memset(buff , 0 , len+1 );
  pstr= buff;

  while( pin != '/0' && (pin < pin_end) )
  {
   if( *(pin) == '%' )
   {
    if( (pin + 2) < pin_end )
    {    
     sca[0] = *(pin+1);
     sca[1] = *(pin+2);
     sca[2] = '/0';
     sscanf_s( sca , "%02X", &c );
     *pstr += (char)c;

     pin ++;
     pin ++;
     pin ++;
     pstr++;
    }
    else
    {
     free(buff);   
     return -1;
    }
   }
   else
   {
    *pstr += *pin;
    pin++;
    pstr++;
   }
  }
  *pstr = '/0';
  *pout = buff;
  return (pstr-buff);
 }

 int  lo_UrlDecode(wchar_t** pout,const char* pin,  int len)
 {  
  char* poutA = 0;
  int outlen = 0;

  outlen = lo_UrlDecodeA(&poutA , pin , len );
  if( outlen > 0 )
  {//
   outlen = lo_C2W(pout , poutA);
  }
  
  if( poutA ) free(poutA);

  return outlen;
 }

 int lo_UrlEncodeA(char** pout, const char* pin ,  int len)
 {
  //将保留字符转化为16进制格式.
  // according to RFC 2396
  char sca[4] = "%";
  unsigned char c;
  char* buff = 0 , *pstr = 0;
  int i = 0 , bufflen = 0;

  if( !pin )
   return 0;

  if( len <= 0 ){
   //
   len = strlen( pin );
   if( len <= 0 )
    return 0;
  }
  bufflen = (len * 3) + 2;

  buff = (char*)malloc( bufflen );
  memset(buff , 0 , bufflen );
  pstr = buff;

  while( *pin != '/0' && i < len )
  {
   c = *pin;
   if( isalpha( c ) || isdigit( c ) ||
    c == '-' || c == '_' ||  c == '.' || c == '!' || c == '~' || c == '*' || c == '/'' || c == '(' || c == ')' )
   {
    // found an unreserved character
    strncat(pstr, pin , 1);
    pstr++;
   }
   else if( c == ' ' )
   {
    strcat(pstr , "%20");
    pstr += 3;
   }
   else
   {
    sprintf( &(sca[1]) , "%02X", c );
    sca[3] = '/0';
    strcat(pstr , sca );
    pstr += 3;
   }
   pin++;
   i++;
  }
  *pstr ='/0';
  *pout = buff;
  return (pstr-buff);
 }

 int lo_UrlEncode(wchar_t** pout, const char* pin ,  int len)
 {
  char* poutA = 0;
  int outlen = 0;

  outlen = lo_UrlEncodeA(&poutA , pin , len);
  if( outlen )
  {
   outlen = lo_C2W(pout , poutA);
  }

  if( poutA ) free( poutA  );

  return 0;
 }


#ifdef __cplusplus
}
#endif

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值