File: checksum.c

package info (click to toggle)
cfitsio 3.470-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 14,484 kB
  • sloc: ansic: 106,145; yacc: 4,883; sh: 3,259; fortran: 2,613; lex: 504; makefile: 162
file content (508 lines) | stat: -rw-r--r-- 17,601 bytes parent folder | download | duplicates (6)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*  This file, checksum.c, contains the checksum-related routines in the   */
/*  FITSIO library.                                                        */

/*  The FITSIO software was written by William Pence at the High Energy    */
/*  Astrophysic Science Archive Research Center (HEASARC) at the NASA      */
/*  Goddard Space Flight Center.                                           */

#include <string.h>
#include <stdlib.h>
#include "fitsio2.h"
/*------------------------------------------------------------------------*/
int ffcsum(fitsfile *fptr,      /* I - FITS file pointer                  */
           long nrec,           /* I - number of 2880-byte blocks to sum  */
           unsigned long *sum,  /* IO - accumulated checksum              */
           int *status)         /* IO - error status                      */
/*
    Calculate a 32-bit 1's complement checksum of the FITS 2880-byte blocks.
    This routine is based on the C algorithm developed by Rob
    Seaman at NOAO that was presented at the 1994 ADASS conference,  
    published in the Astronomical Society of the Pacific Conference Series.
    This uses a 32-bit 1's complement checksum in which the overflow bits
    are permuted back into the sum and therefore all bit positions are
    sampled evenly. 
*/
{
    long ii, jj;
    unsigned short sbuf[1440];
    unsigned long hi, lo, hicarry, locarry;

    if (*status > 0)
        return(*status);
  /*
    Sum the specified number of FITS 2880-byte records.  This assumes that
    the FITSIO file pointer points to the start of the records to be summed.
    Read each FITS block as 1440 short values (do byte swapping if needed).
  */
    for (jj = 0; jj < nrec; jj++)
    {
      ffgbyt(fptr, 2880, sbuf, status);

#if BYTESWAPPED

      ffswap2( (short *)sbuf, 1440); /* reverse order of bytes in each value */

#endif

      hi = (*sum >> 16);
      lo = *sum & 0xFFFF;

      for (ii = 0; ii < 1440; ii += 2)
      {
        hi += sbuf[ii];
        lo += sbuf[ii+1];
      }

      hicarry = hi >> 16;    /* fold carry bits in */
      locarry = lo >> 16;

      while (hicarry | locarry)
      {
        hi = (hi & 0xFFFF) + locarry;
        lo = (lo & 0xFFFF) + hicarry;
        hicarry = hi >> 16;
        locarry = lo >> 16;
      }

      *sum = (hi << 16) + lo;
    }
    return(*status);
}
/*-------------------------------------------------------------------------*/
void ffesum(unsigned long sum,  /* I - accumulated checksum                */
           int complm,          /* I - = 1 to encode complement of the sum */
           char *ascii)         /* O - 16-char ASCII encoded checksum      */
/*
    encode the 32 bit checksum by converting every 
    2 bits of each byte into an ASCII character (32 bit word encoded 
    as 16 character string).   Only ASCII letters and digits are used
    to encode the values (no ASCII punctuation characters).

    If complm=TRUE, then the complement of the sum will be encoded.

    This routine is based on the C algorithm developed by Rob
    Seaman at NOAO that was presented at the 1994 ADASS conference,
    published in the Astronomical Society of the Pacific Conference Series.
*/
{
    unsigned int exclude[13] = { 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
                                       0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60 };
    unsigned long mask[4] = { 0xff000000, 0xff0000, 0xff00, 0xff  };

    int offset = 0x30;     /* ASCII 0 (zero) */

    unsigned long value;
    int byte, quotient, remainder, ch[4], check, ii, jj, kk;
    char asc[32];

    if (complm)
        value = 0xFFFFFFFF - sum;   /* complement each bit of the value */
    else
        value = sum;

    for (ii = 0; ii < 4; ii++)
    {
        byte = (value & mask[ii]) >> (24 - (8 * ii));
        quotient = byte / 4 + offset;
        remainder = byte % 4;
        for (jj = 0; jj < 4; jj++)
            ch[jj] = quotient;

        ch[0] += remainder;

        for (check = 1; check;)   /* avoid ASCII  punctuation */
            for (check = 0, kk = 0; kk < 13; kk++)
                for (jj = 0; jj < 4; jj += 2)
                    if ((unsigned char) ch[jj] == exclude[kk] ||
                        (unsigned char) ch[jj+1] == exclude[kk])
                    {
                        ch[jj]++;
                        ch[jj+1]--;
                        check++;
                    }

        for (jj = 0; jj < 4; jj++)        /* assign the bytes */
            asc[4*jj+ii] = ch[jj];
    }

    for (ii = 0; ii < 16; ii++)       /* shift the bytes 1 to the right */
        ascii[ii] = asc[(ii+15)%16];

    ascii[16] = '\0';
}
/*-------------------------------------------------------------------------*/
unsigned long ffdsum(char *ascii,  /* I - 16-char ASCII encoded checksum   */
                     int complm,   /* I - =1 to decode complement of the   */
                     unsigned long *sum)  /* O - 32-bit checksum           */
