unsigned short int UbirchSIM800::HTTP_get(const char *url, unsigned long int &length, STREAM &file) { unsigned short int status = HTTP_get(url, length); PRINT("HTTP STATUS: "); DEBUGLN(status); PRINT("FILE LENGTH: "); DEBUGLN(length); if (length == 0) return status; char *buffer = (char *) malloc(SIM800_BUFSIZE); uint32_t pos = 0; do { size_t r = HTTP_read(buffer, pos, SIM800_BUFSIZE); #if !defined(NDEBUG) && defined(DEBUG_PROGRESS) if ((pos % 10240) == 0) { PRINT(" "); DEBUGLN(pos); } else if (pos % (1024) == 0) { PRINT("<"); } #endif pos += r; file.write(buffer, r); } while (pos < length); free(buffer); PRINTLN(""); return status; }
int RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, int age) { FILE *f = NULL; char *path, date[64], cctim[64]; long pos = 0; time_t ctim = -1, cnow; int i, got = 0, ret = 0; unsigned int hlen; struct info in = {0}; struct HTTP_ctx http = {0}; HTTPResult httpres; z_stream zs = {0}; AVal home, hpre; date[0] = '\0'; #ifdef _WIN32 #ifdef XBMC4XBOX hpre.av_val = "Q:"; hpre.av_len = 2; home.av_val = "\\UserData"; #else hpre.av_val = getenv("HOMEDRIVE"); hpre.av_len = strlen(hpre.av_val); home.av_val = getenv("HOMEPATH"); #endif #define DIRSEP "\\" #else /* !_WIN32 */ hpre.av_val = ""; hpre.av_len = 0; home.av_val = getenv("HOME"); #define DIRSEP "/" #endif if (!home.av_val) home.av_val = "."; home.av_len = strlen(home.av_val); /* SWF hash info is cached in a fixed-format file. * url: <url of SWF file> * ctim: HTTP datestamp of when we last checked it. * date: HTTP datestamp of the SWF's last modification. * size: SWF size in hex * hash: SWF hash in hex * * These fields must be present in this order. All fields * besides URL are fixed size. */ path = malloc(hpre.av_len + home.av_len + sizeof(DIRSEP ".swfinfo")); sprintf(path, "%s%s" DIRSEP ".swfinfo", hpre.av_val, home.av_val); f = fopen(path, "r+"); while (f) { char buf[4096], *file, *p; file = strchr(url, '/'); if (!file) break; file += 2; file = strchr(file, '/'); if (!file) break; file++; hlen = file - url; p = strrchr(file, '/'); if (p) file = p; else file--; while (fgets(buf, sizeof(buf), f)) { char *r1; got = 0; if (strncmp(buf, "url: ", 5)) continue; if (strncmp(buf + 5, url, hlen)) continue; r1 = strrchr(buf, '/'); i = strlen(r1); r1[--i] = '\0'; if (strncmp(r1, file, i)) continue; pos = ftell(f); while (got < 4 && fgets(buf, sizeof(buf), f)) { if (!strncmp(buf, "size: ", 6)) { *size = strtol(buf + 6, NULL, 16); got++; } else if (!strncmp(buf, "hash: ", 6)) { unsigned char *ptr = hash, *in = (unsigned char *) buf + 6; int l = strlen((char *) in) - 1; for (i = 0; i < l; i += 2) *ptr++ = (HEX2BIN(in[i]) << 4) | HEX2BIN(in[i + 1]); got++; } else if (!strncmp(buf, "date: ", 6)) { buf[strlen(buf) - 1] = '\0'; strncpy(date, buf + 6, sizeof(date)); got++; } else if (!strncmp(buf, "ctim: ", 6)) { buf[strlen(buf) - 1] = '\0'; ctim = make_unix_time(buf + 6); got++; } else if (!strncmp(buf, "url: ", 5)) break; } break; } break; } cnow = time(NULL); /* If we got a cache time, see if it's young enough to use directly */ if (age && ctim > 0) { ctim = cnow - ctim; ctim /= 3600 * 24; /* seconds to days */ if (ctim < age) /* ok, it's new enough */ goto out; } in.first = 1; HMAC_setup(in.ctx, "Genuine Adobe Flash Player 001", 30); inflateInit(&zs); in.zs = &zs; http.date = date; http.data = ∈ httpres = HTTP_get(&http, url, swfcrunch); inflateEnd(&zs); if (httpres != HTTPRES_OK && httpres != HTTPRES_OK_NOT_MODIFIED) { ret = -1; if (httpres == HTTPRES_LOST_CONNECTION) RTMP_Log(RTMP_LOGERROR, "%s: connection lost while downloading swfurl %s", __FUNCTION__, url); else if (httpres == HTTPRES_NOT_FOUND) RTMP_Log(RTMP_LOGERROR, "%s: swfurl %s not found", __FUNCTION__, url); else RTMP_Log(RTMP_LOGERROR, "%s: couldn't contact swfurl %s (HTTP error %d)", __FUNCTION__, url, http.status); } else { if (got && pos) fseek(f, pos, SEEK_SET); else { char *q; if (!f) f = fopen(path, "w"); if (!f) { int err = errno; RTMP_Log(RTMP_LOGERROR, "%s: couldn't open %s for writing, errno %d (%s)", __FUNCTION__, path, err, strerror(err)); ret = -1; goto out; } fseek(f, 0, SEEK_END); q = strchr(url, '?'); if (q) i = q - url; else i = strlen(url); fprintf(f, "url: %.*s\n", i, url); } strtime(&cnow, cctim); fprintf(f, "ctim: %s\n", cctim); if (!in.first) { HMAC_finish(in.ctx, hash, hlen); *size = in.size; fprintf(f, "date: %s\n", date); fprintf(f, "size: %08x\n", in.size); fprintf(f, "hash: "); for (i = 0; i < SHA256_DIGEST_LENGTH; i++) fprintf(f, "%02x", hash[i]); fprintf(f, "\n"); } } HMAC_close(in.ctx); out: free(path); if (f) fclose(f); return ret; }