aboutsummaryrefslogtreecommitdiff
path: root/src/stringprep/stringprep_drv.c
blob: e6eb3d378cdb7227486543ecf0231de4fadd8b77 (plain) (blame)
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
/* $Id$ */

#include <stdio.h>
#include <erl_driver.h>
#include <ei.h>
#include <iconv.h>

#include "uni_data.c"

typedef struct {
      ErlDrvPort port;
} stringprep_data;


static ErlDrvData stringprep_erl_start(ErlDrvPort port, char *buff)
{
   stringprep_data* d = (stringprep_data*)driver_alloc(sizeof(stringprep_data));
   d->port = port;

   set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
   
   return (ErlDrvData)d;
}

static void stringprep_erl_stop(ErlDrvData handle)
{
   driver_free((char*)handle);
}

static int stringprep_erl_control(ErlDrvData drv_data,
				  unsigned int command,
				  char *buf, int len,
				  char **rbuf, int rlen)
{
   int i, j=0;
   unsigned char c;
   int bad = 0;
   int uc, ruc;
   int size;
   int info;
   ErlDrvBinary *b;
   char *rstring;

   size = len;

   rstring = malloc(size);

   for(i=0; i < len; i++)
   {
      c = buf[i];
      if(c < 0x80) {
	 uc = c;
      } else if(c < 0xC0) {
	 bad = 1;
      } else if(c < 0xE0) {
	 if(i+1 < len && (buf[i+1] & 0xC0) == 0x80) {
	    uc = ((c & 0x1F) << 6) | (buf[i+1] & 0x3F);
	    i++;
	 } else {
	    bad = 1;
	 }
      } else if(c < 0xF0) {
	 if(i+2 < len && (buf[i+1] & 0xC0) == 0x80 &&
	    (buf[i+2] & 0xC0) == 0x80) {
	    uc = ((c & 0x1F) << 12) | ((buf[i+1] & 0x1F) << 6)
	       | (buf[i+2] & 0x3F);
	    i += 2;
	 } else {
	    bad = 1;
	 }
      } else {
	 // TODO
	 bad = 1;
      }

      if(bad) {
	 *rbuf = (char*)(b = driver_alloc_binary(1));
	 b->orig_bytes[0] = 0;
	 free(rstring);
	 return 1;
      }
      
      
      info = GetUniCharInfo(uc);
      ruc = uc + GetDelta(info);

      if(ruc < 0x80) {
	 if(j >= size) {
	    size = 2*size + 1;
	    rstring = realloc(rstring, size);
	 }
	 rstring[j] = (char) ruc;
	 j++;
      } else if(ruc < 0x7FF) {
	 if(j >= size) {
	    size = 2*size + 2;
	    rstring = realloc(rstring, size);
	 }
	 rstring[j] = (char) ((ruc >> 6) | 0xC0);
	 rstring[j+1] = (char) ((ruc | 0x80) & 0xBF);
	 j += 2;
      } else if(ruc < 0xFFFF) {
	 if(j >= size) {
	    size = 2*size + 3;
	    rstring = realloc(rstring, size);
	 }
	 rstring[j] = (char) ((ruc >> 12) | 0xE0);
	 rstring[j+1] = (char) (((ruc >> 6) | 0x80) & 0xBF);
	 rstring[j+2] = (char) ((ruc | 0x80) & 0xBF);
	 j += 3;
      }
   }
   
   
   
   *rbuf = (char*)(b = driver_alloc_binary(j));
   memcpy(b->orig_bytes, rstring, j);
   free(rstring);
   
   return j;
}



ErlDrvEntry stringprep_driver_entry = {
   NULL,                       /* F_PTR init, N/A */
   stringprep_erl_start,          /* L_PTR start, called when port is opened */
   stringprep_erl_stop,           /* F_PTR stop, called when port is closed */
   NULL,         /* F_PTR output, called when erlang has sent */
   NULL,                       /* F_PTR ready_input, called when input descriptor ready */
   NULL,                       /* F_PTR ready_output, called when output descriptor ready */
   "stringprep_drv",              /* char *driver_name, the argument to open_port */
   NULL,                       /* F_PTR finish, called when unloaded */
   NULL,                       /* handle */
   stringprep_erl_control,          /* F_PTR control, port_command callback */
   NULL,                       /* F_PTR timeout, reserved */
   NULL                        /* F_PTR outputv, reserved */
};

#ifdef WIN32
__declspec(dllexport)
#endif
DRIVER_INIT(stringprep_erl) /* must match name in driver_entry */
{
   return &stringprep_driver_entry;
}