/*
    decode the 16-char ASCII encoded checksum into an unsigned 32-bit long.
    If complm=TRUE, then the complement of the sum will be decoded.

    This routine is based on the C algorithm developed by Rob
    Seaman at NOAO that was presented at the 1994 ADASS conference,
    published in the Astronomical Society of the Pacific Conference Series.
*/
{
    char cbuf[16];
    unsigned long hi = 0, lo = 0, hicarry, locarry;
    int ii;

    /* remove the permuted FITS byte alignment and the ASCII 0 offset */
    for (ii = 0; ii < 16; ii++)
    {
        cbuf[ii] = ascii[(ii+1)%16];
        cbuf[ii] -= 0x30;
    }

    for (ii = 0; ii < 16; ii += 4)
    {
        hi += (cbuf[ii]   << 8) + cbuf[ii+1];
        lo += (cbuf[ii+2] << 8) + cbuf[ii+3];
    }

    hicarry = hi >> 16;
    locarry = lo >> 16;
    while (hicarry || locarry)
    {
        hi = (hi & 0xFFFF) + locarry;
        lo = (lo & 0xFFFF) + hicarry;
        hicarry = hi >> 16;
        locarry = lo >> 16;
    }

    *sum = (hi << 16) + lo;
    if (complm)
        *sum = 0xFFFFFFFF - *sum;   /* complement each bit of the value */

    return(*sum);
}
/*------------------------------------------------------------------------*/
int ffpcks(fitsfile *fptr,      /* I - FITS file pointer                  */
           int *status)         /* IO - error status                      */
