PRBool XDR(XPTArena *arena, XPTCursor *cursor, struct TestData *str) { TRY("Do32", XPT_Do32(cursor, &str->bit32)); TRY("Do16", XPT_Do16(cursor, &str->bit16)); TRY("Do8", XPT_Do8 (cursor, &str->bit8[0])); TRY("Do8", XPT_Do8 (cursor, &str->bit8[1])); TRY("DoCString", XPT_DoCString(arena, cursor, &str->cstr)); TRY("DoString", XPT_DoString(arena, cursor, &str->str)); return 0; }
XPT_DoStringInline(XPTArena *arena, XPTCursor *cursor, XPTString **strp) { XPTString *str = *strp; XPTMode mode = cursor->state->mode; int i; if (mode == XPT_DECODE) { str = XPT_NEWZAP(arena, XPTString); if (!str) return PR_FALSE; *strp = str; } if (!XPT_Do16(cursor, &str->length)) goto error; if (mode == XPT_DECODE) if (!(str->bytes = (char*)XPT_MALLOC(arena, str->length + 1u))) goto error; for (i = 0; i < str->length; i++) if (!XPT_Do8(cursor, (uint8_t *)&str->bytes[i])) goto error_2; if (mode == XPT_DECODE) str->bytes[str->length] = 0; return PR_TRUE; error_2: XPT_DELETE(arena, str->bytes); error: XPT_DELETE(arena, str); return PR_FALSE; }
XPT_SkipStringInline(NotNull<XPTCursor*> cursor) { uint16_t length; if (!XPT_Do16(cursor, &length)) return false; uint8_t byte; for (uint16_t i = 0; i < length; i++) if (!XPT_Do8(cursor, &byte)) return false; return true; }
XPT_DoIID(XPTCursor *cursor, nsID *iidp) { int i; if (!XPT_Do32(cursor, &iidp->m0) || !XPT_Do16(cursor, &iidp->m1) || !XPT_Do16(cursor, &iidp->m2)) return PR_FALSE; for (i = 0; i < 8; i++) if (!XPT_Do8(cursor, (uint8_t *)&iidp->m3[i])) return PR_FALSE; return PR_TRUE; }
XPT_DoIID(NotNull<XPTCursor*> cursor, nsID *iidp) { int i; if (!XPT_Do32(cursor, &iidp->m0) || !XPT_Do16(cursor, &iidp->m1) || !XPT_Do16(cursor, &iidp->m2)) return false; for (i = 0; i < 8; i++) if (!XPT_Do8(cursor, (uint8_t *)&iidp->m3[i])) return false; return true; }
XPT_DoCString(XPTArena *arena, XPTCursor *cursor, char **identp) { XPTCursor my_cursor; char *ident = *identp; uint32_t offset = 0; XPTMode mode = cursor->state->mode; if (mode == XPT_DECODE) { char *start, *end; int len; if (!XPT_Do32(cursor, &offset)) return PR_FALSE; if (!offset) { *identp = NULL; return PR_TRUE; } my_cursor.pool = XPT_DATA; my_cursor.offset = offset; my_cursor.state = cursor->state; start = &CURS_POINT(&my_cursor); end = strchr(start, 0); /* find the end of the string */ if (!end) { fprintf(stderr, "didn't find end of string on decode!\n"); return PR_FALSE; } len = end - start; XPT_ASSERT(len > 0); ident = (char*)XPT_MALLOC(arena, len + 1u); if (!ident) return PR_FALSE; memcpy(ident, start, (size_t)len); ident[len] = 0; *identp = ident; } else { if (!ident) { offset = 0; if (!XPT_Do32(cursor, &offset)) return PR_FALSE; return PR_TRUE; } if (!XPT_MakeCursor(cursor->state, XPT_DATA, strlen(ident) + 1, &my_cursor) || !XPT_Do32(cursor, &my_cursor.offset)) return PR_FALSE; while(*ident) if (!XPT_Do8(&my_cursor, (uint8_t *)ident++)) return PR_FALSE; if (!XPT_Do8(&my_cursor, (uint8_t *)ident)) /* write trailing zero */ return PR_FALSE; } return PR_TRUE; }