static u16 parse_header(const u08 *buf, u16 num) { // "P6\n" header? if(num<3) { return PIC_HEADER_ERROR; } if((buf[0]!='P')||(buf[1]!='6')||(buf[2]!='\n')) { return PIC_HEADER_ERROR; } u16 hdr_pos = 3; // parse header lines u08 num_lines = 2; while(num_lines > 0) { // find newline u16 nl_pos = hdr_pos; while(nl_pos < num) { if(buf[nl_pos]=='\n') { break; } nl_pos ++; } // no newline found! if(nl_pos >= num) { return PIC_HEADER_TOO_LONG; } // ignore comment line if(buf[hdr_pos]!='#') { if(num_lines == 2) { // parse width u08 digits = parse_dec(buf+hdr_pos, &width); if(digits == 0) { return PIC_WIDTH_ERROR; } // parse height digits = parse_dec(buf+hdr_pos+digits+1, &height); if(digits == 0) { return PIC_HEIGHT_ERROR; } } num_lines --; } // skip line hdr_pos = nl_pos + 1; } // set buffer to start buf_pos = hdr_pos; buf_avail = num - hdr_pos; return CMD_OK; }
/* Converts a string with an html entity to it's encoded form, which is written * to the output string. */ static const char *entity_convert(const char *s, char *output, char terminator) { /* TODO(falmeida): Handle wide char encodings */ struct entityfilter_table_s *t = entityfilter_table; if (s[0] == '#') { if (s[1] == 'x' || s[1] == 'X') { /* hex */ return parse_hex(s + 2, output); } else { /* decimal */ return parse_dec(s + 1, output); } } while (t->entity != NULL) { if (strcasecmp(t->entity, s) == 0) return t->value; t++; } snprintf(output, HTMLPARSER_MAX_ENTITY_SIZE, "&%s%c", s, terminator); output[HTMLPARSER_MAX_ENTITY_SIZE - 1] = '\0'; return output; }
static int scan(int (*peek)(void *userp), void (*pop)(void *userp), void *userp, const char *fmt, va_list ap) { char ch; int n = 0; int n_chars = 0; int r; long lval; bool skip=false; unsigned long ulval; while ((ch = *fmt++) != '\0') { bool literal = false; if (ch == '%') { ch = *fmt++; if(ch== '*') /* We should process this, but not store it in an argument */ { ch=*fmt++; skip=true; } else { skip=false; } switch (ch) { case 'x': n_chars += skip_spaces(peek, pop, userp); if ((r = parse_hex(peek, pop, userp, &ulval)) >= 0) { if(skip==false) { *(va_arg(ap, unsigned int *)) = ulval; n++; } n_chars += r; } else return n; break; case 'd': n_chars += skip_spaces(peek, pop, userp); if ((r = parse_dec(peek, pop, userp, &lval)) >= 0) { if(skip==false) { *(va_arg(ap, int *)) = lval; n++; } n_chars += r; } else return n; break; case 'n': if(skip==false) { *(va_arg(ap, int *)) = n_chars; n++; } break; case 'l': n_chars += skip_spaces(peek, pop, userp); ch = *fmt++; switch (ch) { case 'x': if ((r = parse_hex(peek, pop, userp, &ulval)) >= 0) { if(skip==false) { *(va_arg(ap, unsigned long *)) = ulval; n++; } n_chars += r; } else return n; break; case 'd': if ((r = parse_dec(peek, pop, userp, &lval)) >= 0) { if(skip==false) { *(va_arg(ap, long *)) = lval; n++; } n_chars += r; } else return n; break; case '\0': return n; default: literal = true; break; } break; case 's': n_chars += skip_spaces(peek, pop, userp); n_chars += parse_chars(peek,pop, userp,skip?0:va_arg(ap, char *), skip ); if(skip==false) { n++; } break; case '\0': return n; default: literal = true; break; } } else
static bool parse_sap_header(int fd, struct mp3entry* id3, int file_len) { int module_index = 0; int sap_signature = -1; int duration_index = 0; unsigned char cur_char = 0; int i; /* set defaults */ int numSongs = 1; int defSong = 0; int durations[MAX_SONGS]; for (i = 0; i < MAX_SONGS; i++) durations[i] = -1; /* use id3v2 buffer for our strings */ char* buffer = id3->id3v2buf; char* buffer_end = id3->id3v2buf + ID3V2_BUF_SIZE; /* parse file */ while (1) { char line[256]; char *p; if (module_index + 8 >= file_len) return false; /* read a char */ read(fd,&cur_char,1); /* end of header */ if (cur_char == 0xff) break; i = 0; while (cur_char != 0x0d) { line[i++] = cur_char; module_index++; if (module_index >= file_len || (unsigned)i >= sizeof(line) - 1) return false; /* read a char */ read(fd,&cur_char,1); } if (++module_index >= file_len ) return false; /* read a char */ read(fd,&cur_char,1); if ( cur_char != 0x0a) return false; line[i] = '\0'; for (p = line; *p != '\0'; p++) { if (*p == ' ') { *p++ = '\0'; break; } } /* parse tags */ if(strcmp(line, "SAP") == 0) sap_signature = 1; if (sap_signature == -1) return false; if (strcmp(line, "AUTHOR") == 0) { if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false) return false; } else if(strcmp(line, "NAME") == 0) { if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false) return false; } else if(strcmp(line, "DATE") == 0) { if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false) return false; } else if (strcmp(line, "SONGS") == 0) { if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false ) return false; } else if (strcmp(line, "DEFSONG") == 0) { if (parse_dec(&defSong, p, 0, MAX_SONGS) == false) return false; } else if (strcmp(line, "TIME") == 0) { int durationTemp = ASAP_ParseDuration(p); if (durationTemp < 0 || duration_index >= MAX_SONGS) return false; durations[duration_index++] = durationTemp; } } /* set length: */ int length = durations[defSong]; if (length < 0) length = 180 * 1000; id3->length = length; lseek(fd, 0, SEEK_SET); return true; }