summaryrefslogtreecommitdiff
path: root/src/libnml/cms/cms_up.cc
blob: fee13788f065f263df967d71569c100400bfd6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/********************************************************************
* Description: cms_up.cc
*   Provides the interface to CMS used by NML update functions
*   including a CMS update function for all the basic C data types.
*
*   Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: LGPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/

#include "cms.hh"		/* class CMS */
#include "cms_up.hh"		/* class CMS_UPDATER */
#include "rcs_print.hh"		/* rcs_print_error() */

#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>		// malloc(), free()
#include <string.h>		// memset()
#ifdef __cplusplus
}
#endif

CMS_UPDATER::CMS_UPDATER(CMS * _cms_parent, int create_encoded_data,
    long _neutral_size_factor):
  /**********************************************
  * Aliases to variables in the CMS parent
  * using aliases lets CMS and its CMS_UPDATER share this information
  * more conveniently and allowed the CMS_UPDATER functions to be pulled out
  * of CMS with fewer changes. (WPS - 6/12/95)
  *********************************************/
encoded_data(_cms_parent->encoded_data),
encoded_header(_cms_parent->encoded_header),
encoded_queuing_header(_cms_parent->encoded_queuing_header),
status(_cms_parent->status),
size(_cms_parent->size),
encoded_header_size(_cms_parent->encoded_header_size),
encoded_queuing_header_size(_cms_parent->encoded_queuing_header_size),
using_external_encoded_data(_cms_parent->using_external_encoded_data),
pointer_check_disabled(_cms_parent->pointer_check_disabled),
encoded_data_size(_cms_parent->encoded_data_size)
{
    cms_parent = _cms_parent;
    mode = CMS_NO_UPDATE;
    neutral_size_factor = _neutral_size_factor;
    if (encoded_data == NULL && create_encoded_data) {
	if (cms_parent->enc_max_size > 0
	    && cms_parent->enc_max_size < neutral_size_factor * size) {
	    set_encoded_data(malloc(cms_parent->enc_max_size),
		cms_parent->enc_max_size);
	} else {
	    set_encoded_data(malloc(neutral_size_factor * size),
		neutral_size_factor * size);
	}
	using_external_encoded_data = 0;
    }
}

CMS_UPDATER::~CMS_UPDATER()
{
}

void CMS_UPDATER::rewind()
{
}

int CMS_UPDATER::set_mode(CMS_UPDATER_MODE _mode)
{
    mode = _mode;
    switch (mode) {
    case CMS_NO_UPDATE:
	break;

    case CMS_ENCODE_DATA:
	encoding = 1;
	break;

    case CMS_DECODE_DATA:
	encoding = 0;
	break;

    case CMS_ENCODE_HEADER:
	encoding = 1;
	break;

    case CMS_DECODE_HEADER:
	encoding = 0;
	break;

    case CMS_ENCODE_QUEUING_HEADER:
	encoding = 1;
	break;

    case CMS_DECODE_QUEUING_HEADER:
	encoding = 0;
	break;

    default:
	rcs_print_error("CMS updater in invalid mode.\n");
	return (-1);
    }
    return (0);
}

CMS_UPDATER_MODE CMS_UPDATER::get_mode()
{
    return mode;
}

int CMS_UPDATER::check_pointer(char *_pointer, long _bytes)
{
    if (NULL == cms_parent) {
	return (-1);
    }
    return (cms_parent->check_pointer(_pointer, _bytes));
}

void CMS_UPDATER::set_encoded_data(void *_encoded_data,
    long _encoded_data_size)
{
    if (NULL != encoded_data &&
	!using_external_encoded_data && encoded_data != _encoded_data) {
	free(encoded_data);
	encoded_data = NULL;
    }
    encoded_data = _encoded_data;
    encoded_data_size = _encoded_data_size;
    using_external_encoded_data = 1;
}