/*
   Create or update the checksum keywords in the CHDU.  These keywords
   provide a checksum verification of the FITS HDU based on the ASCII
   coded 1's complement checksum algorithm developed by Rob Seaman at NOAO.
*/
{
    char datestr[20], checksum[FLEN_VALUE], datasum[FLEN_VALUE];
    char  comm[FLEN_COMMENT], chkcomm[FLEN_COMMENT], datacomm[FLEN_COMMENT];
    int tstatus;
    long nrec;
    LONGLONG headstart, datastart, dataend;
    unsigned long dsum, olddsum, sum;
    double tdouble;

    if (*status > 0)           /* inherit input status value if > 0 */
        return(*status);

    /* generate current date string and construct the keyword comments */
    ffgstm(datestr, NULL, status);
    strcpy(chkcomm, "HDU checksum updated ");
    strcat(chkcomm, datestr);
    strcpy(datacomm, "data unit checksum updated ");
    strcat(datacomm, datestr);

    /* write the CHECKSUM keyword if it does not exist */
    tstatus = *status;
    if (ffgkys(fptr, "CHECKSUM", checksum, comm, status) == KEY_NO_EXIST)
    {
        *status = tstatus;
        strcpy(checksum, "0000000000000000");
        ffpkys(fptr, "CHECKSUM", checksum, chkcomm, status);
    }

    /* write the DATASUM keyword if it does not exist */
    tstatus = *status;
    if (ffgkys(fptr, "DATASUM", datasum, comm, status) == KEY_NO_EXIST)
    {
        *status = tstatus;
        olddsum = 0;
        ffpkys(fptr, "DATASUM", "         0", datacomm, status);

        /* set the CHECKSUM keyword as undefined, if it isn't already */
        if (strcmp(checksum, "0000000000000000") )
        {
            strcpy(checksum, "0000000000000000");
            ffmkys(fptr, "CHECKSUM", checksum, chkcomm, status);
        }
    }
    else
    {
        /* decode the datasum into an unsigned long variable */

        /* olddsum = strtoul(datasum, 0, 10); doesn't work on SUN OS */

        tdouble = atof(datasum);
        olddsum = (unsigned long) tdouble;
    }

    /* close header: rewrite END keyword and following blank fill */
    /* and re-read the required keywords to determine the structure */
    if (ffrdef(fptr, status) > 0)
        return(*status);

    if ((fptr->Fptr)->heapsize > 0)
         ffuptf(fptr, status);  /* update the variable length TFORM values */

    /* write the correct data fill values, if they are not already correct */
    if (ffpdfl(fptr, status) > 0)
        return(*status);

    /* calc size of data unit, in FITS 2880-byte blocks */
    if (ffghadll(fptr, &headstart, &datastart, &dataend, status) > 0)
        return(*status);

    nrec = (long) ((dataend - datastart) / 2880);
    dsum = 0;

    if (nrec > 0)
    {
        /* accumulate the 32-bit 1's complement checksum */
        ffmbyt(fptr, datastart, REPORT_EOF, status);
        if (ffcsum(fptr, nrec, &dsum, status) > 0)
            return(*status);
    }

    if (dsum != olddsum)
    {
        /* update the DATASUM keyword with the correct value */ 
        snprintf(datasum, FLEN_VALUE, "%lu", dsum);
        ffmkys(fptr, "DATASUM", datasum, datacomm, status);

        /* set the CHECKSUM keyword as undefined, if it isn't already */
        if (strcmp(checksum, "0000000000000000") )
        {
            strcpy(checksum, "0000000000000000");
            ffmkys(fptr, "CHECKSUM", checksum, chkcomm, status);
        }
    }        

    if (strcmp(checksum, "0000000000000000") )
    {
        /* check if CHECKSUM is still OK; move to the start of the header */
        ffmbyt(fptr, headstart, REPORT_EOF, status);

        /* accumulate the header checksum into the previous data checksum */
        nrec = (long) ((datastart - headstart) / 2880);
        sum = dsum;
        if (ffcsum(fptr, nrec, &sum, status) > 0)
            return(*status);

        if (sum == 0 || sum == 0xFFFFFFFF)
           return(*status);            /* CHECKSUM is correct */

        /* Zero the CHECKSUM and recompute the new value */
        ffmkys(fptr, "CHECKSUM", "0000000000000000", chkcomm, status);
    }

    /* move to the start of the header */
    ffmbyt(fptr, headstart, REPORT_EOF, status);

    /* accumulate the header checksum into the previous data checksum */
    nrec = (long) ((datastart - headstart) / 2880);
    sum = dsum;
    if (ffcsum(fptr, nrec, &sum, status) > 0)
           return(*status);

    /* encode the COMPLEMENT of the checksum into a 16-character string */
    ffesum(sum, TRUE, checksum);

    /* update the CHECKSUM keyword value with the new string */
    ffmkys(fptr, "CHECKSUM", checksum, "&", status);

    return(*status);
}
/*------------------------------------------------------------------------*/
int ffupck(fitsfile *fptr,      /* I - FITS file pointer                  */
           int *status)         /* IO - error status                      */
/*
   Update the CHECKSUM keyword value.  This assumes that the DATASUM
   keyword exists and has the correct value.
*/
{
    char datestr[20], chkcomm[FLEN_COMMENT], comm[FLEN_COMMENT];
    char checksum[FLEN_VALUE], datasum[FLEN_VALUE];
    int tstatus;
    long nrec;
    LONGLONG headstart, datastart, dataend;
    unsigned long sum, dsum;
    double tdouble;

    if (*status > 0)           /* inherit input status value if > 0 */
        return(*status);

    /* generate current date string and construct the keyword comments */
    ffgstm(datestr, NULL, status);
    strcpy(chkcomm, "HDU checksum updated ");
    strcat(chkcomm, datestr);

    /* get the DATASUM keyword and convert it to a unsigned long */
    if (ffgkys(fptr, "DATASUM", datasum, comm, status) == KEY_NO_EXIST)
    {
        ffpmsg("DATASUM keyword not found (ffupck");
        return(*status);
    }

    tdouble = atof(datasum); /* read as a double as a workaround */
    dsum = (unsigned long) tdouble;

    /* get size of the HDU */
    if (ffghadll(fptr, &headstart, &datastart, &dataend, status) > 0)
        return(*status);

    /* get the checksum keyword, if it exists */
    tstatus = *status;
    if (ffgkys(fptr, "CHECKSUM", checksum, comm, status) == KEY_NO_EXIST)
    {
        *status = tstatus;
        strcpy(checksum, "0000000000000000");
        ffpkys(fptr, "CHECKSUM", checksum, chkcomm, status);
    }
    else
    {
        /* check if CHECKSUM is still OK */
        /* rewrite END keyword and following blank fill */
        if (ffwend(fptr, status) > 0)
            return(*status);

        /* move to the start of the header */
        ffmbyt(fptr, headstart, REPORT_EOF, status);

        /* accumulate the header checksum into the previous data checksum */
        nrec = (long) ((datastart - headstart) / 2880);
        sum = dsum;
        if (ffcsum(fptr, nrec, &sum, status) > 0)
           return(*status);

        if (sum == 0 || sum == 0xFFFFFFFF)
           return(*status);    /* CHECKSUM is already correct */

        /* Zero the CHECKSUM and recompute the new value */
        ffmkys(fptr, "CHECKSUM", "0000000000000000", chkcomm, status);
    }

    /* move to the start of the header */
    ffmbyt(fptr, headstart, REPORT_EOF, status);

    /* accumulate the header checksum into the previous data checksum */
    nrec = (long) ((datastart - headstart) / 2880);
    sum = dsum;
    if (ffcsum(fptr, nrec, &sum, status) > 0)
           return(*status);

    /* encode the COMPLEMENT of the checksum into a 16-character string */
    ffesum(sum, TRUE, checksum);

    /* update the CHECKSUM keyword value with the new string */
    ffmkys(fptr, "CHECKSUM", checksum, "&", status);

    return(*status);
}
/*------------------------------------------------------------------------*/
int ffvcks(fitsfile *fptr,      /* I - FITS file pointer                  */
           int *datastatus,     /* O - data checksum status               */
           int *hdustatus,      /* O - hdu checksum status                */
                                /*     1  verification is correct         */
                                /*     0  checksum keyword is not present */
                                /*    -1 verification not correct         */
           int *status)         /* IO - error status                      */
