aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qpdf-ctest.c
blob: 90ebdcd47f594cb3799d132c1901943e387af15a (plain) (blame)
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
#include <qpdf/qpdf-c.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "../libqpdf/qpdf/qpdf-config.h" // for LL_FMT

static char* whoami = 0;
static qpdf_data qpdf = 0;

static FILE* safe_fopen(char const* filename, char const* mode)
{
    // This function is basically a "C" port of QUtil::safe_fopen.
    FILE* f = 0;
#ifdef _MSC_VER
    errno_t err = fopen_s(&f, filename, mode);
    if (err != 0)
    {
        char buf[94];
        strerror_s(buf, sizeof(buf), errno);
	fprintf(stderr, "%s: unable to open %s: %s\n",
		whoami, filename, buf);
	exit(2);
    }
#else
    f = fopen(filename, mode);
    if (f == NULL)
    {
	fprintf(stderr, "%s: unable to open %s: %s\n",
		whoami, filename, strerror(errno));
	exit(2);
    }
#endif
    return f;
}

static void print_error(char const* label, qpdf_data qpdf, qpdf_error e)
{
#define POS_FMT "  pos : " LL_FMT "\n"
    printf("%s: %s\n", label, qpdf_get_error_full_text(qpdf, e));
    printf("  code: %d\n", qpdf_get_error_code(qpdf, e));
    printf("  file: %s\n", qpdf_get_error_filename(qpdf, e));
    printf(POS_FMT, qpdf_get_error_file_position(qpdf, e));
    printf("  text: %s\n", qpdf_get_error_message_detail(qpdf, e));
}

static void report_errors()
{
    qpdf_error e = 0;
    while (qpdf_more_warnings(qpdf))
    {
	e = qpdf_next_warning(qpdf);
        print_error("warning", qpdf, e);
    }
    if (qpdf_has_error(qpdf))
    {
	e = qpdf_get_error(qpdf);
	assert(qpdf_has_error(qpdf) == QPDF_FALSE);
        print_error("error", qpdf, e);
    }
    else
    {
	e = qpdf_get_error(qpdf);
	assert(e == 0);
	assert(qpdf_get_error_code(qpdf, e) == qpdf_e_success);
	// Call these to ensure that they can be called on a null
	// error pointer.
	(void)qpdf_get_error_full_text(qpdf, e);
	(void)qpdf_get_error_filename(qpdf, e);
	(void)qpdf_get_error_file_position(qpdf, e);
	(void)qpdf_get_error_message_detail(qpdf, e);
    }
}

static void handle_oh_error(qpdf_data qpdf, qpdf_error error, void* data)
{
    char const* label = "oh error";
    if (data)
    {
        label = *((char const**)data);
    }
    print_error(label, qpdf, error);
}

static void read_file_into_memory(char const* filename,
				  char** buf, unsigned long* size)
{
    char* buf_p = 0;
    FILE* f = NULL;
    size_t bytes_read = 0;
    size_t len = 0;

    f = safe_fopen(filename, "rb");
    fseek(f, 0, SEEK_END);
    *size = (unsigned long) ftell(f);
    fseek(f, 0, SEEK_SET);
    *buf = malloc(*size);
    if (*buf == NULL)
    {
	fprintf(stderr, "%s: unable to allocate %lu bytes\n",
		whoami, *size);
	exit(2);
    }
    buf_p = *buf;
    bytes_read = 0;
    len = 0;
    while ((len = fread(buf_p + bytes_read, 1, *size - bytes_read, f)) > 0)
    {
	bytes_read += len;
    }
    if (bytes_read != *size)
    {
	if (ferror(f))
	{
	    fprintf(stderr, "%s: failure reading file %s into memory:",
		    whoami, filename);
	}
	else
	{
	    fprintf(stderr, "%s: premature EOF reading file %s:",
		    whoami, filename);
	}
	fprintf(stderr, " read %lu, wanted %lu\n",
		(unsigned long) bytes_read, (unsigned long) *size);
	exit(2);
    }
    fclose(f);
}

static void count_progress(int percent, void* data)
{
    ++(*(int*)data);
}

static void test01(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    printf("version: %s\n", qpdf_get_pdf_version(qpdf));
    if (qpdf_get_pdf_extension_level(qpdf) > 0)
    {
        printf("extension level: %d\n", qpdf_get_pdf_extension_level(qpdf));
    }
    printf("linearized: %d\n", qpdf_is_linearized(qpdf));
    printf("encrypted: %d\n", qpdf_is_encrypted(qpdf));
    if (qpdf_is_encrypted(qpdf))
    {
	printf("user password: %s\n", qpdf_get_user_password(qpdf));
	printf("extract for accessibility: %d\n",
	       qpdf_allow_accessibility(qpdf));
	printf("extract for any purpose: %d\n",
	       qpdf_allow_extract_all(qpdf));
	printf("print low resolution: %d\n",
	       qpdf_allow_print_low_res(qpdf));
	printf("print high resolution: %d\n",
	       qpdf_allow_print_high_res(qpdf));
	printf("modify document assembly: %d\n",
	       qpdf_allow_modify_assembly(qpdf));
	printf("modify forms: %d\n",
	       qpdf_allow_modify_form(qpdf));
	printf("modify annotations: %d\n",
	       qpdf_allow_modify_annotation(qpdf));
	printf("modify other: %d\n",
	       qpdf_allow_modify_other(qpdf));
	printf("modify anything: %d\n",
	       qpdf_allow_modify_all(qpdf));
    }
    report_errors();
}

