File: t_ncx.c

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (1250 lines) | stat: -rw-r--r-- 27,654 bytes parent folder | download | duplicates (2)
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/*
 *  Copyright 1996, University Corporation for Atmospheric Research
 *  See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */
/* $Id: t_ncx.c,v 1.1 2005/07/15 21:56:39 andy Exp $ */

#include <stdio.h>
#include <limits.h>
/* alias poorly named limits.h macros */
#define  SHORT_MAX  SHRT_MAX
#define  SHORT_MIN  SHRT_MIN
#define USHORT_MAX USHRT_MAX
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <string.h>
#include "ncx.h"

#define NO_UNSIGNED
#define NO_UNSIGNED_LONG

/*
 * This program tests the xdr_mem implementation and
 * the ncx_ implementation, and compares the two.
 * Link like this: 
 * cc t_ncx.c ncx.o [-lsome_xdr_lib] -o t_nxc
 * Successful output is:
  xdr_encode ends at byte 640
  xdr_check  ends at byte 640
  ncx_encode ends at byte 640
  ncx_check  ends at byte 640
  xdr_check  ends at byte 640
  ncx_check  ends at byte 640
 * with exit status 0;
 */

#define XBSZ 1024

char xdrb[XBSZ];
char ncxb[XBSZ];

#define ArraySize(thang) (sizeof(thang)/sizeof(thang[0]))
#define uiArraySize(thang) ((u_int)ArraySize(thang))
#define eSizeOf(thang) ((u_int)(sizeof(thang[0])))

/*
 * Some test data
 */

static char text[] = { "Hiya sailor. New in town?" };

/*
 * Some test data
 * The ideas is that ncx_putn_type_type(...., types)
 * should not return NC_ERANGE.
 */

#if SCHAR_MAX == X_SCHAR_MAX && SCHAR_MIN == X_SCHAR_MIN
static schar schars[] = {
  SCHAR_MIN, SCHAR_MIN +1,
  -1, 0, 1,
  SCHAR_MAX - 1, SCHAR_MAX
};
#else
/* The implementation and this test assume 8 bit bytes. */
#error "Not 8 bit bytes ??"
#endif

static short shorts[] = {
#if SHORT_MAX <= X_SHORT_MAX
  SHORT_MIN, SHORT_MIN + 1,
#  if SCHAR_MAX < X_SHORT_MAX
  SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
#  endif
  -1, 0, 1,
#  if SCHAR_MAX < X_SHORT_MAX
  SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
#  endif
  SHORT_MAX - 1, SHORT_MAX
#else
  X_SHORT_MIN, X_SHORT_MIN + 1,
#  if SCHAR_MAX < X_SHORT_MAX
  SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
#  endif
  -1, 0, 1,
#  if SCHAR_MAX < X_SHORT_MAX
  SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
#  endif
  X_SHORT_MAX - 1, X_SHORT_MAX
#endif
};

static int ints[] = {
#if INT_MAX <= X_INT_MAX
  INT_MIN, INT_MIN +1,
#  if SHORT_MAX < X_INT_MAX
  SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
#  endif
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
#  endif
  -1, 0, 1,
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
#  endif
#  if SHORT_MAX < X_INT_MAX
  SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
#  endif
  INT_MAX - 1, INT_MAX
#else
  X_INT_MIN, X_INT_MIN +1,
#  if SHORT_MAX < X_INT_MAX
  SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
#  endif
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
#  endif
  -1, 0, 1,
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
#  endif
#  if SHORT_MAX < X_INT_MAX
  SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
#  endif
  X_INT_MAX - 1, X_INT_MAX
#endif /* INT */
};


/* N.B. only testing longs over X_INT range for now */
static long longs[] = {
#if LONG_MAX <= X_INT_MAX
  LONG_MIN, LONG_MIN +1,
#  if INT_MAX < X_INT_MAX
  INT_MIN -1, INT_MIN, INT_MIN + 1,
#  endif
#  if SHORT_MAX < X_INT_MAX
  SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
#  endif
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
#  endif
  -1, 0, 1,
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
#  endif
#  if SHORT_MAX < X_INT_MAX
  SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
#  endif
#  if INT_MAX < X_INT_MAX
  INT_MAX -1, INT_MAX, INT_MAX + 1,
#  endif
  LONG_MAX - 1, LONG_MAX
#else
  X_INT_MIN, X_INT_MIN +1,
#  if SHORT_MAX < X_INT_MAX
  SHORT_MIN -1, SHORT_MIN, SHORT_MIN + 1,
#  endif
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MIN - 1, SCHAR_MIN, SCHAR_MIN + 1,
#  endif
  -1, 0, 1,
#  if SCHAR_MAX < X_INT_MAX
  SCHAR_MAX - 1, SCHAR_MAX, SCHAR_MAX + 1,
#  endif
#  if SHORT_MAX < X_INT_MAX
  SHORT_MAX - 1, SHORT_MAX, SHORT_MAX +1,
#  endif
  X_INT_MAX - 1, X_INT_MAX
#endif
};