/*
    Verify the HDU by comparing the value of the computed checksums against
    the values of the DATASUM and CHECKSUM keywords if they are present.
*/
{
    int tstatus;
    double tdouble;
    unsigned long datasum, hdusum, olddatasum;
    char chksum[FLEN_VALUE], comm[FLEN_COMMENT];

    if (*status > 0)           /* inherit input status value if > 0 */
        return(*status);

    *datastatus = -1;
    *hdustatus  = -1;

    tstatus = *status;
    if (ffgkys(fptr, "CHECKSUM", chksum, comm, status) == KEY_NO_EXIST)
    {
        *hdustatus = 0;             /* CHECKSUM keyword does not exist */
        *status = tstatus;
    }
    if (chksum[0] == '\0')
        *hdustatus = 0;    /* all blank checksum means it is undefined */

    if (ffgkys(fptr, "DATASUM", chksum, comm, status) == KEY_NO_EXIST)
    {
        *datastatus = 0;            /* DATASUM keyword does not exist */
        *status = tstatus;
    }
    if (chksum[0] == '\0')
        *datastatus = 0;    /* all blank checksum means it is undefined */

    if ( *status > 0 || (!(*hdustatus) && !(*datastatus)) )
        return(*status);            /* return if neither keywords exist */

    /* convert string to unsigned long */

    /* olddatasum = strtoul(chksum, 0, 10);  doesn't work w/ gcc on SUN OS */
    /* sscanf(chksum, "%u", &olddatasum);   doesn't work w/ cc on VAX/VMS */

    tdouble = atof(chksum); /* read as a double as a workaround */
    olddatasum = (unsigned long) tdouble;

    /*  calculate the data checksum and the HDU checksum */
    if (ffgcks(fptr, &datasum, &hdusum, status) > 0)
        return(*status);

    if (*datastatus)
        if (datasum == olddatasum)
            *datastatus = 1;

    if (*hdustatus)
        if (hdusum == 0 || hdusum == 0xFFFFFFFF)
            *hdustatus = 1;

    return(*status);
}
/*------------------------------------------------------------------------*/
int ffgcks(fitsfile *fptr,           /* I - FITS file pointer             */
           unsigned long *datasum,   /* O - data checksum                 */
           unsigned long *hdusum,    /* O - hdu checksum                  */
           int *status)              /* IO - error status                 */

    /* calculate the checksums of the data unit and the total HDU */
{
    long nrec;
    LONGLONG headstart, datastart, dataend;

    if (*status > 0)           /* inherit input status value if > 0 */
        return(*status);

    /* get size of the HDU */
    if (ffghadll(fptr, &headstart, &datastart, &dataend, status) > 0)
        return(*status);

    nrec = (long) ((dataend - datastart) / 2880);

    *datasum = 0;

    if (nrec > 0)
    {
        /* accumulate the 32-bit 1's complement checksum */
        ffmbyt(fptr, datastart, REPORT_EOF, status);
        if (ffcsum(fptr, nrec, datasum, status) > 0)
            return(*status);
    }

    /* move to the start of the header and calc. size of header */
    ffmbyt(fptr, headstart, REPORT_EOF, status);
    nrec = (long) ((datastart - headstart) / 2880);

    /* accumulate the header checksum into the previous data checksum */
    *hdusum = *datasum;
    ffcsum(fptr, nrec, hdusum, status);

    return(*status);
}