~ubuntu-branches/ubuntu/lucid/libmtp/lucid

« back to all changes in this revision

Viewing changes to src/unicode.c

  • Committer: Bazaar Package Importer
  • Author(s): Savvas Radevic, Savvas Radevic, Flávio Martins
  • Date: 2009-04-18 19:58:14 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090418195814-lbizx2w4c0mgknc8
Tags: 0.3.7-3ubuntu1
[ Savvas Radevic ]
* Merge from debian unstable, remaining changes:
  + debian/control: Add Breaks to ensure the right udev version gets used.
  + debian/libmtp.install.in, debian/libmtp.preinst.in, 
    debian/libmtp.postinst.in: Clean up the mess where files were still 
    going into the wrong directory.
* debian/libmtp.preinst.in: Dropped check on unsupported 0.2.5-1 (hardy has 
  0.2.6.1-2ubuntu1)

[ Flávio Martins ]
* debian/rules: Append proper prefix 45- to libmtp rules in UDEV variable
* debian/libmtp.install.in: ../../45-libmtp@SOVERSION@.rules lib/udev/rules.d

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 */
43
43
#define STRING_BUFFER_LENGTH 1024
44
44
 
45
 
/** 
46
 
 * Gets the length (in characters, not bytes) of a unicode 
 
45
/**
 
46
 * Gets the length (in characters, not bytes) of a unicode
47
47
 * UCS-2 string, eg a string which physically is 0x00 0x41 0x00 0x00
48
48
 * will return a value of 1.
49
49
 *
50
50
 * @param unicstr a UCS-2 Unicode string
51
 
 * @return the length of the string, in number of characters. If you 
 
51
 * @return the length of the string, in number of characters. If you
52
52
 *         want to know the length in bytes, multiply this by two and
53
53
 *         add two (for zero terminator).
54
54
 */
55
55
int ucs2_strlen(uint16_t const * const unicstr)
56
56
{
57
57
  int length;
58
 
  
 
58
 
59
59
  /* Unicode strings are terminated with 2 * 0x00 */
60
60
  for(length = 0; unicstr[length] != 0x0000U; length ++);
61
61
  return length;
79
79
  size_t nconv;
80
80
  size_t convlen = (ucs2_strlen(unicstr)+1) * sizeof(uint16_t); // UCS-2 is 16 bit wide, include terminator
81
81
  size_t convmax = STRING_BUFFER_LENGTH*3;
82
 
  
 
82
 
83
83
  loclstr[0]='\0';
84
84
  /* Do the conversion.  */
85
85
  nconv = iconv(params->cd_ucs2_to_locale, &stringp, &convlen, &locp, &convmax);
96
96
}
97
97
 
98
98
/**
 
99
 * Converts a UTF-8 string to a big-endian UTF-16 2-byte string
 
100
 * Actually just a UCS-2 internal conversion.
 
101
 *
 
102
 * @param device a pointer to the current device.
 
103
 * @param localstr the UTF-8 unicode string to convert
 
104
 * @return a UTF-16 string.
 
105
 */
 
106
uint16_t *utf8_to_utf16(LIBMTP_mtpdevice_t *device, const char *localstr)
 
107
{
 
108
  PTPParams *params = (PTPParams *) device->params;
 
109
  char *stringp = (char *) localstr; // cast away "const"
 
110
  char unicstr[(STRING_BUFFER_LENGTH+1)*2]; // UCS2 encoding is 2 bytes per UTF-8 char.
 
111
  char *unip = unicstr;
 
112
  size_t nconv = 0;
 
113
  size_t convlen = strlen(localstr)+1; // utf8 bytes, include terminator
 
114
  size_t convmax = STRING_BUFFER_LENGTH*2;
 
115
 
 
116
  unicstr[0]='\0';
 
117
  unicstr[1]='\0';
 
118
 
 
119
  /* Do the conversion.  */
 
120
  nconv = iconv(params->cd_locale_to_ucs2, &stringp, &convlen, &unip, &convmax);
 
121
 
 
122
  if (nconv == (size_t) -1) {
 
123
    // Return partial string anyway.
 
124
    unip[0] = '\0';
 
125
    unip[1] = '\0';
 
126
  }
 
127
  // make sure the string is null terminated
 
128
  unicstr[STRING_BUFFER_LENGTH*2] = '\0';
 
129
  unicstr[STRING_BUFFER_LENGTH*2+1] = '\0';
 
130
 
 
131
  // allocate the string to be returned
 
132
  // Note: can't use strdup since every other byte is a null byte
 
133
  int ret_len = ucs2_strlen((uint16_t*)unicstr)*sizeof(uint16_t)+2;
 
134
  uint16_t* ret = malloc(ret_len);
 
135
  memcpy(ret,unicstr,(size_t)ret_len);
 
136
  return ret;
 
137
}
 
138
 
 
139
/**
99
140
 * This helper function simply removes any consecutive chars
100
141
 * > 0x7F and replace then with an underscore. In UTF-8
101
142
 * consequtive chars > 0x7F represent one single character so