Search code examples
cwindowsdeflate

Using wininet to download deflate XML on Windows MSVC, but gets broken data


This code download deflated XML document https://api.bilibili.com/x/v1/dm/list.so?oid=162677333 and save it to temp.Z, which however seems broken. How is that?

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wininet.h>

#pragma comment(linker, "/entry:\"mainCRTStartup\"")
#pragma comment(lib, "wininet.lib")

char *download(char *link, int *size)
{
    int prealloc_size = 100000;
    char *buf = malloc(prealloc_size);
    DWORD num;
    HINTERNET hinet;
    HINTERNET hurl;
    *size = 0;

    hinet = InternetOpen("Microsoft Internet Explorer",
        INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_INVALID_PORT_NUMBER);
    hurl = InternetOpenUrl(hinet, link, NULL, 0, INTERNET_FLAG_NEED_FILE, 0);
 
    while (TRUE == InternetReadFile(hurl, buf + *size, 1024, &num) && num > 0)
    {
        *size += num;
        
        if (*size + 1024 > prealloc_size)
        {
            prealloc_size += prealloc_size / 2;
            buf = realloc(buf, prealloc_size);
        }
    }
    
    InternetCloseHandle(hurl);
    InternetCloseHandle(hinet);
    return buf;
}

int main(void) 
{
    char *link = "https://api.bilibili.com/x/v1/dm/list.so?oid=162677333";
    FILE *f = fopen("temp.Z", "wb");
    int siz;
    char *dat = download(link, &siz);
    
    fwrite(dat, 1, siz, f); 
    fclose(f);
    free(dat);
    
    return 0;
}

I tried Fiddler and it gets the same data, however, Fiddler can decode it, and says it is deflate.


Solution

  • It is something between deflate, zlib and gzip. I don't know. But I can decode it now.

    Just use zlib, with inflateInit2(&strm, -MAX_WBITS) instead of inflateInit(&strm).

    Yes, it is totally good. But why did I think it broken? Because my archive manager don't decode this! Anyway, I need to call zlib by my own. I have suggested the archive manager developers add this feature - which is useful, no?