static float floats[] = {
   -100.625, -100.5, -100.375, -100.25, -100.125,
  -1.0, -.125, 0., .125, 1.,
   100.125, 100.25, 100.375, 100.5, 100.625
};

/* The big numbers require 25 bits: 2^(25-i)+1/2^i, i = 2, 3, ..., 6 */
static double doubles[] = {
  -8388608.25, -4194304.125, -2097152.0625, -1048576.03125, -524288.015625
  -100.625, -100.5, -100.375, -100.25, -100.125,
  -1.0, -.125, 0., .125, 1.,
  100.125, 100.25, 100.375, 100.5, 100.625,
  524288.015625, 1048576.03125, 2097152.0625, 4194304.125, 8388608.25
};

/* End of test data */

/*
 *  Copyright 1993, University Corporation for Atmospheric Research
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
 */
/*  $Id: t_ncx.c,v 1.1 2005/07/15 21:56:39 andy Exp $ */

/* putget.c */
/*
 * xdr 1 - 3 bytes, leaving adjoining bytes within the word ok.
 * (minimum unit of io is 4 bytes)
 */
static bool_t
xdr_NCvbyte(XDR *xdrs, unsigned rem, unsigned count, char *value) 
{
  char buf[4] ;
  u_int origin ;
  enum xdr_op  x_op = xdrs->x_op ; /* save state */

  if(x_op == XDR_ENCODE)
  {
  /*
   * Since we only read/write multiples of four bytes,
   * We will read in the word to change one byte in it.
   */
    origin = xdr_getpos( xdrs ) ;
    /* next op is a get */
    xdrs->x_op = XDR_DECODE  ;
  }

  if(!xdr_opaque(xdrs, buf, 4))
  {
    /* get failed, assume we are trying to read off the end */
    (void)memset(buf, 0, sizeof(buf)) ;
  }

  if(x_op == XDR_ENCODE) /* back to encode */
    xdrs->x_op = x_op ;

  while(count-- != 0)
  {
    if(x_op == XDR_ENCODE)
      buf[rem] = *value ;
    else
      *value = buf[rem] ;
  
    rem++ ;
    value++ ;
  }

  if(x_op == XDR_ENCODE)
  {
    if( !xdr_setpos(xdrs, origin) )
      return(FALSE) ;
    if( !xdr_opaque(xdrs, buf, 4))
      return(FALSE) ;
  }

  return(TRUE) ;
}


/* xdrshorts.c */
/* you may wish to tune this: big on a cray, small on a PC? */
#define NC_SHRT_BUFSIZ 8192
#define NC_NSHRTS_PER (NC_SHRT_BUFSIZ/2) /* number of netshorts the buffer holds */

/*
 * xdr a short leaving adjoining short within the word ok.
 * (minimum unit of io is 4 bytes)
 */
static bool_t
xdr_NCvshort(XDR *xdrs, unsigned which, short *value)
{
  unsigned char buf[4] ; /* unsigned is important here */
  u_int origin ;
  enum xdr_op  x_op = xdrs->x_op ; /* save state */

  if(x_op == XDR_ENCODE)
  {
    origin = xdr_getpos( xdrs ) ;
    /* next op is a get */
    xdrs->x_op = XDR_DECODE  ;
  }

  if(!xdr_opaque(xdrs, (caddr_t)buf, 4))
  {
    /* get failed, assume we are trying to read off the end */
    (void)memset(buf, 0, sizeof(buf)) ;
  }

  if(x_op == XDR_ENCODE) /* back to encode */
    xdrs->x_op = x_op ;

  if(which != 0) which = 2 ;

  if(xdrs->x_op == XDR_ENCODE)
  {
    buf[which +1] = *value % 256 ;
    buf[which] = (*value >> 8) ;

    if( !xdr_setpos(xdrs, origin) )
      return(FALSE) ;
    if( !xdr_opaque(xdrs, (caddr_t)buf, 4))
      return(FALSE) ;
  }
  else
  {
    *value = (((unsigned)buf[which] & 0x7f) << 8) +
       (unsigned)buf[which + 1] ;
    if((unsigned)buf[which] & 0x80)
    {
      /* extern is neg */
      *value -= 0x8000 ;
    }
  }
  return(TRUE) ;
}

