OSDN Git Service

add tadsegment structure validation.
[bbk/bchanf.git] / src / tad / tadsegment.c
1 /*
2  * tadsegment.c
3  *
4  * Copyright (c) 2014 project bchan
5  *
6  * This software is provided 'as-is', without any express or implied
7  * warranty. In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include "tadsegment.h"
28
29 #include        <basic.h>
30 #include        <btron/dp.h>
31 #include    <tad.h>
32
33 EXPORT W tadsegment_getvariable(tadsegment *segment, UB *segid, UW *seglen, UB **segdata)
34 {
35         LTADSEG *seg;
36         TC *ch;
37
38         if (segment->type != TADSEGMENT_TYPE_VARIABLE) {
39                 return -1; /* TODO */
40         }
41
42         ch = (TC*)segment->value.variable.raw;
43
44         *segid = *ch & 0xFF;
45         seg = (LTADSEG*)ch;
46         if (seg->len == 0xffff) {
47                 *seglen = seg->llen;
48                 *segdata = ((UB*)seg) + 8;
49         } else {
50                 *seglen = seg->len;
51                 *segdata = ((UB*)seg) + 4;
52         }
53
54         return 0;
55 }
56
57 EXPORT Bool tadsegment_isvalid(tadsegment *segment)
58 {
59         LTADSEG *seg;
60
61         switch (segment->type) {
62         case TADSEGMENT_TYPE_VARIABLE:
63                 seg = (LTADSEG*)segment->value.variable.raw;
64                 if (seg->len == 0xffff) {
65                         if (segment->value.variable.rawlen != (seg->llen + 8)) {
66                                 return False;
67                         }
68                 } else {
69                         if (segment->value.variable.rawlen != (seg->len + 4)) {
70                                 return False;
71                         }
72                 }
73                 return True;
74         case TADSEGMENT_TYPE_CHARACTOR:
75                 if ((segment->value.ch & 0xFF00) == 0xFE00) {
76                         return False;
77                 }
78                 return True;
79         case TADSEGMENT_TYPE_LANGCODE:
80                 return True;
81         }
82         return False;
83 }