void IFCParser::parse() { wstring line = getline(); if (line != FILE_TYPE) THROW_BAD_FORMAT(FILE_TYPE, lineno); parseheader(); parsedata(); line = getline(); if (line != END_FILE_TYPE) THROW_BAD_FORMAT(END_FILE_TYPE, lineno); }
static void fill_cache(message_data_t *m) { rewind(m->data); /* let's fill that header cache */ for (;;) { char *name, *body; strarray_t *contents; if (parseheader(m->data, &name, &body) < 0) { break; } /* put it in the hash table */ contents = (strarray_t *)hash_lookup(name, &m->cache); if (!contents) contents = hash_insert(name, strarray_new(), &m->cache); strarray_appendm(contents, body); free(name); } m->cache_full = 1; }
static char* genhttp(Protocol *proto, char *host, char *req, HTTPHeader *hdr, int wfd, int rfd, vlong rtotal) { int n, m, total, want; char buf[8192], *data; Pfd *fd; if(chattyhttp > 1){ fprint(2, "--HTTP Request:\n"); fprint(2, "%s", req); fprint(2, "--\n"); } fd = proto->connect(host); if(fd == nil){ if(chattyhttp > 0) fprint(2, "connect %s: %r\n", host); return nil; } n = strlen(req); if(proto->write(fd, req, n) != n){ if(chattyhttp > 0) fprint(2, "write %s: %r\n", host); proto->close(fd); return nil; } if(rfd >= 0){ while(rtotal > 0){ m = sizeof buf; if(m > rtotal) m = rtotal; if((n = read(rfd, buf, m)) <= 0){ fprint(2, "read: missing data\n"); proto->close(fd); return nil; } if(proto->write(fd, buf, n) != n){ fprint(2, "write data: %r\n"); proto->close(fd); return nil; } rtotal -= n; } } total = 0; while(!haveheader(buf, total)){ n = proto->read(fd, buf+total, sizeof buf-total); if(n <= 0){ if(chattyhttp > 0) fprint(2, "read missing header\n"); proto->close(fd); return nil; } total += n; } n = parseheader(buf, total, hdr); if(n < 0){ fprint(2, "failed response parse: %r\n"); proto->close(fd); return nil; } if(hdr->contentlength >= MaxResponse){ werrstr("response too long"); proto->close(fd); return nil; } if(hdr->contentlength >= 0 && n > hdr->contentlength) n = hdr->contentlength; want = sizeof buf; data = nil; total = 0; goto didread; while(want > 0 && (n = proto->read(fd, buf, want)) > 0){ didread: if(wfd >= 0){ if(writen(wfd, buf, n) < 0){ proto->close(fd); werrstr("write error"); return nil; } }else{ data = erealloc(data, total+n); memmove(data+total, buf, n); } total += n; if(total > MaxResponse){ proto->close(fd); werrstr("response too long"); return nil; } if(hdr->contentlength >= 0 && total + want > hdr->contentlength) want = hdr->contentlength - total; } proto->close(fd); if(hdr->contentlength >= 0 && total != hdr->contentlength){ werrstr("got wrong content size %d %d", total, hdr->contentlength); return nil; } hdr->contentlength = total; if(wfd >= 0) return (void*)1; else{ data = erealloc(data, total+1); data[total] = 0; } return data; }
int main(int argc, char **argv) { char *indir = NULL; int x = 1, origoffset = 0; if(argc<3) { printf("usage: %s [-z] indir outbin",argv[0]); return 0; } if(strncmp(argv[1],"-z",2)==0) { compressFile = 1; x++; } outfile = fopen(argv[x+1],"wb"); indir = (char*)calloc(strlen(argv[x])+3,sizeof(char)); strcpy(indir, argv[x]); strcat(indir,"\\*"); outname = argv[x+1]; parsecount(indir); //printf("%08x %08x\n",fileCount, offset); parseheader(indir); origoffset = listfiles*8; offset = origoffset; parsedir(indir); if(compressFile) { FILE *infile = fopen(argv[x+1], "rb"); unsigned char *buffer = NULL, *comp = NULL; int len = 0; printf("Compressing file...\n"); fseek(infile,0,SEEK_END); len = ftell(infile); rewind(infile); buffer = (unsigned char*)calloc(len,sizeof(unsigned char)); fread(buffer,1,len,infile); fclose(infile); printf("%08x\n",len); comp = CompressLZLR(buffer, len); free(buffer); outfile = fopen(argv[x+1],"wb"); fwrite(comp,1,lzlroutlen,outfile); fclose(outfile); free(comp); } return 0; }