cell_t *parse_sexpr(file_io_t * in, file_io_t * err) { int c; if (in != NULL) while ((c = wrap_get(in)) != '\0') { if (isspace(c)) continue; if (isdigit(c)) { wrap_ungetc(in, c); return parse_number(in, err); } switch (c) { case '(': return parse_list(in, err); case '"': return parse_string(in, err); case EOF: print_error("EOF, nothing to parse", err); return NULL; case ')': print_error("Unmatched ')'", err); return NULL; default: wrap_ungetc(in, c); return parse_symbol(in, err); } } print_error("parse_expr in == NULL", err); return NULL; }
/* Reader replacement prepares the hidden handle storage for next mpg123_open_fd() or plain mpg123_open(). */ int attribute_align_arg mpg123_replace_reader(mpg123_handle *mh, ssize_t (*r_read) (int, void *, size_t), long (*r_lseek)(int, long, int) ) { struct wrap_data* ioh; if(mh == NULL) return MPG123_ERR; mpg123_close(mh); ioh = wrap_get(mh); if(ioh == NULL) return MPG123_ERR; /* If both callbacks are NULL, switch totally to internal I/O, else just use fallback for at most half of them. */ if(r_read == NULL && r_lseek == NULL) { /* Only the type is actually important to disable the code. */ ioh->iotype = 0; ioh->fd = -1; ioh->r_read = NULL; ioh->r_lseek = NULL; } else { ioh->iotype = IO_FD; ioh->fd = -1; /* On next mpg123_open_fd(), this gets a value. */ ioh->r_read = r_read != NULL ? r_read : fallback_read; ioh->r_lseek = r_lseek != NULL ? r_lseek : fallback_lseek; } /* The real reader replacement will happen while opening. */ return MPG123_OK; }
/* int mpg123_set_index(mpg123_handle *mh, off_t *offsets, off_t step, size_t fill); */ int attribute_align_arg mpg123_set_index(mpg123_handle *mh, long *offsets, long step, size_t fill) { int err; size_t i; struct wrap_data *whd; off_t *indextmp; whd = wrap_get(mh); if(whd == NULL) return MPG123_ERR; /* Expensive temporary storage... for staying outside at the API layer. */ indextmp = malloc(fill*sizeof(off_t)); if(indextmp == NULL) { mh->err = MPG123_OUT_OF_MEM; return MPG123_ERR; } if(fill > 0 && offsets == NULL) { mh->err = MPG123_BAD_INDEX_PAR; err = MPG123_ERR; } else { /* Fill the large-file copy of the provided index, then feed it to mpg123. */ for(i=0; i<fill; ++i) indextmp[i] = offsets[i]; err = MPG123_LARGENAME(mpg123_set_index)(mh, indextmp, step, fill); } free(indextmp); return err; }
/* int mpg123_index(mpg123_handle *mh, off_t **offsets, off_t *step, size_t *fill) */ int attribute_align_arg mpg123_index(mpg123_handle *mh, long **offsets, long *step, size_t *fill) { int err; size_t i; long smallstep; size_t thefill; off_t largestep; off_t *largeoffsets; struct wrap_data *whd; whd = wrap_get(mh); if(whd == NULL) return MPG123_ERR; err = MPG123_LARGENAME(mpg123_index)(mh, &largeoffsets, &largestep, &thefill); if(err != MPG123_OK) return err; /* For a _very_ large file, even the step could overflow. */ smallstep = largestep; if(smallstep != largestep) { mh->err = MPG123_LFS_OVERFLOW; return MPG123_ERR; } if(step != NULL) *step = smallstep; /* When there are no values stored, there is no table content to take care of. Table pointer does not matter. Mission completed. */ if(thefill == 0) return MPG123_OK; if(fill != NULL) *fill = thefill; /* Construct a copy of the index to hand over to the small-minded client. */ *offsets = safe_realloc(whd->indextable, (*fill)*sizeof(long)); if(*offsets == NULL) { mh->err = MPG123_OUT_OF_MEM; return MPG123_ERR; } whd->indextable = *offsets; /* Elaborate conversion of each index value, with overflow check. */ for(i=0; i<*fill; ++i) { whd->indextable[i] = largeoffsets[i]; if(whd->indextable[i] != largeoffsets[i]) { mh->err = MPG123_LFS_OVERFLOW; return MPG123_ERR; } } /* If we came that far... there should be a valid copy of the table now. */ return MPG123_OK; }
int attribute_align_arg mpg123_replace_reader_handle(mpg123_handle *mh, ssize_t (*r_read) (void*, void *, size_t), long (*r_lseek)(void*, long, int), void (*cleanup)(void*)) { struct wrap_data* ioh; if(mh == NULL) return MPG123_ERR; mpg123_close(mh); ioh = wrap_get(mh); if(ioh == NULL) return MPG123_ERR; ioh->iotype = IO_HANDLE; ioh->handle = NULL; ioh->r_h_read = r_read; ioh->r_h_lseek = r_lseek; ioh->h_cleanup = cleanup; /* The real reader replacement will happen while opening. */ return MPG123_OK; }
/*parse a number*/ static cell_t *parse_number(file_io_t * in, file_io_t * err) { char buf[MAX_STR] = { 0 }; int i = 0, c; cell_t *cell_num = calloc(1, sizeof(cell_t)); if (cell_num == NULL) { print_error("calloc() failed", err); return NULL; } while ((c = wrap_get(in)) != EOF) { if (i >= MAX_STR) { /*This should instead be set the maximum length of a number-string */ print_error("String too long for a number.", err); goto FAIL; } if (isdigit(c)) { buf[i] = c; i++; } else if (c == '(' || c == ')' || c == '"') { wrap_ungetc(in, c); goto SUCCESS; } else if (isspace(c)) { goto SUCCESS; } else { print_error("Not a valid digit.", err); goto FAIL; } } SUCCESS: cell_num->type = type_number; cell_num->car.i = atoi(buf); cell_num->cdr.cell = NULL; /*stating this explicitly. */ return cell_num; FAIL: print_error("parsing number failed.", err); free(cell_num); return NULL; }
char * get(void * webpayRef, LPCTSTR name) { char * value; if(wrap_get == NULL){ // Method not available fprintf(stderr, "wrap_get not available \r\n"); fprintf(stderr, "wrap_get not available \r\n"); fprintf(stderr, "Error: While trying get(). Check init() and the location of the Webpay client .so file : [%s]\n", WEBPAY_CLIENT_LIB); return "Error: While trying get(). Check init() and the location of the Webpay client .so file"; } if(webpayRef == NULL){ fprintf(stderr, "Error during get: webpayRef is null. Ensure that newBundle returns valid webpayRef. \r\n"); return "Error: while trying get(): webpayRef is Null, check value returned from NewBundle is valid."; } debug_logging("get: [%d] %s\n",webpayRef,name); value = wrap_get(webpayRef,name); if(value == NULL){ value = ""; } debug_logging("Get returning: [%d]: %s\n",webpayRef,value); return value; }
static cell_t *parse_list(file_io_t * in, file_io_t * err) { cell_t *head, *child = NULL, *cell_lst = calloc(1, sizeof(cell_t)); int c; head = cell_lst; if (cell_lst == NULL) { print_error("calloc() failed", err); return NULL; } while ((c = wrap_get(in)) != EOF) { if (isspace(c)) continue; if (isdigit(c)) { wrap_ungetc(in, c); child = parse_number(in, err); if (append(head, child, err)) goto FAIL; head = child; continue; } switch (c) { case ')': /*finished */ goto SUCCESS; case '(': /*encountered a nested list */ child = calloc(1, sizeof(cell_t)); if (child == NULL) goto FAIL; head->cdr.cell = child; child->car.cell = parse_list(in, err); if (child->car.cell == NULL) goto FAIL; child->type = type_list; head = child; break; case '"': /*parse string */ child = parse_string(in, err); if (append(head, child, err)) goto FAIL; head = child; break; case EOF: /*Failed */ print_error("EOF occured before end of list did.", err); goto FAIL; default: /*parse symbol */ wrap_ungetc(in, c); child = parse_symbol(in, err); if (append(head, child, err)) goto FAIL; head = child; break; } } SUCCESS: cell_lst->type = type_list; return cell_lst; FAIL: print_error("parsing list failed.", err); free(cell_lst); return NULL; }
/*parse a symbol*/ static cell_t *parse_symbol(file_io_t * in, file_io_t * err) { char buf[MAX_STR]; int i = 0, c; cell_t *cell_sym = calloc(1, sizeof(cell_t)); if (cell_sym == NULL) { print_error("calloc() failed", err); return NULL; } while ((c = wrap_get(in)) != EOF) { if (i >= MAX_STR) { print_error("String (symbol) too long.", err); goto FAIL; } if (isspace(c)) goto SUCCESS; if (c == '(' || c == ')') { wrap_ungetc(in, c); goto SUCCESS; } switch (c) { case '\\': switch (c = wrap_get(in)) { case '"': case '(': case ')': buf[i] = c; i++; continue; default: print_error("Not an escape character", err); goto FAIL; } case '"': print_error ("Unescaped \" or incorrectly formatted input.", err); goto FAIL; case EOF: print_error("EOF encountered while processing symbol", err); goto FAIL; default: /*just add the character into the buffer */ buf[i] = c; i++; } } SUCCESS: cell_sym->car.s = calloc(i + 1, sizeof(char)); if (cell_sym->car.s == NULL) { print_error("calloc() failed", err); goto FAIL; } cell_sym->type = type_symbol; memcpy(cell_sym->car.s, buf, i); cell_sym->cdr.cell = NULL; /*stating this explicitly. */ return cell_sym; FAIL: print_error("parsing symbol failed.", err); free(cell_sym); return NULL; }
/*parse a string*/ static cell_t *parse_string(file_io_t * in, file_io_t * err) { char buf[MAX_STR]; int i = 0, c; cell_t *cell_str = calloc(1, sizeof(cell_t)); if (cell_str == NULL) { print_error("calloc() failed", err); return NULL; } while ((c = wrap_get(in)) != EOF) { if (i >= MAX_STR) { print_error("String too long.", err); goto FAIL; } switch (c) { case '"': /*success */ cell_str->type = type_str; /*could add in string length here. */ cell_str->car.s = calloc(i + 1, sizeof(char)); if (cell_str->car.s == NULL) { print_error("calloc() failed", err); goto FAIL; } memcpy(cell_str->car.s, buf, i); cell_str->cdr.cell = NULL; /*stating this explicitly. */ return cell_str; case '\\': /*escape char */ switch (c = wrap_get(in)) { case '\\': /*fall through */ case '"': buf[i] = c; i++; continue; case 'n': buf[i] = '\n'; i++; continue; case EOF: print_error ("EOF encountered while processing escape char", err); goto FAIL; default: print_error("Not an escape character", err); goto FAIL; } case EOF: print_error("EOF encountered while processing symbol", err); goto FAIL; default: /*just add the character into the buffer */ buf[i] = c; i++; } } FAIL: print_error("parsing string failed.", err); free(cell_str); return NULL; }