/* 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_0027.pgc"
/* @tier easy @module mg @transform msg-gateway-online */
/*
 * mg_ol_0027.pgc - 전문게이트웨이 전문 길이 검증 서비스 (MG_OL_0027)  [tier=easy]
 *
 * LLLL(선두 4자리 길이 prefix) 프레이밍 규약에 따라, 전문 앞 4바이트에
 * 선언된 길이와 실제 본문 길이가 일치하는지 검증한다. 길이 prefix가
 * 숫자가 아니거나 선언길이 != 실제길이면 반려한다.
 * (순수 검증 로직 - DB 접근 없음)
 */
#include <stdio.h>
#include <string.h>

#include "txcore.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 17 "app/online/mg_ol_0027.pgc"


#define MG_LEN_PREFIX  4

TX_SERVICE(MG_OL_0027, ctx)
{
    char raw[512];
    int  rawlen, declared, actual, i;

    tx_log(TX_LOG_INFO, "[MG_OL_0027] 전문 길이 검증 서비스 진입");

    /* 1) 원시전문 수신 (LLLL + 본문) */
    rawlen = tx_buf_get(ctx->in, "RAWMSG", raw, sizeof(raw));
    if (rawlen <= 0) {
        tx_log(TX_LOG_ERROR, "[MG_OL_0027] 원시전문(RAWMSG) 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 2) 최소 길이(프레임 헤더) 확인 */
    actual = (int)strlen(raw);
    if (actual < MG_LEN_PREFIX) {
        tx_log(TX_LOG_WARN, "[MG_OL_0027] 프레임 헤더 부족 len=%d", actual);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESP", RESP_INVALID);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 3) 길이 prefix 숫자 검증 */
    for (i = 0; i < MG_LEN_PREFIX; i++) {
        if (raw[i] < '0' || raw[i] > '9') {
            tx_log(TX_LOG_WARN, "[MG_OL_0027] 길이 prefix 비숫자 pos=%d", i);
            tx_buf_reset(ctx->out);
            tx_buf_sets(ctx->out, "RESP", RESP_INVALID);
            tx_return(ctx, TX_FAIL, ctx->out);
            return;
        }
    }

    /* 4) 선언길이 vs 실제 본문길이 비교 */
    declared = (raw[0] - '0') * 1000 + (raw[1] - '0') * 100 +
               (raw[2] - '0') * 10   + (raw[3] - '0');
    actual  -= MG_LEN_PREFIX;   /* 본문 실제 길이 */

    tx_buf_reset(ctx->out);
    tx_buf_setlong(ctx->out, "DECLARED", (long)declared);
    tx_buf_setlong(ctx->out, "ACTUAL", (long)actual);

    if (declared != actual) {
        tx_log(TX_LOG_WARN, "[MG_OL_0027] 길이 불일치 선언=%d 실제=%d",
               declared, actual);
        tx_buf_sets(ctx->out, "RESP", RESP_INVALID);
        tx_buf_sets(ctx->out, "MATCH", "N");
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    tx_buf_sets(ctx->out, "RESP", RESP_OK);
    tx_buf_sets(ctx->out, "MATCH", "Y");
    tx_log(TX_LOG_INFO, "[MG_OL_0027] 길이 일치 len=%d", declared);
    tx_return(ctx, TX_OK, ctx->out);
}
