/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "app/online/mg_ol_0011.pgc"
/* @tier medium @module mg @transform msg-gateway-online */
/*
 * mg_ol_0011.pgc - ISO8583식 필드 파싱 서비스 (MG_OL_0011)  [tier=medium]
 *
 * 요청의 primary bitmap(16 hex 문자 = 64bit)을 파싱하여 present 필드
 * 번호 목록을 추출하고, 비트온 개수/이차비트맵 사용여부를 응답한다.
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "mg_core.h"


#line 1 "/usr/include/postgresql/sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H

#ifndef PGDLLIMPORT
#if  defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif							/* __CYGWIN__ */
#endif							/* PGDLLIMPORT */

#define SQLERRMC_LEN	150

#ifdef __cplusplus
extern "C"
{
#endif

struct sqlca_t
{
	char		sqlcaid[8];
	long		sqlabc;
	long		sqlcode;
	struct
	{
		int			sqlerrml;
		char		sqlerrmc[SQLERRMC_LEN];
	}			sqlerrm;
	char		sqlerrp[8];
	long		sqlerrd[6];
	/* Element 0: empty						*/
	/* 1: OID of processed tuple if applicable			*/
	/* 2: number of rows processed				*/
	/* after an INSERT, UPDATE or				*/
	/* DELETE statement					*/
	/* 3: empty						*/
	/* 4: empty						*/
	/* 5: empty						*/
	char		sqlwarn[8];
	/* Element 0: set to 'W' if at least one other is 'W'	*/
	/* 1: if 'W' at least one character string		*/
	/* value was truncated when it was			*/
	/* stored into a host variable.             */

	/*
	 * 2: if 'W' a (hopefully) non-fatal notice occurred
	 */	/* 3: empty */
	/* 4: empty						*/
	/* 5: empty						*/
	/* 6: empty						*/
	/* 7: empty						*/

	char		sqlstate[5];
};

struct sqlca_t *ECPGget_sqlca(void);

#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif

#ifdef __cplusplus
}
#endif

#endif

#line 16 "app/online/mg_ol_0011.pgc"


TX_SERVICE(MG_OL_0011, ctx)
{
    char bitmap[24];
    char present[160];
    char hexbuf[8];
    int  i, nib, bit, fieldno, cnt = 0;
    int  secondary = 0;

    tx_log(TX_LOG_INFO, "[MG_OL_0011] ISO8583 비트맵 파싱 진입");

    if (tx_buf_get(ctx->in, "BITMAP", bitmap, sizeof(bitmap)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[MG_OL_0011] BITMAP 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    if (strlen(bitmap) < 16) {
        tx_log(TX_LOG_WARN, "[MG_OL_0011] 비트맵 길이부족 len=%d", (int)strlen(bitmap));
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    present[0] = '\0';

    /* 16 hex 문자 -> 64 bit, MSB = 필드1 */
    for (i = 0; i < 16; i++) {
        char c = bitmap[i];
        if (c >= '0' && c <= '9')      nib = c - '0';
        else if (c >= 'A' && c <= 'F') nib = c - 'A' + 10;
        else if (c >= 'a' && c <= 'f') nib = c - 'a' + 10;
        else {
            tx_log(TX_LOG_WARN, "[MG_OL_0011] 비16진 문자 pos=%d", i);
            tx_return(ctx, TX_EINVAL, ctx->out);
            return;
        }

        for (bit = 0; bit < 4; bit++) {
            if (nib & (1 << (3 - bit))) {
                fieldno = i * 4 + bit + 1;
                if (fieldno == 1)
                    secondary = 1;   /* 필드1 온 = secondary bitmap 존재 */
                snprintf(hexbuf, sizeof(hexbuf), "%d ", fieldno);
                if (strlen(present) + strlen(hexbuf) < sizeof(present))
                    strcat(present, hexbuf);
                cnt++;
            }
        }
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "FIELDS", present);
    tx_buf_setlong(ctx->out, "COUNT", (long)cnt);
    tx_buf_setlong(ctx->out, "SECONDARY", (long)secondary);

    /* 비트맵 파싱 결과(mg_bitmap_log)를 임베디드 SQL 로 적재 */
    {
        /* exec sql begin declare section */
         
         
         
        
#line 75 "app/online/mg_ol_0011.pgc"
 char h_bm_bitmap [ 24 ] ;
 
#line 76 "app/online/mg_ol_0011.pgc"
 long h_bm_count ;
 
#line 77 "app/online/mg_ol_0011.pgc"
 long h_bm_secondary ;
/* exec sql end declare section */
#line 78 "app/online/mg_ol_0011.pgc"


        strncpy(h_bm_bitmap, bitmap, sizeof(h_bm_bitmap) - 1);
        h_bm_bitmap[sizeof(h_bm_bitmap) - 1] = '\0';
        h_bm_count     = (long)cnt;
        h_bm_secondary = (long)secondary;

        if (tx_begin() == TX_OK) {
            { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into mg_bitmap_log ( primary_bitmap , field_count , secondary_yn ) values ( $1  , $2  , $3  )", 
	ECPGt_char,(h_bm_bitmap),(long)24,(long)1,(24)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_bm_count),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_bm_secondary),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
#line 88 "app/online/mg_ol_0011.pgc"

            if (sqlca.sqlcode != TX_SQL_OK) {
                TX_DBIO_LOG_ERR("INSERT mg_bitmap_log");
                tx_abort();
            } else {
                tx_commit();
            }
        }
    }

    tx_log(TX_LOG_INFO, "[MG_OL_0011] 파싱완료 present=%d secondary=%d", cnt, secondary);
    tx_return(ctx, TX_OK, ctx->out);
}
