// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/arc.c
// Function: do_pk_comments
static void do_pk_comments(deark *c, lctx *d)
{
	int found = 0;
	i64 sig_pos;
	i64 comments_descr_pos;
	int has_file_comments = 0;
	int has_archive_comment = 0;
	i64 file_comments_pos = 0;
	de_ucstring *archive_comment = NULL;
	u8 dscr[4];

	if(!d->prescan_found_eoa) return;
	sig_pos = c->infile->len-8;
	if(is_pkcomment_sig_at(c, d, sig_pos)){
		found = 1;
	}

	if(!found) {
		i64 count;

		// The PK signature normally appears 8 bytes from the end of the file,
		// but we'll scan for it, in case the file has padding appended to it.
		// The assumption is that it appears after a whole number of 32-byte
		// records. In case of multiple signatures, we use the earliest one that
		// looks valid.
		sig_pos = d->prescan_pos_after_eoa+32;
		count = 0;

		while(1) {
			if(sig_pos >= c->infile->len-8) break;
			if(count > d->num_top_level_members+10) break;
			if(is_pkcomment_sig_at(c, d, sig_pos)){
				found = 1;
				break;
			}
			sig_pos += 32;
			count++;
		}
	}

	if(!found) return;

	de_dbg(c, "PKARC/PKPAK comment block found, ID at %"I64_FMT, sig_pos);
	de_dbg_indent(c, 1);
	mark_end_of_known_data(d, sig_pos+8);
	init_trailer_data(c, d);

	// Note: This logic is based on reverse engineering, and could be wrong.
	comments_descr_pos = de_getu32le(sig_pos+4);
	de_dbg(c, "descriptor pos: %"I64_FMT, comments_descr_pos);
	// comments_descr_pos has already been validated, by is_pkcomment_sig_at().

	de_read(dscr, comments_descr_pos, 4);
	if(dscr[0]==0x20 && dscr[1]==0x20 && dscr[2]==0x20 && dscr[3]==0x00) {
		has_file_comments = 0;
		has_archive_comment = 1;
	}
	else if(dscr[0]==0x01 && dscr[3]==0x20) {
		has_file_comments = 1;
		has_archive_comment = 0;
	}
	else if(dscr[0]==0x01 && dscr[3]==0x00) {
		has_file_comments = 1;
		has_archive_comment = 1;
	}
	else {
		de_dbg(c, "[unrecognized comments descriptor]");
	}

	if(has_file_comments) {
		file_comments_pos = comments_descr_pos + 32;
		if(sig_pos - file_comments_pos < 32) {
			has_file_comments = 0;
		}
	}

	if(has_archive_comment) {
		archive_comment = ucstring_create(c);
		read_one_pk_comment(c, d, comments_descr_pos-32, archive_comment);
		de_dbg(c, "archive comment: \"%s\"", ucstring_getpsz_d(archive_comment));
	}

	if(has_file_comments) {
		i64 num_file_comments;
		i64 i;

		num_file_comments = (sig_pos - file_comments_pos)/32;
		de_dbg(c, "apparent number of file comments: %d", (int)num_file_comments);

		for(i=0; i<num_file_comments && i<d->num_top_level_members; i++) {
			if(!d->persistent_md[i].comment) {
				d->persistent_md[i].comment = ucstring_create(c);
			}
			if(ucstring_isnonempty(d->persistent_md[i].comment)) continue;
			read_one_pk_comment(c, d,file_comments_pos + i*32, d->persistent_md[i].comment);
		}
	}

	ucstring_destroy(archive_comment);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x25(%rax)
jne 0x305d4
jmp 0x30959
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq $0x8, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x30c20
cmpl $0x0, %eax
je 0x30605
movl $0x1, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x30694
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x18(%rcx), %rcx
subq $0x8, %rcx
cmpq %rcx, %rax
jl 0x30642
jmp 0x30692
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
addq $0xa, %rcx
cmpq %rcx, %rax
jle 0x30659
jmp 0x30692
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x30c20
cmpl $0x0, %eax
je 0x30678
movl $0x1, -0x14(%rbp)
jmp 0x30692
movq -0x20(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x30627
jmp 0x30694
cmpl $0x0, -0x14(%rbp)
jne 0x3069f
jmp 0x30959
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x16fe8a(%rip), %rsi    # 0x1a0538
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x8, %rsi
callq 0x30560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x30d00
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x4, %rsi
callq 0x165540
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x16fe5a(%rip), %rsi    # 0x1a0563
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x44(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x4, %ecx
callq 0x164d10
movzbl -0x44(%rbp), %eax
cmpl $0x20, %eax
jne 0x3075e
movzbl -0x43(%rbp), %eax
cmpl $0x20, %eax
jne 0x3075e
movzbl -0x42(%rbp), %eax
cmpl $0x20, %eax
jne 0x3075e
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0x3075e
movl $0x0, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x307b8
movzbl -0x44(%rbp), %eax
cmpl $0x1, %eax
jne 0x30780
movzbl -0x41(%rbp), %eax
cmpl $0x20, %eax
jne 0x30780
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
jmp 0x307b6
movzbl -0x44(%rbp), %eax
cmpl $0x1, %eax
jne 0x307a2
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0x307a2
movl $0x1, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
jmp 0x307b4
movq -0x8(%rbp), %rdi
leaq 0x16fdca(%rip), %rsi    # 0x1a0577
movb $0x0, %al
callq 0x15b0d0
jmp 0x307b6
jmp 0x307b8
cmpl $0x0, -0x2c(%rbp)
je 0x307e1
movq -0x28(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
subq -0x38(%rbp), %rax
cmpq $0x20, %rax
jge 0x307df
movl $0x0, -0x2c(%rbp)
jmp 0x307e1
cmpl $0x0, -0x30(%rbp)
je 0x30833
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
subq $0x20, %rdx
movq -0x40(%rbp), %rcx
callq 0x30d50
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172650
movq -0x68(%rbp), %rdi
movq %rax, %rdx
leaq 0x16fd6e(%rip), %rsi    # 0x1a059a
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x2c(%rbp)
je 0x30942
movq -0x20(%rbp), %rax
subq -0x38(%rbp), %rax
movl $0x20, %ecx
cqto
idivq %rcx
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %edx
leaq 0x16fd4c(%rip), %rsi    # 0x1a05b0
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq -0x58(%rbp), %rcx
movb %al, -0x69(%rbp)
jge 0x30894
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
setl %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x308a0
jmp 0x30940
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0x308db
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x1717f0
cmpl $0x0, %eax
je 0x308fd
jmp 0x3092f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
callq 0x30d50
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x30873
jmp 0x30942
movq -0x40(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x70, %rsp
popq %rbp
retq
nop