/* * make sure tail is larger than sz */ size_t buf_readjust(buf_t *buf, size_t sz) { while (buf->sz - buf->idx_write < sz) { if (buf->idx_read < buf->sz / 2) { buf_realloc(buf, buf->sz * 2); } else { buf_realloc(buf, buf->sz); } } return buf->sz; }
static int my_real_read(int fd, buf_t *buf, int *done) { int left, n; uint32_t pktlen; char *ptr; ptr = buf->ptr; *done = 0; left = buf->size - buf->used; ptr = buf->ptr + buf->used; AGAIN: if( (n = read(fd, ptr, left)) < 0 ){ if(errno == EINTR){ goto AGAIN; } else if( errno == EAGAIN || errno == EWOULDBLOCK){ return 0 ; } else { return n; } } else if(n == 0) {//zero indicates end of file info(g_log, "my_real_read failed, fd:%d, ret:%d, errno:%d, errmsg:%s\n", fd, n, errno, strerror(errno)) ; return -1; } else { buf->used += n; buf->pos += n; if(buf->used >= HEADER_SIZE){ //如果已经读取到了4个字节的固定长度,那么就可以拿到数据包有多大了,也就是这次应该读取的长度是多少大 ptr = buf->ptr; pktlen = 0; memcpy(&pktlen, ptr, 3); if((pktlen + HEADER_SIZE) > buf->size){//总大小是否超过了当前缓冲的大小,如果是需要重新申请一块大内存 if(buf_realloc(buf, pktlen + HEADER_SIZE) == NULL){ return -1; } } if(buf->used >= (pktlen + HEADER_SIZE)){//读取完毕了 *done = 1; } } return n; } return 0 ; }
static bool page_begin(struct urf_context *ctx) { if (ctx->page_hdr->bpp != 24) { URF_SET_ERROR(ctx, "unsupported bpp", -ctx->page_hdr->bpp); return false; } IMPL(ctx)->zlen = 2 * deflateBound(&IMPL(ctx)->strm, ctx->page_line_bytes); if (!buf_realloc(ctx, &IMPL(ctx)->zbuf, IMPL(ctx)->zlen)) { return false; } IMPL(ctx)->idx = 0; return xprintf(ctx, "%%%%Page: %" PRIu32 " %" PRIu32 "\n" "%%%%PageBoundingBox: 0 0 %" PRIu32 " %" PRIu32 "\n" "save\n" "/DeviceRGB setcolorspace\n" "<<\n" " /ImageType 1\n" " /Width %" PRIu32 "\n" " /Height %" PRIu32 "\n" //" /ImageMatrix [ %" PRIu32 " 0 0 -%" PRIu32 " 0 %" PRIu32 " ]\n" " /ImageMatrix [ 1 0 0 -1 0 %" PRIu32 " ]\n" " /BitsPerComponent 8\n" " /Interpolate true\n" " /Decode [ 0 1 0 1 0 1 ]\n" " /DataSource currentfile\n" //" /ASCIIHexDecode filter\n" #if ASCII85 == 1 " /ASCII85Decode filter\n" #else " /ASCIIHexDecode filter\n" #endif #ifndef NODEFLATE " /FlateDecode filter\n" #endif ">> image\n", ctx->page_n, ctx->page_n, ctx->page_hdr->width, ctx->page_hdr->height, ctx->page_hdr->width, ctx->page_hdr->height, // ctx->page_hdr->width, ctx->page_hdr->height, ctx->page_hdr->height); }