/*
 * internal function, bulk xdr of an even number of shorts, less than NC_NSHRTS_PER
 */
static
bool_t
NCxdr_shortsb(XDR *xdrs, short *sp, u_int nshorts)
{
  unsigned char buf[NC_SHRT_BUFSIZ] ;
  unsigned char *cp ;
  unsigned int nbytes = nshorts * 2;

  /* assert(nshorts <= NC_NSHRTS_PER) ; */
  /* assert(nshorts > 0) ; */

  if(xdrs->x_op == XDR_ENCODE)
  {
    for(cp = buf ; cp < &buf[nbytes] ; sp++, cp += 2 )
    {
      *(cp +1) = *sp % 256 ;
      *cp = (*sp >> 8) ;
    }
  }

  if(!xdr_opaque(xdrs, (caddr_t)buf, nbytes))
    return FALSE ;
  
  if(xdrs->x_op == XDR_DECODE)
  {
    for(cp = buf ; cp < &buf[nbytes] ; sp++, cp += 2 )
    {
      *sp = (((unsigned)*cp & 0x7f) << 8) +
         (unsigned)*(cp +1) ;
      if((unsigned)*cp & 0x80)
      {
        /* extern is neg */
        *sp -= 0x8000 ;
      }
    }
  }

  return TRUE ;
}


/*
 * Translate an array of cnt short integers at sp.
 */
bool_t
xdr_shorts(XDR *xdrs, short *sp, u_int cnt)
{
  int odd ; /* 1 if cnt is odd, 0 otherwise */

  if(cnt == 0)
    return TRUE ;  /* ? */

  odd = cnt % 2 ;
  if(odd) 
    cnt-- ;
  /* cnt is even, odd is set if apropos */

  while(cnt > NC_NSHRTS_PER)
  {
    if(!NCxdr_shortsb(xdrs, sp, NC_NSHRTS_PER))
      return FALSE ;
    /* else */
    sp += NC_NSHRTS_PER ;
    cnt -= NC_NSHRTS_PER ;
  }

  /* we know cnt <= NC_NSHRTS_PER at this point */

  if(cnt != 0)
  {
    if(!NCxdr_shortsb(xdrs, sp, cnt))
      return FALSE ;
    /* else */
    sp += cnt ;
    cnt = 0 ;
  }

  if(odd)
    if(!xdr_NCvshort(xdrs, 0, sp))
      return FALSE ;

  return TRUE ;
}

/*
 * Use standard xdr interface (plus the netcdf xdr_shorts())
 * to encode data to 'buf'
 * Returns 0 on success.
 */
static int
xdr_encode(char *buf, u_int sz)
{
  XDR xdrs[1];
  u_int pos;
  int ii;

  xdrmem_create(xdrs, buf, sz, XDR_ENCODE);

  if(!xdr_opaque(xdrs, (caddr_t)text, (u_int)sizeof(text)))
    return 1;

  if(!xdr_opaque(xdrs, (caddr_t)schars, (u_int)sizeof(schars)))
    return 2;

  if(!xdr_shorts(xdrs, shorts, uiArraySize(shorts)))
    return 3;

  if(!xdr_vector(xdrs, (char *)ints,
      uiArraySize(ints), eSizeOf(ints),
      (xdrproc_t)xdr_int))
    return 4;

  /* double the ints to check both ncx_ interfaces */
  if(!xdr_vector(xdrs, (char *)ints,
      uiArraySize(ints), eSizeOf(ints),
      (xdrproc_t)xdr_int))
    return 5;

#ifndef NO_UNSIGNED
  if(!xdr_vector(xdrs, (char *)u_ints,
      uiArraySize(u_ints), eSizeOf(u_ints),
      (xdrproc_t)xdr_u_int))
    return 6;
#endif

  if(!xdr_vector(xdrs, (char *)longs,
      uiArraySize(longs), eSizeOf(longs),
      (xdrproc_t)xdr_long))
    return 7;

#ifndef NO_UNSIGNED_LONG
  if(!xdr_vector(xdrs, (char *)u_longs,
      uiArraySize(u_longs), eSizeOf(u_longs),
      (xdrproc_t)xdr_u_long))
    return 9;
#endif

  if(!xdr_vector(xdrs, (char *)floats,
      uiArraySize(floats), eSizeOf(floats),
      (xdrproc_t)xdr_float))
    return 10;

  if(!xdr_vector(xdrs, (char *)doubles,
      uiArraySize(doubles), eSizeOf(doubles),
      (xdrproc_t)xdr_double))
    return 11;

  /* mix it up */
  for(ii = 1; ii < 5; ii++)
  {
    if(
        !xdr_opaque(xdrs, (caddr_t)text, ii)
        || !xdr_shorts(xdrs, shorts, ii)
        || !xdr_opaque(xdrs, (caddr_t)schars, ii)
    )
      return (11 + ii);
  }

  /*
   * Test non-aligned unit ops used by netcdf.
   */

  for(ii = 1; ii < 5; ii++)
  {
    pos = xdr_getpos(xdrs);
    if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii, &text[ii]))
      return (15 + ii);
    if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
      return (15 + ii);
  }

  for(ii = 1; ii < 5; ii++)
  {
    pos = xdr_getpos(xdrs);
    if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii,
        (char *)&schars[ii]))
      return (19 + ii);
    if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
      return (18 + ii);
  }

  for(ii = 1; ii < 3; ii++)
  {
    pos = xdr_getpos(xdrs);
    if(!xdr_NCvshort(xdrs, ii%2, &shorts[ii]))
      return (23 + ii);
    if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
      return (23 + ii);
  }

  pos = xdr_getpos(xdrs);
  (void) printf("xdr_encode ends at byte %u\n", pos);

  return 0;
}


