// File: /workspace/github_c_makefile_star2_O0/JimKent[P]linearSat/lib/cheapcgi.c
// Function: cgiParseMultipart
static void cgiParseMultipart(struct hash **retHash, struct cgiVar **retList)
/* process a multipart form */
{
char h[1024];  /* hold mime header line */
char *s = NULL, *ct = NULL;
struct dyString *dy = newDyString(256);
struct mimeBuf *mb = NULL;
struct mimePart *mp = NULL;
char **env = NULL;
struct hash *hash = newHash(6);
struct cgiVar *list = NULL, *el;
extern char **environ;


//debug
//fprintf(stderr,"GALT: top of cgiParseMultipart()\n");
//fflush(stderr);

/* find the CONTENT_ environment strings, use to make Alternate Header string for MIME */
for(env=environ; *env; env++)
    if (startsWith("CONTENT_",*env))
	{
	//debug
        //fprintf(stderr,"%s\n",*env);  //debug
	safef(h,sizeof(h),"%s",*env);
	s = strchr(h,'_');    /* change env syntax to MIME style header, from _= to -: */
	if (!s)
	    errAbort("expecting '_' parsing env var %s for MIME alt header", *env);
	*s = '-';
	s = strchr(h,'=');
	if (!s)
	    errAbort("expecting '=' parsing env var %s for MIME alt header", *env);
	*s = ':';
	dyStringPrintf(dy,"%s\r\n",h);
	}
dyStringAppend(dy,"\r\n");  /* blank line at end means end of headers */

//debug
//fprintf(stderr,"Alternate Header Text:\n%s",dy->string);
//fflush(stderr);
mb = initMimeBuf(STDIN_FILENO);
//debug
//fprintf(stderr,"got past initMimeBuf(STDIN_FILENO)\n");
//fflush(stderr);
mp = parseMultiParts(mb, cloneString(dy->string)); /* The Alternate Header will get freed */
freeDyString(&dy);
if(!mp->multi) /* expecting multipart child parts */
    errAbort("Malformatted multipart-form.");

//debug
//fprintf(stderr,"GALT: Wow got past parse of MIME!\n");
//fflush(stderr);

ct = hashFindVal(mp->hdr,"content-type");
//debug
//fprintf(stderr,"GALT: main content-type: %s\n",ct);
//fflush(stderr);
if (!startsWith("multipart/form-data",ct))
    errAbort("main content-type expected starts with [multipart/form-data], found [%s]",ct);

for(mp=mp->multi;mp;mp=mp->next)
    {
    char *cd = NULL, *cdMain = NULL, *cdName = NULL, *cdFileName = NULL, *ct = NULL;
    cd = hashFindVal(mp->hdr,"content-disposition");
    ct = hashFindVal(mp->hdr,"content-type");
    //debug
    //fprintf(stderr,"GALT: content-disposition: %s\n",cd);
    //fprintf(stderr,"GALT: content-type: %s\n",ct);
    //fflush(stderr);
    cdMain=getMimeHeaderMainVal(cd);
    cdName=getMimeHeaderFieldVal(cd,"name");
    cdFileName=getMimeHeaderFieldVal(cd,"filename");
    //debug
    //fprintf(stderr,"cgiParseMultipart: main:[%s], name:[%s], filename:[%s]\n",cdMain,cdName,cdFileName);
    //fflush(stderr);
    if (!sameString(cdMain,"form-data"))
	errAbort("main content-type expected [form-data], found [%s]",cdMain);

    //debug
    //fprintf(stderr,"GALT: mp->size[%llu], mp->binary=[%d], mp->fileName=[%s], mp=>data:[%s]\n",
        //(unsigned long long) mp->size, mp->binary, mp->fileName,
        //mp->binary && mp->data ? "<binary data not safe to print>" : mp->data);
    //fflush(stderr);

    /* filename if there is one */
    /* Internet Explorer on Windows is sending full path names, strip
     * directory name from those.  Using \ and / and : as potential
     * path separator characters, e.g.:
     *	 C:\Documents and Settings\tmp\file.txt.gz
     */
    if (cdFileName)
	{
	char *lastPathSep = strrchr(cdFileName, (int) '\\');
	if (!lastPathSep)
		lastPathSep = strrchr(cdFileName, (int) '/');
	if (!lastPathSep)
		lastPathSep = strrchr(cdFileName, (int) ':');
	char varNameFilename[256];
	safef(varNameFilename, sizeof(varNameFilename), "%s__filename", cdName);
	AllocVar(el);
	if (lastPathSep)
	    el->val = cloneString(lastPathSep+1);
	else
	    el->val = cloneString(cdFileName);
        slAddHead(&list, el);
        hashAddSaveName(hash, varNameFilename, el, &el->name);
        }

    if (mp->data)
        {
        if (mp->binary)
	    {
	    char varNameBinary[256];
	    char addrSizeBuf[40];
	    safef(varNameBinary,sizeof(varNameBinary),"%s__binary",cdName);
            safef(addrSizeBuf,sizeof(addrSizeBuf),"%lu %llu",
		(unsigned long)mp->data,
		(unsigned long long)mp->size);
	    AllocVar(el);
	    el->val = cloneString(addrSizeBuf);
	    slAddHead(&list, el);
	    hashAddSaveName(hash, varNameBinary, el, &el->name);
	    }
	else  /* normal variable, not too big, does not contain zeros */
	    {
	    AllocVar(el);
	    el->val = mp->data;
	    slAddHead(&list, el);
	    hashAddSaveName(hash, cdName, el, &el->name);
	    }
        }
    else if (mp->fileName)
	{
	char varNameData[256];
	safef(varNameData, sizeof(varNameData), "%s__data", cdName);
	AllocVar(el);
        el->val = mp->fileName;
        slAddHead(&list, el);
        hashAddSaveName(hash, varNameData, el, &el->name);
        //debug
        //fprintf(stderr,"GALT special: saved varNameData:[%s], mp=>fileName:[%s]\n",el->name,el->val);
        //fflush(stderr);
        }
    else if (mp->multi)
	{
	warn("unexpected nested MIME structures");
	}
    else
	{
	errAbort("mp-> type not data,fileName, or multi - unexpected MIME structure");
	}

    freez(&cdMain);
    freez(&cdName);
    freez(&cdFileName);
    }

slReverse(&list);
*retList = list;
*retHash = hash;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x7d0, %rsp            # imm = 0x7D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x418(%rbp)
movq $0x0, -0x420(%rbp)
movl $0x100, %edi            # imm = 0x100
callq 0xf030
movq %rax, -0x428(%rbp)
movq $0x0, -0x430(%rbp)
movq $0x0, -0x438(%rbp)
movq $0x0, -0x440(%rbp)
movl $0x6, %edi
movl $0x1, %esi
callq 0x10f70
movq %rax, -0x448(%rbp)
movq $0x0, -0x450(%rbp)
movq 0x12795(%rip), %rax     # 0x31fd8
movq (%rax), %rax
movq %rax, -0x440(%rbp)
movq -0x440(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1f95e
movq -0x440(%rbp), %rax
movq (%rax), %rsi
leaq 0x79fe(%rip), %rdi      # 0x2726d
callq 0xa4b0
cmpl $0x0, %eax
je 0x1f945
leaq -0x410(%rbp), %rdi
movq -0x440(%rbp), %rax
movq (%rax), %rcx
movl $0x400, %esi            # imm = 0x400
leaq 0x4d12(%rip), %rdx      # 0x245ac
movb $0x0, %al
callq 0xdc90
leaq -0x410(%rbp), %rdi
movl $0x5f, %esi
callq 0x3250
movq %rax, -0x418(%rbp)
cmpq $0x0, -0x418(%rbp)
jne 0x1f8db
movq -0x440(%rbp), %rax
movq (%rax), %rsi
leaq 0x79a2(%rip), %rdi      # 0x27276
movb $0x0, %al
callq 0xfd50
movq -0x418(%rbp), %rax
movb $0x2d, (%rax)
leaq -0x410(%rbp), %rdi
movl $0x3d, %esi
callq 0x3250
movq %rax, -0x418(%rbp)
cmpq $0x0, -0x418(%rbp)
jne 0x1f91f
movq -0x440(%rbp), %rax
movq (%rax), %rsi
leaq 0x7993(%rip), %rdi      # 0x272ab
movb $0x0, %al
callq 0xfd50
movq -0x418(%rbp), %rax
movb $0x3a, (%rax)
movq -0x428(%rbp), %rdi
leaq -0x410(%rbp), %rdx
leaq 0x79a2(%rip), %rsi      # 0x272e0
movb $0x0, %al
callq 0xf550
jmp 0x1f947
movq -0x440(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x440(%rbp)
jmp 0x1f84d
movq -0x428(%rbp), %rdi
leaq 0x7b6e(%rip), %rsi      # 0x274da
callq 0xf3c0
xorl %edi, %edi
callq 0x20b60
movq %rax, -0x430(%rbp)
movq -0x430(%rbp), %rax
movq %rax, -0x7c8(%rbp)
movq -0x428(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7530
movq -0x7c8(%rbp), %rdi
movq %rax, %rsi
callq 0x21080
movq %rax, -0x438(%rbp)
leaq -0x428(%rbp), %rdi
callq 0xf090
movq -0x438(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x1f9db
leaq 0x7911(%rip), %rdi      # 0x272e5
movb $0x0, %al
callq 0xfd50
movq -0x438(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x7915(%rip), %rsi      # 0x27302
callq 0x10d00
movq %rax, -0x420(%rbp)
movq -0x420(%rbp), %rsi
leaq 0x7798(%rip), %rdi      # 0x2719f
callq 0xa4b0
cmpl $0x0, %eax
jne 0x1fa26
movq -0x420(%rbp), %rsi
leaq 0x78f0(%rip), %rdi      # 0x2730f
movb $0x0, %al
callq 0xfd50
movq -0x438(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x438(%rbp)
cmpq $0x0, -0x438(%rbp)
je 0x1fe80
movq $0x0, -0x460(%rbp)
movq $0x0, -0x468(%rbp)
movq $0x0, -0x470(%rbp)
movq $0x0, -0x478(%rbp)
movq $0x0, -0x480(%rbp)
movq -0x438(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x78c9(%rip), %rsi      # 0x27358
callq 0x10d00
movq %rax, -0x460(%rbp)
movq -0x438(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x7855(%rip), %rsi      # 0x27302
callq 0x10d00
movq %rax, -0x480(%rbp)
movq -0x460(%rbp), %rdi
callq 0x20d50
movq %rax, -0x468(%rbp)
movq -0x460(%rbp), %rdi
leaq 0x78cd(%rip), %rsi      # 0x273a7
callq 0x20e40
movq %rax, -0x470(%rbp)
movq -0x460(%rbp), %rdi
leaq 0x78af(%rip), %rsi      # 0x273a3
callq 0x20e40
movq %rax, -0x478(%rbp)
movq -0x468(%rbp), %rdi
leaq 0x769b(%rip), %rsi      # 0x271a9
callq 0x3410
cmpl $0x0, %eax
je 0x1fb2d
movq -0x468(%rbp), %rsi
leaq 0x7846(%rip), %rdi      # 0x2736c
movb $0x0, %al
callq 0xfd50
cmpq $0x0, -0x478(%rbp)
je 0x1fc45
movq -0x478(%rbp), %rdi
movl $0x5c, %esi
callq 0x32a0
movq %rax, -0x488(%rbp)
cmpq $0x0, -0x488(%rbp)
jne 0x1fb75
movq -0x478(%rbp), %rdi
movl $0x2f, %esi
callq 0x32a0
movq %rax, -0x488(%rbp)
cmpq $0x0, -0x488(%rbp)
jne 0x1fb97
movq -0x478(%rbp), %rdi
movl $0x3a, %esi
callq 0x32a0
movq %rax, -0x488(%rbp)
leaq -0x590(%rbp), %rdi
movq -0x470(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x77ee(%rip), %rdx      # 0x2739f
movb $0x0, %al
callq 0xdc90
movl $0x20, %edi
callq 0x152f0
movq %rax, -0x458(%rbp)
cmpq $0x0, -0x488(%rbp)
je 0x1fbf3
movq -0x488(%rbp), %rdi
addq $0x1, %rdi
callq 0x7530
movq %rax, %rcx
movq -0x458(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1fc0d
movq -0x478(%rbp), %rdi
callq 0x7530
movq %rax, %rcx
movq -0x458(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x458(%rbp), %rsi
leaq -0x450(%rbp), %rdi
callq 0x1c050
movq -0x448(%rbp), %rdi
leaq -0x590(%rbp), %rsi
movq -0x458(%rbp), %rdx
movq -0x458(%rbp), %rcx
addq $0x8, %rcx
callq 0x10b20
movq -0x438(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x1fd82
movq -0x438(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x1fd1e
leaq -0x690(%rbp), %rdi
movq -0x470(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x772a(%rip), %rdx      # 0x273ac
movb $0x0, %al
callq 0xdc90
leaq -0x6c0(%rbp), %rdi
movq -0x438(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x438(%rbp), %rax
movq 0x10(%rax), %r8
movl $0x28, %esi
leaq 0x7705(%rip), %rdx      # 0x273b7
movb $0x0, %al
callq 0xdc90
movl $0x20, %edi
callq 0x152f0
movq %rax, -0x458(%rbp)
leaq -0x6c0(%rbp), %rdi
callq 0x7530
movq %rax, %rcx
movq -0x458(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x458(%rbp), %rsi
leaq -0x450(%rbp), %rdi
callq 0x1c050
movq -0x448(%rbp), %rdi
leaq -0x690(%rbp), %rsi
movq -0x458(%rbp), %rdx
movq -0x458(%rbp), %rcx
addq $0x8, %rcx
callq 0x10b20
jmp 0x1fd7d
movl $0x20, %edi
callq 0x152f0
movq %rax, -0x458(%rbp)
movq -0x438(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x458(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x458(%rbp), %rsi
leaq -0x450(%rbp), %rdi
callq 0x1c050
movq -0x448(%rbp), %rdi
movq -0x470(%rbp), %rsi
movq -0x458(%rbp), %rdx
movq -0x458(%rbp), %rcx
addq $0x8, %rcx
callq 0x10b20
jmp 0x1fe46
movq -0x438(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1fe16
leaq -0x7c0(%rbp), %rdi
movq -0x470(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x7612(%rip), %rdx      # 0x273c0
movb $0x0, %al
callq 0xdc90
movl $0x20, %edi
callq 0x152f0
movq %rax, -0x458(%rbp)
movq -0x438(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x458(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x458(%rbp), %rsi
leaq -0x450(%rbp), %rdi
callq 0x1c050
movq -0x448(%rbp), %rdi
leaq -0x7c0(%rbp), %rsi
movq -0x458(%rbp), %rdx
movq -0x458(%rbp), %rcx
addq $0x8, %rcx
callq 0x10b20
jmp 0x1fe44
movq -0x438(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x1fe34
leaq 0x759e(%rip), %rdi      # 0x273c9
movb $0x0, %al
callq 0xf8f0
jmp 0x1fe42
leaq 0x75b0(%rip), %rdi      # 0x273eb
movb $0x0, %al
callq 0xfd50
jmp 0x1fe44
jmp 0x1fe46
leaq -0x468(%rbp), %rdi
callq 0x15410
leaq -0x470(%rbp), %rdi
callq 0x15410
leaq -0x478(%rbp), %rdi
callq 0x15410
movq -0x438(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x438(%rbp)
jmp 0x1fa38
leaq -0x450(%rbp), %rdi
callq 0x7bb0
movq -0x450(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x448(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
addq $0x7d0, %rsp            # imm = 0x7D0
popq %rbp
retq
nopw %cs:(%rax,%rax)