2014-04-01 23:14:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "guac_iconv.h"
|
|
|
|
|
2014-04-01 23:46:29 +00:00
|
|
|
#include <guacamole/unicode.h>
|
2014-04-02 00:42:04 +00:00
|
|
|
#include <stdint.h>
|
2014-04-01 23:46:29 +00:00
|
|
|
|
2014-04-01 23:14:25 +00:00
|
|
|
void guac_iconv(guac_iconv_read* reader, char** input, int in_remaining,
|
|
|
|
guac_iconv_write* writer, char** output, int out_remaining) {
|
2014-04-01 23:46:29 +00:00
|
|
|
|
|
|
|
while (in_remaining > 0 && out_remaining > 0) {
|
|
|
|
|
|
|
|
int value;
|
|
|
|
char* read_start;
|
|
|
|
char* write_start;
|
|
|
|
|
|
|
|
/* Read character */
|
|
|
|
read_start = *input;
|
|
|
|
value = reader(input, in_remaining);
|
|
|
|
in_remaining -= *input - read_start;
|
|
|
|
|
|
|
|
/* Write character */
|
|
|
|
write_start = *output;
|
|
|
|
writer(output, out_remaining, value);
|
|
|
|
out_remaining -= *output - write_start;
|
|
|
|
|
|
|
|
/* Stop if null terminator reached */
|
|
|
|
if (value == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-01 23:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GUAC_READ_UTF8(char** input, int remaining) {
|
|
|
|
|
2014-04-01 23:46:29 +00:00
|
|
|
int value;
|
|
|
|
|
|
|
|
*input += guac_utf8_read(*input, remaining, &value);
|
|
|
|
return value;
|
|
|
|
|
|
|
|
}
|
2014-04-01 23:14:25 +00:00
|
|
|
|
|
|
|
int GUAC_READ_UTF16(char** input, int remaining) {
|
2014-04-02 00:42:04 +00:00
|
|
|
|
|
|
|
int value;
|
|
|
|
|
|
|
|
/* Bail if not enough data */
|
|
|
|
if (remaining < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Read two bytes as integer */
|
|
|
|
value = *((uint16_t*) *input);
|
|
|
|
*input += 2;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
2014-04-01 23:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GUAC_WRITE_UTF8(char** output, int remaining, int value) {
|
2014-04-01 23:46:29 +00:00
|
|
|
*output += guac_utf8_write(value, *output, remaining);
|
2014-04-01 23:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GUAC_WRITE_UTF16(char** output, int remaining, int value) {
|
2014-04-02 00:42:04 +00:00
|
|
|
|
|
|
|
/* Bail if not enough data */
|
|
|
|
if (remaining < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Write two bytes as integer */
|
|
|
|
*((uint16_t*) *output) = value;
|
|
|
|
*output += 2;
|
|
|
|
|
2014-04-01 23:14:25 +00:00
|
|
|
}
|
|
|
|
|