// File: /workspace/github_cpp_makefile_star2_O2/atsidaev[P]trx2x/DiskImage.cpp
// Function: unpack_td0(unsigned char*, long&)
bool unpack_td0(unsigned char *data, long &size)
{
   if(size < 12) return false;
   if( (*(short*)data != WORD2('T','D'))&&(*(short*)data != WORD2('t','d')) )
      return false;             // non TD0
   if( TD0CRC(data,10) != *((uint16_t*)(data+0x0A)) ) 
      return false;             // CRC bad...
   if( data[4] > 21 ) 
      return false;             // version > 2.1...

   unsigned char *snbuf = (unsigned char*)new char[size*2+1500000];  // if compressed then UUUUFF ;-/
   if(!snbuf) return false;

   memcpy(snbuf, data, size);
   if( *(short*)snbuf == WORD2('t','d') ) // packed disk
   {
      if( snbuf[4] < 20 )    // unsupported Old Advanced compression
      {
         delete snbuf;
         return false;
      }
      unpack_lzh((unsigned char*)data+12, size-12, (unsigned char*)snbuf+12), *(short*)snbuf = WORD2('T','D');
   }

   td0_src = snbuf, td0_dst = data;
   td0_move(12);

   if(snbuf[7] & 0x80)  // additional info...
   {
      unsigned short *cs = (unsigned short*)(snbuf+12+2);

      if( TD0CRC(snbuf+12+2, 8+*cs) != cs[-1] )
      {
         delete snbuf;
         return false;
      }
      td0_move(10);
      td0_move( *((unsigned short*)(snbuf+12+2)) );
   }

   for(;;) 
   {
      unsigned char s = *td0_src;
      td0_move(4); 
      if(s == 0xFF) break;
      for(; s; s--) 
      {
//         unsigned char *sec = td0_src;
         unsigned size = 128; if(td0_src[3]) size <<= td0_src[3];
         td0_move(6); 
         *(unsigned short*)td0_dst = size+1; td0_dst += 2;
         *td0_dst++ = 0; 
         unsigned char *dst = td0_dst;
         unsigned src_size = *(unsigned short*)td0_src; td0_src += 2;
         unsigned char *end_packed_data = td0_src + src_size;
         memset(td0_dst, 0, size);
         switch (*td0_src++) 
         {
            case 0:
               memcpy(dst, td0_src, src_size-1); break;
            case 1:
            {
               unsigned n = *(unsigned short*)td0_src; 
               td0_src += 2;
               unsigned short data = *(unsigned short*)td0_src;
               for(; n; n--) *(unsigned short*)dst = data, dst += 2;
               break;
            }
            case 2:
            {
               unsigned short data; 
               unsigned char s;
               do 
               {
                  switch( *td0_src++ ) 
                  {
                     case 0:
                        for(s = *td0_src++; s; s--) *dst++ = *td0_src++;
                        break;
                     case 1:
                        s = *td0_src++;
                        data = *(unsigned short*)td0_src; 
                        td0_src += 2;
                        for( ; s; s--) *(unsigned short*)dst = data, dst += 2;
                        break;
                     default: shit:
                        delete snbuf;
                        return false;  // "bad TD0 file"
                  }
               } while( td0_src < end_packed_data );
               break;
            }
            default: goto shit;
         }
         td0_dst += size; 
         td0_src = end_packed_data;
      }
   }
   size = (unsigned int)(td0_dst - data);
   delete snbuf;
   return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq (%rsi), %r12
cmpq $0xc, %r12
jl 0x64fc
movq %rsi, %rbx
movq %rdi, %r14
movzwl (%rdi), %eax
cmpl $0x6474, %eax           # imm = 0x6474
je 0x64e3
cmpl $0x4454, %eax           # imm = 0x4454
jne 0x64fc
pushq $0xa
popq %rsi
movq %r14, %rdi
callq 0x6484
cmpw 0xa(%r14), %ax
jne 0x64fc
cmpb $0x15, 0x4(%r14)
jbe 0x650f
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16e360(,%r12,2), %rdi
callq 0x1030
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x10d0
cmpw $0x6474, (%r15)         # imm = 0x6474
jne 0x655d
cmpb $0x14, 0x4(%r15)
jae 0x6543
xorl %ebx, %ebx
jmp 0x6791
leaq 0xc(%r14), %rdi
addl $-0xc, %r12d
leaq 0xc(%r15), %rdx
movl %r12d, %esi
callq 0x67e5
movw $0x4454, (%r15)         # imm = 0x4454
movl 0x8(%r15), %eax
movl %eax, 0x8(%r14)
movq (%r15), %rax
movq %rax, (%r14)
movq %r14, 0x10(%rsp)
leaq 0xc(%r14), %r12
movq %r12, 0x406d(%rip)      # 0xa5e8
movq %r15, %r14
addq $0xc, %r15
movq %r15, 0x4067(%rip)      # 0xa5f0
cmpb $0x0, 0x7(%r14)
js 0x659b
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
jmp 0x6606
leaq 0xe(%r14), %rdi
movzwl 0xe(%r14), %r13d
leal 0x8(%r13), %esi
callq 0x6484
cmpw 0xc(%r14), %ax
jne 0x67a1
movq %rbx, 0x8(%rsp)
movzwl 0x8(%r15), %eax
movw %ax, 0x8(%r12)
movq (%r15), %rax
movq %rax, (%r12)
movq 0x10(%rsp), %rbx
leaq 0x16(%rbx), %rdi
leaq 0x16(%r14), %rsi
movq %r13, %rdx
callq 0x10d0
leaq (%rbx,%r13), %r12
addq $0x16, %r12
movq %r12, 0x3ff5(%rip)      # 0xa5e8
movq %r14, (%rsp)
leaq (%r14,%r13), %r15
addq $0x16, %r15
movq %r15, 0x3fea(%rip)      # 0xa5f0
movb (%r15), %bl
movl (%r15), %eax
movl %eax, (%r12)
movq 0x3fd1(%rip), %r12      # 0xa5e8
addq $0x4, %r12
movq %r12, 0x3fc6(%rip)      # 0xa5e8
movq 0x3fc7(%rip), %r15      # 0xa5f0
addq $0x4, %r15
movq %r15, 0x3fbc(%rip)      # 0xa5f0
cmpb $-0x1, %bl
je 0x67a8
subb $0x1, %bl
jb 0x6606
movb 0x3(%r15), %cl
movl $0x80, %r13d
shll %cl, %r13d
movzwl 0x4(%r15), %eax
movw %ax, 0x4(%r12)
movl (%r15), %eax
movl %eax, (%r12)
movq 0x3f80(%rip), %rax      # 0xa5e8
movq 0x3f81(%rip), %r14      # 0xa5f0
leal 0x1(%r13), %ecx
movw %cx, 0x6(%rax)
leaq 0x9(%rax), %r12
movq %r12, 0x3f66(%rip)      # 0xa5e8
movb $0x0, 0x8(%rax)
movzwl 0x6(%r14), %ebp
leaq (%r14,%rbp), %r15
addq $0x8, %r15
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1090
leaq 0x9(%r14), %rsi
movq %rsi, 0x3f45(%rip)      # 0xa5f0
movzbl 0x8(%r14), %eax
testl %eax, %eax
je 0x6768
cmpl $0x1, %eax
je 0x674e
cmpl $0x2, %eax
jne 0x678b
leaq 0x1(%rsi), %rax
movq %rax, 0x3f1b(%rip)      # 0xa5f0
movzbl (%rsi), %eax
cmpl $0x1, %eax
je 0x6714
testl %eax, %eax
jne 0x678b
leaq 0x2(%rsi), %rax
movq %rax, 0x3f00(%rip)      # 0xa5f0
movb 0x1(%rsi), %al
movq 0x3ef6(%rip), %rsi      # 0xa5f0
subb $0x1, %al
jb 0x6740
leaq 0x1(%rsi), %rcx
movq %rcx, 0x3ee7(%rip)      # 0xa5f0
movb (%rsi), %cl
movb %cl, (%r12)
incq %r12
jmp 0x66f3
leaq 0x2(%rsi), %rax
movq %rax, 0x3ed1(%rip)      # 0xa5f0
movb 0x1(%rsi), %al
movzwl 0x2(%rsi), %ecx
addq $0x4, %rsi
movq %rsi, 0x3ebf(%rip)      # 0xa5f0
subb $0x1, %al
jb 0x6740
movw %cx, (%r12)
addq $0x2, %r12
jmp 0x6731
cmpq %r15, %rsi
jb 0x66ca
movq 0x3e9c(%rip), %r12      # 0xa5e8
jmp 0x6775
movzwl 0x9(%r14), %eax
movzwl 0xb(%r14), %ecx
xorl %edx, %edx
cmpl %edx, %eax
je 0x6775
movw %cx, (%r12,%rdx,2)
incq %rdx
jmp 0x675a
decl %ebp
movq %r12, %rdi
movq %rbp, %rdx
callq 0x10d0
addq %r13, %r12
movq %r12, 0x3e69(%rip)      # 0xa5e8
movq %r15, 0x3e6a(%rip)      # 0xa5f0
jmp 0x663d
xorl %ebx, %ebx
movq (%rsp), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x1140
jmp 0x64fe
xorl %ebx, %ebx
movq %r14, %r15
jmp 0x6791
subl 0x10(%rsp), %r12d
movq 0x8(%rsp), %rax
movq %r12, (%rax)
movb $0x1, %bl
jmp 0x678d