// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
movq %rdx, %rbx
movq %rsi, %r14
movl 0x8(%rdx), %eax
subl 0x10(%rdx), %eax
cmpl $0x10, %eax
jb 0x282e
xorl %r15d, %r15d
pushq $0x8
popq %rbp
cmpq $0x10, %r15
je 0x2868
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3184
movb %al, (%r14,%r15)
incq %r15
jmp 0x2815
movq 0x67a3(%rip), %rdi      # 0x8fd8
leaq 0x39ad(%rip), %rsi      # 0x61e9
callq 0x2180
movl 0x8(%rbx), %esi
subl 0x10(%rbx), %esi
movq %rax, %rdi
callq 0x20f0
leaq 0x39b9(%rip), %rsi      # 0x620f
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
jmp 0x2870
movl 0x8(%rbx), %eax
cmpl 0x10(%rbx), %eax
jne 0x28e9
movq 0x6761(%rip), %rdi      # 0x8fd8
leaq 0x39aa(%rip), %rsi      # 0x6228
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movq 0x6746(%rip), %rdi      # 0x8fd8
leaq 0x3a18(%rip), %rsi      # 0x62b1
callq 0x2180
movl 0x10(%r14), %esi
movq %rax, %rdi
callq 0x20f0
leaq 0x3a2c(%rip), %rsi      # 0x62dd
movq %rax, %rdi
callq 0x2180
movl 0x8(%rbx), %esi
subl 0x10(%rbx), %esi
movq %rax, %rdi
callq 0x20f0
leaq 0x3941(%rip), %rsi      # 0x620f
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x3184
movl %eax, %r15d
movzbl %r15b, %eax
cmpl $0x7f, %eax
ja 0x292f
movl %eax, 0x10(%r14)
movl 0x8(%rbx), %edx
movl 0x10(%rbx), %ecx
subl %ecx, %edx
cmpl %eax, %edx
jb 0x288b
movq %rcx, %rdx
addq (%rbx), %rdx
movq %rdx, 0x18(%r14)
addl %ecx, %eax
movl %eax, 0x10(%rbx)
movq %r14, %rdi
callq 0x26c8
movb $0x1, %al
jmp 0x28e0
andl $0x7f, %r15d
movl 0x8(%rbx), %eax
subl 0x10(%rbx), %eax
cmpl %r15d, %eax
jae 0x2988
movq 0x6693(%rip), %rdi      # 0x8fd8
leaq 0x3965(%rip), %rsi      # 0x62b1
callq 0x2180
movq %rax, %rdi
movl %r15d, %esi
callq 0x2270
leaq 0x3954(%rip), %rsi      # 0x62b7
movq %rax, %rdi
callq 0x2180
movl 0x8(%rbx), %esi
subl 0x10(%rbx), %esi
movq %rax, %rdi
callq 0x20f0
leaq 0x388f(%rip), %rsi      # 0x620f
movq %rax, %rdi
jmp 0x287e
cmpl $0x5, %r15d
jb 0x29b8
movq 0x6643(%rip), %rdi      # 0x8fd8
leaq 0x38bb(%rip), %rsi      # 0x6257
callq 0x2180
leaq 0x38fa(%rip), %rsi      # 0x62a2
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
leal -0x8(,%r15,8), %ebp
xorl %eax, %eax
pushq $0x8
popq %r12
movl %eax, 0x10(%r14)
testl %r15d, %r15d
jle 0x2904
decl %r15d
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3184
movl %ebp, %ecx
shll %cl, %eax
orl 0x10(%r14), %eax
addl $-0x8, %ebp
jmp 0x29c6