Permalink
Browse files

replaced utf8 encoding functions

null values are now properly handled
  • Loading branch information...
1 parent fbcb35d commit cc0364050408979c557eb9e7019abb99dfeeb49b @toddtreece committed Oct 27, 2011
Showing with 196 additions and 271 deletions.
  1. +1 −1 common.h
  2. +132 −0 encodings.c
  3. +46 −0 encodings.h
  4. +17 −3 io.c
  5. BIN lib_mysqludf_mongodb.so
  6. +0 −233 utf8.c
  7. +0 −34 utf8.h
View
@@ -14,7 +14,7 @@ typedef long long longlong;
#include <mysql.h>
#include "mongo.h"
-#include "utf8.h"
+#include "encodings.h"
#define VERSION_STRING "1.0"
#define VERSION_STRING_LENGTH 3
View
@@ -0,0 +1,132 @@
+/*
+ This file is part of libXMLRPC - a C library for xml-encoded function calls.
+
+ Author: Dan Libby (dan@libby.com)
+ Epinions.com may be contacted at feedback@epinions-inc.com
+*/
+
+/*
+ Copyright 2000 Epinions, Inc.
+
+ Subject to the following 3 conditions, Epinions, Inc. permits you, free
+ of charge, to (a) use, copy, distribute, modify, perform and display this
+ software and associated documentation files (the "Software"), and (b)
+ permit others to whom the Software is furnished to do so as well.
+
+ 1) The above copyright notice and this permission notice shall be included
+ without modification in all copies or substantial portions of the
+ Software.
+
+ 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
+ ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
+ IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ PURPOSE OR NONINFRINGEMENT.
+
+ 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
+ SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
+ OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
+ NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
+ DAMAGES.
+
+*/
+
+#include "common.h"
+#include <errno.h>
+
+#ifdef HAVE_GICONV_H
+#include <giconv.h>
+#else
+#include <iconv.h>
+#endif
+
+#include "encodings.h"
+
+#ifndef ICONV_CSNMAXLEN
+#define ICONV_CSNMAXLEN 64
+#endif
+
+static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
+ char* outbuf = 0;
+
+ if(src && src_len && from_enc && to_enc) {
+ size_t outlenleft = src_len;
+ size_t inlenleft = src_len;
+ int outlen = src_len;
+ iconv_t ic;
+ char* out_ptr = 0;
+
+ if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
+ return NULL;
+ }
+ ic = iconv_open(to_enc, from_enc);
+ if(ic != (iconv_t)-1) {
+ size_t st;
+ outbuf = (char*)malloc(outlen + 1);
+
+ if(outbuf) {
+ out_ptr = (char*)outbuf;
+ while(inlenleft) {
+ st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
+ if(st == -1) {
+ if(errno == E2BIG) {
+ int diff = out_ptr - outbuf;
+ outlen += inlenleft;
+ outlenleft += inlenleft;
+ outbuf = (char*)realloc(outbuf, outlen + 1);
+ if(!outbuf) {
+ break;
+ }
+ out_ptr = outbuf + diff;
+ }
+ else {
+ free(outbuf);
+ outbuf = 0;
+ break;
+ }
+ }
+ }
+ }
+ iconv_close(ic);
+ }
+ outlen -= outlenleft;
+
+ if(new_len) {
+ *new_len = outbuf ? outlen : 0;
+ }
+ if(outbuf) {
+ outbuf[outlen] = 0;
+ }
+ }
+ return outbuf;
+}
+
+/* returns a new string that must be freed */
+char* utf8_encode(const char *str) {
+
+ char *out;
+
+ if(strlen(str)) {
+ int alen, len;
+ alen = strlen(str);
+ out = convert(str, alen, &len, "ISO-8859-1", "UTF-8");
+ }
+
+ return out;
+
+}
+
+/* returns a new string, possibly decoded */
+char* utf8_decode(const char *str) {
+
+ char *out;
+
+ if (strlen(str)) {
+ int alen, len;
+ alen = strlen(str);
+ out = convert(str, alen, &len, "UTF-8", "ISO-8859-1");
+ }
+
+ return out;
+
+}
+
View
@@ -0,0 +1,46 @@
+/*
+ This file is part of libXMLRPC - a C library for xml-encoded function calls.
+
+ Author: Dan Libby (dan@libby.com)
+ Epinions.com may be contacted at feedback@epinions-inc.com
+*/
+
+/*
+ Copyright 2000 Epinions, Inc.
+
+ Subject to the following 3 conditions, Epinions, Inc. permits you, free
+ of charge, to (a) use, copy, distribute, modify, perform and display this
+ software and associated documentation files (the "Software"), and (b)
+ permit others to whom the Software is furnished to do so as well.
+
+ 1) The above copyright notice and this permission notice shall be included
+ without modification in all copies or substantial portions of the
+ Software.
+
+ 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
+ ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
+ IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ PURPOSE OR NONINFRINGEMENT.
+
+ 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
+ SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
+ OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
+ NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
+ DAMAGES.
+
+*/
+
+
+#ifndef __ENCODINGS__H
+#define __ENCODINGS__H
+
+/* these defines are for legacy purposes. */
+#define encoding_utf_8 "UTF-8"
+typedef const char* ENCODING_ID;
+#define utf8_get_encoding_id_string(desired_enc) ((const char*)desired_enc)
+#define utf8_get_encoding_id_from_string(id_string) ((ENCODING_ID)id_string)
+
+char* utf8_encode(const char *str);
+char* utf8_decode(const char *str);
+
+#endif /* __ENCODINGS__H */
View
20 io.c
@@ -55,11 +55,25 @@ long long mongodb_save(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned
if (args->args[i]) {
if (args->arg_type[i] == STRING_RESULT || args->arg_type[i] == DECIMAL_RESULT) {
-
+
//args array is not null terminated by default
args->args[i][args->lengths[i]] = '\0';
-
- bson_append_string(b, args->attributes[i],utf8_encode(args->args[i]));
+
+ if(strcmp(args->args[i], "NULL") == 0 || args->lengths[i] == 0) {
+
+ bson_append_null(b, args->attributes[i]);
+
+ } else {
+
+ char *item;
+
+ item = utf8_encode(args->args[i]);
+
+ bson_append_string(b, args->attributes[i],item);
+
+ free(item);
+
+ }
} else if (args->arg_type[i] == INT_RESULT) {
View
Binary file not shown.
Oops, something went wrong.

0 comments on commit cc03640

Please sign in to comment.