// File: /workspace/github_cpp_makefile_star2_O1/salleaffaire[P]MXFParser/klv.cpp
// Function: klv_parser::get_klv_item(klv_item&, bit_extractor&)
bool klv_parser::get_klv_item(klv_item &klvi, bit_extractor &be) {
   bool rval = true;
   
   // Read Key
   // ------------------------------------------------------------------------------
   // Test for enough byte for a LU Key
   if (be.get_bytes_left() >= 16) {
      for (int i=0;i<16;++i) {
         klvi.mKey[i] = be.get_bits(8);
      }
   }
   else {
      std::cout << "Need 16 bytes for a KLV Key but only " << be.get_bytes_left()
                << " bytes left in the file." << std::endl;
      rval = false;
   }
   
   uint8_t l0;
   if (rval && (be.get_bytes_left() >= 1)) {
      // Read Length (BER encoded)
      l0 = be.get_bits(8);
   }
   else {
      std::cout << "Need 1 byte for a BER first byte file is empty" << std::endl;
      rval = false;
   }
   
   if (rval) {
      // Short form
      if (!(l0 & 0x80)) {
         klvi.mLength = l0;
      } 
      // Long form
      else {
         int num_of_octets = l0 & 0x7F;
         if (be.get_bytes_left() >= num_of_octets) {
            if (num_of_octets > 4) {
               std::cout << " Error : This library doesn't support BER long form with number of octets "
                         << "bigger than 4." << std::endl;
            }
            int i;
            klvi.mLength = 0;
            for (i=num_of_octets-1;i>=0;--i) {
               uint32_t o = be.get_bits(8);
               klvi.mLength |= (o << (i*8));
            }
         }
         else {
            std::cout << "Need " << num_of_octets
                      << " bytes for a KLV Length BER but only " << be.get_bytes_left()
                      << " bytes left in the file." << std::endl;
            rval = false;
         }
      }
      
   }
   
   if (rval && (be.get_bytes_left() >= klvi.mLength)) {
      klvi.mValue = be.get_buffer();
      be.skip_n_bytes(klvi.mLength);
   }
   else {
      std::cout << "Need " << klvi.mLength
                << " bytes for a KLV Value but only " << be.get_bytes_left()
                << " bytes left in the file." << std::endl;
      rval = false;
   }

   if (rval)  {
      klvi.parse_value();
   }
   
   return rval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %rbx
movl 0x8(%rdx), %eax
subl 0x10(%rdx), %eax
cmpl $0x10, %eax
jb 0x29d0
xorl %r15d, %r15d
movq %r14, %rdi
movl $0x8, %esi
callq 0x3cac
movzbl 0xc(%r14), %ecx
movl %ecx, %edx
addl $0x8, %edx
shrl $0x3, %edx
addl %edx, 0x10(%r14)
andb $0x7, %cl
movb %cl, 0xc(%r14)
movb %al, (%rbx,%r15)
incq %r15
cmpq $0x10, %r15
jne 0x2999
movb $0x1, %r13b
jmp 0x2a3c
movq 0x7601(%rip), %r15      # 0x9fd8
leaq 0x480b(%rip), %rsi      # 0x71e9
movl $0x25, %edx
movq %r15, %rdi
callq 0x21a0
movl 0x8(%r14), %esi
subl 0x10(%r14), %esi
movq %r15, %rdi
callq 0x20d0
movq %rax, %r15
leaq 0x480a(%rip), %rsi      # 0x720f
movl $0x18, %edx
movq %rax, %rdi
callq 0x21a0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20e0
xorl %r13d, %r13d
testb %r13b, %r13b
je 0x2a4b
movl 0x8(%r14), %eax
cmpl 0x10(%r14), %eax
jne 0x2a95
movq 0x7586(%rip), %r15      # 0x9fd8
leaq 0x47cf(%rip), %rsi      # 0x7228
movl $0x2e, %edx
movq %r15, %rdi
callq 0x21a0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20e0
xorl %r15d, %r15d
xorl %r13d, %r13d
jmp 0x2abe
movq %r14, %rdi
movl $0x8, %esi
callq 0x3cac
movzbl 0xc(%r14), %ecx
movl %ecx, %edx
addl $0x8, %edx
shrl $0x3, %edx
addl %edx, 0x10(%r14)
andb $0x7, %cl
movb %cl, 0xc(%r14)
movzbl %al, %r15d
testb %r13b, %r13b
je 0x2c2d
cmpl $0x7f, %r15d
ja 0x2ad6
movl %r15d, 0x10(%rbx)
jmp 0x2c2d
andl $0x7f, %r15d
movl 0x8(%r14), %eax
subl 0x10(%r14), %eax
cmpl %r15d, %eax
jae 0x2b7e
movq 0x74e6(%rip), %r12      # 0x9fd8
leaq 0x47b8(%rip), %rsi      # 0x72b1
movl $0x5, %edx
movq %r12, %rdi
callq 0x21a0
movq %r12, %rdi
movl %r15d, %esi
callq 0x2230
movq %rax, %r15
leaq 0x479c(%rip), %rsi      # 0x72b7
movl $0x25, %edx
movq %rax, %rdi
callq 0x21a0
movl 0x8(%r14), %esi
subl 0x10(%r14), %esi
movq %r15, %rdi
callq 0x20d0
movq %rax, %r15
leaq 0x46cd(%rip), %rsi      # 0x720f
movl $0x18, %edx
movq %rax, %rdi
callq 0x21a0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20e0
xorl %r13d, %r13d
jmp 0x2c2d
cmpl $0x5, %r15d
jb 0x2bdb
movq 0x744d(%rip), %r12      # 0x9fd8
leaq 0x46c5(%rip), %rsi      # 0x7257
movl $0x4a, %edx
movq %r12, %rdi
callq 0x21a0
leaq 0x46fc(%rip), %rsi      # 0x72a2
movl $0xe, %edx
movq %r12, %rdi
callq 0x21a0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20e0
movl $0x0, 0x10(%rbx)
testl %r15d, %r15d
je 0x2c2d
movl %r15d, %r12d
incl %r12d
leal -0x8(,%r15,8), %ebp
movq %r14, %rdi
movl $0x8, %esi
callq 0x3cac
movzbl 0xc(%r14), %edx
movl %edx, %ecx
addl $0x8, %ecx
shrl $0x3, %ecx
addl %ecx, 0x10(%r14)
andb $0x7, %dl
movl %ebp, %ecx
shll %cl, %eax
movb %dl, 0xc(%r14)
orl %eax, 0x10(%rbx)
decl %r12d
addl $-0x8, %ebp
cmpl $0x1, %r12d
jg 0x2bf5
testb %r13b, %r13b
je 0x2c47
movl 0x8(%r14), %edx
movl 0x10(%r14), %eax
subl %eax, %edx
movl 0x10(%rbx), %ecx
cmpl %ecx, %edx
jae 0x2cd6
movq 0x738a(%rip), %r15      # 0x9fd8
leaq 0x465c(%rip), %rsi      # 0x72b1
movl $0x5, %edx
movq %r15, %rdi
callq 0x21a0
movl 0x10(%rbx), %esi
movq %r15, %rdi
callq 0x20d0
movq %rax, %r15
leaq 0x4666(%rip), %rsi      # 0x72dd
movl $0x20, %edx
movq %rax, %rdi
callq 0x21a0
movl 0x8(%r14), %esi
subl 0x10(%r14), %esi
movq %r15, %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x4571(%rip), %rsi      # 0x720f
movl $0x18, %edx
movq %rax, %rdi
callq 0x21a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20e0
xorl %ebp, %ebp
jmp 0x2ce9
movq %rax, %rdx
addq (%r14), %rdx
movq %rdx, 0x18(%rbx)
addl %eax, %ecx
movl %ecx, 0x10(%r14)
movb $0x1, %bpl
testb %bpl, %bpl
je 0x2cf6
movq %rbx, %rdi
callq 0x27f0
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop