int vrt_rdev_begin_superblock_read(vrt_realdev_t *rdev, uint64_t sb_version, superblock_read_op_t *op) { superblock_header_t headers[2]; int i; int err; err = __read_sb_headers(rdev, headers); if (err != 0) return err; for (i = 0; i < 2; i++) { if (__check_sb_header(&headers[i], i) != 0) continue; if (__check_sb_checksum(rdev->checksum_sb_streams[i], headers[i].data_size, headers[i].checksum) != 0) continue; if (headers[i].sb_version == sb_version) break; } if (i == 2) return -VRT_ERR_SB_NOT_FOUND; op->header = headers[i]; op->stream = rdev->sb_data_streams[op->header.position]; /* Rewind so that the stream is ready for reading. */ return stream_rewind(op->stream); }
static void url_enfle_close(URL url) { URL_enfle *urlp = (URL_enfle *)url; stream_rewind(urlp->st); free(url); }
DEFINE_LOADER_PLUGIN_LOAD(p, st, vw #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , c #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , priv #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif ) { debug_message("gif loader: load() called\n"); #ifdef IDENTIFY_BEFORE_LOAD { LoaderStatus status; if ((status = identify(p, st, vw, c, priv)) != LOAD_OK) return status; stream_rewind(st); } #endif return load_image(p, st); }
/** * Check a checksummed stream's checksum against an expected value. * * @param[in] checksum_stream The stream to check * @param[in] size The size to read * @param[in] expected_checksum The expected checksum * * @return 0 if checksums match, a negative error code if read failed, * -VRT_ERR_SB_CHECKSUM if the checksums don't match. */ static int __check_sb_checksum(stream_t *checksum_stream, uint64_t size, checksum_t expected_checksum) { int err; uint64_t remaining; checksum_t computed_checksum; err = stream_rewind(checksum_stream); if (err != 0) return err; remaining = size; while (remaining > 0) { int r, n; char buf[512]; /* We don't really mind the size of the buffer, the * underlying blockdevice_stream caches. */ n = MIN(sizeof(buf), remaining); /* We don't care about the read contents, we just read to let the * checksum stream do his job. You'll be informed as soon as possible */ r = stream_read(checksum_stream, buf, n); if (r < 0) return err; else if (r != n) return -EIO; remaining -= r; } computed_checksum = checksum_stream_get_value(checksum_stream); if (computed_checksum != expected_checksum) { exalog_error("Invalid checksum "CHECKSUM_FMT", expected "CHECKSUM_FMT, computed_checksum, expected_checksum); return -VRT_ERR_SB_CHECKSUM; } /* Rewind so that the stream is ready. This also has the effect of resetting the checksum stream's checksum. */ return stream_rewind(checksum_stream); }
/* * The fastpath header may be two or three bytes long. * This function assumes that at least two bytes are available in the stream * and doesn't touch third byte. */ UINT16 fastpath_header_length(STREAM* s) { BYTE length1; stream_seek_BYTE(s); stream_read_BYTE(s, length1); stream_rewind(s, 2); return ((length1 & 0x80) != 0 ? 3 : 2); }
static int __read_sb_headers(vrt_realdev_t *rdev, superblock_header_t headers[2]) { int err; err = stream_rewind(rdev->raw_sb_stream); if (err != 0) return err; return rdev_superblock_header_read_both(headers, rdev->raw_sb_stream); }
/* * The fastpath header may be two or three bytes long. * This function assumes that at least two bytes are available in the stream * and doesn't touch third byte. */ uint16 fastpath_header_length(STREAM* s) { uint8 length1; stream_seek_uint8(s); stream_read_uint8(s, length1); stream_rewind(s, 2); return ((length1 & 0x80) != 0 ? 3 : 2); }
DEFINE_LOADER_PLUGIN_LOAD(p, st, vw, c, priv) { debug_message("webp loader: load() called\n"); stream_rewind(st); if (!load_image(p, st)) return LOAD_ERROR; return LOAD_OK; }
STREAM* http_request_write(HttpContext* http_context, HttpRequest* http_request) { int i; STREAM* s; int length = 0; http_request->count = 9; http_request->lines = (char**) xmalloc(sizeof(char*) * http_request->count); http_encode_line(http_request->lines[0], "%s %s HTTP/1.1", http_request->Method, http_request->URI); http_encode_line(http_request->lines[1], "Cache-Control: %s", http_context->CacheControl); http_encode_line(http_request->lines[2], "Connection: %s", http_context->Connection); http_encode_line(http_request->lines[3], "Pragma: %s", http_context->Pragma); http_encode_line(http_request->lines[4], "Accept: %s", http_context->Accept); http_encode_line(http_request->lines[5], "User-Agent: %s", http_context->UserAgent); http_encode_line(http_request->lines[6], "Content-Length: %d", http_request->ContentLength); http_encode_line(http_request->lines[7], "Host: %s", http_context->Host); if (http_request->Authorization != NULL) { http_encode_line(http_request->lines[8], "Authorization: %s", http_request->Authorization); } else if ((http_request->AuthScheme != NULL) && (http_request->AuthParam != NULL)) { http_encode_line(http_request->lines[8], "Authorization: %s %s", http_request->AuthScheme, http_request->AuthParam); } for (i = 0; i < http_request->count; i++) { length += (strlen(http_request->lines[i]) + 1); /* add +1 for each '\n' character */ } length += 1; /* empty line "\n" at end of header */ length += 1; /* null terminator */ s = stream_new(length); for (i = 0; i < http_request->count; i++) { stream_write(s, http_request->lines[i], strlen(http_request->lines[i])); stream_write(s, "\n", 1); xfree(http_request->lines[i]); } stream_write(s, "\n", 1); xfree(http_request->lines); stream_write(s, "\0", 1); /* append null terminator */ stream_rewind(s, 1); /* don't include null terminator in length */ stream_seal(s); return s; }
boolean ber_read_contextual_tag(STREAM* s, uint8 tag, int* length, boolean pc) { uint8 byte; stream_read_uint8(s, byte); if (byte != ((BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag))) { stream_rewind(s, 1); return false; } ber_read_length(s, length); return true; }
BOOL ber_read_contextual_tag(wStream* s, BYTE tag, int* length, BOOL pc) { BYTE byte; if(stream_get_left(s) < 1) return FALSE; stream_read_BYTE(s, byte); if (byte != ((BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag))) { stream_rewind(s, 1); return FALSE; } return ber_read_length(s, length); }
int vrt_rdev_begin_superblock_write(vrt_realdev_t *rdev, uint64_t old_version, uint64_t new_version, superblock_write_op_t *op) { superblock_header_t headers[2]; int write_position; int i; int err; err = __read_sb_headers(rdev, headers); if (err != 0) return err; write_position = -1; for (i = 0; i < 2; i++) { if (__check_sb_header(&headers[i], i) != 0) continue; /* We want to keep the old version => pick the header whose version is *not* old_version (or a header whose version is zero, ie none) */ if (headers[i].sb_version == 0 || headers[i].sb_version != old_version) { write_position = i; break; } } if (write_position != 0 && write_position != 1) { exalog_error("No superblock available for writing version %"PRIu64, new_version); return -VRT_ERR_SB_CORRUPTION; } op->header = headers[write_position]; op->new_sb_version = new_version; op->stream = rdev->checksum_sb_streams[write_position]; /* Rewind so that the stream is ready for writing. This also has the effect of resetting the checksum stream's checksum. */ return stream_rewind(op->stream); }
tbool scard_async_op(IRP* irp) { uint32 ioctl_code; /* peek ahead */ stream_seek(irp->input, 8); stream_read_uint32(irp->input, ioctl_code); stream_rewind(irp->input, 12); switch (ioctl_code) { /* non-blocking events */ case SCARD_IOCTL_ACCESS_STARTED_EVENT: case SCARD_IOCTL_ESTABLISH_CONTEXT: case SCARD_IOCTL_RELEASE_CONTEXT: case SCARD_IOCTL_IS_VALID_CONTEXT: return false; break; /* async events */ case SCARD_IOCTL_GET_STATUS_CHANGE: case SCARD_IOCTL_GET_STATUS_CHANGE + 4: case SCARD_IOCTL_TRANSMIT: case SCARD_IOCTL_STATUS: case SCARD_IOCTL_STATUS + 4: return true; break; default: break; } /* default to async */ return true; }
boolean license_recv(rdpLicense* license, STREAM* s) { uint16 length; uint16 channelId; uint16 sec_flags; uint8 flags; uint8 bMsgType; uint16 wMsgSize; if (!rdp_read_header(license->rdp, s, &length, &channelId)) { printf("Incorrect RDP header.\n"); return false; } rdp_read_security_header(s, &sec_flags); if (!(sec_flags & SEC_LICENSE_PKT)) { stream_rewind(s, RDP_SECURITY_HEADER_LENGTH); if (rdp_recv_out_of_sequence_pdu(license->rdp, s) != true) { printf("Unexpected license packet.\n"); return false; } return true; } license_read_preamble(s, &bMsgType, &flags, &wMsgSize); /* preamble (4 bytes) */ DEBUG_LICENSE("Receiving %s Packet", LICENSE_MESSAGE_STRINGS[bMsgType & 0x1F]); switch (bMsgType) { case LICENSE_REQUEST: license_read_license_request_packet(license, s); license_send_new_license_request_packet(license); break; case PLATFORM_CHALLENGE: license_read_platform_challenge_packet(license, s); license_send_platform_challenge_response_packet(license); break; case NEW_LICENSE: license_read_new_license_packet(license, s); break; case UPGRADE_LICENSE: license_read_upgrade_license_packet(license, s); break; case ERROR_ALERT: license_read_error_alert_packet(license, s); break; default: printf("invalid bMsgType:%d\n", bMsgType); return false; } return true; }
DEFINE_LOADER_PLUGIN_LOAD(p, st, vw #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , c #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , priv #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif ) { jas_image_t *ji; jas_stream_t *js; unsigned char *d; char *buf = NULL; int k, cmp[3]; unsigned int i, j; int tlx, tly; int vs, hs; //debug_message("JasPer: load() called\n"); #ifdef IDENTIFY_BEFORE_LOAD { LoaderStatus status; if ((status = identify(p, st, vw, c, priv)) != LOAD_OK) return status; stream_rewind(st); } #endif /* Read whole stream into buffer... */ { char *tmp; int size = 0, len; int bufsize = 65536; for (;;) { if ((tmp = realloc(buf, bufsize)) == NULL) { free(buf); return LOAD_ERROR; } buf = tmp; len = stream_read(st, (unsigned char *)(buf + size), bufsize - size); size += len; if (len < bufsize - size) break; bufsize += 65536; } if ((js = jas_stream_memopen(buf, size)) == NULL) { free(buf); return LOAD_ERROR; } } /* loading... */ if ((ji = jas_image_decode(js, -1, 0)) == NULL) { err_message_fnc("jas_image_decode() failed.\n"); goto error_clear; } /* colorspace conversion */ { jas_cmprof_t *jc; jas_image_t *new_ji; if ((jc = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB)) == NULL) goto error_destroy_free; if ((new_ji = jas_image_chclrspc(ji, jc, JAS_CMXFORM_INTENT_PER)) == NULL) goto error_destroy_free; jas_image_destroy(ji); ji = new_ji; } jas_stream_close(js); free(buf); debug_message("JasPer: jas_image_decode() OK: (%ld,%ld)\n", jas_image_cmptwidth(ji, 0), jas_image_cmptheight(ji, 0)); /* convert to enfle format */ p->bits_per_pixel = 24; p->type = _RGB24; p->depth = 24; cmp[0] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); cmp[1] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); cmp[2] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); /* dimension */ image_width(p) = jas_image_cmptwidth(ji, cmp[0]); image_height(p) = jas_image_cmptheight(ji, cmp[0]); image_left(p) = 0; image_top(p) = 0; image_bpl(p) = image_width(p) * 3; tlx = jas_image_cmpttlx(ji, cmp[0]); tly = jas_image_cmpttly(ji, cmp[0]); vs = jas_image_cmptvstep(ji, cmp[0]); hs = jas_image_cmpthstep(ji, cmp[0]); debug_message("JasPer: tlx %d tly %d vs %d hs %d ncomponents %d\n", tlx, tly, vs, hs, jas_image_numcmpts(ji)); /* memory allocation */ if ((d = memory_alloc(image_image(p), image_bpl(p) * image_height(p))) == NULL) { err_message("No enough memory (%d bytes)\n", image_bpl(p) * image_height(p)); goto error_destroy_free; } for (i = 0; i < image_height(p); i++) { for (j = 0; j < image_width(p); j++) { for (k = 0; k < 3; k++) *d++ = jas_image_readcmptsample(ji, cmp[k], j, i); } } jas_image_destroy(ji); return LOAD_OK; error_destroy_free: jas_image_destroy(ji); error_clear: return LOAD_ERROR; }