xref: /haiku/src/add-ons/kernel/file_systems/exfat/Utility.cpp (revision 762b846cf8ba678f31a854a85e68003d2071be8d)
1*762b846cSJohn Scipione /*
2*762b846cSJohn Scipione  * Copyright 2001-2009, Axel Dörfler, axeld@pinc-software.de.
3*762b846cSJohn Scipione  * Copyright 2014 Haiku, Inc. All rights reserved.
4*762b846cSJohn Scipione  *
5*762b846cSJohn Scipione  * Distributed under the terms of the MIT License.
6*762b846cSJohn Scipione  *
7*762b846cSJohn Scipione  * Authors:
8*762b846cSJohn Scipione  *		Axel Dörfler, axeld@pinc-software.de
9*762b846cSJohn Scipione  *		John Scipione, jscipione@gmail.com
10*762b846cSJohn Scipione  */
11*762b846cSJohn Scipione 
12*762b846cSJohn Scipione 
13*762b846cSJohn Scipione #include "Utility.h"
14*762b846cSJohn Scipione 
15*762b846cSJohn Scipione #include <stdio.h>
16*762b846cSJohn Scipione #include <stdlib.h>
17*762b846cSJohn Scipione #include <string.h>
18*762b846cSJohn Scipione 
19*762b846cSJohn Scipione #include <Errors.h>
20*762b846cSJohn Scipione 
21*762b846cSJohn Scipione #include "encodings.h"
22*762b846cSJohn Scipione 
23*762b846cSJohn Scipione 
24*762b846cSJohn Scipione status_t
25*762b846cSJohn Scipione volume_name(struct exfat_entry* entry, char* _name)
26*762b846cSJohn Scipione {
27*762b846cSJohn Scipione 	if (entry == NULL || _name == NULL)
28*762b846cSJohn Scipione 		return B_BAD_VALUE;
29*762b846cSJohn Scipione 
30*762b846cSJohn Scipione 	if (entry->type == EXFAT_ENTRY_TYPE_NOT_IN_USE) {
31*762b846cSJohn Scipione 		const char* untitled = "Untitled";
32*762b846cSJohn Scipione 		size_t length = strlen(untitled);
33*762b846cSJohn Scipione 		strncpy(_name, untitled, length);
34*762b846cSJohn Scipione 		if (strlen(_name) < length)
35*762b846cSJohn Scipione 			return B_NAME_TOO_LONG;
36*762b846cSJohn Scipione 	} else if (entry->type == EXFAT_ENTRY_TYPE_LABEL) {
37*762b846cSJohn Scipione 		// UCS-2 can encode codepoints in the range U+0000 to U+FFFF
38*762b846cSJohn Scipione 		// UTF-8 needs at most 3 bytes to encode values in this range
39*762b846cSJohn Scipione 		size_t utf8NameLength = entry->label.length * 3;
40*762b846cSJohn Scipione 		memset(_name, 0, utf8NameLength + 1);
41*762b846cSJohn Scipione 			// zero out the character array
42*762b846cSJohn Scipione 		unicode_to_utf8((const uchar*)entry->label.name,
43*762b846cSJohn Scipione 			entry->label.length * 2, (uint8*)_name, &utf8NameLength);
44*762b846cSJohn Scipione 		if (strlen(_name) < utf8NameLength)
45*762b846cSJohn Scipione 			return B_NAME_TOO_LONG;
46*762b846cSJohn Scipione 	} else
47*762b846cSJohn Scipione 		return B_NAME_NOT_FOUND;
48*762b846cSJohn Scipione 
49*762b846cSJohn Scipione 	return B_OK;
50*762b846cSJohn Scipione }
51