1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/* 2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project 3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License"); 5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License. 6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at 7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * http://www.apache.org/licenses/LICENSE-2.0 9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software 11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS, 12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and 14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License. 15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.dex.file; 18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.rop.cst.CstUtf8; 20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.AnnotatedOutput; 21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.ByteArray; 22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.Hex; 23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulimport com.android.dexgen.util.Leb128Utils; 24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/** 26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Representation of string data for a particular string, in a Dalvik file. 27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class StringDataItem extends OffsettedItem { 29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@code non-null;} the string value */ 30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private final CstUtf8 value; 31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Constructs an instance. 34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param value {@code non-null;} the string value 36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public StringDataItem(CstUtf8 value) { 38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul super(1, writeSize(value)); 39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul this.value = value; 41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** 44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Gets the write size for a given value. 45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * 46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @param value {@code non-null;} the string value 47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * @return {@code >= 2}; the write size, in bytes 48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */ 49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul private static int writeSize(CstUtf8 value) { 50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int utf16Size = value.getUtf16Size(); 51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // The +1 is for the '\0' termination byte. 53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return Leb128Utils.unsignedLeb128Size(utf16Size) 54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul + value.getUtf8Size() + 1; 55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public ItemType itemType() { 60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return ItemType.TYPE_STRING_DATA_ITEM; 61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void addContents(DexFile file) { 66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul // Nothing to do here. 67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public void writeTo0(DexFile file, AnnotatedOutput out) { 72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul ByteArray bytes = value.getBytes(); 73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul int utf16Size = value.getUtf16Size(); 74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul if (out.annotates()) { 76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.annotate(Leb128Utils.unsignedLeb128Size(utf16Size), 77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul "utf16_size: " + Hex.u4(utf16Size)); 78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.annotate(bytes.size() + 1, value.toQuoted()); 79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeUnsignedLeb128(utf16Size); 82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.write(bytes); 83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul out.writeByte(0); 84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul public String toHuman() { 89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return value.toQuoted(); 90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul /** {@inheritDoc} */ 93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul @Override 94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul protected int compareTo0(OffsettedItem other) { 95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul StringDataItem otherData = (StringDataItem) other; 96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul 97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul return value.compareTo(otherData.value); 98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul } 99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul} 100