jinyedge's note
{http://www.jinyedge.pe.kr}


Hi, this is jinyedge. I'm a software developer. I hope you can find some useful information in my homepage.


jinyedge at gmail.com
Since 2001.12.05

 

 
Subj Cont
 
Subj: C, punycode2unicode with libidn.
Mtime: 2010-03-09 23:12:24

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <idna.h>
#include <curl/curl.h>

//---------------------------------------------------------------------
char* punycode2unicode(char* url){
    char* protocol = (char*)"";
    char* temp_url = NULL;
    char* res_url = NULL;

    if(!url){
        return NULL;
    }

    char* dec_url = curl_easy_unescape(NULL, url, 0, NULL);

    if(strncmp("http://", dec_url, 7) == 0){
        protocol = (char*)"http://";
    }
    else if(strncmp("https://", dec_url, 8) == 0){
        protocol = (char*)"https://";
    }

    int rc = idna_to_unicode_8z8z(dec_url + strlen(protocol), &temp_url, 0);
    if(rc != IDNA_SUCCESS){
        res_url = strdup(dec_url);
    }
    else{
        res_url = (char*)calloc(strlen(temp_url + strlen(protocol) + 1), sizeof(char));
        sprintf(res_url, "%s%s", protocol, temp_url);
    }

    curl_free(dec_url);
    free(temp_url);

    return res_url;
}

//---------------------------------------------------------------------
int main(int argc, char** argv){
    char* url = "http://www.xn--rksmrgsa-0zap8p.com";

    char* res = punycode2unicode(url);
    puts(res);

    free(res);
}