int getlnmaxsep (buffer_ref b, char *d, unsigned int max, unsigned int *w, char const *sep, unsigned int seplen) { register int ok = 1 ; if (max < *w) return (errno = EINVAL, -1) ; for (;;) { unsigned int len = (*w + buffer_len(b) > max) ? max - *w : buffer_len(b) ; unsigned int pos = byte_in(buffer_PEEK(b), len, sep, seplen) ; byte_copy(d + *w, pos, buffer_PEEK(b)) ; *w += pos ; buffer_SEEK(b, pos) ; if (*w >= max) return (errno = ERANGE, -1) ; if (pos < len) { d[(*w)++] = *buffer_PEEK(b) ; buffer_SEEK(b, 1) ; return ok ; } { register int r = buffer_fill(b) ; if (r <= 0) return r ; } ok = 2 ; } }
static inline int ftrigio_read (ftrigio_t *p) { unsigned int i = FTRIGRD_MAXREADS ; while (i--) { regmatch_t pmatch ; unsigned int blen ; register int r = sanitize_read(buffer_fill(&p->b)) ; if (!r) break ; if (r < 0) return (trig(p->id, 'd', errno), 0) ; blen = buffer_len(&p->b) ; if (!stralloc_readyplus(&p->sa, blen+1)) dienomem() ; buffer_getnofill(&p->b, p->sa.s + p->sa.len, blen) ; p->sa.len += blen ; p->sa.s[p->sa.len] = 0 ; while (!regexec(&p->re, p->sa.s, 1, &pmatch, REG_NOTBOL | REG_NOTEOL)) { trig(p->id, '!', p->sa.s[pmatch.rm_eo - 1]) ; if (!(p->options & FTRIGR_REPEAT)) return 0 ; byte_copy(p->sa.s, p->sa.len + 1 - pmatch.rm_eo, p->sa.s + pmatch.rm_eo) ; p->sa.len -= pmatch.rm_eo ; } } return 1 ; }
static void __prebuffer(void) { int limit_chunks; BUG_ON(producer_status != PS_PLAYING); if (ip_is_remote(ip)) { limit_chunks = buffer_nr_chunks; } else { int limit_ms, limit_size; limit_ms = 250; limit_size = limit_ms * buffer_second_size() / 1000; limit_chunks = limit_size / CHUNK_SIZE; if (limit_chunks < 1) limit_chunks = 1; } while (1) { int nr_read, size, filled; char *wpos; filled = buffer_get_filled_chunks(); /* d_print("PREBUF: %2d / %2d\n", filled, limit_chunks); */ /* not fatal */ //BUG_ON(filled > limit_chunks); if (filled >= limit_chunks) break; size = buffer_get_wpos(&wpos); nr_read = ip_read(ip, wpos, size); if (nr_read < 0) { if (nr_read == -1 && errno == EAGAIN) continue; player_ip_error(nr_read, "reading file %s", ip_get_filename(ip)); /* ip_read sets eof */ nr_read = 0; } if (ip_metadata_changed(ip)) metadata_changed(); /* buffer_fill with 0 count marks current chunk filled */ buffer_fill(nr_read); __producer_buffer_fill_update(); if (nr_read == 0) { /* EOF */ break; } } }
static int buffer_fill_min (struct aac_data *data, int len) { int rc; assert (len < BUFFER_SIZE); while (buffer_length(data) < len) { rc = buffer_fill (data); if (rc <= 0) return rc; } return 1; }
int skagetlnsep (buffer *b, stralloc *sa, char const *sep, unsigned int seplen) { unsigned int start = sa->len ; for (;;) { siovec_t v[2] ; unsigned int pos ; int r ; buffer_rpeek(b, v) ; pos = siovec_bytein(v, 2, sep, seplen) ; r = pos < buffer_len(b) ; pos += r ; if (!stralloc_readyplus(sa, pos)) return -1 ; buffer_getnofill(b, sa->s + sa->len, pos) ; sa->len += pos ; if (r) return 1 ; r = buffer_fill(b) ; if (r < 0) return r ; if (!r) return (sa->s && (sa->len > start)) ? (errno = EPIPE, -1) : 0 ; } }
int netstring_get (buffer_ref b, stralloc *sa, unsigned int *unread) { unsigned int written ; int ok = 1 ; int r ; if (!sa->s || (!sa->len && !*unread)) { char *x ; unsigned int n ; unsigned int len ; for (;;) { x = buffer_PEEK(b) ; n = byte_chr(x, buffer_len(b), ':') ; /* XXX: accepts :, as a valid netstring */ if (n >= ULONG_FMT) return (errno = EINVAL, -1) ; /* XXX: breaks on too many leading '0's */ if (n < buffer_len(b)) break ; r = buffer_fill(b) ; if (r == -1) return -1 ; if (!r) return (buffer_isempty(b) ? 0 : (errno = EPIPE, -1)) ; ok = 2 ; } if (n != uint_scan(x, &len)) return (errno = EINVAL, -1) ; if (!stralloc_readyplus(sa, len+1)) return -1 ; buffer_SEEK(b, n+1) ; *unread = len + 1 ; } written = sa->len ; r = buffer_getall(b, sa->s + sa->len, sa->len + *unread, &written) ; if (r <= 0) { *unread -= written - sa->len ; sa->len = written ; return r ? r : (errno = EINVAL, -1) ; } if (r == 2) ok = 2 ; sa->len += *unread ; *unread = 0 ; return (sa->s[--sa->len] == ',') ? ok : (errno = EINVAL, -1) ; }
int getlnmaxsep (buffer *b, char *d, unsigned int max, unsigned int *w, char const *sep, unsigned int seplen) { if (max < *w) return (errno = EINVAL, -1) ; for (;;) { siovec_t v[2] ; unsigned int len = buffer_len(b) ; unsigned int pos ; int r ; buffer_rpeek(b, v) ; if (len > max - *w) len = max - *w ; pos = siovec_bytein(v, 2, sep, seplen) ; if (pos > len) pos = len ; r = pos < len ; buffer_getnofill(b, d + *w, pos + r) ; *w += pos ; if (*w >= max) return (errno = ERANGE, -1) ; if (r) return 1 ; r = buffer_fill(b) ; if (r <= 0) return r ; } }
int skagetln (buffer_ref b, stralloc *sa, int sep) { unsigned int start = sa->len ; int ok = 1 ; for (;;) { char *s = buffer_PEEK(b) ; unsigned int pos = byte_chr(s, buffer_len(b), sep) ; int r = (pos < buffer_len(b)) ; if (!stralloc_catb(sa, s, pos)) return -1 ; buffer_SEEK(b, pos) ; if (r) { if (!stralloc_catb(sa, buffer_PEEK(b), 1)) return -1 ; buffer_SEEK(b, 1) ; return ok ; } r = buffer_fill(b) ; if (r == -1) return -1 ; if (!r) return (sa->s && (sa->len > start)) ? (errno = EPIPE, -1) : 0 ; ok = 2 ; } }
static void *producer_loop(void *arg) { while (1) { /* number of chunks to fill * too big => seeking is slow * too small => underruns? */ const int chunks = 1; int size, nr_read, i; char *wpos; producer_lock(); if (!producer_running) break; if (producer_status == PS_UNLOADED || producer_status == PS_PAUSED || producer_status == PS_STOPPED || ip_eof(ip)) { pthread_cond_wait(&producer_playing, &producer_mutex); producer_unlock(); continue; } for (i = 0; ; i++) { size = buffer_get_wpos(&wpos); if (size == 0) { /* buffer is full */ producer_unlock(); ms_sleep(50); break; } nr_read = ip_read(ip, wpos, size); if (nr_read < 0) { if (nr_read != -1 || errno != EAGAIN) { player_ip_error(nr_read, "reading file %s", ip_get_filename(ip)); /* ip_read sets eof */ nr_read = 0; } else { producer_unlock(); ms_sleep(50); break; } } if (ip_metadata_changed(ip)) metadata_changed(); /* buffer_fill with 0 count marks current chunk filled */ buffer_fill(nr_read); if (nr_read == 0) { /* consumer handles EOF */ producer_unlock(); ms_sleep(50); break; } if (i == chunks) { producer_unlock(); /* don't sleep! */ break; } } __producer_buffer_fill_update(); } __producer_unload(); producer_unlock(); return NULL; }
void buffer_clear(buffer_t *self) { buffer_fill(self, 0); }
int main(int argc, char *argv[], char *envp[]) { struct ecc_state_s ecc; uint8_t buffer[0x1000], ecc_payload[0x40], regs[3], *jffs; int ret, len, eccbyte, count, partition; /* Check if we're called by "raw2flash.spitz" or similar */ len = strlen(argv[0]); if (!strcasecmp(argv[0] + len - 5, "akita")) ecc.style = &akita; else if (!strcasecmp(argv[0] + len - 6, "borzoi")) ecc.style = &borzoi; else if (!strcasecmp(argv[0] + len - 7, "terrier")) ecc.style = &terrier; else ecc.style = &spitz; # ifdef VERBOSE fprintf(stderr, "["); # endif /* Skip first 10 bytes */ TFR(read(0, buffer, 0x10)); len = 0; jffs = (uint8_t *) malloc(PARTITION_START); while (len < PARTITION_START) { ret = TFR(read(0, jffs + len, PARTITION_START - len)); if (ret <= 0) break; len += ret; } /* Convert data from stdin */ partition = len = eccbyte = count = 0; memset(ecc_payload, 0xff, ecc.style->oob_size); jffs2_format(&ecc, ecc_payload); while (count < ecc.style->romsize) { buffer_fill(&ecc, buffer, &len, &partition, count, jffs); buffer_digest(&ecc, buffer, regs); ecc_payload[ecc.style->eccpos[eccbyte ++]] = regs[0]; ecc_payload[ecc.style->eccpos[eccbyte ++]] = regs[1]; ecc_payload[ecc.style->eccpos[eccbyte ++]] = regs[2]; TFR(write(1, buffer, ecc.style->eccbytes)); count += ecc.style->eccbytes; len -= ecc.style->eccbytes; memmove(buffer, buffer + ecc.style->eccbytes, len); if (eccbyte >= ecc.style->eccsize) { TFR(write(1, ecc_payload, ecc.style->oob_size)); eccbyte = 0; memset(ecc_payload, 0xff, ecc.style->oob_size); if (partition < 2) jffs2_format(&ecc, ecc_payload); } # ifdef VERBOSE if (count * PBAR_LEN / ecc.style->romsize > (count - ecc.style->eccbytes) * PBAR_LEN / ecc.style->romsize) fprintf(stderr, "#"); # endif } # ifdef VERBOSE fprintf(stderr, "]\n"); # endif free(jffs); return 0; }
int buffer_feed (register buffer_ref b) { return (b->n > b->p) ? ((int)b->n - (int)b->p) : buffer_fill(b) ; }