static int
cmp_chars(const char *c1, const char *c2, size_t nchars)
{
  int status = 0;
  const char *const end = c1 + nchars;

  while(c1 < end)
  {
    if(*c1 != *c2)
    {
      (void) fprintf(stderr,
          "%c != %c char\n",
          *c1,
          *c2);
      if(status == 0)
        status = *c2 < *c1 ? -1 : 1;
    }
    c1++, c2++;
  }

  return status;
}

static int
cmp_schars(const schar *b1, const schar *b2, size_t nbytes)
{
  int status = 0;
  const schar *const end = b1 + nbytes;

  while(b1 < end)
  {
    if(*b1 != *b2)
    {
      (void) fprintf(stderr,
          "0x%02x != 0x%02x byte\n",
          (unsigned)(*b1),
          (unsigned)(*b2));
        
      if(status == 0)
        status = *b2 < *b1 ? -1 : 1;
    }
    b1++, b2++;
  }

  return status;
}

static int
cmp_shorts(const short *s1, const short *s2, size_t nshorts)
{
  int status = 0;
  const short *const end = s1 + nshorts;

  while(s1 < end)
  {
    if(*s1 != *s2)
    {
      (void) fprintf(stderr,
          "0x%04x != 0x%04x (%hd) short\n",
          (unsigned)(*s1),
          (unsigned)(*s2), *s2);
      if(status == 0)
        status = *s2 < *s1 ? -1 : 1;
    }
    s1++, s2++;
  }

  return status;
}

static int
cmp_ints(const int *i1, const int *i2, size_t nints)
{
  int status = 0;
  const int *const end = i1 + nints;

  while(i1 < end)
  {
    if(*i1 != *i2)
    {
      (void) fprintf(stderr,
          "0x%08x != 0x%08x int\n",
          (unsigned)(*i1),
          (unsigned)(*i2));
      if(status == 0)
        status = *i2 < *i1 ? -1 : 1;
    }
    i1++, i2++;
  }

  return status;
}

#ifndef NO_UNSIGNED
static int
cmp_u_ints(const unsigned int *i1, const unsigned int *i2, size_t nints)
{
  int status = 0;
  const unsigned int *const end = i1 + nints;

  while(i1 < end)
  {
    if(*i1 != *i2)
    {
      (void) fprintf(stderr,
          "(%u) 0x%08x != 0x%08x (%u) uint\n",
          *i1, *i1,
          *i2, *i2);
      if(status == 0)
        status = *i2 < *i1 ? -1 : 1;
    }
    i1++, i2++;
  }

  return status;
}
#endif

static int
cmp_longs(const long *l1, const long *l2, size_t nlongs)
{
  int status = 0;
  const long *const end = l1 + nlongs;

  while(l1 < end)
  {
    if(*l1 != *l2)
    {
      (void) fprintf(stderr,
          "0x%016lx != 0x%016lx long\n",
          (unsigned long)(*l1),
          (unsigned long)(*l2));
      if(status == 0)
        status = *l2 < *l1 ? -1 : 1;
    }
    l1++, l2++;
  }

  return status;
}

#ifndef NO_UNSIGNED_LONG
static int
cmp_u_longs(const unsigned long *l1, const unsigned long *l2, size_t nlongs)
{
  int status = 0;
  const unsigned long *const end = l1 + nlongs;

  while(l1 < end)
  {
    if(*l1 != *l2)
    {
      (void) fprintf(stderr,
          "0x%016lx != 0x%016lx ulong\n",
          *l1,
          *l2);
      if(status == 0)
        status = *l2 < *l1 ? -1 : 1;
    }
    l1++, l2++;
  }

  return status;
}
#endif

