forked from yifanlu/KindleTool
/
create.c
2808 lines (2680 loc) · 116 KB
/
create.c
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
/*
** KindleTool, create.c
**
** Copyright (C) 2011-2012 Yifan Lu
** Copyright (C) 2012-2019 NiLuJe
** Concept based on an original Python implementation by Igor Skochinsky & Jean-Yves Avenard,
** cf., http://www.mobileread.com/forums/showthread.php?t=63225
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "create.h"
static const char*
convert_bundle_version(BundleVersion bundlev)
{
switch (bundlev) {
case UpdateSignature:
return "Signature";
case OTAUpdateV2:
return "OTA V2";
case OTAUpdate:
return "OTA V1";
case RecoveryUpdate:
return "Recovery";
case RecoveryUpdateV2:
return "Recovery V2";
case UnknownUpdate:
default:
return "Unknown";
}
}
static struct rsa_private_key
get_default_key(void)
{
// Make nettle happy... (Array created from the bin2h (grub2 has one) output of pkcs1-conv on our pem file)
static const uint8_t sign_key_sexp[] = {
0x28, 0x31, 0x31, 0x3a, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x2d, 0x6b, 0x65, 0x79, 0x28, 0x39,
0x3a, 0x72, 0x73, 0x61, 0x2d, 0x70, 0x6b, 0x63, 0x73, 0x31, 0x28, 0x31, 0x3a, 0x6e, 0x31, 0x32, 0x39,
0x3a, 0x00, 0xc9, 0x9f, 0x58, 0xd6, 0x53, 0xec, 0x71, 0x56, 0xff, 0xde, 0x44, 0xa7, 0xc2, 0x3d, 0x1f,
0x5e, 0xe3, 0xb9, 0x4f, 0x58, 0xdd, 0xab, 0x1f, 0x7d, 0xf3, 0xf5, 0x06, 0xdf, 0x9e, 0xa9, 0x82, 0xc4,
0x14, 0x4b, 0x3f, 0xa9, 0x8c, 0x8c, 0x6c, 0xba, 0x00, 0xfc, 0xb2, 0x71, 0x05, 0xe0, 0xde, 0x73, 0xe2,
0xe5, 0xf7, 0x1b, 0xef, 0x96, 0xa5, 0x66, 0x8f, 0x8e, 0x87, 0x4d, 0x76, 0x1e, 0x93, 0x1e, 0xf4, 0xb9,
0xe9, 0x78, 0x48, 0x25, 0xa0, 0x87, 0x66, 0xd4, 0x4e, 0x0b, 0x3a, 0xcc, 0xab, 0xcf, 0x89, 0x2d, 0xb5,
0x0b, 0x46, 0x46, 0x5c, 0xc2, 0x12, 0xb9, 0x81, 0x1a, 0xde, 0xbe, 0x70, 0x05, 0x44, 0x57, 0xce, 0xb2,
0xda, 0x98, 0x4e, 0x27, 0x79, 0x8b, 0x93, 0x41, 0x24, 0xf5, 0x44, 0x17, 0x6c, 0x85, 0x1f, 0xae, 0xfc,
0x89, 0x9d, 0x2d, 0x8c, 0x28, 0xb1, 0xb6, 0x71, 0xcc, 0xe3, 0x95, 0x29, 0x28, 0x31, 0x3a, 0x65, 0x33,
0x3a, 0x01, 0x00, 0x01, 0x29, 0x28, 0x31, 0x3a, 0x64, 0x31, 0x32, 0x38, 0x3a, 0x48, 0xbc, 0xa6, 0xd4,
0xf3, 0x83, 0xda, 0x43, 0xb3, 0x9d, 0x21, 0x11, 0x90, 0x5e, 0x72, 0xa1, 0xcd, 0xef, 0xbd, 0x73, 0x66,
0xcc, 0xe4, 0x58, 0x91, 0x19, 0x35, 0x78, 0x99, 0x09, 0xb8, 0x36, 0x3a, 0xc8, 0x06, 0xd8, 0x88, 0xee,
0xe4, 0x0e, 0x9a, 0x6a, 0x8f, 0x89, 0x7c, 0xc0, 0x6a, 0x20, 0x4e, 0x9b, 0xfd, 0xf0, 0xe3, 0x17, 0x6a,
0xe6, 0x3c, 0x26, 0x04, 0x23, 0xea, 0xd8, 0x0e, 0xe4, 0xb9, 0x18, 0xda, 0xea, 0x6d, 0xb6, 0xe9, 0x03,
0xaf, 0xcb, 0xa1, 0x13, 0x6c, 0xfd, 0x0e, 0x1e, 0xc7, 0x31, 0x95, 0x7f, 0xac, 0x36, 0x1a, 0xfb, 0xda,
0xf2, 0x6c, 0x9b, 0xac, 0x46, 0x20, 0x10, 0x0e, 0x61, 0x7e, 0x54, 0x2c, 0xd8, 0xd8, 0x78, 0xab, 0x8e,
0x9b, 0x12, 0xce, 0x04, 0x6e, 0xd2, 0xbf, 0x36, 0x34, 0x2f, 0x33, 0x9c, 0xd9, 0xb6, 0x78, 0x63, 0x91,
0xca, 0xcf, 0x41, 0xbe, 0x61, 0x29, 0x28, 0x31, 0x3a, 0x70, 0x36, 0x35, 0x3a, 0x00, 0xe8, 0x22, 0x89,
0x0e, 0xaf, 0x47, 0xd8, 0xcf, 0x75, 0x13, 0x49, 0xb1, 0xdf, 0x0f, 0x77, 0xa7, 0x81, 0x71, 0x4f, 0x67,
0xe2, 0x5a, 0x26, 0xa5, 0x3c, 0xc5, 0xac, 0x91, 0xec, 0x2f, 0x86, 0xa7, 0x92, 0x34, 0x0a, 0x04, 0xa7,
0x08, 0x34, 0xd0, 0x56, 0x07, 0x64, 0x54, 0x66, 0xcf, 0xb8, 0xb5, 0x58, 0x89, 0x60, 0xc8, 0x70, 0x46,
0xb1, 0x8e, 0xf5, 0x6b, 0x85, 0x76, 0x2d, 0xd8, 0x07, 0x3d, 0x29, 0x28, 0x31, 0x3a, 0x71, 0x36, 0x35,
0x3a, 0x00, 0xde, 0x59, 0xc4, 0x46, 0x08, 0x34, 0x46, 0x65, 0x81, 0x0b, 0x72, 0xbc, 0xb6, 0x80, 0xb2,
0x7c, 0x3b, 0xeb, 0xf1, 0xe5, 0xda, 0xa3, 0xec, 0x60, 0x50, 0x9d, 0xe5, 0x35, 0x66, 0xea, 0x4b, 0x41,
0xed, 0xc3, 0x17, 0x33, 0xc2, 0x72, 0x04, 0x1f, 0x8f, 0x48, 0x20, 0x3a, 0x23, 0x6d, 0x39, 0xcb, 0x52,
0xbd, 0xce, 0x8a, 0xd1, 0x4c, 0x66, 0xe6, 0x89, 0xb9, 0x3d, 0x8c, 0xb5, 0x6c, 0xd3, 0x39, 0x29, 0x28,
0x31, 0x3a, 0x61, 0x36, 0x35, 0x3a, 0x00, 0xae, 0x86, 0x08, 0x75, 0x39, 0xe2, 0xd2, 0x66, 0x66, 0xa6,
0xf1, 0xa9, 0x01, 0x03, 0x27, 0xfa, 0x8f, 0x9f, 0x19, 0x0c, 0x09, 0x69, 0xad, 0xd4, 0x5d, 0x34, 0x60,
0xe1, 0xf4, 0xa8, 0x66, 0x9c, 0x65, 0x97, 0x2a, 0x51, 0x05, 0x23, 0x6e, 0x51, 0x93, 0xdc, 0x4a, 0xda,
0x09, 0xd1, 0xf2, 0x14, 0xa5, 0x53, 0xe3, 0xa7, 0xce, 0x81, 0xd7, 0xcc, 0x9b, 0x47, 0x13, 0x38, 0x1e,
0x8f, 0x64, 0x21, 0x29, 0x28, 0x31, 0x3a, 0x62, 0x36, 0x35, 0x3a, 0x00, 0xc8, 0xb3, 0x96, 0x6a, 0xf0,
0x74, 0xdf, 0x26, 0x38, 0x39, 0x31, 0x34, 0x0e, 0x38, 0x54, 0xe3, 0xb6, 0xe2, 0xde, 0xd2, 0x6f, 0x6c,
0x8f, 0xac, 0xd0, 0x97, 0xf5, 0x91, 0x22, 0x78, 0x51, 0xbe, 0x0c, 0xf3, 0x90, 0x39, 0xf4, 0x46, 0x1e,
0x5a, 0xae, 0x66, 0x98, 0x50, 0x62, 0x31, 0xf1, 0x7d, 0x0a, 0x0e, 0xb2, 0x24, 0xb3, 0x8f, 0x97, 0x42,
0x79, 0x06, 0x6f, 0xfc, 0x56, 0xb7, 0x08, 0x61, 0x29, 0x28, 0x31, 0x3a, 0x63, 0x36, 0x35, 0x3a, 0x00,
0xdc, 0x57, 0x67, 0xae, 0xc1, 0x62, 0x08, 0xd3, 0x49, 0x86, 0xf8, 0xad, 0xd9, 0xa4, 0xe6, 0xb4, 0xbc,
0xd7, 0xc5, 0x4e, 0x3a, 0x2b, 0xeb, 0x15, 0xe8, 0xd2, 0x18, 0xd6, 0xd1, 0x09, 0x1b, 0xe4, 0x45, 0xcc,
0xb4, 0x70, 0x3b, 0x82, 0x05, 0x0d, 0x8e, 0x1a, 0xfd, 0xda, 0x28, 0x87, 0x56, 0x21, 0xd6, 0x21, 0x45,
0x1a, 0x37, 0x26, 0xa6, 0xac, 0xda, 0xea, 0xd4, 0x6e, 0xb5, 0xac, 0x3c, 0xcc, 0x29, 0x29, 0x29
};
struct rsa_private_key rsa_pkey;
rsa_private_key_init(&rsa_pkey);
if (!rsa_keypair_from_sexp(NULL, &rsa_pkey, 0, sizeof(sign_key_sexp), sign_key_sexp)) {
fprintf(stderr, "Invalid default private key!\n");
// In the unlikely event this ever happens, it'll be caught later on in sign_file ;).
}
return rsa_pkey;
}
static int
sign_file(FILE* in_file, const struct rsa_private_key* rsa_pkey, FILE* sigout_file)
{
unsigned char buffer[BUFFER_SIZE];
size_t len;
struct sha256_ctx hash;
mpz_t sig;
// NOTE: Don't do this at home, kids! We can get away with it because we know we can't use keys > 2K anyway...
unsigned char raw_sig[CERTIFICATE_2K_SIZE];
size_t siglen;
// Like we just said, handle 2K keys at most!
if (rsa_pkey->size > CERTIFICATE_2K_SIZE) {
fprintf(stderr, "RSA key is too large (2K at most)!\n");
return -1;
}
sha256_init(&hash);
while ((len = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, in_file)) > 0) {
sha256_update(&hash, len, buffer);
}
if (ferror(in_file) != 0) {
fprintf(stderr, "Error reading input file: %s.\n", strerror(errno));
return -1;
}
mpz_init(sig);
if (!rsa_sha256_sign(rsa_pkey, &hash, sig)) {
fprintf(stderr, "RSA key is too small!\n");
mpz_clear(sig);
return -1;
}
// NOTE: mpz_out_raw outputs a format that doesn't quite fit our needs (it prepends 4 bytes of size info)...
// Do it ourselves with mpz_export! That's:
// Words of the proper amount of bytes for the host, most significant word & byte first (BE), full words.
mpz_export(raw_sig, &siglen, 1, sizeof(unsigned char*), 1, 0, sig);
mpz_clear(sig);
// Check that the sig looks sane...
if (siglen * sizeof(unsigned char*) != rsa_pkey->size) {
fprintf(stderr, "Signature is too short (or too large?) for our key!\n");
return -1;
}
// And finally, write our sig!
if (fwrite(raw_sig, sizeof(unsigned char), rsa_pkey->size, sigout_file) < rsa_pkey->size) {
fprintf(stderr, "Error writing signature file: %s.\n", strerror(errno));
return -1;
}
return 0;
}
// As usual, largely based on libarchive's doc, examples, and source ;)
static int
metadata_filter(struct archive* a, void* _data __attribute__((unused)), struct archive_entry* entry)
{
struct archive* matching;
int r;
// Don't exclude directories!
if (archive_read_disk_can_descend(a)) {
// It's a directory, don't even try to perform pattern matching, just walk it
archive_read_disk_descend(a);
return 1;
} else {
// Exclude *.sig files in a case insensitive way, to avoid duplicates
matching = archive_match_new();
if (archive_match_exclude_pattern(matching, "./*\\.[Ss][Ii][Gg]$") != ARCHIVE_OK) {
fprintf(stderr, "archive_match_exclude_pattern() failed: %s.\n", archive_error_string(matching));
}
// Exclude *.dat too, to avoid ending up with multiple bundlefiles!
// NOTE: If we wanted to be more lenient, we could exclude "./update*\\.[Dd][Aa][Tt]$" instead
if (archive_match_exclude_pattern(matching, "./*\\.[Dd][Aa][Tt]$") != ARCHIVE_OK) {
fprintf(stderr, "archive_match_exclude_pattern() failed: %s.\n", archive_error_string(matching));
}
// Exclude *nix hidden files, too?
// NOTE: The ARCHIVE_READDISK_MAC_COPYFILE flag for read_disk is disabled by default,
// so we should already be creating 'sane' archives on OS X, without the crazy ._* acl/xattr files ;)
// On the other hand, if the user passed us a self-built tarball, we can't do anything about it.
// OS X users: export COPYFILE_DISABLE=1 is your friend!
/*
if(archive_match_exclude_pattern(matching, "./\\.*$") != ARCHIVE_OK) {
fprintf(stderr, "archive_match_exclude_pattern() failed: %s.\n", archive_error_string(matching));
}
*/
#if defined(_WIN32) && !defined(__CYGWIN__)
// NOTE: Exclude our own tempfiles, since we may create them in PWD, because otherwise,
// depending on what the user uses as input (i.e., * or .), we might inadvertently snarf them up.
// Right now, the only one susceptible of being part of our directory walking
// is our own tarball temporary file...
if (archive_match_exclude_pattern(matching, "^kindletool_create_tarball_*") != ARCHIVE_OK) {
fprintf(stderr, "archive_match_exclude_pattern() failed: %s.\n", archive_error_string(matching));
}
#endif
r = archive_match_path_excluded(matching, entry);
if (r < 0) {
fprintf(stderr, "archive_match_path_excluded() failed: %s.\n", archive_error_string(matching));
archive_match_free(matching);
return 0;
}
if (r) {
// Skip original bundle/sig files to avoid duplicates
fprintf(stderr, "! %s\n", archive_entry_pathname(entry));
archive_match_free(matching);
return 0;
} else {
// We're a nice, proper file, carry on ;)
archive_match_free(matching);
return 1;
}
}
}
// Write a single file (or directory or other filesystem object) to the archive [from libarchive's tar/write.c].
static int
write_file(const struct kttar* kttar, struct archive* a, struct archive* in_a, struct archive_entry* entry)
{
if (write_entry(kttar, a, in_a, entry) != 0) {
return 1;
}
return 0;
}
// Write a single entry to the archive [from libarchive's tar/write.c].
static int
write_entry(const struct kttar* kttar, struct archive* a, struct archive* in_a, struct archive_entry* entry)
{
int e;
e = archive_write_header(a, entry);
if (e != ARCHIVE_OK) {
fprintf(stderr, "archive_write_header() failed: %s.\n", archive_error_string(a));
}
if (e == ARCHIVE_FATAL) {
return 1;
}
// If we opened a file earlier, write it out now.
// Note that the format handler might have reset the size field to zero
// to inform us that the archive body won't get stored.
// In that case, just skip the write.
if (e >= ARCHIVE_WARN && archive_entry_size(entry) > 0) {
if (copy_file_data_block(kttar, a, in_a, entry) != 0) {
return 1;
}
}
return 0;
}
// Helper function to copy file to archive [from libarchive's tar/write.c].
static int
copy_file_data_block(const struct kttar* kttar, struct archive* a, struct archive* in_a, struct archive_entry* entry)
{
size_t bytes_read;
ssize_t bytes_written;
int64_t offset, progress = 0;
unsigned char* null_buff = NULL;
const void* buff;
int r;
while ((r = archive_read_data_block(in_a, &buff, &bytes_read, &offset)) == ARCHIVE_OK) {
if (offset > progress) {
int64_t sparse = offset - progress;
size_t ns;
if (null_buff == NULL) {
null_buff = kttar->buff;
memset(null_buff, 0, kttar->buff_size);
}
while (sparse > 0) {
if (sparse > (int64_t) kttar->buff_size) {
ns = kttar->buff_size;
} else {
ns = (size_t) sparse;
}
bytes_written = archive_write_data(a, null_buff, ns);
if (bytes_written < 0) {
// Write failed; this is bad
fprintf(stderr, "archive_write_data() failed: %s.\n", archive_error_string(a));
return -1;
}
if ((size_t) bytes_written < ns) {
// Write was truncated; warn but continue.
fprintf(stderr,
"%s: Truncated write; file may have grown while being archived.\n",
archive_entry_pathname(entry));
return 0;
}
progress += bytes_written;
sparse -= bytes_written;
}
}
bytes_written = archive_write_data(a, buff, bytes_read);
if (bytes_written < 0) {
// Write failed; this is bad
fprintf(stderr, "archive_write_data() failed: %s.\n", archive_error_string(a));
return -1;
}
if ((size_t) bytes_written < bytes_read) {
// Write was truncated; warn but continue.
fprintf(stderr,
"%s: Truncated write; file may have grown while being archived.\n",
archive_entry_pathname(entry));
return 0;
}
progress += bytes_written;
}
if (r < ARCHIVE_WARN) {
fprintf(stderr, "archive_read_data_block() failed: %s.\n", archive_error_string(a));
return -1;
}
return 0;
}
// Helper function to populate & write entries from a read_disk_open loop, tailored to our needs.
// Helps avoid code duplication, since we're doing this in two passes.
static int
create_from_archive_read_disk(struct kttar* kttar,
struct archive* a,
const char* input_filename,
bool first_pass,
const char* signame,
const unsigned int real_blocksize)
{
int r;
bool is_exec = false;
bool is_kernel = false;
char* original_path = NULL;
char* tweaked_path = NULL;
struct archive* disk;
struct archive_entry* entry;
disk = archive_read_disk_new();
entry = archive_entry_new();
if (first_pass) {
// Perform pattern matching in a metadata filter to apply our exclude list to reguar files.
// NOTE: We're not using archive_read_disk_set_matching anymore
// because it does *pattern* matching too early to determine if we're a directory...
archive_read_disk_set_metadata_filter_callback(disk, metadata_filter, NULL);
}
archive_read_disk_set_standard_lookup(disk);
r = archive_read_disk_open(disk, input_filename);
if (r != ARCHIVE_OK) {
fprintf(stderr, "archive_read_disk_open() failed: %s.\n", archive_error_string(disk));
archive_read_free(disk);
archive_entry_free(entry);
return 1;
}
for (;;) {
archive_entry_clear(entry);
r = archive_read_next_header2(disk, entry);
if (r == ARCHIVE_EOF) {
break;
} else if (r != ARCHIVE_OK) {
fprintf(stderr, "archive_read_next_header2() failed: %s", archive_error_string(disk));
if (r == ARCHIVE_FATAL) {
fprintf(stderr, " (FATAL).\n");
goto cleanup;
} else if (r < ARCHIVE_WARN) {
fprintf(stderr, " (FAILED).\n");
// NOTE: We don't want to end up with an incomplete archive, abort.
goto cleanup;
}
}
if (!first_pass) {
// Fix the entry pathname, we used a tempfile...
// (if we're in legacy mode, signame has already been set to the tweaked path ;))
archive_entry_copy_pathname(entry, signame);
} else {
// Tweak the pathname if we were asked to behave like Yifan's KindleTool...
if (kttar->tweak_pointer_index != 0) {
// Handle the 'root' source directory itself..
// NOTE: We check that strlen <= pointer_index
// because libarchive strips trailing path separators in the entry pathname,
// but we might have passed one on the CL,
// so pointer_index might be larger than strlen ;)
if (archive_entry_filetype(entry) == AE_IFDIR &&
// Flawfinder: ignore
strlen(archive_entry_pathname(entry)) <= kttar->tweak_pointer_index) {
// Print what we're stripping, ala GNU tar...
fprintf(stderr,
"kindletool: Removing leading '%s/' from member names.\n",
archive_entry_pathname(entry));
// Just skip it, we don't need a redundant and explicit root directory entry in our tarball...
archive_read_disk_descend(disk);
continue;
} else {
original_path = strdup(archive_entry_pathname(entry));
// Try to handle a trailing path separator properly...
// NOTE: This probably isn't very robust.
// Also, no need to handle MinGW,
// it already spectacularly fails to handle this case ^^
if (original_path[kttar->tweak_pointer_index] == '/') {
// We found a path separator, skip it, too
tweaked_path = original_path + (kttar->tweak_pointer_index + 1);
} else {
tweaked_path = original_path + kttar->tweak_pointer_index;
}
archive_entry_copy_pathname(entry, tweaked_path);
}
}
}
// And then override a bunch of stuff (namely, uid/gid/chmod)
archive_entry_set_uid(entry, 0);
archive_entry_set_uname(entry, "root");
archive_entry_set_gid(entry, 0);
archive_entry_set_gname(entry, "root");
if (first_pass) {
// If we have a regular file, and it's a script, make it executable (probably overkill, but hey :))
if (archive_entry_filetype(entry) == AE_IFREG &&
(IS_SCRIPT(archive_entry_pathname(entry)) || IS_SHELL(archive_entry_pathname(entry)))) {
archive_entry_set_perm(entry, 0755);
// It's a script, keep track of it
is_exec = true;
kttar->has_script = is_exec;
is_kernel = false;
}
// If we have a regular file, and it's a kernel, and we're a recovery update, keep track of it
else if (archive_entry_filetype(entry) == AE_IFREG && real_blocksize == RECOVERY_BLOCK_SIZE &&
IS_UIMAGE(archive_entry_pathname(entry))) {
archive_entry_set_perm(entry, 0644);
is_exec = false;
// It's a kernel, keep track of it
is_kernel = true;
}
// If we have a directory, make it searchable...
else if (archive_entry_filetype(entry) == AE_IFDIR) {
archive_entry_set_perm(entry, 0755);
is_exec = false;
is_kernel = false;
} else {
archive_entry_set_perm(entry, 0644);
is_exec = false;
is_kernel = false;
}
// Non-regular files get archived with zero size.
if (archive_entry_filetype(entry) != AE_IFREG) {
archive_entry_set_size(entry, 0);
}
} else {
archive_entry_set_perm(entry, 0644);
}
archive_read_disk_descend(disk);
// Print what we're adding, ala bsdtar
fprintf(stderr,
"a %s%s\n",
archive_entry_pathname(entry),
(is_kernel ? "\t\t|<" : (is_exec ? "\t\t<-" : "")));
// Write our entry to the archive, completely via libarchive,
// to avoid having to open our entry file again, which would fail on non-POSIX systems...
if (write_file(kttar, a, disk, entry) != 0) {
goto cleanup;
}
if (first_pass) {
// If we just added a regular file, hash it, sign it, add it to the index, and put the sig in our tarball
if (archive_entry_filetype(entry) == AE_IFREG) {
// But just build a filelist for now, and do it later,
// I'm not up to refactoring sign_file & md5_sum to be useable during copy_file_data_block...
// We can't just do it now with the current sign_file & md5_sum implementation
// because we'd need to open() the input file (to sign & hash it),
// while it's already open through libarchive's read_disk API.
// That's apparently not possible on non POSIX systems.
// (You get a very helpful 'Permission denied' error on Windows...)
kttar->to_sign_and_bundle_list = realloc(kttar->to_sign_and_bundle_list,
++kttar->sign_and_bundle_index * sizeof(char*));
// And do the same with our tweaked pathname for legacy mode...
kttar->tweaked_to_sign_and_bundle_list = realloc(
kttar->tweaked_to_sign_and_bundle_list, kttar->sign_and_bundle_index * sizeof(char*));
// Use the correct paths if we tweaked the entry pathname...
if (kttar->tweak_pointer_index != 0) {
kttar->to_sign_and_bundle_list[kttar->sign_and_bundle_index - 1] =
strdup(original_path);
kttar->tweaked_to_sign_and_bundle_list[kttar->sign_and_bundle_index - 1] =
strdup(tweaked_path);
} else {
kttar->to_sign_and_bundle_list[kttar->sign_and_bundle_index - 1] =
strdup(archive_entry_pathname(entry));
kttar->tweaked_to_sign_and_bundle_list[kttar->sign_and_bundle_index - 1] =
strdup(archive_entry_pathname(entry));
}
}
} else {
// Delete the file once we're done, be it a signature or the bundlefile
unlink(input_filename);
}
free(original_path);
tweaked_path = NULL;
}
archive_read_close(disk);
archive_read_free(disk);
archive_entry_free(entry);
return 0;
cleanup:
free(original_path);
tweaked_path = NULL;
archive_read_close(disk);
archive_read_free(disk);
archive_entry_free(entry);
return 1;
}
// Archiving code inspired from libarchive tar/write.c ;).
static int
kindle_create_package_archive(const int outfd,
char** filename,
const unsigned int total_files,
const struct rsa_private_key* rsa_pkey_file,
const unsigned int legacy,
const unsigned int real_blocksize)
{
struct archive* a;
struct kttar * kttar, kttar_storage;
unsigned int i;
FILE* file;
FILE* sigfile;
char md5[MD5_HASH_LENGTH + 1];
uint8_t bundlefile_status = 0;
size_t pathlen;
char* signame = NULL;
char sigabsolutepath[PATH_MAX];
snprintf(sigabsolutepath, PATH_MAX, "%s/%s", kt_tempdir, "/kindletool_create_sig_XXXXXX");
int sigfd;
char* pathnamecpy = NULL;
char bundle_filename[PATH_MAX];
snprintf(bundle_filename, PATH_MAX, "%s/%s", kt_tempdir, "/kindletool_create_idx_XXXXXX");
int bundle_fd = -1;
FILE* bundlefile = NULL;
struct stat st;
// Use a pointer for consistency, but stack-allocated storage for ease of cleanup.
kttar = &kttar_storage;
memset(kttar, 0, sizeof(*kttar));
// Choose a suitable copy buffer size
kttar->buff_size = 64 * 1024;
while (kttar->buff_size < (size_t) DEFAULT_BYTES_PER_BLOCK) {
kttar->buff_size *= 2;
}
// Try to compensate for space we'll lose to alignment.
kttar->buff_size += 16 * 1024;
// Allocate a buffer for file data.
if ((kttar->buff = malloc(kttar->buff_size)) == NULL) {
fprintf(stderr, "Cannot allocate memory for archive copy buffer.\n");
return 1;
}
a = archive_write_new();
archive_write_add_filter_gzip(a);
archive_write_set_format_gnutar(a);
// These should be the default (cf. archive_write_new @ libarchive/archive_write.c), but reset them to be on the safe side...
archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK);
archive_write_set_bytes_in_last_block(a, -1);
archive_write_open_fd(a, outfd);
// Loop over our input files/directories...
for (i = 0; i < total_files; i++) {
// Don't tweak entries pathname by default
kttar->tweak_pointer_index = 0;
// Check if we want to behave like Yifan's KindleTool
if (legacy) {
if (stat(filename[i], &st) == 0) {
if (S_ISDIR(st.st_mode)) {
kttar->tweak_pointer_index = strlen(filename[i]); // Flawfinder: ignore
}
}
}
// Populate & write our entries from read_disk_open's directory walking...
if (create_from_archive_read_disk(kttar, a, filename[i], true, NULL, real_blocksize) != 0) {
goto cleanup;
}
}
// Add our bundle index to the end of the list...
// And we'll be creating it in a tempfile, to add to the fun...
bundle_fd = mkstemp(bundle_filename);
if (bundle_fd == -1) {
fprintf(stderr, "Couldn't open temporary file: %s.\n", strerror(errno));
goto cleanup;
}
if ((bundlefile = fdopen(bundle_fd, "w+b")) == NULL) {
fprintf(stderr, "Cannot open temp bundlefile '%s' for writing: %s.\n", bundle_filename, strerror(errno));
close(bundle_fd);
unlink(bundle_filename);
goto cleanup;
}
// Now that it's there, mark it as open and created
bundlefile_status = BUNDLE_OPEN | BUNDLE_CREATED;
// And append it as the last file...
kttar->to_sign_and_bundle_list =
realloc(kttar->to_sign_and_bundle_list, ++kttar->sign_and_bundle_index * sizeof(char*));
kttar->to_sign_and_bundle_list[kttar->sign_and_bundle_index - 1] = strdup(bundle_filename);
// We'll never tweak the bundlefile pathname, but we rely on this being sane & consistent, so set it
kttar->tweaked_to_sign_and_bundle_list =
realloc(kttar->tweaked_to_sign_and_bundle_list, kttar->sign_and_bundle_index * sizeof(char*));
kttar->tweaked_to_sign_and_bundle_list[kttar->sign_and_bundle_index - 1] = strdup(bundle_filename);
// And now loop again over the stuff we need to sign, hash & bundle...
for (i = 0; i <= kttar->sign_and_bundle_index; i++) {
// Dirty hack ahoy. If we're the last file in our list, that means we're the bundlefile, close our fd
if (i == kttar->sign_and_bundle_index - 1) {
fclose(bundlefile);
// It's closed, remove our flag
bundlefile_status &= (uint8_t) ~BUNDLE_OPEN;
}
// Dirty hack, the return. We loop twice on the bundlefile, once to sign it, and once to archive it...
if (i == kttar->sign_and_bundle_index) {
// It's the second time we're looping over the bundlefile, just archive it
// Just set the correct pathnames...
signame = strdup(INDEX_FILE_NAME);
// NOTE: Fragile, sigabsolutepath & bundle_filename need to be of the same length...
// Slightly less of a concern now that both are using PATH_MAX, but, still...
strcpy(sigabsolutepath, bundle_filename);
} else {
// First things first, if we're not the bundlefile,
// we're gonna need our size for a field of the bundlefile, so get on that...
if ((bundlefile_status & BUNDLE_OPEN) == BUNDLE_OPEN) {
// We're out of a libarchive read loop, so do a stat call ourselves
stat(kttar->to_sign_and_bundle_list[i], &st);
}
// Go on as usual, hash, sign & bundle :)
if ((file = fopen(kttar->to_sign_and_bundle_list[i], "rb")) == NULL) {
fprintf(stderr,
"Cannot open '%s' for reading: %s!\n",
kttar->to_sign_and_bundle_list[i],
strerror(errno));
// Avoid a double free (beginning from the second iteration,
// since we freed signame at the end of the first iteration,
// but it's not allocated yet, and cleanup will try to free...)
signame = NULL;
goto cleanup;
}
// Don't hash our bundlefile
if ((bundlefile_status & BUNDLE_OPEN) == BUNDLE_OPEN) {
if (md5_sum(file, md5) != 0) {
fprintf(stderr,
"Cannot calculate hash sum for '%s'.\n",
kttar->to_sign_and_bundle_list[i]);
fclose(file);
// Avoid a double free, bis.
signame = NULL;
goto cleanup;
}
md5[MD5_HASH_LENGTH] = 0;
rewind(file);
}
// If we're the bundlefile, fix the relative path to not use the tempfile path...
if ((bundlefile_status & BUNDLE_OPEN) != BUNDLE_OPEN) {
pathlen = strlen(INDEX_FILE_NAME); // Flawfinder: ignore
signame = malloc(pathlen + 4 + 1);
snprintf(signame, pathlen + 4 + 1, "%s.%s", INDEX_FILE_NAME, "sig");
} else {
// Always use the tweaked paths
// (they're properly set to the real path when we're not in legacy mode)
pathlen = strlen(kttar->tweaked_to_sign_and_bundle_list[i]); // Flawfinder: ignore
signame = malloc(pathlen + 4 + 1);
snprintf(
signame, pathlen + 4 + 1, "%s.%s", kttar->tweaked_to_sign_and_bundle_list[i], "sig");
}
// Create our sigfile in a tempfile
// We have to make sure mkstemp's template is reset first...
snprintf(sigabsolutepath, PATH_MAX, "%s/%s", kt_tempdir, "/kindletool_create_sig_XXXXXX");
sigfd = mkstemp(sigabsolutepath);
if (sigfd == -1) {
fprintf(stderr, "Couldn't open temporary signature file: %s.\n", strerror(errno));
fclose(file);
goto cleanup;
}
if ((sigfile = fdopen(sigfd, "wb")) == NULL) {
fprintf(stderr,
"Cannot open temp signature file '%s' for writing: %s.\n",
signame,
strerror(errno));
fclose(file);
close(sigfd);
unlink(sigabsolutepath);
goto cleanup;
}
if (sign_file(file, rsa_pkey_file, sigfile) < 0) {
fprintf(stderr, "Cannot sign '%s'.\n", kttar->to_sign_and_bundle_list[i]);
fclose(file);
fclose(sigfile);
unlink(sigabsolutepath); // Delete empty/broken sigfile
goto cleanup;
}
// Don't add the bundlefile to itself
if ((bundlefile_status & BUNDLE_OPEN) == BUNDLE_OPEN) {
// The last field is a display name, take a hint from the Python tool,
// and use the file's basename with a simple suffix.
// Use a copy of to_sign_and_bundle_list[i] to get our basename,
// since the POSIX implementation may alter its arg, and that would be very bad...
// And we're using the tweaked pathname in case we're in legacy mode ;)
pathnamecpy = strdup(kttar->to_sign_and_bundle_list[i]);
// Only flag kernels in recovery update...
// FWIW, the format is as follows:
// file_type_id md5sum file_name blocksize file_display_name
// where the id is 1 for kernel images (in recovery updates only),
// 129 for install scripts, and 128 for assets,
// and the blocksize is based on the file size relative to the update type blocksize.
if (fprintf(bundlefile,
"%u %s %s %jd %s_ktool_file\n",
((real_blocksize == RECOVERY_BLOCK_SIZE &&
IS_UIMAGE(kttar->to_sign_and_bundle_list[i])
? 1U
: (IS_SCRIPT(kttar->to_sign_and_bundle_list[i]) ||
IS_SHELL(kttar->to_sign_and_bundle_list[i]))
? 129U
: 128U)),
md5,
kttar->tweaked_to_sign_and_bundle_list[i],
(intmax_t) st.st_size / real_blocksize,
basename(pathnamecpy)) < 0) {
fprintf(stderr, "Cannot write to bundle index file.\n");
// Cleanup a bit before crapping out
fclose(file);
fclose(sigfile);
unlink(sigabsolutepath);
free(pathnamecpy);
goto cleanup;
}
free(pathnamecpy);
}
// Cleanup
fclose(file);
fclose(sigfile);
}
// And now, for the fun part! Append our sigfile to the archive...
// Populate & write our entries...
if (create_from_archive_read_disk(kttar, a, sigabsolutepath, false, signame, real_blocksize) != 0) {
unlink(sigabsolutepath);
goto cleanup;
}
// Cleanup
free(signame);
}
free(kttar->buff);
for (i = 0; i < kttar->sign_and_bundle_index; i++) {
free(kttar->to_sign_and_bundle_list[i]);
}
free(kttar->to_sign_and_bundle_list);
for (i = 0; i < kttar->sign_and_bundle_index; i++) {
free(kttar->tweaked_to_sign_and_bundle_list[i]);
}
free(kttar->tweaked_to_sign_and_bundle_list);
archive_write_close(a);
archive_write_free(a);
// Print a warning if no scripts were detected (in an OTA update)...
if (!kttar->has_script && real_blocksize == BLOCK_SIZE) {
fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
fprintf(stderr, "@ No script was detected in your input, this update package won't do a thing! @\n");
fprintf(stderr, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
// If we're building a recovery update, warn that this possibly isn't the brightest idea, given the very specific requirements...
if (real_blocksize == RECOVERY_BLOCK_SIZE) {
fprintf(stderr,
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
fprintf(stderr,
"@ You're building a recovery update from scratch! Make sure you know what you're doing... @\n");
fprintf(stderr,
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
return 0;
cleanup:
// Close & remove the bundlefile if we crapped out in the middle of processing
if ((bundlefile_status & BUNDLE_OPEN) == BUNDLE_OPEN) {
fclose(bundlefile);
unlink(bundle_filename);
} else {
// And if it's not open anymore, but was created, remove it too
// (that's a short window of time, namely, when we're looping over the bundlefile itself & its sigfile)
if ((bundlefile_status & BUNDLE_CREATED) == BUNDLE_CREATED) {
unlink(bundle_filename);
}
}
// Free what we might have alloc'ed
free(signame);
// The big stuff, too...
free(kttar->buff);
if (kttar->sign_and_bundle_index > 0) {
for (i = 0; i < kttar->sign_and_bundle_index; i++) {
free(kttar->to_sign_and_bundle_list[i]);
}
free(kttar->to_sign_and_bundle_list);
for (i = 0; i < kttar->sign_and_bundle_index; i++) {
free(kttar->tweaked_to_sign_and_bundle_list[i]);
}
free(kttar->tweaked_to_sign_and_bundle_list);
}
archive_write_close(a);
archive_write_free(a);
return 1;
}
static int
kindle_create(const UpdateInformation* info, FILE* input_tgz, FILE* output, const bool fake_sign)
{
unsigned char buffer[BUFFER_SIZE];
size_t count;
FILE* temp;
switch (info->version) {
case OTAUpdateV2:
if ((temp = tmpfile()) == NULL) {
fprintf(stderr, "Error opening temp file: %s.\n", strerror(errno));
return -1;
}
// Create the update
if (kindle_create_ota_update_v2(info, input_tgz, temp, fake_sign) < 0) {
fprintf(stderr, "Error creating update package.\n");
fclose(temp);
return -1;
}
rewind(temp); // Rewind the file before reading back
if (!fake_sign) {
// Write the signature (unless we asked for an unsigned package)
if (kindle_create_signature(info, temp, output) < 0) {
fprintf(stderr, "Error signing update package.\n");
fclose(temp);
return -1;
}
rewind(temp); // Rewind the file before writing it to output
}
// Write the update
while ((count = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, temp)) > 0) {
if (fwrite(buffer, sizeof(unsigned char), count, output) < count) {
fprintf(stderr, "Error writing update to output: %s.\n", strerror(errno));
fclose(temp);
return -1;
}
}
if (ferror(temp) != 0) {
fprintf(stderr, "Error reading generated update: %s.\n", strerror(errno));
fclose(temp);
return -1;
}
fclose(temp);
return 0;
break;
case OTAUpdate:
return kindle_create_ota_update(info, input_tgz, output, fake_sign);
break;
case RecoveryUpdate:
// NOTE: I was assuming that this, even FB02 @ rev. 2, shouldn't be wrapped in an UpdateSignature...
// That happened to be the case until Rex, where the target ota field also appeared...
// So wrap FB02h2 by default, you can always unwrap it later if need be...
if (memcmp(info->magic_number, "FB02", MAGIC_NUMBER_LENGTH) == 0 && info->header_rev == 2) {
if ((temp = tmpfile()) == NULL) {
fprintf(stderr, "Error opening temp file: %s.\n", strerror(errno));
return -1;
}
if (kindle_create_recovery(info, input_tgz, temp, fake_sign) < 0) {
fprintf(stderr, "Error creating update package.\n");
fclose(temp);
return -1;
}
rewind(temp);
if (!fake_sign) {
if (kindle_create_signature(info, temp, output) < 0) {
fprintf(stderr, "Error signing update package.\n");
fclose(temp);
return -1;
}
rewind(temp);
}
while ((count = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, temp)) > 0) {
if (fwrite(buffer, sizeof(unsigned char), count, output) < count) {
fprintf(stderr, "Error writing update to output: %s.\n", strerror(errno));
fclose(temp);
return -1;
}
}
if (ferror(temp) != 0) {
fprintf(stderr, "Error reading generated update: %s.\n", strerror(errno));
fclose(temp);
return -1;
}
fclose(temp);
return 0;
} else {
return kindle_create_recovery(info, input_tgz, output, fake_sign);
}
break;
case RecoveryUpdateV2:
if ((temp = tmpfile()) == NULL) {
fprintf(stderr, "Error opening temp file: %s.\n", strerror(errno));
return -1;
}
if (kindle_create_recovery_v2(info, input_tgz, temp, fake_sign) < 0) {
fprintf(stderr, "Error creating update package.\n");
fclose(temp);
return -1;
}
rewind(temp);
if (!fake_sign) {
if (kindle_create_signature(info, temp, output) < 0) {
fprintf(stderr, "Error signing update package.\n");
fclose(temp);
return -1;
}
rewind(temp);
}
while ((count = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, temp)) > 0) {
if (fwrite(buffer, sizeof(unsigned char), count, output) < count) {
fprintf(stderr, "Error writing update to output: %s.\n", strerror(errno));
fclose(temp);
return -1;
}
}
if (ferror(temp) != 0) {
fprintf(stderr, "Error reading generated update: %s.\n", strerror(errno));
fclose(temp);
return -1;
}
fclose(temp);
return 0;
break;
case UpdateSignature:
// NOTE: Should only be reached when building a signed userdata package
// We only need to sign the input tarball...
if (kindle_create_signature(info, input_tgz, output) < 0) {
fprintf(stderr, "Error signing userdata package.\n");
return -1;
}
rewind(input_tgz);
// ...And then simply append the input tarball as-is
while ((count = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, input_tgz)) > 0) {
if (fwrite(buffer, sizeof(unsigned char), count, output) < count) {
fprintf(
stderr, "Error appending userdata tarball to output: %s.\n", strerror(errno));
return -1;
}
}
if (ferror(input_tgz) != 0) {
fprintf(stderr, "Error reading original userdata tarball update: %s.\n", strerror(errno));
return -1;
}
return 0;
break;
case UnknownUpdate:
default:
fprintf(stderr, "Unknown update type.\n");
break;
}
return -1;
}
static int
kindle_create_ota_update_v2(const UpdateInformation* info, FILE* input_tgz, FILE* output, const bool fake_sign)
{
size_t header_size;
unsigned char* header;
size_t hindex = 0;
int i;
FILE* demunged_tgz;
size_t str_len;
demunged_tgz = NULL;
// First part of the set sized data
header_size = MAGIC_NUMBER_LENGTH + OTA_UPDATE_V2_BLOCK_SIZE;
header = malloc(header_size);
strncpy((char*) header, info->magic_number, MAGIC_NUMBER_LENGTH); // Flawfinder: ignore
hindex += MAGIC_NUMBER_LENGTH;
memcpy(&header[hindex], &info->source_revision, sizeof(uint64_t)); // Source
hindex += sizeof(uint64_t);
memcpy(&header[hindex], &info->target_revision, sizeof(uint64_t)); // Target