sum2.c (f74fd6ec7ad22a2ee3f40cad8977ee86abefb9c5) sum2.c (3fa15ce5d8c2bff56d3e11f1a47e264ae30514d3)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37#endif
38static const char rcsid[] =
39 "$FreeBSD$";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <unistd.h>
44
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37#endif
38static const char rcsid[] =
39 "$FreeBSD$";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <unistd.h>
44
45#include "extern.h"
46
45int
46csum2(fd, cval, clen)
47int
48csum2(fd, cval, clen)
47 register int fd;
49 int fd;
48 u_int32_t *cval, *clen;
49{
50 u_int32_t *cval, *clen;
51{
50 register u_int32_t crc, total;
51 register int nr;
52 register u_char *p;
52 u_int32_t lcrc, total;
53 int nr;
54 u_char *p;
53 u_char buf[8192];
54
55 /*
56 * Draft 8 POSIX 1003.2:
57 *
58 * s = sum of all bytes
59 * r = s % 2^16 + (s % 2^32) / 2^16
55 u_char buf[8192];
56
57 /*
58 * Draft 8 POSIX 1003.2:
59 *
60 * s = sum of all bytes
61 * r = s % 2^16 + (s % 2^32) / 2^16
60 * crc = (r % 2^16) + r / 2^16
62 * lcrc = (r % 2^16) + r / 2^16
61 */
63 */
62 crc = total = 0;
64 lcrc = total = 0;
63 while ((nr = read(fd, buf, sizeof(buf))) > 0)
64 for (total += nr, p = buf; nr--; ++p)
65 while ((nr = read(fd, buf, sizeof(buf))) > 0)
66 for (total += nr, p = buf; nr--; ++p)
65 crc += *p;
67 lcrc += *p;
66 if (nr < 0)
67 return(1);
68
68 if (nr < 0)
69 return(1);
70
69 crc = (crc & 0xffff) + (crc >> 16);
70 crc = (crc & 0xffff) + (crc >> 16);
71 lcrc = (lcrc & 0xffff) + (lcrc >> 16);
72 lcrc = (lcrc & 0xffff) + (lcrc >> 16);
71
73
72 *cval = crc;
74 *cval = lcrc;
73 *clen = total;
74 return(0);
75}
75 *clen = total;
76 return(0);
77}