headers_t headers_parse( char* raw ) { headers_t headers; header_t* header; memset( &headers, 0, sizeof( headers ) ); char* ptr; while ( headers.num < sizeof( headers ) / sizeof( header_t ) && !startswith( raw, HTTP_HDR_ENDL ) && *raw != '\n' && *raw != '\0' ) { header = &headers.list[ headers.num ]; // name ptr = strstr( raw, ":" ); if ( !ptr || !( ptr - raw ) ) break; strncpy( header->name, raw, SMALLEST( ptr - raw, sizeof( header->name ) - 1 ) ); header_formatname( strtolower( headers.list[ headers.num ].name ) ); raw = ptr + 1; while ( *raw == ' ' ) raw++; // value if ( !( ptr = strstr( raw, HTTP_HDR_ENDL ) ) ) break; strncpy( header->value, raw, SMALLEST( ptr - raw, sizeof( header->value ) - 1 ) ); raw = ptr + strlen( HTTP_HDR_ENDL ); headers.num++; } return headers; }
int main() { int x,y,z; printf("Enter thr 3 numbers\n"); scanf("%d%d%d",&x,&y,&z); SMALLEST(x,y,z); getch(); }
static jdwpError writeBytes(PacketOutputStream *stream, void *source, int size) { jbyte *bytes = (jbyte *)source; if (stream->error) { return stream->error; } while (size > 0) { jint count; if (stream->left == 0) { jint segSize = SMALLEST(2 * stream->segment->length, MAX_SEGMENT_SIZE); jbyte *newSeg = jvmtiAllocate(segSize); struct PacketData *newHeader = jvmtiAllocate(sizeof(*newHeader)); if ((newSeg == NULL) || (newHeader == NULL)) { jvmtiDeallocate(newSeg); jvmtiDeallocate(newHeader); stream->error = JDWP_ERROR(OUT_OF_MEMORY); return stream->error; } newHeader->length = 0; newHeader->data = newSeg; newHeader->next = NULL; stream->segment->next = newHeader; stream->segment = newHeader; stream->current = newHeader->data; stream->left = segSize; } count = SMALLEST(size, stream->left); (void)memcpy(stream->current, bytes, count); stream->current += count; stream->left -= count; stream->segment->length += count; size -= count; bytes += count; } return JDWP_ERROR(NONE); }
static void add(char *s1, char *s2) { char *a, *b, *c; int la, lb, len; int i, j; int carry; int add; a = BIGGEST(s1, s2); b = SMALLEST(s1, s2); la = strlen(a); lb = strlen(b); len = la + 1; if (!(c = memset(malloc((len + 1) * sizeof(char)), '0', len))) return ; c[len] = 0; for (i = la - 1, j = lb - 1, carry = 0; i >= IS_NEG(a); --i, --j) { if (IS_NEG(a) == IS_NEG(b)) { add = ALIGN(a, i) + ALIGN(b, j) + carry; carry = add / 10; } else { add = ALIGN(a, i) - ALIGN(b, j) - carry; carry = (add < 0); add = POS(add); } c[i + 1] = ASCII(POS(add % 10)); } if (IS_NEG(a) && (a != SMALLEST(b, a) || IS_NEG(b))) printf("-"); print_nb(c); free(c); }
BOOL8 PIXROW::extend( //image array IMAGELINE *imlines, BOX &imbox, PIXROW *prev, //for prev blob PIXROW *next, //for next blob INT16 foreground_colour) { INT16 i; INT16 x_offset = imbox.left (); INT16 limit; INT16 left_limit; INT16 right_limit; UINT8 *pixels = NULL; UINT8 *pixels_below = NULL; //row below current UINT8 *pixels_above = NULL; //row above current BOOL8 changed = FALSE; pixels_above = imlines[0].pixels; for (i = 0; i < row_count; i++) { pixels_below = pixels; pixels = pixels_above; if (i < (row_count - 1)) pixels_above = imlines[i + 1].pixels; else pixels_above = NULL; /* Extend Left by one pixel*/ if (prev == NULL || prev->max[i] < prev->min[i]) limit = imbox.left (); else limit = prev->max[i] + 1; if ((min[i] <= max[i]) && (min[i] > limit) && (pixels[min[i] - 1 - x_offset] == foreground_colour)) { min[i]--; changed = TRUE; } /* Extend Right by one pixel*/ if (next == NULL || next->min[i] > next->max[i]) limit = imbox.right () - 1;//-1 to index inside pix else limit = next->min[i] - 1; if ((min[i] <= max[i]) && (max[i] < limit) && (pixels[max[i] + 1 - x_offset] == foreground_colour)) { max[i]++; changed = TRUE; } /* Extend down by one row */ if (pixels_below != NULL) { if (min[i] < min[i - 1]) { //row goes left of row below if (prev == NULL || prev->max[i - 1] < prev->min[i - 1]) left_limit = min[i]; else left_limit = LARGEST (min[i], prev->max[i - 1] + 1); } else left_limit = min[i - 1]; if (max[i] > max[i - 1]) { //row goes right of row below if (next == NULL || next->min[i - 1] > next->max[i - 1]) right_limit = max[i]; else right_limit = SMALLEST (max[i], next->min[i - 1] - 1); } else right_limit = max[i - 1]; while ((left_limit <= right_limit) && (pixels_below[left_limit - x_offset] != foreground_colour)) left_limit++; //find black extremity if ((left_limit <= right_limit) && (left_limit < min[i - 1])) { min[i - 1] = left_limit; //widen left if poss changed = TRUE; } while ((left_limit <= right_limit) && (pixels_below[right_limit - x_offset] != foreground_colour)) right_limit--; //find black extremity if ((left_limit <= right_limit) && (right_limit > max[i - 1])) { max[i - 1] = right_limit;//widen right if poss changed = TRUE; } } /* Extend up by one row */ if (pixels_above != NULL) { if (min[i] < min[i + 1]) { //row goes left of row above if (prev == NULL || prev->min[i + 1] > prev->max[i + 1]) left_limit = min[i]; else left_limit = LARGEST (min[i], prev->max[i + 1] + 1); } else left_limit = min[i + 1]; if (max[i] > max[i + 1]) { //row goes right of row above if (next == NULL || next->min[i + 1] > next->max[i + 1]) right_limit = max[i]; else right_limit = SMALLEST (max[i], next->min[i + 1] - 1); } else right_limit = max[i + 1]; while ((left_limit <= right_limit) && (pixels_above[left_limit - x_offset] != foreground_colour)) left_limit++; //find black extremity if ((left_limit <= right_limit) && (left_limit < min[i + 1])) { min[i + 1] = left_limit; //widen left if poss changed = TRUE; } while ((left_limit <= right_limit) && (pixels_above[right_limit - x_offset] != foreground_colour)) right_limit--; //find black extremity if ((left_limit <= right_limit) && (right_limit > max[i + 1])) { max[i + 1] = right_limit;//widen right if poss changed = TRUE; } } } return changed; }
connection_t* connection_create( website_t* website, int sockfd, char* raw, size_t size ) { connection_t* connection = connection_init(); connection->website = website; connection->sockfd = sockfd; char* ptr,* tmp,* start = raw, urlbuf[ sizeof( connection->request.full_url ) ]; memcpy( &connection->ip, raw, sizeof( connection->ip ) ); raw += sizeof( connection->ip ); strncpy( connection->hostname, raw, sizeof( connection->hostname ) ); raw += strlen( raw ) + 1; // type ///////////////////////////////// if ( startswith( raw, HTTP_GET ) ) { connection->request.type = HTTP_GET_TYPE; raw += strlen( HTTP_GET ); } else if ( startswith( raw, HTTP_POST ) ) { connection->request.type = HTTP_POST_TYPE; raw += strlen( HTTP_POST ); } else goto fail; raw++; // skip over space //////////////////////////////////////// // url ///////////////////////////////// if ( !( tmp = strstr( raw, HTTP_HDR_ENDL ) ) ) goto fail; if ( !( ptr = strnstr( raw, "?", ( tmp - raw ) ) ) && !( ptr = strnstr( raw, " ", ( tmp - raw ) ) ) ) goto fail; url_decode( urlbuf, raw, ptr - raw ); normalize_path( connection->request.full_url, urlbuf ); while ( startswith( connection->request.full_url, "../" ) ) { strcpy( urlbuf, connection->request.full_url + 3 ); strcpy( connection->request.full_url, urlbuf ); } // Skip over websites leading url. // if website is "example.com/test/" and url is "/test/hi" "/test" gets removed connection->request.url = strchr( connection->website->url, '/' ); if ( connection->request.url ) connection->request.url = &connection->request.full_url[ ( connection->website->url + strlen( connection->website->url ) ) - connection->request.url ]; else connection->request.url = connection->request.full_url; if ( *connection->request.url == '/' ) connection->request.url++; //////////////////////////////////////// // parse qstring params //////////////// tmp = strstr( raw, HTTP_HDR_ENDL ); if ( ptr[ 0 ] == '?' ) { raw = ptr + 1; if ( !( ptr = strnstr( raw, " ", tmp - raw ) ) ) goto fail; params_parse_qs( &connection->request.params, raw, ptr - raw, PARAM_TYPE_GET ); } raw = ptr + 1; //////////////////////////////////////// // version ///////////////////////////// if ( !startswith( raw, "HTTP/" ) ) goto fail; raw += 5; // skips "HTTP/" if ( !( ptr = strstr( raw, HTTP_HDR_ENDL ) ) ) goto fail; strncpy( connection->http_version, raw, SMALLEST( ptr - raw, sizeof( connection->http_version ) - 1 ) ); connection->http_version[ SMALLEST( ptr - raw, sizeof( connection->http_version ) - 1 ) ] = '\0'; raw = ptr + strlen( HTTP_HDR_ENDL ); //////////////////////////////////////// // headers ///////////////////////////// connection->request.headers = headers_parse( raw ); //////////////////////////////////////// // cookies ///////////////////////////// header_t* header = headers_get_header( &connection->request.headers, "Cookie" ); if ( header ) connection->cookies = cookies_parse( header->value ); else connection->cookies = cookies_parse( "" ); //////////////////////////////////////// // body //////////////////////////////// if ( ( connection->request.type == HTTP_POST_TYPE ) && ( ptr = strstr( raw, HTTP_HDR_ENDL HTTP_HDR_ENDL ) ) != NULL ) { ptr += strlen( HTTP_HDR_ENDL HTTP_HDR_ENDL ); header_t* header = headers_get_header( &connection->request.headers, "Content-Type" ); char boundary[HEADER_VALUE_MAX_LEN] = "",* boundary_ptr = NULL; if ( website_options_get( website, WS_OPTION_PARSE_MULTIPARTS ) && header && startswith( headers_get_header_attr( header, NULL ), "multipart" ) && ( boundary_ptr = headers_get_header_attr( header, "boundary" ) ) ) { printf("parsing multiparts: %d\n", website->options ); strcat( boundary, "--" ); strcat( boundary, boundary_ptr ); int i = -1; if ( startswith( ptr, boundary ) ) { ptr += strlen( boundary ) + strlen( HTTP_HDR_ENDL ); while ( ( boundary_ptr = strstr( ptr, boundary ) ) ) { connection->request.parts[++i] = (request_t*) malloc( sizeof( request_t ) ); memset( connection->request.parts[i], 0, sizeof( request_t ) ); connection->request.parts[i]->headers = headers_parse( ptr ); connection->request.parts[i]->post_body = NULL; if ( !( ptr = strstr( ptr, HTTP_HDR_ENDL HTTP_HDR_ENDL ) ) ) break; ptr += sizeof( HTTP_HDR_ENDL HTTP_HDR_ENDL ) - 1; connection->request.parts[i]->post_body_len = ( boundary_ptr - ptr ) - strlen( HTTP_HDR_ENDL ); connection->request.parts[i]->post_body = (char*) malloc( connection->request.parts[i]->post_body_len + 1 ); memset( connection->request.parts[i]->post_body, 0, connection->request.parts[i]->post_body_len + 1 ); strncpy(connection->request.parts[i]->post_body, ptr, connection->request.parts[i]->post_body_len ); char* charset; header = headers_get_header( &connection->request.parts[i]->headers, "Content-Type" ); if ( header && ( charset = headers_get_header_attr( header, "charset" ) ) ) strcpy( connection->request.parts[i]->charset, charset ); else // ISO-8859-1 is the default charset for http defined by RFC strcpy( connection->request.parts[i]->charset, "ISO-8859-1" ); ptr = boundary_ptr + strlen( boundary ); if ( startswith( ptr, "--" HTTP_HDR_ENDL ) || i >= CONN_MAX_HTTP_MULTIPART ) break; ptr += strlen( HTTP_HDR_ENDL ); } if ( header && strcmp( headers_get_header_attr( header, NULL ), "multipart/form-data" ) == 0 ) params_parse_multiparts( &connection->request.params, connection->request.parts, PARAM_TYPE_POST ); } } else { connection->request.post_body_len = size - (long) ( ptr - start ); connection->request.post_body = (char*) malloc( connection->request.post_body_len + 1 ); memset( connection->request.post_body, 0, connection->request.post_body_len + 1 ); strncpy( connection->request.post_body, ptr, connection->request.post_body_len ); if ( header && strcmp( headers_get_header_attr( header, NULL ), "application/x-www-form-urlencoded" ) == 0 ) params_parse_qs( &connection->request.params, connection->request.post_body, connection->request.post_body_len, PARAM_TYPE_POST ); } char* charset; header = headers_get_header( &connection->request.headers, "Content-Type" ); if ( header && ( charset = headers_get_header_attr( header, "charset" ) ) ) strcpy( connection->request.charset, charset ); else // ISO-8859-1 is the default charset for http defined by RFC strcpy( connection->request.charset, "ISO-8859-1" ); } //////////////////////////////////////// return connection; fail: connection_free( connection ); return NULL; }