static int
cmp_floats(const float *f1, const float *f2, size_t nfloats)
{
#define F_EPS 1.0e-6

  int status = 0;
  const float *const end = f1 + nfloats;

  while(f1 < end)
  {
    if(*f1 < *f2 && *f2 - *f1 > F_EPS)
    {
      (void) fprintf(stderr,
          "%.9e != %.9e float (diff %.9e)\n",
          *f1, *f2, *f1 - *f2);
      if(status == 0)
        status = 1;
    }
    else if( *f2 < *f1 && *f1 - *f2 > F_EPS)
    {
      (void) fprintf(stderr,
          "%.9e != %.9e float (diff %.9e)\n",
          *f1, *f2, *f1 - *f2);
      if(status == 0)
        status = -1;
    }
    f1++, f2++;
  }

  return status;
}

static int
cmp_doubles(const double *d1, const double *d2, size_t ndoubles)
{
#define D_EPS 1.0e-15

  int status = 0;
  const double *const end = d1 + ndoubles;

  while(d1 < end)
  {
    if(*d1 < *d2 && *d2 - *d1 > D_EPS)
    {
      (void) fprintf(stderr,
          "%.17e != %.17e double (diff %.17e)\n",
          *d1, *d2, *d1 - *d2);
      if(status == 0)
        status = 1;
    }
    else if( *d2 < *d1 && *d1 - *d2 > D_EPS)
    {
      (void) fprintf(stderr,
          "%.17e != %.17e double (diff %.17e)\n",
          *d1, *d2, *d1 - *d2);
      if(status == 0)
        status = -1;
    }
    d1++, d2++;
  }

  return status;
}

/*
 * Verify that data in buf is as encoded
 * by xdr_encode() above.
 * Returns zero on sucess.
 */
static int
xdr_check(char *buf, u_int sz)
{
  XDR xdrs[1];
  char tbuf[XBSZ];
  u_int pos;
  int ii;
  int jj;

  xdrmem_create(xdrs, buf, sz, XDR_DECODE);

  (void) memset(tbuf, 0, sizeof(text)+4);
  if(!xdr_opaque(xdrs, (caddr_t)tbuf, (u_int)sizeof(text))
      || cmp_chars(tbuf, text,
         sizeof(text)) != 0)
    return 1;

  (void) memset(tbuf, 0, sizeof(schars)+4);
  if(!xdr_opaque(xdrs, (caddr_t)tbuf, (u_int)sizeof(schars))
      || cmp_schars((schar *)tbuf, schars,
        sizeof(schars)) != 0)
    return 2;

  (void) memset(tbuf, 0, sizeof(shorts)+4);
  if(!xdr_shorts(xdrs, (short *)tbuf, uiArraySize(shorts))
      || cmp_shorts((short *)tbuf, shorts,
         ArraySize(shorts)) != 0)
    return 3;

  (void) memset(tbuf, 0, sizeof(ints)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(ints), eSizeOf(ints),
    (xdrproc_t)xdr_int)
      || cmp_ints((int *)tbuf, ints,
         ArraySize(ints)) != 0)
    return 4;

  /* double the ints to check both ncx_ interfaces */
  (void) memset(tbuf, 0, sizeof(ints)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(ints), eSizeOf(ints),
    (xdrproc_t)xdr_int)
      || cmp_ints((int *)tbuf, ints,
         ArraySize(ints)) != 0)
    return 5;

#ifndef NO_UNSIGNED
  (void) memset(tbuf, 0, sizeof(u_ints)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(u_ints), eSizeOf(u_ints),
    (xdrproc_t)xdr_u_int)
      || cmp_u_ints((unsigned int *)tbuf, u_ints,
        ArraySize(u_ints)) != 0)
    return 6;
#endif

  (void) memset(tbuf, 0, sizeof(longs)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(longs), eSizeOf(longs), (xdrproc_t)xdr_long)
      || cmp_longs((long *)tbuf, longs,
         ArraySize(longs)) != 0)
    return 7;

#ifndef NO_UNSIGNED_LONG
  (void) memset(tbuf, 0, sizeof(u_longs)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(u_longs), eSizeOf(u_longs), (xdrproc_t)xdr_u_long)
      || cmp_u_longs((unsigned long *)tbuf, u_longs,
        ArraySize(u_longs)) != 0)
    return 9;