static void test02(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_set_suppress_warnings(qpdf, QPDF_TRUE);
    if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) &&
	((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0))
    {
	qpdf_set_static_ID(qpdf, QPDF_TRUE);
	qpdf_write(qpdf);
    }
    report_errors();
}

static void test03(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_content_normalization(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test04(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_set_ignore_xref_streams(qpdf, QPDF_TRUE);
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test05(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    int count = 0;
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_register_progress_reporter(qpdf, count_progress, (void*)&count);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_linearization(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    /* make sure progress reporter was called */
    assert(count > 0);
    report_errors();
}

static void test06(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    char* buf = NULL;
    unsigned long size = 0;
    read_file_into_memory(infile, &buf, &size);
    qpdf_read_memory(qpdf, infile, buf, size, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_object_stream_mode(qpdf, qpdf_o_generate);
    qpdf_write(qpdf);
    report_errors();
    free(buf);
}

static void test07(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_qdf_mode(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test08(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_qdf_mode(qpdf, QPDF_TRUE);
    qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test09(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_stream_data_mode(qpdf, qpdf_s_uncompress);
    qpdf_write(qpdf);
    report_errors();
}

static void test10(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_set_attempt_recovery(qpdf, QPDF_FALSE);
    qpdf_read(qpdf, infile, password);
    report_errors();
}

static void test11(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_r2_encryption_parameters(
	qpdf, "user1", "owner1", QPDF_FALSE, QPDF_TRUE, QPDF_TRUE, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test12(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_r3_encryption_parameters2(
	qpdf, "user2", "owner2", QPDF_TRUE, QPDF_TRUE,
        QPDF_TRUE, QPDF_TRUE, QPDF_TRUE, QPDF_TRUE,
	qpdf_r3p_low);
    qpdf_write(qpdf);
    report_errors();
}

static void test13(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    printf("user password: %s\n", qpdf_get_user_password(qpdf));
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_preserve_encryption(qpdf, QPDF_FALSE);
    qpdf_write(qpdf);
    report_errors();
}

static void test14(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_minimum_pdf_version_and_extension(qpdf, "1.7", 8);
    qpdf_write(qpdf);
    qpdf_init_write(qpdf, xarg);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_force_pdf_version(qpdf, "1.4");
    qpdf_write(qpdf);
    report_errors();
}

static void test15(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_r4_encryption_parameters2(
	qpdf, "user2", "owner2", QPDF_TRUE, QPDF_TRUE,
        QPDF_TRUE, QPDF_TRUE, QPDF_TRUE, QPDF_TRUE,
	qpdf_r3p_low, QPDF_TRUE, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void print_info(char const* key)
{
    char const* value = qpdf_get_info_key(qpdf, key);
    printf("Info key %s: %s\n",
	   key, (value ? value : "(null)"));
}

static void test16(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    unsigned long buflen = 0L;
    unsigned char const* buf = 0;
    FILE* f = 0;

    qpdf_read(qpdf, infile, password);
    print_info("/Author");
    print_info("/Producer");
    print_info("/Creator");
    qpdf_set_info_key(qpdf, "/Author", "Mr. Potato Head");
    qpdf_set_info_key(qpdf, "/Producer", "QPDF library");
    qpdf_set_info_key(qpdf, "/Creator", 0);
    print_info("/Author");
    print_info("/Producer");
    print_info("/Creator");
    qpdf_init_write_memory(qpdf);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_stream_data_mode(qpdf, qpdf_s_uncompress);
    qpdf_write(qpdf);
    f = safe_fopen(outfile, "wb");
    buflen = (unsigned long)(qpdf_get_buffer_length(qpdf));
    buf = qpdf_get_buffer(qpdf);
    fwrite(buf, 1, buflen, f);
    fclose(f);
    report_errors();
}

static void test17(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_r5_encryption_parameters2(
	qpdf, "user3", "owner3", QPDF_TRUE, QPDF_TRUE,
        QPDF_TRUE, QPDF_TRUE, QPDF_TRUE, QPDF_TRUE,
	qpdf_r3p_low, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test18(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_r6_encryption_parameters2(
	qpdf, "user4", "owner4", QPDF_TRUE, QPDF_TRUE,
        QPDF_TRUE, QPDF_TRUE, QPDF_TRUE, QPDF_TRUE,
	qpdf_r3p_low, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test19(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_deterministic_ID(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test20(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_compress_streams(qpdf, QPDF_FALSE);
    qpdf_set_decode_level(qpdf, qpdf_dl_specialized);
    qpdf_write(qpdf);
    report_errors();
}

static void test21(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_preserve_unreferenced_objects(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test22(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    qpdf_read(qpdf, infile, password);
    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
    qpdf_set_compress_streams(qpdf, QPDF_FALSE);
    qpdf_set_newline_before_endstream(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test23(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    QPDF_ERROR_CODE status = 0;
    qpdf_read(qpdf, infile, password);
    status = qpdf_check_pdf(qpdf);
    printf("status: %d\n", status);
    report_errors();
}

static void test24(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test case is designed for minimal.pdf. Pull objects out of
     * minimal.pdf to make sure all our accessors work as expected.
     */

    qpdf_read(qpdf, infile, password);
    qpdf_oh trailer = qpdf_get_trailer(qpdf);
    /* The library never returns 0 */
    assert(trailer == 1);

    /* Get root two different ways */
    qpdf_oh root = qpdf_get_root(qpdf);
    assert(qpdf_oh_get_generation(qpdf, root) == 0);
    qpdf_oh root_from_trailer = qpdf_oh_get_key(qpdf, trailer, "/Root");
    assert(qpdf_oh_get_object_id(qpdf, root) ==
           qpdf_oh_get_object_id(qpdf, root_from_trailer));

    /* Go to the first page and look at all the keys */
    qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages");
    assert(qpdf_oh_is_dictionary(qpdf, pages));
    assert(qpdf_oh_is_initialized(qpdf, pages));
    qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids");
    assert(qpdf_oh_is_array(qpdf, kids));
    assert(qpdf_oh_get_array_n_items(qpdf, kids) == 1);
    qpdf_oh page1 = qpdf_oh_get_array_item(qpdf, kids, 0);
    qpdf_oh_begin_dict_key_iter(qpdf, page1);
    while (qpdf_oh_dict_more_keys(qpdf))
    {
        printf("page dictionary key: %s\n", qpdf_oh_dict_next_key(qpdf));
    }

    /* Inspect the first page */
    qpdf_oh type = qpdf_oh_get_key(qpdf, page1, "/Type");
    assert(qpdf_oh_is_name(qpdf, type));
    assert(strcmp(qpdf_oh_get_name(qpdf, type), "/Page") == 0);
    qpdf_oh parent = qpdf_oh_get_key(qpdf, page1, "/Parent");
    assert(qpdf_oh_is_indirect(qpdf, parent));
    qpdf_oh mediabox = qpdf_oh_get_key(qpdf, page1, "/MediaBox");
    assert(! qpdf_oh_is_scalar(qpdf, mediabox));
    assert(qpdf_oh_is_array(qpdf, mediabox));
    assert(qpdf_oh_get_array_n_items(qpdf, mediabox) == 4);
    for (int i = 0; i < 4; ++i)
    {
        qpdf_oh item = qpdf_oh_get_array_item(qpdf, mediabox, i);
        printf("item %d: %d %.2f\n",
               i, qpdf_oh_get_int_value_as_int(qpdf, item),
               qpdf_oh_get_numeric_value(qpdf, item));
    }

    /* Exercise different ways of looking at integers */
    qpdf_oh i2 = qpdf_oh_get_array_item(qpdf, mediabox, 2);
    assert(qpdf_oh_get_int_value_as_int(qpdf, i2) == 612);
    assert(qpdf_oh_get_int_value(qpdf, i2) == 612ll);
    assert(qpdf_oh_get_uint_value_as_uint(qpdf, i2) == 612u);
    assert(qpdf_oh_get_uint_value(qpdf, i2) == 612ull);
    /* Exercise accessors of other object types */
    assert(! qpdf_oh_is_operator(qpdf, i2));
    assert(! qpdf_oh_is_inline_image(qpdf, i2));
    /* Chain calls. */
    qpdf_oh encoding = qpdf_oh_get_key(
        qpdf, qpdf_oh_get_key(
            qpdf, qpdf_oh_get_key(
                qpdf, qpdf_oh_get_key(
                    qpdf, page1, "/Resources"),
                "/Font"),
            "/F1"),
        "/Encoding");
    assert(strcmp(qpdf_oh_get_name(qpdf, encoding), "/WinAnsiEncoding") == 0);

    /* Look at page contents to exercise stream functions */
    qpdf_oh contents = qpdf_oh_get_key(qpdf, page1, "/Contents");
    assert(qpdf_oh_is_stream(qpdf, contents));
    qpdf_oh contents_dict = qpdf_oh_get_dict(qpdf, contents);
    assert(! qpdf_oh_is_scalar(qpdf, contents));
    assert(! qpdf_oh_is_scalar(qpdf, contents_dict));
    qpdf_oh contents_length = qpdf_oh_get_key(qpdf, contents_dict, "/Length");
    assert(qpdf_oh_is_integer(qpdf, contents_length));
    assert(qpdf_oh_is_scalar(qpdf, contents_length));
    assert(qpdf_oh_is_number(qpdf, contents_length));
    qpdf_oh contents_array = qpdf_oh_wrap_in_array(qpdf, contents);
    assert(qpdf_oh_get_array_n_items(qpdf, contents_array) == 1);
    assert(qpdf_oh_get_object_id(
               qpdf, qpdf_oh_get_array_item(qpdf, contents_array, 0)) ==
           qpdf_oh_get_object_id(qpdf, contents));
    /* Wrap in array for a non-trivial case */
    qpdf_oh wrapped_contents_array =
        qpdf_oh_wrap_in_array(qpdf, contents_array);
    assert(qpdf_oh_get_array_n_items(qpdf, wrapped_contents_array) == 1);
    assert(qpdf_oh_get_object_id(
               qpdf, qpdf_oh_get_array_item(qpdf, wrapped_contents_array, 0)) ==
           qpdf_oh_get_object_id(qpdf, contents));

    /* Exercise functions that work with indirect objects */
    qpdf_oh resources = qpdf_oh_get_key(qpdf, page1, "/Resources");
    qpdf_oh procset = qpdf_oh_get_key(qpdf, resources, "/ProcSet");
    assert(strcmp(qpdf_oh_unparse(qpdf, procset),
           "5 0 R") == 0);
    assert(strcmp(qpdf_oh_unparse_resolved(qpdf, procset),
           "[ /PDF /Text ]") == 0);
    qpdf_oh_make_direct(qpdf, procset);
    assert(strcmp(qpdf_oh_unparse(qpdf, procset),
           "[ /PDF /Text ]") == 0);
    /* The replaced /ProcSet can be seen to be a direct object in the
     * expected output PDF.
     */
    qpdf_oh_replace_key(qpdf, resources, "/ProcSet", procset);

    /* Release and access to exercise handling of object handle errors
     * and to show that write still works after releasing. This test
     * uses the default oh error handler, so messages get written to
     * stderr. The warning about using the default error handler only
     * appears once.
     */
    qpdf_oh_release(qpdf, page1);
    contents = qpdf_oh_get_key(qpdf, page1, "/Contents");
    assert(qpdf_oh_is_null(qpdf, contents));
    assert(qpdf_oh_is_array(qpdf, mediabox));
    qpdf_oh_release_all(qpdf);
    assert(! qpdf_oh_is_null(qpdf, mediabox));
    assert(! qpdf_oh_is_array(qpdf, mediabox));
    /* Make sure something is assigned when we exit so we check that
     * it gets properly freed.
     */
    qpdf_get_root(qpdf);

    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_qdf_mode(qpdf, QPDF_TRUE);
    qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test25(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test case is designed for minimal.pdf. */
    qpdf_read(qpdf, infile, password);
    qpdf_oh root = qpdf_get_root(qpdf);

    /* Parse objects from a string */
    qpdf_oh parsed = qpdf_oh_parse(
        qpdf, "[ 1 2.0 (3\xf7) << /Four [/Five] >> null true ]");
    qpdf_oh p_int = qpdf_oh_get_array_item(qpdf, parsed, 0);
    qpdf_oh p_real = qpdf_oh_get_array_item(qpdf, parsed, 1);
    qpdf_oh p_string = qpdf_oh_get_array_item(qpdf, parsed, 2);
    qpdf_oh p_dict = qpdf_oh_get_array_item(qpdf, parsed, 3);
    qpdf_oh p_null = qpdf_oh_get_array_item(qpdf, parsed, 4);
    qpdf_oh p_bool = qpdf_oh_get_array_item(qpdf, parsed, 5);
    assert(qpdf_oh_is_integer(qpdf, p_int) &&
           qpdf_oh_get_int_value_as_int(qpdf, p_int) == 1);
    assert(qpdf_oh_is_real(qpdf, p_real) &&
           (strcmp(qpdf_oh_get_real_value(qpdf, p_real), "2.0") == 0) &&
           qpdf_oh_get_numeric_value(qpdf, p_real) == 2.0);
    assert(qpdf_oh_is_string(qpdf, p_string) &&
           (strcmp(qpdf_oh_get_string_value(qpdf, p_string), "3\xf7") == 0) &&
           (strcmp(qpdf_oh_get_utf8_value(qpdf, p_string), "3\xc3\xb7") == 0) &&
           (strcmp(qpdf_oh_unparse_binary(qpdf, p_string), "<33f7>") == 0));
    assert(qpdf_oh_is_dictionary(qpdf, p_dict));
    qpdf_oh p_five = qpdf_oh_get_key(qpdf, p_dict, "/Four");
    assert(qpdf_oh_is_or_has_name(qpdf, p_five, "/Five"));
    assert(qpdf_oh_is_or_has_name(
               qpdf, qpdf_oh_get_array_item(qpdf, p_five, 0), "/Five"));
    assert(qpdf_oh_is_null(qpdf, p_null));
    assert(qpdf_oh_is_bool(qpdf, p_bool) &&
           (qpdf_oh_get_bool_value(qpdf, p_bool) == QPDF_TRUE));
    qpdf_oh_erase_item(qpdf, parsed, 4);
    qpdf_oh_insert_item(
        qpdf, parsed, 2,
        qpdf_oh_parse(qpdf, "<</A 1 /B 2 /C 3 /D 4>>"));
    qpdf_oh new_dict = qpdf_oh_get_array_item(qpdf, parsed, 2);
    assert(qpdf_oh_has_key(qpdf, new_dict, "/A"));
    assert(qpdf_oh_has_key(qpdf, new_dict, "/D"));
    qpdf_oh new_array = qpdf_oh_new_array(qpdf);
    qpdf_oh_replace_or_remove_key(
        qpdf, new_dict, "/A", qpdf_oh_new_null(qpdf));
    qpdf_oh_replace_or_remove_key(
        qpdf, new_dict, "/B", new_array);
    qpdf_oh_replace_key(
        qpdf, new_dict, "/C", qpdf_oh_new_dictionary(qpdf));
    qpdf_oh_remove_key(qpdf, new_dict, "/D");
    assert(! qpdf_oh_has_key(qpdf, new_dict, "/A"));
    assert(! qpdf_oh_has_key(qpdf, new_dict, "/D"));
    qpdf_oh_append_item(
        qpdf, new_array, qpdf_oh_new_string(qpdf, "potato"));
    qpdf_oh_append_item(
        qpdf, new_array,
        qpdf_oh_new_unicode_string(qpdf, "qww\xc3\xb7\xcf\x80"));
    qpdf_oh_append_item(qpdf, new_array, qpdf_oh_new_null(qpdf)); /* 2 */
    qpdf_oh_append_item(qpdf, new_array, qpdf_oh_new_null(qpdf)); /* 3 */
    qpdf_oh_set_array_item(
        qpdf, new_array, 2,
        qpdf_oh_new_name(qpdf, "/Quack"));
    qpdf_oh_append_item(
        qpdf, new_array,
        qpdf_oh_new_real_from_double(qpdf, 4.123, 2));
    qpdf_oh_append_item(
        qpdf, new_array,
        qpdf_oh_new_real_from_string(qpdf, "5.0"));
    qpdf_oh_append_item(
        qpdf, new_array,
        qpdf_oh_new_integer(qpdf, 6));
    qpdf_oh_append_item(
        qpdf, new_array, qpdf_oh_new_bool(qpdf, QPDF_TRUE));
    qpdf_oh_replace_key(qpdf, root, "/QTest", new_dict);

    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_qdf_mode(qpdf, QPDF_TRUE);
    qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}
static void test26(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* Make sure we detect uninitialized objects */
    qpdf_data qpdf2 = qpdf_init();
    qpdf_oh trailer = qpdf_get_trailer(qpdf2);
    assert(! qpdf_oh_is_initialized(qpdf2, trailer));
    qpdf_cleanup(&qpdf2);
}

static void test27(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* Exercise a string with a null. Since the regular methods return
     * char*, we can't see past the null character without looking
     * explicitly at the length.
     */
    qpdf_oh p_string_with_null = qpdf_oh_parse(qpdf, "<6f6e650074776f>");
    assert(qpdf_oh_is_string(qpdf, p_string_with_null));
    assert(strcmp(qpdf_oh_get_string_value(qpdf, p_string_with_null),
                  "one") == 0);
    assert(qpdf_get_last_string_length(qpdf) == 7);
    assert(memcmp(qpdf_oh_get_string_value(qpdf, p_string_with_null),
                  "one\000two", 7) == 0);
}

static void test28(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test case is designed for minimal.pdf. */

    /* Look at the media box. The media box is in array. Trivially
     * wrap it and also clone it and make sure we get different
     * handles with the same contents.
     */
    qpdf_read(qpdf, infile, password);
    qpdf_oh root = qpdf_get_root(qpdf);
    qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages");
    qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids");
    qpdf_oh page1 = qpdf_oh_get_array_item(qpdf, kids, 0);
    qpdf_oh mediabox = qpdf_oh_get_key(qpdf, page1, "/MediaBox");
    qpdf_oh wrapped_mediabox = qpdf_oh_wrap_in_array(qpdf, mediabox);
    qpdf_oh cloned_mediabox = qpdf_oh_new_object(qpdf, mediabox);
    assert(wrapped_mediabox != mediabox);
    assert(cloned_mediabox != mediabox);
    assert(qpdf_oh_get_array_n_items(qpdf, wrapped_mediabox) == 4);
    for (int i = 0; i < 4; ++i)
    {
        qpdf_oh item = qpdf_oh_get_array_item(qpdf, mediabox, i);
        qpdf_oh item2 = qpdf_oh_get_array_item(qpdf, wrapped_mediabox, i);
        qpdf_oh item3 = qpdf_oh_get_array_item(qpdf, cloned_mediabox, i);
        assert(qpdf_oh_get_int_value_as_int(qpdf, item) ==
               (i == 0 ? 0 :
                i == 1 ? 0 :
                i == 2 ? 612 :
                i == 3 ? 792 :
                -1));
        assert(qpdf_oh_get_int_value_as_int(qpdf, item) ==
               qpdf_oh_get_int_value_as_int(qpdf, item2));
        assert(qpdf_oh_get_int_value_as_int(qpdf, item) ==
               qpdf_oh_get_int_value_as_int(qpdf, item3));
        qpdf_oh_release(qpdf, item);
    }
}

static void test29(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* Trap exceptions thrown by object accessors. Type mismatches are
     * errors rather than warnings when they don't have an owning QPDF
     * object.
     */
    char const* label = "oh error";
    qpdf_register_oh_error_handler(qpdf, handle_oh_error, (void*)&label);

    /* get_root fails when we have no trailer */
    label = "get root";
    qpdf_oh root = qpdf_get_root(qpdf);
    assert(root != 0);
    assert(! qpdf_oh_is_initialized(qpdf, root));

    label = "bad parse";
    assert(! qpdf_oh_is_initialized(qpdf, qpdf_oh_parse(qpdf, "[oops")));
    report_errors();

    label = "type mismatch";
    assert(qpdf_oh_get_int_value_as_int(
               qpdf, qpdf_oh_new_string(qpdf, "x")) == 0);
    qpdf_oh int_oh = qpdf_oh_new_integer(qpdf, 12);
    assert(strlen(qpdf_oh_get_string_value(qpdf, int_oh)) == 0);

    // This doesn't test every possible error flow, but it tests each
    // way of handling errors in the library code.
    label = "array type mismatch";
    assert(qpdf_oh_get_array_n_items(qpdf, int_oh) == 0);
    assert(qpdf_oh_is_null(qpdf, qpdf_oh_get_array_item(qpdf, int_oh, 3)));
    label = "append to non-array";
    qpdf_oh_append_item(qpdf, int_oh, qpdf_oh_new_null(qpdf));
    qpdf_oh array = qpdf_oh_new_array(qpdf);
    label = "array bounds";
    assert(qpdf_oh_is_null(qpdf, qpdf_oh_get_array_item(qpdf, array, 3)));

    label = "dictionary iter type mismatch";
    qpdf_oh_begin_dict_key_iter(qpdf, int_oh);
    assert(qpdf_oh_dict_more_keys(qpdf) == QPDF_FALSE);
    label = "dictionary type mismatch";
    assert(qpdf_oh_is_null(qpdf, qpdf_oh_get_key(qpdf, int_oh, "potato")));
    assert(qpdf_oh_has_key(qpdf, int_oh, "potato") == QPDF_FALSE);

    report_errors();
}

static void test30(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    assert(qpdf_read(qpdf, infile, password) & QPDF_ERRORS);
    /* Fail to handle error */
}

static void test31(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* Make sure type warnings have a specific error code. This test
     * case is designed for minimal.pdf.
     */
    qpdf_read(qpdf, infile, password);
    qpdf_oh trailer = qpdf_get_trailer(qpdf);
    assert(qpdf_oh_get_int_value(qpdf, trailer) == 0LL);
    assert(! qpdf_has_error(qpdf));
    assert(qpdf_more_warnings(qpdf));
    qpdf_error e = qpdf_next_warning(qpdf);
    assert(qpdf_get_error_code(qpdf, e) == qpdf_e_object);
    report_errors();
}

static void test32(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test case is designed for minimal.pdf. */
    assert(qpdf_read(qpdf, infile, password) == 0);
    qpdf_oh page = qpdf_get_object_by_id(qpdf, 3, 0);
    assert(qpdf_oh_is_dictionary(qpdf, page));
    assert(qpdf_oh_has_key(qpdf, page, "/MediaBox"));
    report_errors();
}

static void test33(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test case is designed for minimal.pdf. */

    /* Convert a direct object to an indirect object and replace it. */
    assert(qpdf_read(qpdf, infile, password) == 0);
    qpdf_oh root = qpdf_get_root(qpdf);
    qpdf_oh pages = qpdf_oh_get_key(qpdf, root, "/Pages");
    qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids");
    qpdf_oh page1 = qpdf_oh_get_array_item(qpdf, kids, 0);
    qpdf_oh mediabox = qpdf_oh_get_key(qpdf, page1, "/MediaBox");
    assert(! qpdf_oh_is_indirect(qpdf, mediabox));
    qpdf_oh i_mediabox = qpdf_make_indirect_object(qpdf, mediabox);
    assert(qpdf_oh_is_indirect(qpdf, i_mediabox));
    qpdf_oh_replace_key(qpdf, page1, "/MediaBox", i_mediabox);

    /* Replace a different indirect object */
    qpdf_oh resources = qpdf_oh_get_key(qpdf, page1, "/Resources");
    qpdf_oh procset = qpdf_oh_get_key(qpdf, resources, "/ProcSet");
    assert(qpdf_oh_is_indirect(qpdf, procset));
    qpdf_replace_object(
        qpdf,
        qpdf_oh_get_object_id(qpdf, procset),
        qpdf_oh_get_generation(qpdf, procset),
        qpdf_oh_parse(qpdf, "[/PDF]"));

    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_qdf_mode(qpdf, QPDF_TRUE);
    qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
}

static void test34(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test expects 11-pages.pdf as file1 and minimal.pdf as xarg. */

    /* Non-error cases for page API */

    qpdf_data qpdf2 = qpdf_init();
    assert(qpdf_read(qpdf, infile, password) == 0);
    assert(qpdf_read(qpdf2, xarg, "") == 0);
    assert(qpdf_get_num_pages(qpdf) == 11);
    assert(qpdf_get_num_pages(qpdf2) == 1);

    /* At this time, there is no C API for accessing stream data, so
     * we hard-code object IDs from a known input file.
     */
    assert(qpdf_oh_get_object_id(qpdf, qpdf_get_page_n(qpdf, 0)) == 4);
    assert(qpdf_oh_get_object_id(qpdf, qpdf_get_page_n(qpdf, 10)) == 14);
    qpdf_oh page3 = qpdf_get_page_n(qpdf, 3);
    assert(qpdf_find_page_by_oh(qpdf, page3) == 3);
    assert(qpdf_find_page_by_id(
               qpdf, qpdf_oh_get_object_id(qpdf, page3), 0) == 3);

    /* Add other page to the end */
    qpdf_oh opage0 = qpdf_get_page_n(qpdf2, 0);
    assert(qpdf_add_page(qpdf, qpdf2, opage0, QPDF_FALSE) == 0);
    /* Add other page before page 3 */
    assert(qpdf_add_page_at(qpdf, qpdf2, opage0, QPDF_TRUE, page3) == 0);
    /* Remove page 3 */
    assert(qpdf_remove_page(qpdf, page3) == 0);
    /* At page 3 back at the beginning */
    assert(qpdf_add_page(qpdf, qpdf, page3, QPDF_TRUE) == 0);

    qpdf_init_write(qpdf, outfile);
    qpdf_set_static_ID(qpdf, QPDF_TRUE);
    qpdf_set_qdf_mode(qpdf, QPDF_TRUE);
    qpdf_set_suppress_original_object_IDs(qpdf, QPDF_TRUE);
    qpdf_write(qpdf);
    report_errors();
    qpdf_cleanup(&qpdf2);
}

static void test35(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test uses 11-pages.pdf */

    assert(qpdf_get_num_pages(qpdf) == -1);
    assert(qpdf_has_error(qpdf));
    qpdf_error e = qpdf_get_error(qpdf);
    assert(qpdf_get_error_code(qpdf, e) != QPDF_SUCCESS);
    assert(! qpdf_has_error(qpdf));

    assert(qpdf_read(qpdf, infile, password) == 0);

    qpdf_oh range = qpdf_get_page_n(qpdf, 11);
    assert(! qpdf_oh_is_initialized(qpdf, range));
    assert(qpdf_has_error(qpdf));
    e = qpdf_get_error(qpdf);
    assert(qpdf_get_error_code(qpdf, e) != QPDF_SUCCESS);
    assert(! qpdf_has_error(qpdf));

    assert(qpdf_find_page_by_id(qpdf, 100, 0) == -1);
    assert(qpdf_has_error(qpdf));
    e = qpdf_get_error(qpdf);
    assert(qpdf_get_error_code(qpdf, e) != QPDF_SUCCESS);
    assert(! qpdf_has_error(qpdf));

    assert(qpdf_find_page_by_oh(qpdf, qpdf_get_root(qpdf)) == -1);
    assert(qpdf_more_warnings(qpdf));
    e = qpdf_next_warning(qpdf);
    assert(qpdf_get_error_code(qpdf, e) != QPDF_SUCCESS);
    assert(! qpdf_has_error(qpdf));

    assert(qpdf_find_page_by_id(qpdf, 100, 0) == -1);
    assert(qpdf_has_error(qpdf));
    e = qpdf_get_error(qpdf);
    assert(qpdf_get_error_code(qpdf, e) != QPDF_SUCCESS);
    assert(! qpdf_has_error(qpdf));

    assert(qpdf_add_page(qpdf, qpdf, 1000, QPDF_FALSE) != 0);

    report_errors();
}

static void test36(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test uses inherited-rotate.pdf */

    assert(qpdf_read(qpdf, infile, password) == 0);

    /* Non-trivially push inherited attributes */
    qpdf_oh page0 = qpdf_get_object_by_id(qpdf, 3, 0);
    assert(qpdf_oh_is_dictionary(qpdf, page0));
    qpdf_oh r = qpdf_oh_get_key(qpdf, page0, "/Rotate");
    assert(qpdf_oh_get_int_value(qpdf, r) == 90);
    qpdf_oh_remove_key(qpdf, page0, "/Rotate");
    assert(! qpdf_oh_has_key(qpdf, page0, "/Rotate"));

    assert(qpdf_push_inherited_attributes_to_page(qpdf) == 0);
    r = qpdf_oh_get_key(qpdf, page0, "/Rotate");
    assert(qpdf_oh_get_int_value(qpdf, r) == 270);

    assert(qpdf_add_page(qpdf, qpdf, page0, QPDF_TRUE) == 0);
}

static void test37(char const* infile,
		   char const* password,
		   char const* outfile,
		   char const* xarg)
{
    /* This test uses 11-pages.pdf */

    /* Manually manipulate pages tree */
    assert(qpdf_read(qpdf, infile, password) == 0);
    assert(qpdf_get_num_pages(qpdf) == 11);
    qpdf_oh pages = qpdf_get_object_by_id(qpdf, 3, 0);
    qpdf_oh kids = qpdf_oh_get_key(qpdf, pages, "/Kids");
    assert(qpdf_oh_get_array_n_items(qpdf, kids) == 11);
    qpdf_oh_erase_item(qpdf, kids, 0);
    assert(qpdf_get_num_pages(qpdf) == 11);
    assert(qpdf_update_all_pages_cache(qpdf) == 0);
    assert(qpdf_get_num_pages(qpdf) == 10);
}

int main(int argc, char* argv[])
{
    char* p = 0;
    int n = 0;
    char const* infile = 0;
    char const* password = 0;
    char const* outfile = 0;
    char const* xarg = 0;
    void (*fn)(char const*, char const*, char const*, char const*) = 0;

    if ((p = strrchr(argv[0], '/')) != NULL)
    {
	whoami = p + 1;
    }
    else if ((p = strrchr(argv[0], '\\')) != NULL)
    {
	whoami = p + 1;
    }
    else
    {
	whoami = argv[0];
    }
    if ((argc == 2) && (strcmp(argv[1], "--version") == 0))
    {
	printf("qpdf-ctest version %s\n", qpdf_get_qpdf_version());
	return 0;
    }

    if (argc < 5)
    {
	fprintf(stderr, "usage: %s n infile password outfile\n", whoami);
	exit(2);
    }

    n = atoi(argv[1]);
    infile = argv[2];
    password = argv[3];
    outfile = argv[4];
    xarg = (argc > 5 ? argv[5] : 0);

    fn = ((n == 1) ? test01 :
	  (n == 2) ? test02 :
	  (n == 3) ? test03 :
	  (n == 4) ? test04 :
	  (n == 5) ? test05 :
	  (n == 6) ? test06 :
	  (n == 7) ? test07 :
	  (n == 8) ? test08 :
	  (n == 9) ? test09 :
	  (n == 10) ? test10 :
	  (n == 11) ? test11 :
	  (n == 12) ? test12 :
	  (n == 13) ? test13 :
	  (n == 14) ? test14 :
	  (n == 15) ? test15 :
	  (n == 16) ? test16 :
	  (n == 17) ? test17 :
	  (n == 18) ? test18 :
	  (n == 19) ? test19 :
	  (n == 20) ? test20 :
	  (n == 21) ? test21 :
	  (n == 22) ? test22 :
	  (n == 23) ? test23 :
	  (n == 24) ? test24 :
	  (n == 25) ? test25 :
	  (n == 26) ? test26 :
	  (n == 27) ? test27 :
	  (n == 28) ? test28 :
	  (n == 29) ? test29 :
	  (n == 30) ? test30 :
	  (n == 31) ? test31 :
	  (n == 32) ? test32 :
	  (n == 33) ? test33 :
	  (n == 34) ? test34 :
	  (n == 35) ? test35 :
	  (n == 36) ? test36 :
	  (n == 37) ? test37 :
	  0);

    if (fn == 0)
    {
	fprintf(stderr, "%s: invalid test number %d\n", whoami, n);
	exit(2);
    }

    qpdf = qpdf_init();
    fn(infile, password, outfile, xarg);
    qpdf_cleanup(&qpdf);
    assert(qpdf == 0);
    printf("C test %d done\n", n);

    return 0;
}