summaryrefslogtreecommitdiff
path: root/support/dktools/dk3bom.c
blob: 628e82158c41b17b11b011ef60433847168c7823 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright (C) 2015-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3bom.ctr
*/

/**	@file dk3bom.c The dk3bom module.
*/


#line 76 "dk3bom.ctr"

#include "dk3conf.h"
#include "dk3const.h"
#include "dk3stm.h"
#include "dk3bom.h"

/**	Initialize detector structure.
	@param	ptr	Detector structure to initialize.
*/
void
dk3bom_detect_init(dk3_bom_detector_t *ptr, int de)
{
  if (NULL != ptr) {
    ptr->defenc = de;
    ptr->found  = de;
    ptr->state  = 0;
    ptr->rjb    = 0;
    ptr->rj[0] = 0x00;
    ptr->rj[1] = 0x00;
    ptr->rj[2] = 0x00;
    ptr->rj[3] = 0x00;
  }
}

/*
        BOMS and states
        ---------------
        UTF-32 LSB:     0xFF (1) 0xFE (2) 0x00 (3) 0x00
        UTF-32 MSB:     0x00 (4) 0x00 (5) 0xFE (6) 0xFF
        UTF-16 LSB:     0xFF (1) 0xFE (2)
        UTF-16 MSB:     0xFE (7) 0xFF
        UTF-8:          0xEF (8) 0xBB (9) 0xBF
*/


int
dk3bom_detect_add(dk3_bom_detector_t *ptr, unsigned char b)
{
  int		 back	=	DK3_STM_ERROR;
  if (NULL != ptr) {
    switch (ptr->state) {
      case 0: {
        ptr->rj[0] = b;
	ptr->rjb = 1;
        switch ((int)(b)) {
	  case 0x00: {
	    ptr->state =  4; back = DK3_STM_ACCEPT;
	  } break;
	  case 0xFF: {
	    ptr->state =  1; back = DK3_STM_ACCEPT;
	  } break;
	  case 0xFE: {
	    ptr->state =  7; back = DK3_STM_ACCEPT;
	  } break;
	  case 0xEF: {
	    ptr->state =  8; back = DK3_STM_ACCEPT;
	  } break;
	  default: {
	    ptr->state = -1; back = DK3_STM_FINISHED;
	  } break;
	}
      } break;
      case 1: {
        ptr->rj[1] = b;
	ptr->rjb = 2;
        if (0xFE == b) {
	  ptr->state =  2;
	  back	     =  DK3_STM_ACCEPT;
	  ptr->found =  DK3_FILE_ENCODING_UTF16_LSB_FIRST;
	  ptr->rjb   =  0;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      case 2: {
	ptr->rj[0] = b;
	ptr->rjb = 1;
        if (0x00 == b) {
	  ptr->state =  3; back = DK3_STM_ACCEPT;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	  ptr->found = DK3_FILE_ENCODING_UTF16_LSB_FIRST;
	}
      } break;
      case 3: {
	ptr->rj[1] = b;
	ptr->rjb = 2;
        if (0x00 == b) {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	  ptr->found = DK3_FILE_ENCODING_UNICODE_LSB_FIRST;
	  ptr->rjb = 0;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	  ptr->found = DK3_FILE_ENCODING_UTF16_LSB_FIRST;
	}
      } break;
      case 4: {
        ptr->rj[1] = b;
	ptr->rjb = 2;
        if (0x00 == b) {
	  ptr->state =  5; back = DK3_STM_ACCEPT;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      case 5: {
        ptr->rj[2] = b;
	ptr->rjb = 3;
        if (0xFE == b) {
	  ptr->state =  6; back = DK3_STM_ACCEPT;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      case 6: {
        ptr->rj[3] = b;
	ptr->rjb = 4;
        if (0xFF == b) {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	  ptr->found = DK3_FILE_ENCODING_UNICODE_MSB_FIRST;
	  ptr->rjb = 0;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      case 7: {
        ptr->rj[1] = b;
	ptr->rjb = 2;
        if (0xFF == b) {
	  ptr->state= -1; back = DK3_STM_FINISHED;
	  ptr->found = DK3_FILE_ENCODING_UTF16_MSB_FIRST;
	  ptr->rjb = 0;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      case 8: {
        ptr->rj[1] = b;
	ptr->rjb = 2;
        if (0xBB == b) {
	  ptr->state =  9; back = DK3_STM_ACCEPT;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      case 9: {
        ptr->rj[2] = b;
	ptr->rjb = 3;
        if (0xBF == b) {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	  ptr->found = DK3_FILE_ENCODING_UTF8;
	  ptr->rjb = 0;
	} else {
	  ptr->state = -1; back = DK3_STM_FINISHED;
	}
      } break;
      /*
      	Default not necessary due to initialization of return variable.
      */
    }
  }
  return back;
}



void
dk3bom_detect_results(
  int *enc, const unsigned char **rjptr, size_t *rjsz, dk3_bom_detector_t *ptr
)
{
  if (NULL != ptr) {
    if (NULL != enc) { *enc = ptr->found; }
    if ((NULL != rjptr) && (NULL != rjsz)) {
      *rjptr = ptr->rj;
      *rjsz  = ptr->rjb;
    } else {
      if (NULL != rjptr) { *rjptr = NULL; }
      if (NULL != rjsz ) { *rjsz  = 0; }
    }
  }
}