'programming'에 해당되는 글 387건

  1. 2013.09.16 encode_url decode_url

반응형

 

 


static const char hex[] = "0123456789abcdef";


static int hexdigit(int x)
{
  return (x <= '9') ? x - '0' : (x & 7) + 9;
}


int decode_url(char *from, char *to)
{
  char c, x1, x2;

  while ((c = *from++) != 0) {

    if (c == '%') {
   
      x1 = *from++;

      if (!isxdigit(x1)) {
        errno = EINVAL;
        return -1;
      }

      x2 = *from++;

      if (!isxdigit(x2)) {
        errno = EINVAL;
        return -1;
      }

      if (x1 == 0 && x2 == 0) {
        errno = EINVAL;
        return -1;
      }
     
      *to++ = (hexdigit(x1) << 4) + hexdigit(x2);

    }
    else {
      *to++ = c;
    }

  }

  *to = 0;

  return 0;
}

void encode_url(const char *from, char *to)
{
  char c;

  while ((c = *from++) != 0) {

    switch (c) {

      case '%':
      case ' ':
      case '?':
      case '+':
      case '&':
        *to++ = '%';
        *to++ = hex[(c >> 4) & 15];
        *to++ = hex[c & 15];
        break;

      default:
        *to++ = c;
        break;
    }
   
  }
 
  *to = 0;
 
}

 

반응형
Posted by 공간사랑
,