Subversion Repositories RepoView

Rev

Rev 41 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 41 Rev 42
Line 1... Line 1...
1
/* $Id: multipart.c 41 2024-08-22 04:26:35Z nishi $ */
1
/* $Id: multipart.c 42 2024-08-22 05:25:09Z nishi $ */
2
 
2
 
3
#include "rv_multipart.h"
3
#include "rv_multipart.h"
4
 
4
 
5
#include "rv_util.h"
5
#include "rv_util.h"
6
 
6
 
Line 37... Line 37...
37
	int incr = 0;
37
	int incr = 0;
38
	int phase = 0;
38
	int phase = 0;
39
	unsigned long long fstart = 0;
39
	unsigned long long fstart = 0;
40
	char* b = rv_strcat3("--", boundary, "\r");
40
	char* b = rv_strcat3("--", boundary, "\r");
41
	char* eb = rv_strcat3("--", boundary, "--\r");
41
	char* eb = rv_strcat3("--", boundary, "--\r");
-
 
42
	multipart_entries = malloc(sizeof(*multipart_entries));
-
 
43
	multipart_entries[0] = NULL;
-
 
44
	char* name = NULL;
42
	for(i = 0;; i++) {
45
	for(i = 0;; i++) {
43
		if(i == length || buffer[i] == '\n') {
46
		if(i == length || buffer[i] == '\n') {
44
			char* line = malloc(i - incr + 1);
47
			char* line = malloc(i - incr + 1);
45
			memcpy(line, buffer + incr, i - incr);
48
			memcpy(line, buffer + incr, i - incr);
46
			line[i - incr] = 0;
49
			line[i - incr] = 0;
Line 50... Line 53...
50
			} else if(strcmp(b, line) == 0 || strcmp(eb, line) == 0) {
53
			} else if(strcmp(b, line) == 0 || strcmp(eb, line) == 0) {
51
				if(phase == 1) {
54
				if(phase == 1) {
52
					unsigned long long fend = i - strlen(line) - 2;
55
					unsigned long long fend = i - strlen(line) - 2;
53
					char* data = buffer + fstart;
56
					char* data = buffer + fstart;
54
					unsigned long long datalen = fend - fstart;
57
					unsigned long long datalen = fend - fstart;
-
 
58
					struct multipart_entry* entry = malloc(sizeof(*entry));
-
 
59
					entry->length = datalen;
-
 
60
					entry->data = malloc(datalen);
-
 
61
					entry->name = rv_strdup(name);
-
 
62
					unsigned long long j;
-
 
63
					for(j = 0; j < datalen; j++) entry->data[j] = data[j];
-
 
64
 
-
 
65
					struct multipart_entry** old_entries = multipart_entries;
-
 
66
					for(j = 0; old_entries[j] != NULL; j++)
-
 
67
						;
-
 
68
					multipart_entries = malloc(sizeof(*multipart_entries) * (j + 2));
-
 
69
					for(j = 0; old_entries[j] != NULL; j++) {
-
 
70
						multipart_entries[j] = old_entries[j];
-
 
71
					}
-
 
72
					multipart_entries[j] = entry;
-
 
73
					multipart_entries[j + 1] = NULL;
-
 
74
					free(old_entries);
55
				}
75
				}
56
				phase = 0;
76
				phase = 0;
57
				if(strcmp(eb, line) == 0) {
77
				if(strcmp(eb, line) == 0) {
58
					free(line);
78
					free(line);
59
					break;
79
					break;
60
				}
80
				}
61
			} else if(phase == 0) {
81
			} else if(phase == 0) {
62
				line[strlen(line) - 1] = 0;
82
				line[strlen(line) - 1] = 0;
-
 
83
				int j;
-
 
84
				for(j = 0; line[j] != 0; j++) {
-
 
85
					if(line[j] == ':') {
-
 
86
						line[j] = 0;
-
 
87
						char* value = "";
-
 
88
						j++;
-
 
89
						for(; line[j] != 0; j++) {
-
 
90
							if(line[j] != ' ' && line[j] != '\t') {
-
 
91
								value = line + j;
-
 
92
								break;
-
 
93
							}
-
 
94
						}
-
 
95
						if(strcasecmp(line, "Content-Disposition") == 0) {
-
 
96
							int j;
-
 
97
							int incrval = 0;
-
 
98
							for(j = 0;; j++) {
-
 
99
								if(value[j] == ';' || value[j] == 0) {
-
 
100
									char oldc = value[j];
-
 
101
									value[j] = 0;
-
 
102
									char* kv = value + incrval;
-
 
103
									j++;
-
 
104
									for(; value[j] != 0; j++) {
-
 
105
										if(value[j] != ' ' && value[j] != '\t') break;
-
 
106
									}
-
 
107
									incrval = j;
-
 
108
									int k;
-
 
109
									for(k = 0; kv[k] != 0; k++) {
-
 
110
										if(kv[k] == '=') {
-
 
111
											kv[k] = 0;
63
				fprintf(stderr, "%s\n", line);
112
											if(strcmp(kv, "name") == 0) {
-
 
113
												if(name != NULL) free(name);
-
 
114
												name = malloc(strlen(kv + k + 1) + 1);
-
 
115
												char* nkv = kv + k + 1;
-
 
116
												int l = 0;
-
 
117
												int incrn = 0;
-
 
118
												for(l = 0; nkv[l] != 0; l++) {
-
 
119
													if(nkv[l] != '"') {
-
 
120
														name[incrn++] = nkv[l];
-
 
121
													}
-
 
122
												}
-
 
123
												name[incrn] = 0;
-
 
124
											}
-
 
125
											break;
-
 
126
										}
-
 
127
									}
-
 
128
									if(oldc == 0) break;
-
 
129
								}
-
 
130
							}
-
 
131
						}
-
 
132
						break;
-
 
133
					}
-
 
134
				}
64
			}
135
			}
65
			free(line);
136
			free(line);
66
			incr = i + 1;
137
			incr = i + 1;
67
			if(i == length) break;
138
			if(i == length) break;
68
		}
139
		}
69
	}
140
	}
-
 
141
	if(name != NULL) free(name);
70
	free(b);
142
	free(b);
71
}
143
}