All pastes #455 Raw Edit

Decode AOLs search queries

public ini v1 · immutable
#455 ·published 2004-08-04 03:45 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <netdb.h>#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <netinet/ip.h>int get(char *server, unsigned short port, char *file, char **out) {  char buffer[256], *content = NULL;  struct hostent *he;  struct sockaddr_in sai;  int s, i, len;     he = gethostbyname(server);  memcpy((char *)&sai.sin_addr, (char *)he->h_addr, sizeof(sai.sin_addr));  sai.sin_family = AF_INET;  sai.sin_port = htons(port);  s = socket(AF_INET, SOCK_STREAM, 0);  if(s < 0) return -1;   if(connect(s, (struct sockaddr *)&sai, sizeof(sai)) < 0) return -2;         len = snprintf(buffer, sizeof(buffer), "GET %s HTTP/1.0\nHost: %s\n\n", file, server);  if(send(s, buffer, len, 0) != len) return -3;  i = 0;  while(recv(s, &buffer[i], 1, 0)) {    if(i >= sizeof(buffer)) return -4;    if(buffer[i] == (char)10) {      if(buffer[0] == 13) goto GETFILE;      buffer[i-1] = 0;      i = 0;    } else {      i++;     }  }GETFILE:   len = 0;  while((i = recv(s, buffer, sizeof(buffer), 0))) {    content = realloc(content, len+i+1);    memcpy(&content[len], buffer, i);    len += i;  }  content[len] = 0;  *out = content;  return len;}int decode(char *in, char *out) {  char url[256], *content = NULL;  char *start, *end;  int ret = 0;  sprintf(url, "/aol/search?encquery=%s", in);  if(get("aolsearch.aol.com", 80, url, &content)) {    start = strstr(content,"adSetSearch(\'");    if(start) {      start += 13;      end = strchr(start, '\'');      if(end) *end = '\0';      strcpy(out, start);      ret = 1;    }  }  if(content) free(content);  return ret;}int main(int argc, char **argv) {  int i;  char *decoded;  if(argc < 2) {    printf("Usage: %s [ENCODED]\n", argv[0]);    return 1;  }  i = strlen(argv[1]);  decoded = malloc(i/2);  if(decode(argv[1], decoded))    printf("[%s]\n", decoded);  else     printf("Nothing found.");  return 0;}