Sha256: 075ac79fd61772ed1ac5fd9da3ef48b29323be673c83f793cf97e57ce7ce51ad

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

#include "urlcode.h"
#include "libmockspotify.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

/*
 * Used from http://www.geekhideout.com/urlcode.shtml at 3rd of March, 2011.
 * Adapted a little bit by Kim Burgestrand.
 * */

/* Converts a hex character to its integer value */
static char from_hex(char ch) {
  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}

/* Converts an integer value to its hex character*/
static char to_hex(char code) {
  static char hex[] = "0123456789ABCDEF";
  return hex[code & 15];
}

/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(char *str) {
  char *pstr = str, *buf = ALLOC_N(char, strlen(str) * 3 + 1), *pbuf = buf;
  while (*pstr) {
    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
      *pbuf++ = *pstr;
    else if (*pstr == ' ')
      *pbuf++ = '+';
    else
      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 0x0F);
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}

/* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(char *str) {
  char *pstr = str, *buf = ALLOC_N(char, strlen(str) + 1), *pbuf = buf;
  while (*pstr) {
    if (*pstr == '%') {
      if (pstr[1] && pstr[2]) {
        *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
        pstr += 2;
      }
    } else if (*pstr == '+') {
      *pbuf++ = ' ';
    } else {
      *pbuf++ = *pstr;
    }
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hallon-0.18.2 spec/mockspotify/libmockspotify/src/urlcode.c
hallon-0.18.1 spec/mockspotify/libmockspotify/src/urlcode.c
hallon-0.18.0 spec/mockspotify/libmockspotify/src/urlcode.c
hallon-0.17.0 spec/mockspotify/libmockspotify/src/urlcode.c
hallon-0.16.0 spec/mockspotify/libmockspotify/src/urlcode.c
hallon-0.15.0 spec/mockspotify/libmockspotify/src/urlcode.c