1 /*- 2 * Copyright 2012 Matthew Endsley 3 * All rights reserved 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted providing that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 static const unsigned char http_chunk_state[] = { 28 /* * LF CR HEX */ 29 0xC1, 0xC1, 0xC1, 1, /* s0: initial hex char */ 30 0xC1, 0xC1, 2, 0x81, /* s1: additional hex chars, followed by CR */ 31 0xC1, 0x83, 0xC1, 0xC1, /* s2: trailing LF */ 32 0xC1, 0xC1, 4, 0xC1, /* s3: CR after chunk block */ 33 0xC1, 0xC0, 0xC1, 0xC1, /* s4: LF after chunk block */ 34 }; 35 36 int http_parse_chunked(int* state, int *size, char ch) 37 { 38 int newstate, code = 0; 39 switch (ch) { 40 case '\n': code = 1; break; 41 case '\r': code = 2; break; 42 case '0': case '1': case '2': case '3': 43 case '4': case '5': case '6': case '7': 44 case '8': case '9': case 'a': case 'b': 45 case 'c': case 'd': case 'e': case 'f': 46 case 'A': case 'B': case 'C': case 'D': 47 case 'E': case 'F': code = 3; break; 48 } 49 50 newstate = http_chunk_state[*state * 4 + code]; 51 *state = (newstate & 0xF); 52 53 switch (newstate) { geqo_selection(PlannerInfo * root,Chromosome * momma,Chromosome * daddy,Pool * pool,double bias)54 case 0xC0: 55 return *size != 0; 56 57 case 0xC1: /* error */ 58 *size = -1; 59 return 0; 60 61 case 0x01: /* initial char */ 62 *size = 0; 63 /* fallthrough */ 64 case 0x81: /* size char */ 65 if (ch >= 'a') 66 *size = *size * 16 + (ch - 'a' + 10); 67 else if (ch >= 'A') 68 *size = *size * 16 + (ch - 'A' + 10); 69 else 70 *size = *size * 16 + (ch - '0'); 71 break; 72 73 case 0x83: 74 return *size == 0; 75 } 76 77 return 1; 78 } 79 80