#endif

  (void) memset(tbuf, 0, sizeof(floats)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(floats), eSizeOf(floats), (xdrproc_t)xdr_float)
      || cmp_floats((float *)tbuf, floats,
         ArraySize(floats)) != 0)
    return 10;

  (void) memset(tbuf, 0, sizeof(doubles)+4);
  if(!xdr_vector(xdrs, tbuf,
    uiArraySize(doubles), eSizeOf(doubles), (xdrproc_t)xdr_double)
      || cmp_doubles((double *)tbuf, doubles,
         ArraySize(doubles)) != 0)
    return 11;

  for(ii = 1; ii < 5; ii++)
  {
    char tx[4];
    short sh[4];
    schar by[4];
    if(
        !xdr_opaque(xdrs, (caddr_t)tx, ii)
        || !xdr_shorts(xdrs, sh, ii)
        || !xdr_opaque(xdrs, (caddr_t)by, ii)
    )
      return (11 + ii);
    for(jj = 0; jj < ii; jj++)
    {
      if(tx[jj] != text[jj])
      {
        (void) fprintf(stderr, "\txdr %c != %c text[%d]\n",
            tx[jj], text[jj], jj);
        return (11 + ii);
      }
      /* else */
      if(sh[jj] != shorts[jj])
      {
        (void) fprintf(stderr, "\txdr %hd != %hd shorts[%d]\n",
            sh[jj], shorts[jj], jj);
        return (11 + ii);
      }
      /* else */
      if(by[jj] != schars[jj])
      {
        (void) fprintf(stderr,
          "\txdr 0x%02x != 0x%02x schars[%d]\n",
            (unsigned) by[jj],
            (unsigned) schars[jj], jj);
        return (11 + ii);
      }
      /* else */
    }
  }

  /*
   * Test non-aligned unit ops used by netcdf.
   */

  for(ii = 1; ii < 5; ii++)
  {
    pos = xdr_getpos(xdrs);
    (void) memset(tbuf, 0, BYTES_PER_XDR_UNIT);
    if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii, tbuf)
        || cmp_chars(&text[ii], tbuf,
          BYTES_PER_XDR_UNIT -ii) != 0)
      return (15 + ii);
    if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
      return (15 + ii);
  }

  for(ii = 1; ii < 5; ii++)
  {
    pos = xdr_getpos(xdrs);
    (void) memset(tbuf, 0, BYTES_PER_XDR_UNIT);
    if(!xdr_NCvbyte(xdrs, ii, BYTES_PER_XDR_UNIT -ii, tbuf)
        || cmp_schars((schar *)tbuf, &schars[ii],
          BYTES_PER_XDR_UNIT -ii) != 0)
      return (19 + ii);
    if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
      return (19 + ii);
  }

  for(ii = 1; ii < 3; ii++)
  {
    pos = xdr_getpos(xdrs);
    (void) memset(tbuf, 0, BYTES_PER_XDR_UNIT);
    if(!xdr_NCvshort(xdrs, ii%2, (short *)tbuf)
        || cmp_shorts((short *)tbuf, &shorts[ii], 1))
      return (23 + ii);
    if(!xdr_setpos(xdrs, pos + BYTES_PER_XDR_UNIT))
      return (23 + ii);
  }

  pos = xdr_getpos(xdrs);
  (void) printf("xdr_check  ends at byte %u\n", pos);

  return 0;
}


/* Poor man's template */
#define NCX_VEC(xpp, nn, vecp, TYPE, proc, step) \
{ \
\
  size_t nelems = (nn); \
  TYPE *elemp = (vecp); \
\
  while(nelems != 0) \
  { \
    status = (proc)((*(xpp)), elemp); \
    if(status != ENOERR) \
      break; \
    (*(xpp)) = (void *)((char *)(*(xpp)) + (step)); \
    elemp ++; \
    nelems--; \
  } \
}


/*
 * Use ncx interface
 * to encode data to 'buf'
 * Returns zero on success.
 */
