📄 ckclib.c
字号:
} if ((maxsiz > -1L) && (z <= maxsiz)) { debug(F111,"fileselect maxsiz skipping",f,maxsiz); /* tlog(F110,"Skipping (too small)",f,0); */ return(0); } } if (nbu) { /* Skipping backup files? */ if (ckmatch(#ifdef CKREGEX "*.~[0-9]*~" /* Not perfect but close enough. */#else "*.~*~" /* Less close. */#endif /* CKREGEX */ ,f,filecase,2+1)) { debug(F110,"fileselect skipping backup",f,0); return(0); } } for (n = 0; xlist && n < nxlist; n++) { if (!xlist[n]) { debug(F101,"fileselect xlist empty",0,n); break; } if (ckmatch(xlist[n],f,filecase,2+1)) { debug(F111,"fileselect xlist",xlist[n],n); debug(F110,"fileselect skipping",f,0); return(0); } } debug(F110,"fileselect selecting",f,0); return(1);}/* C K R A D I X -- Radix converter *//* Call with: s: a number in string format. in: int, specifying the radix of s, 2-36. out: int, specifying the radix to convert to, 2-36. Returns: NULL on error (illegal radix, illegal number, etc.). "-1" on overflow (number too big for unsigned long). Otherwise: Pointer to result.*/#define RXRESULT 127static char rxdigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";static char rxresult[RXRESULT+1];char *ckradix(s,in,out) char * s; int in, out; { char c, *r = rxresult; int d, minus = 0; unsigned long zz = 0L; long z; if (in < 2 || in > 36) /* Verify legal input radix */ return(NULL); if (out < 2 || out > 36) /* and output radix. */ return(NULL); if (*s == '+') { /* Get sign if any */ s++; } else if (*s == '-') { minus++; s++; } while (*s == SP || *s == '0') /* Trim leading blanks or 0's */ s++;/* For detecting overflow, we use a signed copy of the unsigned long accumulator. If it goes negative, we know we'll overflow NEXT time through the loop.*/ for (; *s; s++) { /* Convert from input radix to */ c = *s; /* unsigned long */ if (islower(c)) c = toupper(c); if (c >= '0' && c <= '9') d = c - '0'; else if (c >= 'A' && c <= 'Z') d = c - 'A' + 10; else return(NULL); zz = zz * in + d; if (z < 0L) /* Clever(?) overflow detector */ return("-1"); z = zz; } if (!zz) return("0"); r = &rxresult[RXRESULT]; /* Convert from unsigned long */ *r-- = NUL; /* to output radix. */ while (zz > 0 && r > rxresult) { d = zz % out; *r-- = rxdigits[d]; zz = zz / out; } if (minus) *r-- = '-'; /* Replace original sign */ return((char *)(r+1));}#ifndef NOB64/* Base-64 conversion routines */static char b64[] = { /* Encoding vector */#ifdef pdp11 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="#else 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', 'T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4', '5','6','7','8','9','+','/','=','\0'#endif /* pdp11 */};static int b64tbl[] = { /* Decoding vector */ -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};/* B 8 T O B 6 4 -- Converts 8-bit data to Base64 encoding. Call with: s = Pointer to 8-bit data; n = Number of source bytes to encode (SEE NOTE). If it's a null-terminated string, you can use -1 here. out = Address of output buffer. len = Length of output buffer (should > 4/3 longer than input). Returns: >= 0 if OK, number of bytes placed in output buffer, with the subsequent byte set to NUL if space permits. -1 on error (output buffer space exhausted). NOTE: If this function is to be called repeatedly, e.g. to encode a data stream a chunk at a time, the source length must be a multiple of 3 in all calls but the final one to avoid the generation of extraneous pad characters that would throw the decoder out of sync. When encoding only a single string, this is not a consideration. No internal state is kept, so there is no reset function.*/intb8tob64(s,n,out,len) char * s,* out; int n, len; { int b3, b4, i, x = 0; unsigned int t; if (n < 0) n = strlen(s); for (i = 0; i < n; i += 3,x += 4) { /* Loop through source bytes */ b3 = b4 = 0; t = (unsigned)((unsigned)((unsigned)s[i] & 0xff) << 8); if (n - 1 > i) { /* Do we have another after this? */ t |= (unsigned)(s[i+1] & 0xff); /* Yes, OR it in */ b3 = 1; /* And remember */ } t <<= 8; /* Move over */ if (n - 2 > i) { /* Another one after that? */ t |= (unsigned)(s[i+2] & 0xff); /* Yes, OR it in */ b4 = 1; /* and remember */ } if (x + 4 > len) /* Check output space */ return(-1); out[x+3] = b64[b4 ? (t & 0x3f) : 64]; /* 64 = code for '=' */ t >>= 6; out[x+2] = b64[b3 ? (t & 0x3f) : 64]; t >>= 6; out[x+1] = b64[t & 0x3f]; t >>= 6; out[x] = b64[t & 0x3f]; } if (x < len) out[x] = NUL; /* Null-terminate the string */ return(x);}/* B 6 4 T O B 8 -- Converts Base64 string to 8-bit data. Call with: s = pointer to Base64 string (whitespace ignored). n = length of string, or -1 if null terminated, or 0 to reset. out = address of output buffer. len = length of output buffer. Returns: >= 0 if OK, number of bytes placed in output buffer, with the subsequent byte set to NUL if space permits. < 0 on error: -1 = output buffer too small for input. -2 = input contains illegal characters. -3 = internal coding error. NOTE: Can be called repeatedly to decode a Base64 stream, one chunk at a time. However, if it is to be called for multiple streams in succession, its internal state must be reset at the beginning of the new stream.*/intb64tob8(s,n,out,len) char * s,* out; int len; { /* Decode */ static int bits = 0; static unsigned int r = 0; int i, k = 0, x, t; unsigned char c; if (n == 0) { /* Reset state */ bits = 0; r = 0; return(0); } x = (n < 0) ? strlen(s) : n; /* Source length */ n = ((x + 3) / 4) * 3; /* Compute destination length */ if (x > 0 && s[x-1] == '=') n--; /* Account for padding */ if (x > 1 && s[x-2] == '=') n--; if (n > len) /* Destination not big enough */ return(-1); /* Fail */ for (i = 0; i < x; i++) { /* Loop thru source */ c = (unsigned)s[i]; /* Next char */ t = b64tbl[c]; /* Code for this char */ if (t == -2) { /* Whitespace or Ctrl */ n--; /* Ignore */ continue; } else if (t == -1) { /* Illegal code */ return(-2); /* Fail. */ } else if (t > 63 || t < 0) /* Illegal value */ return(-3); /* fail. */ bits += 6; /* Count bits */ r <<= 6; /* Make space */ r |= (unsigned) t; /* OR in new code */ if (bits >= 8) { /* Have a byte yet? */ bits -= 8; /* Output it */ c = (unsigned) ((r >> bits) & 0xff); out[k++] = c; } } if (k < len) out[k] = NUL; /* Null-terminate in case it's */ return(k); /* a text string */}#endif /* NOB64 *//* C H K N U M -- See if argument string is an integer *//* Returns 1 if OK, zero if not OK *//* If OK, string should be acceptable to atoi() *//* Allows leading space, sign */intchknum(s) char *s; { /* Check Numeric String */ int x = 0; /* Flag for past leading space */ int y = 0; /* Flag for digit seen */ char c; debug(F110,"chknum",s,0); while (c = *s++) { /* For each character in the string */ switch (c) { case SP: /* Allow leading spaces */ case HT: if (x == 0) continue; else return(0); case '+': /* Allow leading sign */ case '-': if (x == 0) x = 1; else return(0); break; default: /* After that, only decimal digits */ if (c >= '0' && c <= '9') { x = y = 1; continue; } else return(0); } } return(y);}/* R D I G I T S -- Verify that all characters in arg ARE DIGITS *//* Returns 1 if so, 0 if not or if string is empty */intrdigits(s) char *s; { if (!s) return(0); do { if (!isdigit(*s)) return(0); s++; } while (*s); return(1);}/* P A R N A M -- Return parity name */char *#ifdef CK_ANSICparnam(char c)#elseparnam(c) char c;#endif /* CK_ANSIC *//* parnam */ { switch (c) { case 'e': return("even"); case 'o': return("odd"); case 'm': return("mark"); case 's': return("space"); case 0: return("none"); default: return("invalid"); }}char * /* Convert seconds to hh:mm:ss */#ifdef CK_ANSIChhmmss(long x)#elsehhmmss(x) long x;#endif /* CK_ANSIC *//* hhmmss(x) */ { static char buf[10]; long s, h, m; h = x / 3600L; /* Hours */ x = x % 3600L; m = x / 60L; /* Minutes */ s = x % 60L; /* Seconds */ if (x > -1L) sprintf(buf,"%02ld:%02ld:%02ld",h,m,s); else buf[0] = NUL; return((char *)buf);}/* L S E T -- Set s into p, right padding to length n with char c; *//* s is a NUL-terminated string. If length(s) > n, only n bytes are moved. The result is NOT NUL terminated unless c == NUL and length(s) < n. The intended of this routine is for filling in fixed-length record fields.*/VOIDlset(p,s,n,c) char *s; char *p; int n; int c; { int x;#ifndef USE_MEMCPY int i;#endif /* USE_MEMCPY */ if (!s) s = ""; x = strlen(s); if (x > n) x = n;#ifdef USE_MEMCPY memcpy(p,s,x); if (n > x) memset(p+x,c,n-x);#else for (i = 0; i < x; i++) *p++ = *s++; for (; i < n; i++) *p++ = c;#endif /* USE_MEMCPY */}/* R S E T -- Right-adjust s in p, left padding to length n with char c */VOIDrset(p,s,n,c) char *s; char *p; int n; int c; { int x;#ifndef USE_MEMCPY int i;#endif /* USE_MEMCPY */ if (!s) s = ""; x = strlen(s); if (x > n) x = n;#ifdef USE_MEMCPY memset(p,c,n-x); memcpy(p+n-x,s,x);#else for (i = 0; i < (n - x); i++) *p++ = c; for (; i < n; i++) *p++ = *s++;#endif /* USE_MEMCPY */}/* U L O N G T O H E X -- Unsigned long to hex *//* Converts unsigned long arg to hex and returns string pointer to rightmost n hex digits left padded with 0's. Allows for longs up to 64 bits. Returns pointer to result.*/char *ulongtohex(z,n) unsigned long z; int n; { static char hexbuf[17]; int i = 16, x, k = 0; hexbuf[16] = '\0'; if (n > 16) n = 16; k = 2 * (sizeof(long)); for (i = 0; i < n; i++) { if (i > k || z == 0) { hexbuf[15-i] = '0'; } else { x = z & 0x0f; z = z >> 4; hexbuf[15-i] = x + ((x < 10) ? '0' : 0x37); } } return((char *)(&hexbuf[16-i]));}/* H E X T O U L O N G -- Hex string to unsigned long *//* Converts n chars from s from hex to unsigned long. Returns: 0L or positive, good result (0L is returned if arg is NULL or empty). -1L on error: non-hex arg or overflow.*/longhextoulong(s,n) char *s; int n; { char buf[64]; unsigned long result = 0L; int d, count = 0, i; int flag = 0; if (!s) s = ""; if (!*s) { return(0L); } if (n < 1) return(0L); if (n > 63) n = 63; strncpy(buf,s,n); buf[n] = '\0'; s = buf; while (*s) { d = *s++; if ((d == '0' || d == ' ')) { if (!flag) continue; } else { flag = 1; } if (islower(d)) d = toupper(d); if (d >= '0' && d <= '9') { d -= 0x30; } else if (d >= 'A' && d <= 'F') { d -= 0x37; } else { return(-1L); } if (++count > (sizeof(long) * 2)) return(-1L); result = (result << 4) | (d & 0x0f); } return(result);}/* End of ckclib.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -