void get_line(int row, int startcol, int stopcol, BYTE *pixels) { if (startcol + g_logical_screen_x_offset >= g_screen_x_dots || row + g_logical_screen_y_offset >= g_screen_y_dots) return; lineread(row + g_logical_screen_y_offset, startcol + g_logical_screen_x_offset, stopcol + g_logical_screen_x_offset, pixels); }
void cgi_init( int *result ) { int cl, i, in_multipart_headers, which_entry, length_gotten; char * boundary; /* Default, no errors, no name/value pairs ("entries"): */ cgi_errno = CGIERR_NONE; cgi_num_entries = 0; length_gotten = 0; /* Check for REQUEST_METHOD (set by HTTP server): */ if (getenv("REQUEST_METHOD") == NULL) { /* None set? Assume the user is invoking the CGI from a shell prompt (for debugging): */ cgi_request_method = CGIREQ_NONE; } else { /* Determine the exact request method, and grab the data (if any) in the appropriate manner: */ if (strcmp(getenv("REQUEST_METHOD"), "POST") == 0) { /* Post method (data is sent to us via "stdin"): */ cgi_request_method = CGIREQ_POST; if (getenv("CONTENT_TYPE") == NULL) { /* Content type is not set! */ cgi_errno = CGIERR_INCORRECT_TYPE; cgi_content_type = CGITYPE_UNKNOWN; CGI_RETURN(cgi_errno); } else if (strcmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded") == 0) { cgi_content_type = CGITYPE_APPLICATION_X_WWW_FORM_URLENCODED; /* How much data do we expect? */ if (getenv("CONTENT_LENGTH") == NULL || sscanf(getenv("CONTENT_LENGTH"), "%d", &cl) != 1) { cgi_errno = CGIERR_BAD_CONTENT_LENGTH; CGI_RETURN(cgi_errno); } /* Create space for it: */ cgi_query = malloc(cl + 1); /* 2.0.1 - Tadek Orlowski ([email protected]) ... "+1" */ if (cgi_query == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; CGI_RETURN(cgi_errno); } /* Read it in: */ fgets(cgi_query, cl + 1, stdin); /* Verify that we got as much data as we expected: */ if (strlen(cgi_query) != cl) cgi_errno = CGIERR_CONTENT_LENGTH_DISCREPANCY; } else if (strstr(getenv("CONTENT_TYPE"), "multipart/form-data") == getenv("CONTENT_TYPE")) { cgi_content_type = CGITYPE_MULTIPART_FORM_DATA; cgi_query = malloc(2050); if (cgi_query == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; CGI_RETURN(cgi_errno); } /* Determine the boundary string: */ if (strstr(getenv("CONTENT_TYPE"), "boundary=") == NULL) { cgi_errno = CGIERR_NO_BOUNDARY; CGI_RETURN(cgi_errno); } boundary = strdup(strstr(getenv("CONTENT_TYPE"), "boundary=") + 9); debug("boundary", boundary); /* Read in until there's no more: */ in_multipart_headers = 0; which_entry = -1; do { length_gotten = lineread(stdin, cgi_query, 2048); debug("cgi_query", cgi_query); if (length_gotten > 0) { if (strstr(cgi_query, boundary) == cgi_query + 2 && cgi_query[0] == '-' && cgi_query[1] == '-') { /* We got a boundary! */ in_multipart_headers = 1; which_entry = -1; } else /* (Not a boundary) */ { if (in_multipart_headers == 1) { /* We had just got a boundary, read headers: */ if (cgi_query[0] == '\r' || cgi_query[0] == '\n') { /* Blank line, end of headers: */ in_multipart_headers = 0; } else /* (Not a blank line) */ { /* What kind of header is it? */ if (strstr(cgi_query, "Content-Disposition: ") == cgi_query) { /* Content-disposition: */ /* For now, just look for "name=": */ if (strstr(cgi_query, "name=\"") != NULL) { /* Add a new entry: */ which_entry = cgi_num_entries; cgi_num_entries++; /* Make more room: */ cgi_entries = realloc(cgi_entries, sizeof(cgi_entry_type) * cgi_num_entries); if (cgi_entries == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; CGI_RETURN(cgi_errno); } /* Fill in the name slot: */ cgi_entries[which_entry].name = strdup(strstr(cgi_query, "name=\"") + 6); /* Truncate after quote: */ if (strchr(cgi_entries[which_entry]. name, '\"') != NULL) { strcpy(strchr(cgi_entries [which_entry].name, '\"'), "\0"); } /* Set default content-type: */ cgi_entries[which_entry]. content_type = "application/octet-stream"; /* Set default content-length: */ cgi_entries[which_entry]. content_length = 0; /* Set default value: */ cgi_entries[which_entry].val = strdup(""); debug("entry.name", cgi_entries[which_entry].name); } } else if (strstr(cgi_query, "Content-Type: ") == cgi_query) { /* Content-type: */ cgi_entries[which_entry].content_type = strdup(strstr(cgi_query, "Content-Type: ") + 14); debug("entry.content_type", cgi_entries[which_entry]. content_type); } } } else /* in_multipart_headers == 0 */ { /* If we're recording into a particular entry, copy the data: */ if (which_entry != -1) { /* Make more room: */ cgi_entries[which_entry].val = realloc(cgi_entries[which_entry].val, strlen(cgi_entries[which_entry]. val) + length_gotten + 1); if (cgi_entries[which_entry].val == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; CGI_RETURN(cgi_errno); } /* Append the data: */ memcpy(cgi_entries[which_entry].val + (cgi_entries[which_entry]. content_length), cgi_query, length_gotten); cgi_entries[which_entry].content_length = (cgi_entries[which_entry].content_length + length_gotten); } } } } } while (length_gotten > 0); free(cgi_query); } else { /* Content type is unrecognized! */ cgi_errno = CGIERR_INCORRECT_TYPE; cgi_content_type = CGITYPE_UNKNOWN; CGI_RETURN(cgi_errno); } } else if (strcmp(getenv("REQUEST_METHOD"), "GET") == 0) { /* For now, assume Content Type of "application/x-www-form-urlencoded" (Is this a bad assumption?) */ cgi_content_type = CGITYPE_APPLICATION_X_WWW_FORM_URLENCODED; /* GET method (data sent via "QUERY_STRING" env. variable): */ cgi_request_method = CGIREQ_GET; /* Get a pointer to the data: */ cgi_query = getenv("QUERY_STRING"); if (cgi_query == NULL) { /* Does the "QUERY_STRING" env. variable not exist!? */ cgi_errno = CGIERR_NULL_QUERY_STRING; CGI_RETURN(cgi_errno); } else { /* Determine the content length by seeing how big the string is: */ cl = strlen(cgi_query); } } else { /* Something else? We can't handle it! */ cgi_request_method = CGIREQ_UNKNOWN; cgi_errno = CGIERR_UNKNOWN_METHOD; cgi_num_entries = 0; CGI_RETURN(cgi_errno); } if (cgi_content_type != CGITYPE_MULTIPART_FORM_DATA) { /* How many entries (name/value pairs) do we need to allocate space for? (They should be separated by "&"'s) */ cgi_num_entries = 0; for (i = 0; i <= cl; i++) if (cgi_query[i] == '&' || cgi_query[i] == '\0') cgi_num_entries++; /* Allocate the space for that many structures: */ cgi_entries = malloc(sizeof(cgi_entry_type) * cgi_num_entries); if (cgi_entries == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; CGI_RETURN(cgi_errno); } /* Grab each name/value pair: */ cgi_num_entries = 0; /* (Begin with the first half of the first pair): */ if (cgi_query[0] != '\0' && cgi_query[0] != '&') { cgi_entries[0].name = cgi_query; cgi_entries[0].content_type = "text/html"; } /* Go through the entire string of characters: */ for (i = 0; i <= cl; i++) { if (cgi_query[i] == '&') { /* "&" represents the end of a name/value pair: */ cgi_entries[cgi_num_entries].name = cgi_query + i + 1; cgi_entries[cgi_num_entries].content_type = "text/html"; cgi_query[i] = '\0'; } else if (cgi_query[i] == '=') { /* "=" is the end of the name half of a name/value pair: */ cgi_entries[cgi_num_entries].val = cgi_query + i + 1; /* plustospace(cgi_entries[cgi_num_entries].val); unescape_url(cgi_entries[cgi_num_entries].val); */ cgi_num_entries++; cgi_query[i] = '\0'; } } for (i = 0; i < cgi_num_entries; i++) { plustospace(cgi_entries[i].val); unescape_url(cgi_entries[i].val); } } /* Fix any NULL strings to be empty strings */ /* 2.0.4 - MJ Pomraning ([email protected]) */ for (i = 0; i < cgi_num_entries; i++) { if (cgi_entries[i].name == NULL) cgi_entries[i].name = ""; if (cgi_entries[i].val == NULL) cgi_entries[i].val = ""; } } CGI_RETURN(CGIERR_NONE); }