static int
ncx_encode(char *buf)
{
  int status = ENOERR;

  void *vp = buf;
  int ii;

  if(ncx_pad_putn_text(&vp, sizeof(text), text))
    return 1;

  if(ncx_pad_putn_schar_schar(&vp, sizeof(schars), schars))
    return 2;

  if(ncx_pad_putn_short_short(&vp, ArraySize(shorts), shorts))
    return 3;

  if(ncx_putn_int_int(&vp, ArraySize(ints), ints))
    return 4;

  NCX_VEC(&vp, ArraySize(ints), ints,
       int, ncx_put_int_int, X_SIZEOF_INT);
  if(status != ENOERR)
    return 5;

#ifndef NO_UNSIGNED
  NCX_VEC(&vp, ArraySize(u_ints), u_ints,
      unsigned int, ncx_put_uint_uint, X_SIZEOF_INT);
  if(status != ENOERR)
    return 6;
#endif

  if(ncx_putn_int_long(&vp, ArraySize(longs), longs))
    return 7;

#ifndef NO_UNSIGNED_LONG
  NCX_VEC(&vp, ArraySize(u_longs), u_longs,
      unsigned long, ncx_put_ulong_ulong, X_SIZEOF_LONG);
  if(status != ENOERR)
    return 9;
#endif

  if(ncx_putn_float_float(&vp, ArraySize(floats), floats))
    return 10;

  if(ncx_putn_double_double(&vp, ArraySize(doubles), doubles))
    return 11;

  /* mix it up */
  for(ii = 1; ii < 5; ii++)
  {
    if(
        ncx_pad_putn_text(&vp, ii, text)
        || ncx_pad_putn_short_short(&vp, ii, shorts)
        || ncx_pad_putn_schar_schar(&vp, ii, schars)
    )
      return (11 + ii);
  }

  /*
   * Test non-aligned unit ops used by netcdf.
   */

  for(ii = 1; ii < 5; ii++)
  {
    vp = (char *)vp + ii;
    if(ncx_putn_text(&vp, X_ALIGN - ii, &text[ii]))
      return (15 + ii);
  }

  for(ii = 1; ii < 5; ii++)
  {
    vp = (char *)vp + ii;
    if(ncx_putn_schar_schar(&vp, X_ALIGN - ii, &schars[ii]))
      return (19 + ii);
  }

  for(ii = 1; ii < 3; ii++)
  {
    char *pos = vp;
    vp = (char *)vp + (ii%2) * 2;
    if(ncx_putn_short_short(&vp, 1, &shorts[ii]))
      return (23 + ii);
    vp = pos + X_ALIGN;
  }

  (void) printf("ncx_encode ends at byte %u\n",
     (unsigned)(((char *)vp) - buf));

  return 0;
}

/* 
 * Verify the ncx_getn_xxx() routines.
 * Returns zero on success.
 */
