int Hwritev(int fd, const struct iovec *vector, size_t count) { size_t i; vbw.used= 0; for (i=0; i<count; i++, vector++) { if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); } return Hwrite(fd,vbw.buf,vbw.used); }
/* ** NAME ** DFwrite -- write a portion of a data element ** USAGE ** int32 DFwrite(dfile, ptr, len) ** DF *dfile; IN: pointer to open DF file ** char *ptr; IN: pointer to data to be written ** int32 len; IN: number of bytes to be written ** RETURNS ** number of bytes written on success, -1 on failure ** DESCRIPTION ** Write bytes to DF file (part of element specified by DFaccess) ** GLOBAL VARIABLES ** COMMENTS, BUGS, ASSUMPTIONS ** This function should check the access mode in DFaccmode. On write ** access, if(!in_mem) Hstartwrite, Hwrite, and set in_mem, otherwise just ** Hwrite. On append access, if(!in_mem) Hstartread, Hinquire(oldsize), ** malloc(oldsize+writesize), Hread to malloc'd area, copy write request ** to end of malloc'd area, set in_mem, otherwise, realloc(area+writesize) ** copy write request to end of area. ** EXAMPLES ** REVISION LOG */ int32 DFwrite(DF * dfile, char *ptr, int32 len) { int32 size, ret, newlen; if (DFIcheck(dfile) != 0) { DFerror = DFE_NOTOPEN; return (-1); } if ((DFelaccmode != DFACC_WRITE) && (DFelaccmode != DFACC_APPEND)) { DFerror = DFE_BADACC; return (-1); } else DFerror = DFE_NONE; size = DFelseekpos + len; if (DFelaccmode == DFACC_WRITE) { if (DFelstat == DFEL_ABSENT) { Hendaccess(DFaid); DFaid = Hstartwrite(DFid, acc_tag, acc_ref, len); Hseek(DFaid, DFelseekpos, DF_START); ret = Hwrite(DFaid, len, (unsigned char *) ptr); } else { if (size <= DFelsize) { Hendaccess(DFaid); DFaid = Hstartwrite(DFid, acc_tag, acc_ref, len); Hseek(DFaid, DFelseekpos, DF_START); ret = Hwrite(DFaid, len, (unsigned char *) ptr); } else { Hendaccess(DFaid); DFerror = DFE_NOTENOUGH; return (-1); } } } else { newlen = size - Hlength(DFid, acc_tag, acc_ref); Hendaccess(DFaid); DFaid = HLcreate(DFid, acc_tag, acc_ref, newlen, (int32) 4); Hseek(DFaid, DFelseekpos, DF_START); ret = Hwrite(DFaid, len, (unsigned char *) ptr); } Hendaccess(DFaid); DFelseekpos += len; DFelsize = size; DFelstat = DFEL_RESIDENT; return (ret); }
intn DFputcomp(int32 file_id, uint16 tag, uint16 ref, const uint8 *image, int32 xdim, int32 ydim, uint8 *palette, uint8 *newpal, int16 scheme, comp_info * cinfo) { CONSTR(FUNC, "DFputcomp"); uint8 *buffer; /* buffer to hold compressed image */ const uint8 *in; /* pointer to input for compression */ uint8 *out; /* pointer to space for compressed output */ int32 cisize; /* maximum size of compressed image */ int32 crowsize; /* maximum size of compressed row */ intn buftype; /* buftype = 1: buffer enough for whole image */ /* buftype = 2: buffer holds 1 row */ int32 n; /* number of compressed bytes produced */ int32 total; /* total compressed bytes produced so far */ int32 i; int32 ret = 0; int32 aid = 0; if (!HDvalidfid(file_id) || !tag || !ref || xdim <= 0 || ydim <= 0 || !image) HRETURN_ERROR(DFE_ARGS, FAIL); switch (scheme) { case DFTAG_RLE: /* RLE compression (8-bit or 24-bit(?) images */ cisize = ydim * (xdim * 121 / 120 + 1); /* 120 chars can compress to 121! */ crowsize = xdim * 121 / 120 + 128; /* allocate buffer for compression */ buffer = (uint8 *) HDmalloc((uint32) cisize); if (!buffer) { buffer = (uint8 *) HDmalloc((uint32) crowsize); if (!buffer) HRETURN_ERROR(DFE_NOSPACE, FAIL); buftype = 2; /* compress and write out row by row */ } else /* can hold whole image, then write */ buftype = 1; in = image; out = buffer; n = total = 0; /* no bytes compressed so far */ if (buftype == 2) { int32 num_blocks; int32 block_length; num_blocks = (ydim > (int32) R8_MAX_BLOCKS) ? (int32) R8_MAX_BLOCKS : ydim; block_length = (xdim > (int32) R8_MAX_LENGTH) ? (int32) R8_MAX_LENGTH : xdim; aid = HLcreate(file_id, tag, ref, block_length, num_blocks); if (aid == FAIL) return FAIL; } /* compress row by row */ for (i = 0; i < ydim; i++) { n = DFCIrle(in, out, xdim); /* compress row */ in += xdim; /* move input pointer */ total += n; /* keep running total */ if (buftype == 1) /* can hold whole image */ out = &buffer[total]; /* move out buffer pointer */ else { /* buffer too small, */ /* write out what was produced */ if (Hwrite(aid, n, buffer) == FAIL) { ret = FAIL; /* flag value */ break; } out = buffer; /* reset output pointer */ } } if (buftype == 1) { /* write out entire image */ ret = Hputelement(file_id, tag, ref, buffer, total); HDfree((VOIDP) buffer); } break; case DFTAG_IMC: /* IMCOMP compression (8-bit images) */ if (!palette || !newpal) /* need palette and newpal */ HRETURN_ERROR(DFE_ARGS, FAIL); cisize = xdim * ydim / 4; /* IMCOMP always cuts to 1/4 */ buffer = (uint8 *) HDmalloc((uint32) cisize); if (!buffer) HRETURN_ERROR(DFE_NOSPACE, FAIL); DFCIimcomp(xdim, ydim, image, buffer, palette, newpal, 0); ret = Hputelement(file_id, tag, ref, buffer, cisize); HDfree((VOIDP) buffer); break; case DFTAG_JPEG5: /* JPEG compression (for 24-bit images) */ case DFTAG_GREYJPEG5: /* JPEG compression (for 8-bit images) */ ret = DFCIjpeg(file_id, tag, ref, xdim, ydim, image, scheme, cinfo); break; default: /* unknown compression scheme */ HRETURN_ERROR(DFE_BADSCHEME, FAIL) } return ((intn) ret); } /* end DFputcomp() */