// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %rbx
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r15
movq %rcx, %rdi
callq 0x62d0
movq %rax, %r13
movq %rbx, %rdi
callq 0x62d0
addl %eax, %r13d
addl $0x2, %r13d
callq 0x6790
movq %rax, %rcx
leaq 0x1(%r14), %rdx
leaq 0x1(%rbx), %rsi
xorl %eax, %eax
movl $0x1, %edi
movsbq (%r15), %r8
movq (%rcx), %r9
testb $0x8, (%r9,%r8,2)
movl $0x1, %r9d
cmovel %r13d, %r9d
testq %r8, %r8
cmovel %edi, %r9d
testq %r12, %r12
je 0x48222
testl %ebp, %ebp
jle 0x4822a
leal (%r9,%rax), %r10d
cmpl %ebp, %r10d
jl 0x4822a
movb $0x0, (%r12)
xorl %r9d, %r9d
jmp 0x482c4
xorl %r12d, %r12d
jmp 0x482be
cmpl $0x1, %r9d
jne 0x4823c
movb %r8b, (%r12)
incq %r12
jmp 0x482be
movb (%r14), %r11b
movq %rdx, %r10
testb %r11b, %r11b
je 0x48256
movb %r11b, (%r12)
incq %r12
movb (%r10), %r11b
incq %r10
jmp 0x48242
movl %r8d, %r10d
shrb $0x4, %r10b
leal 0x30(%r10), %r11d
addb $0x37, %r10b
cmpb $0x3a, %r11b
movzbl %r10b, %r10d
movzbl %r11b, %r11d
cmovael %r10d, %r11d
movb %r11b, (%r12)
movl %r8d, %r10d
andb $0xf, %r10b
leal 0x30(%r10), %r11d
addb $0x37, %r10b
cmpb $0x3a, %r11b
movzbl %r10b, %r10d
movzbl %r11b, %r11d
cmovael %r10d, %r11d
movb %r11b, 0x1(%r12)
addq $0x2, %r12
movb (%rbx), %r10b
testb %r10b, %r10b
je 0x482be
movq %rsi, %r11
movb %r10b, (%r12)
incq %r12
movb (%r11), %r10b
incq %r11
testb %r10b, %r10b
jne 0x482ac
addl %r9d, %eax
movb $0x1, %r9b
testb %r9b, %r9b
je 0x482d9
incq %r15
testb %r8b, %r8b
jne 0x481e6
decl %eax
jmp 0x482de
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq