// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/htmshell.c
// Function: nonAlphaNumericHexEncodeText
int nonAlphaNumericHexEncodeText(char *s, char *out, int outSize, 
   char *prefix, char *postfix)
/* For html tag attributes, it replaces non-alphanumeric characters
 * with <prefix>HH<postfix> hex codes to fight XSS.
 * out result must be large enough to receive the encoded string.
 * Returns size of encoded string or -1 if output larger than outSize. 
 * To just get the final encoded size, pass in NULL for out and 0 for outSize. 
 * To output without checking sizes, pass in non-NULL for out and 0 for outSize. 
 */
{
int encodedSize = strlen(prefix) + 2 + strlen(postfix);
int total = 0;
char c = 0;
do
    {
    c=*s++;
    int size = 1;
    if (!isalnum(c)) // alpha-numeric
	{
	size = encodedSize;
	}
    if (c == 0)
	size = 1;    // do not encode the terminating 0
    if (out)
	{
	if (outSize > 0 && (total+size+1) > outSize) // 1 for terminator
	    {
	    *out = 0;
	    return -1;
	    }
	if (size == 1)
	    *out++ = c;
	else
	    {
	    char x;
	    char *pf = prefix;
	    while ((x = *pf++) != 0)
		*out++ = x;
	    // use (unsigned char) to shift without sign-extension. We want zeros to be added on left side.
	    char h1 = ((unsigned char) c >> 4 ) + 0x30; if (h1 > 0x39) h1 += 7; 
	    *out++ = h1;
	    char h2 = (c & 0xF) + 0x30; if (h2 > 0x39) h2 += 7;
	    *out++ = h2;
	    pf = postfix;
	    while ((x = *pf++) != 0)
		*out++ = x;
	    }
	}
    total += size;
    } while (c != 0);
return total - 1; // do not count terminating 0
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x62c0
addq $0x2, %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
callq 0x62c0
movq %rax, %rcx
movq -0x60(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movb (%rax), %al
movb %al, -0x39(%rbp)
movl $0x1, -0x40(%rbp)
callq 0x6780
movq (%rax), %rax
movsbl -0x39(%rbp), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x68e89
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
movsbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x68e99
movl $0x1, -0x40(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x68fca
cmpl $0x0, -0x1c(%rbp)
jle 0x68ecb
movl -0x38(%rbp), %eax
addl -0x40(%rbp), %eax
addl $0x1, %eax
cmpl -0x1c(%rbp), %eax
jle 0x68ecb
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68fe9
cmpl $0x1, -0x40(%rbp)
jne 0x68eea
movb -0x39(%rbp), %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
jmp 0x68fc8
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x50(%rbp)
movb (%rax), %al
movb %al, -0x41(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
je 0x68f24
movb -0x41(%rbp), %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
jmp 0x68ef2
movzbl -0x39(%rbp), %eax
sarl $0x4, %eax
addl $0x30, %eax
movb %al, -0x51(%rbp)
movsbl -0x51(%rbp), %eax
cmpl $0x39, %eax
jle 0x68f44
movsbl -0x51(%rbp), %eax
addl $0x7, %eax
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
movsbl -0x39(%rbp), %eax
andl $0xf, %eax
addl $0x30, %eax
movb %al, -0x52(%rbp)
movsbl -0x52(%rbp), %eax
cmpl $0x39, %eax
jle 0x68f78
movsbl -0x52(%rbp), %eax
addl $0x7, %eax
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x50(%rbp)
movb (%rax), %al
movb %al, -0x41(%rbp)
movsbl %al, %eax
cmpl $0x0, %eax
je 0x68fc6
movb -0x41(%rbp), %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
jmp 0x68f94
jmp 0x68fc8
jmp 0x68fca
movl -0x40(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movsbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x68e4d
movl -0x38(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)