📄 blkcpy.c
字号:
copyvalue(&sep->e_value, vp++); sep = sep->e_next; } dep = listelement(dlp, (long) dsi); vp = vtemp; i = num; while (i-- > 0) { subtype = dep->e_value.v_subtype; freevalue(&dep->e_value); dep->e_value = *vp++; dep->e_value.v_subtype |= subtype; dep = dep->e_next; } free(vtemp); return 0;}/* * copyblk2file - copy block to file */intcopyblk2file(BLOCK *sblk, long ssi, long num, FILEID id, long dsi){ FILEIO *fiop; FILE *fp; long numw; if (ssi > sblk->datalen) return E_COPY2; if (num < 0) num = sblk->datalen - ssi; if (num == 0) return 0; fiop = findid(id, TRUE); if (fiop == NULL) return E_COPYF1; fp = fiop->fp; if (id == 1 || id == 2) { numw = idfputstr(id, (char *)sblk->data + ssi); /* XXX */ return 0; } if (dsi >= 0) { if (fseek(fp, dsi, 0)) return E_COPYF2; } numw = fwrite(sblk->data + ssi, 1, num, fp); if (numw < num) return E_COPYF3; fflush(fp); return 0;}/* * copyfile2blk - copy file to block */intcopyfile2blk(FILEID id, long ssi, long num, BLOCK *dblk, long dsi, BOOL noreloc){ FILEIO *fiop; FILE *fp; long numw; ZVALUE fsize; long filelen; long newlen; long newsize; OCTET *newdata; if (id < 3) /* excludes copying from stdin */ return E_COPYF1; fiop = findid(id, FALSE); if (fiop == NULL) return E_COPYF1; fp = fiop->fp; if (get_open_siz(fp, &fsize)) return E_COPYF2; if (zge31b(fsize)) { zfree(fsize); return E_COPY5; } filelen = ztoi(fsize); zfree(fsize); if (ssi > filelen) return E_COPY2; if (num < 0) num = filelen - ssi; if (num == 0) return 0; if (ssi + num > filelen) return E_COPY5; if (fseek(fp, ssi, 0)) /* using system fseek XXX */ return E_COPYF2; if (dsi < 0) dsi = dblk->datalen; newlen = dsi + num; if (newlen <= 0) return E_COPY7; if (newlen >= dblk->maxsize) { if (noreloc) return E_COPY17; newsize = (1 + newlen/dblk->blkchunk) * dblk->blkchunk; newdata = (USB8*) realloc(dblk->data, newsize); if (newdata == NULL) { math_error("Out of memory for block-to-block copy"); /*NOTREACHED*/ } dblk->data = newdata; dblk->maxsize = newsize; } numw = fread(dblk->data + dsi, 1, num, fp); if (numw < num) return E_COPYF4; if (newlen > dblk->datalen) dblk->datalen = newlen; return 0;}/* * copystr2file - copy string to file */intcopystr2file(STRING *str, long ssi, long num, FILEID id, long dsi){ long len; FILEIO *fiop; long numw; FILE *fp; len = str->s_len; if (ssi >= len) return E_COPY2; if (num < 0) num = len - ssi; if (num <= 0) /* Nothing to be copied */ return 0; if (ssi + num > len) return E_COPY5; /* Insufficient memory in str */ fiop = findid(id, TRUE); if (fiop == NULL) return E_COPYF1; fp = fiop->fp; if (id == 1 || id == 2) { numw = idfputstr(id, str->s_str + ssi); /* XXX */ return 0; } if (dsi >= 0) { if (fseek(fp, dsi, 0)) return E_COPYF2; } numw = fwrite(str->s_str + ssi, 1, num, fp); if (numw < num) return E_COPYF3; fflush(fp); return 0;}/* * copyblk2blk - copy block to block */intcopyblk2blk(BLOCK *sblk, long ssi, long num, BLOCK *dblk, long dsi, BOOL noreloc){ long newlen; long newsize; USB8 *newdata; if (ssi > sblk->datalen) return E_COPY2; if (num < 0) num = sblk->datalen - ssi; if (num == 0) /* Nothing to be copied */ return 0; if (ssi + num > sblk->datalen) return E_COPY5; if (dsi < 0) dsi = dblk->datalen; newlen = dsi + num; if (newlen <= 0) return E_COPY7; if (newlen >= dblk->maxsize) { if (noreloc) return E_COPY17; newsize = (1 + newlen/dblk->blkchunk) * dblk->blkchunk; newdata = (USB8*) realloc(dblk->data, newsize); if (newdata == NULL) { math_error("Out of memory for block-to-block copy"); /*NOTREACHED*/ } dblk->data = newdata; dblk->maxsize = newsize; } memmove(dblk->data + dsi, sblk->data + ssi, num); if (newlen > dblk->datalen) dblk->datalen = newlen; return 0;}/* * copystr2blk - copy string to block */intcopystr2blk(STRING *str, long ssi, long num, BLOCK *dblk, long dsi, BOOL noreloc){ long len; long newlen; long newsize; USB8 *newdata; len = str->s_len; if (ssi >= len) return E_COPY2; if (num < 0) num = len - ssi; if (num <= 0) /* Nothing to be copied */ return 0; if (dsi < 0) dsi = dblk->datalen; newlen = dsi + num + 1; if (newlen <= 0) return E_COPY7; if (newlen >= dblk->maxsize) { if (noreloc) return E_COPY17; newsize = (1 + newlen/dblk->blkchunk) * dblk->blkchunk; newdata = (USB8*) realloc(dblk->data, newsize); if (newdata == NULL) { math_error("Out of memory for string-to-block copy"); /*NOTREACHED*/ } dblk->data = newdata; dblk->maxsize = newsize; } memmove(dblk->data + dsi, str->s_str + ssi, num); dblk->data[dsi + num] = '\0'; if (newlen > dblk->datalen) dblk->datalen = newlen; return 0;}/* * copystr2str - copy up to num characters from sstr (starting at * index ssi) to dstr (starting at index dsi); num is reduced if there * are insufficient characters in sstr or insufficient space in dstr. */intcopystr2str(STRING *sstr, long ssi, long num, STRING *dstr, long dsi){ char *c, *c1; if (num < 0 || (size_t)(ssi + num) > sstr->s_len) num = sstr->s_len - ssi; if (num <= 0) return 0; /* Nothing to be copied */ if (dsi < 0) /* default destination index */ dsi = 0; if ((size_t)(dsi + num) > dstr->s_len) num = dstr->s_len - dsi; c1 = sstr->s_str + ssi; c = dstr->s_str + dsi; while (num-- > 0) *c++ = *c1++; return 0;}/* * copyblk2str - copy up to num characters from sblk (starting at * index ssi) to str (starting at index dsi); num is reduced if there * is insufficient data in blk or insufficient space in str */intcopyblk2str(BLOCK *sblk, long ssi, long num, STRING *dstr, long dsi){ USB8 *c, *c1; if (num < 0 || ssi + num > sblk->datalen) num = sblk->datalen - ssi; if (num <= 0) return 0; /* Nothing to be copied */ if (dsi < 0) /* default destination index */ dsi = 0; if ((size_t)(dsi + num) > dstr->s_len) num = dstr->s_len - dsi; c1 = sblk->data + ssi; c = (USB8 *)dstr->s_str + dsi; while (num-- > 0) *c++ = *c1++; return 0;}/* * copyostr2str - copy octet-specified string to string */intcopyostr2str(char *sstr, long ssi, long num, STRING *dstr, long dsi){ size_t len; char *c, *c1; len = strlen(sstr); if (num < 0 || (size_t)(ssi + num) > len) num = len - ssi; if (num <= 0) /* Nothing to be copied */ return 0; if (dsi < 0) dsi = 0; /* Default destination index */ if ((size_t)(dsi + num) > dstr->s_len) num = dstr->s_len - dsi; c1 = sstr + ssi; c = dstr->s_str + dsi; while (num-- > 0) *c++ = *c1++; return 0;}/* * copyostr2blk - copy octet-specified string to block */intcopyostr2blk(char *str,long ssi,long num,BLOCK *dblk,long dsi,BOOL noreloc){ size_t len; size_t newlen; size_t newsize; USB8 *newdata; len = strlen(str) + 1; if (ssi > 0 && (size_t)ssi > len) return E_COPY2; if (num < 0 || (size_t)(ssi + num) > len) num = len - ssi; if (num <= 0) /* Nothing to be copied */ return 0; if (dsi < 0) dsi = dblk->datalen; /* Default destination index */ newlen = dsi + num; if (newlen <= 0) return E_COPY7; if (newlen >= (size_t)dblk->maxsize) { if (noreloc) return E_COPY17; newsize = (1 + newlen/dblk->blkchunk) * dblk->blkchunk; newdata = (USB8*) realloc(dblk->data, newsize); if (newdata == NULL) { math_error("Out of memory for string-to-block copy"); /*NOTREACHED*/ } dblk->data = newdata; dblk->maxsize = newsize; } memmove(dblk->data + dsi, str + ssi, num); if (newlen > (size_t)dblk->datalen) dblk->datalen = newlen; return 0;}#if !defined(HAVE_MEMMOVE)/* * memmove - simulate the memory move function that deals with overlap * * Copying between objects that overlap will take place correctly. * * given: * s1 destination * s2 source * n octet count * * returns: * s1 */void *memmove(void *s1, const void *s2, CALC_SIZE_T n){ /* * firewall */ if (s1 == NULL || s2 == NULL) { math_error("bogus memmove NULL ptr"); /*NOTREACHED*/ } if (n <= 0) { /* neg or 0 count does nothing */ return s1; } if ((char *)s1 == (char *)s2) { /* copy to same location does nothing */ return s1; } /* * determine if we need to deal with overlap copy */ if ((char *)s1 > (char *)s2 && (char *)s1 < (char *)s2+n) { /* * we have to copy backwards ... slowly */ while (n-- > 0) { ((char *)s1)[n] = ((char *)s2)[n]; } } else { /* * safe ... no overlap problems */ (void) memcpy(s1, s2, n); } return s1;}#endif/* * copynum2blk - copy number numerator to block */intcopynum2blk(NUMBER *snum, long ssi, long num, BLOCK *dblk, long dsi, BOOL noreloc){ size_t newlen; size_t newsize; USB8 *newdata;#if CALC_BYTE_ORDER == BIG_ENDIAN ZVALUE *swnum; /* byte swapped numerator */#endif if (ssi > snum->num.len) return E_COPY2; if (num < 0) num = snum->num.len - ssi; if (num == 0) /* Nothing to be copied */ return 0; if (ssi + num > snum->num.len) return E_COPY5; if (dsi < 0) dsi = dblk->datalen; newlen = dsi + (num*sizeof(HALF)); if (newlen <= 0) return E_COPY7; if (newlen >= (size_t)dblk->maxsize) { if (noreloc) return E_COPY17; newsize = (1 + newlen/dblk->blkchunk) * dblk->blkchunk; newdata = (USB8*) realloc(dblk->data, newsize); if (newdata == NULL) { math_error("Out of memory for num-to-block copy"); /*NOTREACHED*/ } dblk->data = newdata; dblk->maxsize = newsize; }#if CALC_BYTE_ORDER == LITTLE_ENDIAN memmove(dblk->data+dsi, (char *)(snum->num.v+ssi), num*sizeof(HALF));#else swnum = swap_b8_in_ZVALUE(NULL, &(snum->num), FALSE); memmove(dblk->data+dsi, (char *)(swnum->v+ssi), num*sizeof(HALF)); zfree(*swnum);#endif if (newlen > (size_t)dblk->datalen) dblk->datalen = newlen; return 0;}/* * copyblk2num - copy block to number */intcopyblk2num(BLOCK *sblk, long ssi, long num, NUMBER *dnum, long dsi, NUMBER **res){ size_t newlen; NUMBER *ret; /* cloned and modified numerator */#if CALC_BYTE_ORDER == BIG_ENDIAN HALF *swapped; /* byte swapped input data */ unsigned long halflen; /* length of the input ounded up to HALFs */ HALF *h; /* copy byteswap pointer */ unsigned long i;#endif if (ssi > sblk->datalen) return E_COPY2; if (num < 0) num = sblk->datalen - ssi; if (num == 0) /* Nothing to be copied */ return 0; if (ssi + num > sblk->datalen) return E_COPY5; if (dsi < 0) dsi = dnum->num.len; newlen = dsi + ((num+sizeof(HALF)-1)/sizeof(HALF)); if (newlen <= 0) return E_COPY7; /* quasi-clone the numerator to the new size */ ret = qalloc(); ret->num.sign = dnum->num.sign; ret->num.v = alloc(newlen); ret->num.len = newlen; /* ensure that any trailing octets will be zero filled */ ret->num.v[newlen-1] = 0; zcopyval(dnum->num, ret->num); if (!zisunit(ret->den)) { ret->den.len = dnum->den.len; ret->den.v = alloc(dnum->den.len); zcopyval(dnum->den, ret->den); } /* move the data */#if CALC_BYTE_ORDER == LITTLE_ENDIAN memmove((char *)(ret->num.v + dsi), sblk->data + ssi, num);#else /* form a HALF aligned copy of the input */ halflen = (num+sizeof(HALF)-1) / sizeof(HALF); swapped = (HALF *)malloc(halflen * sizeof(HALF)); if (swapped == NULL) { math_error("Out of memory for block-to-num copy"); /*NOTREACHED*/ } /* ensure that any trailing octets will be zero filled */ swapped[halflen-1] = 0; memcpy(swapped, sblk->data + ssi, num); /* byte swap the copy of the input */ for (i=0, h=swapped; i < halflen; ++i, ++h) { SWAP_B8_IN_HALF(h, h); } /* copy over whole byte-swapped HALFs */ memcpy((char *)(ret->num.v + dsi), swapped, (num/sizeof(HALF))*sizeof(HALF)); /* copy over any octets in the last partial HALF */ i = num % sizeof(HALF); if (i != 0) { memcpy((char *)(ret->num.v + dsi)+num-i, (char *)swapped + num-i, i); } free(swapped);#endif /* save new number */ *res = ret; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -