char *parcel_r_string(struct parcel *p) { char *ret; int len16 = parcel_r_int32(p); int strbytes; if (p->malformed) return NULL; /* This is how a null string is sent */ if (len16 < 0) return NULL; strbytes = PAD_SIZE((len16 + 1) * sizeof(char16_t)); if (p->offset + strbytes > p->size) { ofono_error("%s: parcel is too small", __func__); p->malformed = 1; return NULL; } ret = g_utf16_to_utf8((gunichar2 *) (void *) (p->data + p->offset), len16, NULL, NULL, NULL); if (ret == NULL) { ofono_error("%s: wrong UTF16 coding", __func__); p->malformed = 1; return NULL; } p->offset += strbytes; return ret; }
int parcel_w_string(struct parcel *p, const char *str) { gunichar2 *gs16; glong gs16_len; size_t len; size_t gs16_size; if (str == NULL) { parcel_w_int32(p, -1); return 0; } gs16 = g_utf8_to_utf16(str, -1, NULL, &gs16_len, NULL); if (parcel_w_int32(p, gs16_len) == -1) return -1; gs16_size = gs16_len * sizeof(char16_t); len = gs16_size + sizeof(char16_t); for (;;) { size_t padded = PAD_SIZE(len); if (p->offset + len < p->capacity) { /* There's enough space */ memcpy(p->data + p->offset, gs16, gs16_size); *((char16_t *) (void *) (p->data + p->offset + gs16_size)) = 0; p->offset += padded; p->size += padded; if (padded != len) { #if BYTE_ORDER == BIG_ENDIAN static const uint32_t mask[4] = { 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 }; #endif #if BYTE_ORDER == LITTLE_ENDIAN static const uint32_t mask[4] = { 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff }; #endif *((uint32_t *) (void *) (p->data + p->offset - 4)) &= mask[padded - len]; } break; } else { /* Grow data and retry */ parcel_grow(p, padded); } } g_free(gs16); return 0; }
int parcel_w_string(struct parcel *p, char *str) { char16_t *s16; size_t s16_len; size_t len; if(str == NULL) { parcel_w_int32(p, -1); return 0; } s16_len = strlen8to16(str); s16 = malloc(sizeof(char16_t) * s16_len); strcpy8to16(s16, str, &s16_len); if(parcel_w_int32(p, s16_len) == -1) { return -1; } len = (s16_len + 1) * sizeof(char16_t); for(;;) { size_t padded = PAD_SIZE(len); /*printf("parcel_w_string(\"%s\"): offset %d, cap %d, size %d\n", str, p->offset, p->capacity, p->size);*/ if(p->offset + len < p->capacity) { // There's enough space memcpy(p->data + p->offset, s16, s16_len * sizeof(char16_t)); *((char16_t *) (p->data + p->offset + len)) = 0; p->offset += padded; p->size += padded; if (padded != len) { //printf("Writing %ld bytes, padded to %ld\n", len, padded); #if BYTE_ORDER == BIG_ENDIAN static const uint32_t mask[4] = { 0x00000000, 0xffffff00, 0xffff0000, 0xff000000 }; #endif #if BYTE_ORDER == LITTLE_ENDIAN static const uint32_t mask[4] = { 0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff }; #endif *((uint32_t*)(p->data+p->offset+padded-4)) &= mask[padded-len]; } break; } else { // Grow data and retry if(parcel_grow(p, padded) == -1) { free(s16); return -1; } } } free(s16); return 0; }
char* parcel_r_string(struct parcel *p) { char *ret; int len16 = parcel_r_int32(p); size_t len8; if(len16 < 0) return NULL; // this is how a null string is sent len8 = strnlen16to8((char16_t *) (p->data + p->offset), len16); ret = malloc(len8 + 1); if(ret == NULL) return NULL; strncpy16to8(ret, (char16_t *) (p->data + p->offset), len16); p->offset += PAD_SIZE((len16 + 1) * sizeof(char16_t)); return ret; }
/* * --------------------------------------------------------------------------- * Adds an attribute of 'type' and string payload. * Refer nl_msg_push_string for more details. * --------------------------------------------------------------------------- */ BOOLEAN NlMsgPutHeadString(PNL_BUFFER buf, UINT16 type, PCHAR value) { size_t strLen = strlen(value) + 1; #ifdef DBG /* Attribute length should come within 16 bits (NL_ATTR). * Not a likely case, hence validation only in debug mode. */ if ((strLen + PAD_SIZE(strLen, NLA_ALIGNTO)) > MAXUINT16) { return FALSE; } #endif /* typecast to keep compiler happy */ return (NlMsgPutHeadUnspec(buf, type, value, (UINT16)strLen)); }
char* parcel_r_string(struct parcel *p) { char *ret; int len16 = parcel_r_int32(p); /* This is how a null string is sent */ if (len16 < 0) return NULL; ret = g_utf16_to_utf8((gunichar2 *) (p->data + p->offset), len16, NULL, NULL, NULL); if (ret == NULL) return NULL; p->offset += PAD_SIZE((len16 + 1) * sizeof(char16_t)); return ret; }
OVS_ALIGNED_STRUCT(CACHE_LINE_SIZE, aligned_mutex) { struct ovs_mutex mutex; char pad[PAD_SIZE(sizeof(struct ovs_mutex), CACHE_LINE_SIZE)]; };