/****************************************************************************** * mailbox_copy_payload * This function REQUIRES that the head pointer point to the beginning * of a message with a payload. It should only be called after * mailbox_head returns a payload_len greater than zero. *******************************************************************************/ u8 mailbox_copy_payload(mailbox_t *box, u8 *buf, u8 buflen, u8 offset) { u8 i, plen; plen = read_wrap(box, 1); for(i=0; i<buflen; i++) { if (i == plen) { break; } buf[i] = read_wrap(box, 2 + i + offset); } return i; }
int main( int argc, char const *argv[] ) { wait_for_debugger_attach( "WRAPC_DEBUG" ); init( argc, argv ); fork_exec_wrap( read_source_write_wrap() ); read_wrap(); wait_for_child_processes(); exit( EX_OK ); }
void open_file(void * junk) { int fd, ret; char buff[10]; fd = open("test.txt", O_RDONLY); printf("Reading 1st 10 chars from test.txt...\n"); ret = read_wrap(fd, buff, 10); printf("Buffer contents: "); printBuffer(buff, 10); printf("\n"); printf("return value: %d\n", ret); printf("Next 10...\n"); ret = read_wrap(fd, buff, 10); printf("Buffer contents: "); printBuffer(buff, 10); printf("\n"); printf("return value: %d\n", ret); close(fd); }
void input_wait(void * chars) { int n = *(int *) chars; char * buffer; buffer = (char *) malloc(n * sizeof(char)); printf("Type %d chars: \n", n); read_wrap(STDIN_FILENO, buffer, n); int i; printf("Buffer contents: "); for (i = 0; i < n; i++) { printf("%c ", buffer[i]); } free(buffer); }
/****************************************************************************** * mailbox_head * Return the header of the first message in the queue, if it exists. * Returns zero iff the mailbox is empty. *******************************************************************************/ u8 mailbox_head(mailbox_t *box, u8 *code, u8 *payload_len) { if (box->head == box->tail) { return 0; } *code = *(box->head); if (*code & 0x80) { *payload_len = read_wrap(box, 1); *code &= ~0x80; } else { *payload_len = 0; } return 1; }
/* attempt to read more data from the ircd into our read buffer. * returns 1 if something was read; 0 on timeout; -1 on failure */ static int read_more(sckhld sh, struct readctx *rctx, uint64_t to_us) { /* no sizeof rctx->workbuf here because it's one bigger than WORKBUF_SZ * and we don't want to fill the last byte with data; it's a dummy */ size_t remain = WORKBUF_SZ - (rctx->eptr - rctx->workbuf); if (!remain) { /* no more space left in receive buffer */ D("Buffer is full"); if (rctx->wptr == rctx->workbuf) { /* completely full */ E("input too long"); return -1; } /* make additional room by moving data to the beginning */ size_t datalen = (size_t)(rctx->eptr - rctx->wptr); memmove(rctx->workbuf, rctx->wptr, datalen); rctx->wptr = rctx->workbuf; rctx->eptr = &rctx->workbuf[datalen]; remain = WORKBUF_SZ - (rctx->eptr - rctx->workbuf); D("Moved %zu bytes to the front; space: %zu", datalen, remain); } V("Reading more data (max. %zu bytes, timeout: %"PRIu64, remain, to_us); long n = read_wrap(sh, rctx->eptr, remain, to_us); // >0: Amount of bytes read // 0: timeout // -1: Failure // -2: EOF if (n <= 0) { if (n < 0) n == -2 ? W("read: EOF") : WE("read failed"); else V("Timeout"); return n; } V("Got %ld more bytes", n); rctx->eptr += n; return 1; }
json_object *json_read_separators(json_pull *j, json_separator_callback cb, void *state) { int c; // In case there is an error at the top level if (j->container == NULL) { j->root = NULL; } again: /////////////////////////// Whitespace do { c = read_wrap(j); if (c == EOF) { if (j->container != NULL) { j->error = "Reached EOF without all containers being closed"; } return NULL; } } while (c == ' ' || c == '\t' || c == '\r' || c == '\n'); /////////////////////////// Arrays if (c == '[') { json_object *o = add_object(j, JSON_ARRAY); if (o == NULL) { return NULL; } j->container = o; j->container->expect = JSON_ITEM; if (cb != NULL) { cb(JSON_ARRAY, j, state); } goto again; } else if (c == ']') { if (j->container == NULL) { j->error = "Found ] at top level"; return NULL; } if (j->container->type != JSON_ARRAY) { j->error = "Found ] not in an array"; return NULL; } if (j->container->expect != JSON_COMMA) { if (!(j->container->expect == JSON_ITEM && j->container->length == 0)) { j->error = "Found ] without final element"; return NULL; } } json_object *ret = j->container; j->container = ret->parent; return ret; } /////////////////////////// Hashes if (c == '{') { json_object *o = add_object(j, JSON_HASH); if (o == NULL) { return NULL; } j->container = o; j->container->expect = JSON_KEY; if (cb != NULL) { cb(JSON_HASH, j, state); } goto again; } else if (c == '}') { if (j->container == NULL) { j->error = "Found } at top level"; return NULL; } if (j->container->type != JSON_HASH) { j->error = "Found } not in a hash"; return NULL; } if (j->container->expect != JSON_COMMA) { if (!(j->container->expect == JSON_KEY && j->container->length == 0)) { j->error = "Found } without final element"; return NULL; } } json_object *ret = j->container; j->container = ret->parent; return ret; } /////////////////////////// Null if (c == 'n') { if (read_wrap(j) != 'u' || read_wrap(j) != 'l' || read_wrap(j) != 'l') { j->error = "Found misspelling of null"; return NULL; } return add_object(j, JSON_NULL); } /////////////////////////// True if (c == 't') { if (read_wrap(j) != 'r' || read_wrap(j) != 'u' || read_wrap(j) != 'e') { j->error = "Found misspelling of true"; return NULL; } return add_object(j, JSON_TRUE); } /////////////////////////// False if (c == 'f') { if (read_wrap(j) != 'a' || read_wrap(j) != 'l' || read_wrap(j) != 's' || read_wrap(j) != 'e') { j->error = "Found misspelling of false"; return NULL; } return add_object(j, JSON_FALSE); } /////////////////////////// Comma if (c == ',') { if (j->container == NULL) { j->error = "Found comma at top level"; return NULL; } if (j->container->expect != JSON_COMMA) { j->error = "Found unexpected comma"; return NULL; } if (j->container->type == JSON_HASH) { j->container->expect = JSON_KEY; } else { j->container->expect = JSON_ITEM; } if (cb != NULL) { cb(JSON_COMMA, j, state); } goto again; } /////////////////////////// Colon if (c == ':') { if (j->container == NULL) { j->error = "Found colon at top level"; return NULL; } if (j->container->expect != JSON_COLON) { j->error = "Found unexpected colon"; return NULL; } j->container->expect = JSON_VALUE; if (cb != NULL) { cb(JSON_COLON, j, state); } goto again; } /////////////////////////// Numbers if (c == '-' || (c >= '0' && c <= '9')) { struct string val; string_init(&val); if (c == '-') { string_append(&val, c); c = read_wrap(j); } if (c == '0') { string_append(&val, c); } else if (c >= '1' && c <= '9') { string_append(&val, c); c = peek(j); while (c >= '0' && c <= '9') { string_append(&val, read_wrap(j)); c = peek(j); } } if (peek(j) == '.') { string_append(&val, read_wrap(j)); c = peek(j); while (c >= '0' && c <= '9') { string_append(&val, read_wrap(j)); c = peek(j); } } c = peek(j); if (c == 'e' || c == 'E') { string_append(&val, read_wrap(j)); c = peek(j); if (c == '+' || c == '-') { string_append(&val, read_wrap(j)); } c = peek(j); if (c < '0' || c > '9') { j->error = "Exponent without digits"; string_free(&val); return NULL; } while (c >= '0' && c <= '9') { string_append(&val, read_wrap(j)); c = peek(j); } } json_object *n = add_object(j, JSON_NUMBER); if (n != NULL) { n->number = atof(val.buf); n->string = val.buf; n->length = val.n; } else { string_free(&val); } return n; } /////////////////////////// Strings if (c == '"') { struct string val; string_init(&val); while ((c = read_wrap(j)) != EOF) { if (c == '"') { break; } else if (c == '\\') { c = read_wrap(j); if (c == '"') { string_append(&val, '"'); } else if (c == '\\') { string_append(&val, '\\'); } else if (c == '/') { string_append(&val, '/'); } else if (c == 'b') { string_append(&val, '\b'); } else if (c == 'f') { string_append(&val, '\f'); } else if (c == 'n') { string_append(&val, '\n'); } else if (c == 'r') { string_append(&val, '\r'); } else if (c == 't') { string_append(&val, '\t'); } else if (c == 'u') { char hex[5] = "aaaa"; int i; for (i = 0; i < 4; i++) { hex[i] = read_wrap(j); } unsigned long ch = strtoul(hex, NULL, 16); if (ch <= 0x7F) { string_append(&val, ch); } else if (ch <= 0x7FF) { string_append(&val, 0xC0 | (ch >> 6)); string_append(&val, 0x80 | (ch & 0x3F)); } else { string_append(&val, 0xE0 | (ch >> 12)); string_append(&val, 0x80 | ((ch >> 6) & 0x3F)); string_append(&val, 0x80 | (ch & 0x3F)); } } else {