static int
ncx_check(char *buf)
{
  int status = ENOERR;
  const void *vp = buf;
  char tbuf[XBSZ];
  int ii;
  int jj;

  (void) memset(tbuf, 0, sizeof(text)+4);
  if(ncx_pad_getn_text(&vp, sizeof(text), tbuf)
      || cmp_chars(tbuf, text,
         sizeof(text)) != 0)
    return 1;

  (void) memset(tbuf, 0, sizeof(schars)+4);
  if(ncx_pad_getn_schar_schar(&vp, sizeof(schars), (schar *)tbuf)
      || cmp_schars((schar *)tbuf, schars,
         sizeof(schars)) != 0)
    return 2;

  (void) memset(tbuf, 0, sizeof(shorts)+4);
  if(ncx_pad_getn_short_short(&vp, ArraySize(shorts), (short *)tbuf)
      || cmp_shorts((short *)tbuf, shorts,
         ArraySize(shorts)) != 0)
    return 3;

  (void) memset(tbuf, 0, sizeof(ints)+4);
  if(ncx_getn_int_int(&vp, ArraySize(ints), (int *)tbuf)
      || cmp_ints((int *)tbuf, ints,
         ArraySize(ints)) != 0)
    return 4;

  (void) memset(tbuf, 0, sizeof(ints)+4);
  NCX_VEC(&vp, ArraySize(ints), (int *)tbuf,
        int, ncx_get_int_int, X_SIZEOF_INT);
  if(status != ENOERR
      || cmp_ints((int *)tbuf, ints,
         ArraySize(ints)) != 0)
    return 5;

#ifndef NO_UNSIGNED
  (void) memset(tbuf, 0, sizeof(u_ints)+4);
  NCX_VEC(&vp, ArraySize(u_ints), (unsigned int *)tbuf,
        unsigned, ncx_get_uint_uint, X_SIZEOF_INT);
  if(status != ENOERR
      || cmp_u_ints((unsigned int *)tbuf, u_ints,
         ArraySize(u_ints)) != 0)
    return 6;
#endif

  (void) memset(tbuf, 0, sizeof(longs)+4);
  if(ncx_getn_int_long(&vp, ArraySize(longs), (long *)tbuf)
      || cmp_longs((long *)tbuf, longs,
         ArraySize(longs)) != 0)
    return 7;

#ifndef NO_UNSIGNED_LONG
  (void) memset(tbuf, 0, sizeof(u_longs)+4);
  NCX_VEC(&vp, ArraySize(u_longs), (unsigned long *)tbuf,
      unsigned long, ncx_get_ulong_ulong, X_SIZEOF_LONG);
  if(status != ENOERR
      || cmp_u_longs((unsigned long *)tbuf, u_longs,
        ArraySize(u_longs)) != 0)
    return 9;
#endif

  (void) memset(tbuf, 0, sizeof(floats)+4);
  if(ncx_getn_float_float(&vp, ArraySize(floats), (float *)tbuf)
      || cmp_floats((float *)tbuf, floats,
         ArraySize(floats)) != 0)
    return 10;

  (void) memset(tbuf, 0, sizeof(doubles)+4);
  if(ncx_getn_double_double(&vp, ArraySize(doubles), (double *)tbuf)
      || cmp_doubles((double *)tbuf, doubles,
         ArraySize(doubles)) != 0)
    return 11;

  for(ii = 1; ii < 5; ii++)
  {
    char tx[4];
    short sh[4];
    schar by[4];
    if(
        ncx_pad_getn_text(&vp, ii, tx)
        || ncx_pad_getn_short_short(&vp, ii, sh)
        || ncx_pad_getn_schar_schar(&vp, ii, by)
    )
      return (11 + ii);
    for(jj = 0; jj < ii; jj++)
    {
      if(tx[jj] != text[jj])
      {
        (void) fprintf(stderr,
          "\tncx %c != %c text[%d]\n",
            tx[jj], text[jj], jj);
        return (11 + ii);
      }
      /* else */
      if(sh[jj] != shorts[jj])
      {
        (void) fprintf(stderr,
           "\tncx %hd != %hd shorts[%d]\n",
            sh[jj], shorts[jj], jj);
        return (11 + ii);
      }
      /* else */
      if((unsigned)by[jj] != (unsigned)schars[jj])
      {
        (void) fprintf(stderr,
          "\tncx 0x%02x != 0x%02x schars[%d] %d\n",
            by[jj], schars[jj], jj, ii);
        return (11 + ii);
      }
    }
  }

  /*
   * Test non-aligned unit ops used by netcdf.
   */

  for(ii = 1; ii < 5; ii++)
  {
    (void) memset(tbuf, 0, X_ALIGN);
    vp = (char *)vp + ii;
    if(ncx_getn_text(&vp, X_ALIGN -ii, tbuf)
        || cmp_chars(tbuf, &text[ii],
           X_ALIGN -ii) != 0)
      return (15 + ii);
  }

  for(ii = 1; ii < 5; ii++)
  {
    (void) memset(tbuf, 0, X_ALIGN);
    vp = (char *)vp + ii;
    if(ncx_getn_schar_schar(&vp, X_ALIGN -ii, (schar *)tbuf)
        || cmp_schars((schar *)tbuf, &schars[ii],
           X_ALIGN -ii) != 0)
      return (19 + ii);
  }

  for(ii = 1; ii < 3; ii++)
  {
    const char *pos = vp;
    (void) memset(tbuf, 0, X_ALIGN);
    vp = (char *)vp + (ii%2) *2;
    if(ncx_getn_short_short(&vp, 1, (short *)tbuf)
        || cmp_shorts((short *)tbuf, &shorts[ii],
           1) != 0)
      return (23 + ii);
    vp = pos + X_ALIGN;
  }

  (void) printf("ncx_check  ends at byte %u\n",
     (unsigned)(((char *)vp) - buf));


  return 0;
}


int
main(int ac, char *av[])
{
  int status;

  status = xdr_encode(xdrb, sizeof(xdrb));
  if(status)
  {
    (void) fprintf(stderr,
       "xdr_encode failed %d\n", status);
    return 1;
  }

  status = xdr_check(xdrb, sizeof(xdrb));
  if(status)
  {
    (void) fprintf(stderr,
       "xdr_check of xdrb failed %d\n", status);
    return 1;
  }

  status = ncx_encode(ncxb);
  if(status)
  {
    (void) fprintf(stderr,
       "ncx_encode failed %d\n", status);
    return 1;
  }

  /* cross check */
  status = xdr_check(ncxb, sizeof(ncxb));
  if(status)
  {
    (void) fprintf(stderr,
       "xdr_check of ncxb failed %d\n", status);
    return 1;
  }

  status = ncx_check(xdrb);
  if(status)
  {
    (void) fprintf(stderr,
       "ncx_check of xdrb failed %d\n", status);
    return 1;
  }

  status = ncx_check(ncxb);
  if(status)
  {
    (void) fprintf(stderr,
       "ncx_check of ncxb failed %d\n", status);
    return 1;
  }

  return 0;
}